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 "core/core.h"
11#include "core/thread.h"
12#include "platform/opengl/gl.h"
13
14#define GB_GBA_CENTER ((VIDEO_HORIZONTAL_PIXELS - GB_VIDEO_HORIZONTAL_PIXELS + VIDEO_HORIZONTAL_PIXELS * (VIDEO_VERTICAL_PIXELS - GB_VIDEO_VERTICAL_PIXELS)) / 2)
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 renderer->outputBuffer = malloc(renderer->width * renderer->height * BYTES_PER_PIXEL);
37 memset(renderer->outputBuffer, 0, renderer->width * renderer->height * BYTES_PER_PIXEL);
38 renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, renderer->width);
39
40 GBAGLContextCreate(&renderer->gl);
41 renderer->gl.d.user = renderer;
42 renderer->gl.d.lockAspectRatio = renderer->lockAspectRatio;
43 renderer->gl.d.filter = renderer->filter;
44 renderer->gl.d.swap = mSDLGLCommonSwap;
45 renderer->gl.d.init(&renderer->gl.d, 0);
46 renderer->gl.d.setDimensions(&renderer->gl.d, renderer->width, renderer->height);
47
48 _doViewport(renderer->viewportWidth, renderer->viewportHeight, &renderer->gl.d);
49 return true;
50}
51
52void mSDLGLRunloop(struct mSDLRenderer* renderer, void* user) {
53 struct mCoreThread* context = user;
54 SDL_Event event;
55 struct VideoBackend* v = &renderer->gl.d;
56
57 while (context->state < THREAD_EXITING) {
58 while (SDL_PollEvent(&event)) {
59 mSDLHandleEvent(context, &renderer->player, &event);
60#if SDL_VERSION_ATLEAST(2, 0, 0)
61 // Event handling can change the size of the screen
62 if (renderer->player.windowUpdated) {
63 SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
64 _doViewport(renderer->viewportWidth, renderer->viewportHeight, v);
65 renderer->player.windowUpdated = 0;
66 }
67#endif
68 }
69
70 if (mCoreSyncWaitFrameStart(&context->sync)) {
71 v->postFrame(v, renderer->outputBuffer);
72 }
73 mCoreSyncWaitFrameEnd(&context->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}