all repos — mgba @ 0cd28060e07c587ab10901ce815563a9e998f297

mGBA Game Boy Advance Emulator

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 "gba/supervisor/thread.h"
11#include "platform/opengl/gl.h"
12
13static void _doViewport(int w, int h, struct VideoBackend* v) {
14	v->resized(v, w, h);
15	v->clear(v);
16	v->swap(v);
17	v->clear(v);
18}
19
20static bool GBASDLGLInit(struct SDLSoftwareRenderer* renderer);
21static void GBASDLGLRunloop(struct GBAThread* context, struct SDLSoftwareRenderer* renderer);
22static void GBASDLGLDeinit(struct SDLSoftwareRenderer* renderer);
23
24void GBASDLGLCreate(struct SDLSoftwareRenderer* renderer) {
25	renderer->init = GBASDLGLInit;
26	renderer->deinit = GBASDLGLDeinit;
27	renderer->runloop = GBASDLGLRunloop;
28}
29
30bool GBASDLGLInit(struct SDLSoftwareRenderer* renderer) {
31	GBASDLGLCommonInit(renderer);
32
33	renderer->d.outputBuffer = malloc(256 * 256 * BYTES_PER_PIXEL);
34	renderer->d.outputBufferStride = 256;
35
36	GBAGLContextCreate(&renderer->gl);
37	renderer->gl.d.user = renderer;
38	renderer->gl.d.lockAspectRatio = renderer->lockAspectRatio;
39	renderer->gl.d.filter = renderer->filter;
40	renderer->gl.d.swap = GBASDLGLCommonSwap;
41	renderer->gl.d.init(&renderer->gl.d, 0);
42
43	_doViewport(renderer->viewportWidth, renderer->viewportHeight, &renderer->gl.d);
44	return true;
45}
46
47void GBASDLGLRunloop(struct GBAThread* context, struct SDLSoftwareRenderer* renderer) {
48	SDL_Event event;
49	struct VideoBackend* v = &renderer->gl.d;
50
51	while (context->state < THREAD_EXITING) {
52		while (SDL_PollEvent(&event)) {
53			GBASDLHandleEvent(context, &renderer->player, &event);
54#if SDL_VERSION_ATLEAST(2, 0, 0)
55			// Event handling can change the size of the screen
56			if (renderer->player.windowUpdated) {
57				SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
58				_doViewport(renderer->viewportWidth, renderer->viewportHeight, v);
59				renderer->player.windowUpdated = 0;
60			}
61#endif
62		}
63
64		if (GBASyncWaitFrameStart(&context->sync, context->frameskip)) {
65			v->postFrame(v, renderer->d.outputBuffer);
66		}
67		v->drawFrame(v);
68		GBASyncWaitFrameEnd(&context->sync);
69		v->swap(v);
70	}
71}
72
73void GBASDLGLDeinit(struct SDLSoftwareRenderer* renderer) {
74	if (renderer->gl.d.deinit) {
75		renderer->gl.d.deinit(&renderer->gl.d);
76	}
77	free(renderer->d.outputBuffer);
78#if SDL_VERSION_ATLEAST(2, 0, 0)
79	SDL_GL_DeleteContext(renderer->glCtx);
80#endif
81}