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