src/platform/sdl/gl-sdl.c (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "main.h"
7
8#include "gl-common.h"
9
10#include <mgba/core/core.h>
11#include <mgba/core/thread.h>
12#include <mgba-util/math.h>
13
14#include "platform/opengl/gl.h"
15
16static bool mSDLGLInit(struct mSDLRenderer* renderer);
17static void mSDLGLRunloop(struct mSDLRenderer* renderer, void* user);
18static void mSDLGLDeinit(struct mSDLRenderer* renderer);
19
20void mSDLGLCreate(struct mSDLRenderer* renderer) {
21 renderer->init = mSDLGLInit;
22 renderer->deinit = mSDLGLDeinit;
23 renderer->runloop = mSDLGLRunloop;
24}
25
26bool mSDLGLInit(struct mSDLRenderer* renderer) {
27 mSDLGLCommonInit(renderer);
28
29 size_t size = renderer->width * renderer->height * BYTES_PER_PIXEL;
30 renderer->outputBuffer = malloc(size);
31 memset(renderer->outputBuffer, 0, size);
32 renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, renderer->width);
33
34 mGLContextCreate(&renderer->gl);
35 renderer->gl.d.user = renderer;
36 renderer->gl.d.lockAspectRatio = renderer->lockAspectRatio;
37 renderer->gl.d.lockIntegerScaling = renderer->lockIntegerScaling;
38 renderer->gl.d.filter = renderer->filter;
39 renderer->gl.d.swap = mSDLGLCommonSwap;
40 renderer->gl.d.init(&renderer->gl.d, 0);
41 renderer->gl.d.setDimensions(&renderer->gl.d, renderer->width, renderer->height);
42
43 mSDLGLDoViewport(renderer->viewportWidth, renderer->viewportHeight, &renderer->gl.d);
44 return true;
45}
46
47void mSDLGLRunloop(struct mSDLRenderer* renderer, void* user) {
48 struct mCoreThread* context = user;
49 SDL_Event event;
50 struct VideoBackend* v = &renderer->gl.d;
51
52 while (mCoreThreadIsActive(context)) {
53 while (SDL_PollEvent(&event)) {
54 mSDLHandleEvent(context, &renderer->player, &event);
55#if SDL_VERSION_ATLEAST(2, 0, 0)
56 // Event handling can change the size of the screen
57 if (renderer->player.windowUpdated) {
58 SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
59 mSDLGLDoViewport(renderer->viewportWidth, renderer->viewportHeight, v);
60 renderer->player.windowUpdated = 0;
61 }
62#endif
63 }
64 renderer->core->desiredVideoDimensions(renderer->core, &renderer->width, &renderer->height);
65 if (renderer->width != v->width || renderer->height != v->height) {
66 renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, renderer->width);
67 v->setDimensions(v, renderer->width, renderer->height);
68 }
69
70 if (mCoreSyncWaitFrameStart(&context->impl->sync)) {
71 v->postFrame(v, renderer->outputBuffer);
72 }
73 mCoreSyncWaitFrameEnd(&context->impl->sync);
74 v->drawFrame(v);
75 v->swap(v);
76 }
77}
78
79void mSDLGLDeinit(struct mSDLRenderer* renderer) {
80 if (renderer->gl.d.deinit) {
81 renderer->gl.d.deinit(&renderer->gl.d);
82 }
83 free(renderer->outputBuffer);
84#if SDL_VERSION_ATLEAST(2, 0, 0)
85 SDL_GL_DeleteContext(renderer->glCtx);
86#endif
87}