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 "gba/supervisor/thread.h"
9#include "platform/opengl/gl.h"
10
11static void _sdlSwap(struct VideoBackend* context) {
12 struct SDLSoftwareRenderer* renderer = (struct SDLSoftwareRenderer*) context->user;
13#if SDL_VERSION_ATLEAST(2, 0, 0)
14 SDL_GL_SwapWindow(renderer->window);
15#else
16 SDL_GL_SwapBuffers();
17#endif
18}
19
20static void _doViewport(int w, int h, struct VideoBackend* v) {
21 v->resized(v, w, h);
22 v->clear(v);
23 v->swap(v);
24 v->clear(v);
25}
26
27static bool GBASDLGLInit(struct SDLSoftwareRenderer* renderer);
28static void GBASDLGLRunloop(struct GBAThread* context, struct SDLSoftwareRenderer* renderer);
29static void GBASDLGLDeinit(struct SDLSoftwareRenderer* renderer);
30
31void GBASDLGLCreate(struct SDLSoftwareRenderer* renderer) {
32 renderer->init = GBASDLGLInit;
33 renderer->deinit = GBASDLGLDeinit;
34 renderer->runloop = GBASDLGLRunloop;
35}
36
37bool GBASDLGLInit(struct SDLSoftwareRenderer* renderer) {
38#ifndef COLOR_16_BIT
39 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
40 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
41 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
42#else
43 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
44#ifdef COLOR_5_6_5
45 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
46#else
47 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
48#endif
49 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
50#endif
51
52#if SDL_VERSION_ATLEAST(2, 0, 0)
53 renderer->window = SDL_CreateWindow(projectName, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, renderer->viewportWidth, renderer->viewportHeight, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | (SDL_WINDOW_FULLSCREEN_DESKTOP * renderer->player.fullscreen));
54 SDL_GL_CreateContext(renderer->window);
55 SDL_GL_SetSwapInterval(1);
56 SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
57 renderer->player.window = renderer->window;
58#else
59 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
60#ifdef COLOR_16_BIT
61 SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 16, SDL_OPENGL);
62#else
63 SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 32, SDL_OPENGL);
64#endif
65#endif
66
67 renderer->d.outputBuffer = malloc(256 * 256 * BYTES_PER_PIXEL);
68 renderer->d.outputBufferStride = 256;
69
70 GBAGLContextCreate(&renderer->gl);
71 renderer->gl.d.user = renderer;
72 renderer->gl.d.lockAspectRatio = renderer->lockAspectRatio;
73 renderer->gl.d.filter = renderer->filter;
74 renderer->gl.d.swap = _sdlSwap;
75 renderer->gl.d.init(&renderer->gl.d, 0);
76
77 _doViewport(renderer->viewportWidth, renderer->viewportHeight, &renderer->gl.d);
78 return true;
79}
80
81void GBASDLGLRunloop(struct GBAThread* context, struct SDLSoftwareRenderer* renderer) {
82 SDL_Event event;
83 struct VideoBackend* v = &renderer->gl.d;
84
85 while (context->state < THREAD_EXITING) {
86 while (SDL_PollEvent(&event)) {
87 GBASDLHandleEvent(context, &renderer->player, &event);
88#if SDL_VERSION_ATLEAST(2, 0, 0)
89 // Event handling can change the size of the screen
90 if (renderer->player.windowUpdated) {
91 SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
92 _doViewport(renderer->viewportWidth, renderer->viewportHeight, v);
93 renderer->player.windowUpdated = 0;
94 }
95#endif
96 }
97
98 if (GBASyncWaitFrameStart(&context->sync, context->frameskip)) {
99 v->postFrame(v, renderer->d.outputBuffer);
100 }
101 v->drawFrame(v);
102 GBASyncWaitFrameEnd(&context->sync);
103 v->swap(v);
104 }
105}
106
107void GBASDLGLDeinit(struct SDLSoftwareRenderer* renderer) {
108 if (renderer->gl.d.deinit) {
109 renderer->gl.d.deinit(&renderer->gl.d);
110 }
111 free(renderer->d.outputBuffer);
112}