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 void _doViewport(int w, int h, struct VideoBackend* v) {
17 v->resized(v, w, h);
18 v->clear(v);
19 v->swap(v);
20 v->clear(v);
21}
22
23static bool mSDLGLInit(struct mSDLRenderer* renderer);
24static void mSDLGLRunloop(struct mSDLRenderer* renderer, void* user);
25static void mSDLGLDeinit(struct mSDLRenderer* renderer);
26
27void mSDLGLCreate(struct mSDLRenderer* renderer) {
28 renderer->init = mSDLGLInit;
29 renderer->deinit = mSDLGLDeinit;
30 renderer->runloop = mSDLGLRunloop;
31}
32
33bool mSDLGLInit(struct mSDLRenderer* renderer) {
34 mSDLGLCommonInit(renderer);
35
36 size_t size = toPow2(renderer->width) * toPow2(renderer->height) * BYTES_PER_PIXEL;
37 renderer->outputBuffer = malloc(size);
38 memset(renderer->outputBuffer, 0, size);
39 renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, toPow2(renderer->width));
40
41 mGLContextCreate(&renderer->gl);
42 renderer->gl.d.user = renderer;
43 renderer->gl.d.lockAspectRatio = renderer->lockAspectRatio;
44 renderer->gl.d.lockIntegerScaling = renderer->lockIntegerScaling;
45 renderer->gl.d.filter = renderer->filter;
46 renderer->gl.d.swap = mSDLGLCommonSwap;
47 renderer->gl.d.init(&renderer->gl.d, 0);
48 renderer->gl.d.setDimensions(&renderer->gl.d, renderer->width, renderer->height);
49
50 _doViewport(renderer->viewportWidth, renderer->viewportHeight, &renderer->gl.d);
51 return true;
52}
53
54void mSDLGLRunloop(struct mSDLRenderer* renderer, void* user) {
55 struct mCoreThread* context = user;
56 SDL_Event event;
57 struct VideoBackend* v = &renderer->gl.d;
58
59 while (mCoreThreadIsActive(context)) {
60 while (SDL_PollEvent(&event)) {
61 mSDLHandleEvent(context, &renderer->player, &event);
62#if SDL_VERSION_ATLEAST(2, 0, 0)
63 // Event handling can change the size of the screen
64 if (renderer->player.windowUpdated) {
65 SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
66 _doViewport(renderer->viewportWidth, renderer->viewportHeight, v);
67 renderer->player.windowUpdated = 0;
68 }
69#endif
70 }
71 if (renderer->width != v->width || renderer->height != v->height) {
72 v->setDimensions(v, renderer->width, renderer->height);
73 }
74
75 if (mCoreSyncWaitFrameStart(&context->impl->sync)) {
76 v->postFrame(v, renderer->outputBuffer);
77 }
78 mCoreSyncWaitFrameEnd(&context->impl->sync);
79 v->drawFrame(v);
80 v->swap(v);
81 }
82}
83
84void mSDLGLDeinit(struct mSDLRenderer* renderer) {
85 if (renderer->gl.d.deinit) {
86 renderer->gl.d.deinit(&renderer->gl.d);
87 }
88 free(renderer->outputBuffer);
89#if SDL_VERSION_ATLEAST(2, 0, 0)
90 SDL_GL_DeleteContext(renderer->glCtx);
91#endif
92}