all repos — mgba @ f6755a6e1b7b0cf2b944cd8ca842746f11d6bf82

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 <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 bool mSDLGLInit(struct mSDLRenderer* renderer);
17static void mSDLGLRunloop(struct mSDLRenderer* renderer, void* user);
18static void mSDLGLDeinit(struct mSDLRenderer* renderer);
19
20void mSDLGLCreate(struct mSDLRenderer* renderer) {
21	renderer->init = mSDLGLInit;
22	renderer->deinit = mSDLGLDeinit;
23	renderer->runloop = mSDLGLRunloop;
24}
25
26bool mSDLGLInit(struct mSDLRenderer* renderer) {
27	mSDLGLCommonInit(renderer);
28
29	size_t size = renderer->width * renderer->height * BYTES_PER_PIXEL;
30	renderer->outputBuffer = malloc(size);
31	memset(renderer->outputBuffer, 0, size);
32	renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, renderer->width);
33
34	mGLContextCreate(&renderer->gl);
35	renderer->gl.d.user = renderer;
36	renderer->gl.d.lockAspectRatio = renderer->lockAspectRatio;
37	renderer->gl.d.lockIntegerScaling = renderer->lockIntegerScaling;
38	renderer->gl.d.interframeBlending = renderer->interframeBlending;
39	renderer->gl.d.filter = renderer->filter;
40	renderer->gl.d.swap = mSDLGLCommonSwap;
41	renderer->gl.d.init(&renderer->gl.d, 0);
42	renderer->gl.d.setDimensions(&renderer->gl.d, renderer->width, renderer->height);
43
44	mSDLGLDoViewport(renderer->viewportWidth, renderer->viewportHeight, &renderer->gl.d);
45	return true;
46}
47
48void mSDLGLRunloop(struct mSDLRenderer* renderer, void* user) {
49	struct mCoreThread* context = user;
50	SDL_Event event;
51	struct VideoBackend* v = &renderer->gl.d;
52
53	while (mCoreThreadIsActive(context)) {
54		while (SDL_PollEvent(&event)) {
55			mSDLHandleEvent(context, &renderer->player, &event);
56			// Event handling can change the size of the screen
57			if (renderer->player.windowUpdated) {
58#if SDL_VERSION_ATLEAST(2, 0, 0)
59				SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
60#else
61				renderer->viewportWidth = renderer->player.newWidth;
62				renderer->viewportHeight = renderer->player.newHeight;
63				mSDLGLCommonInit(renderer);
64#endif
65				mSDLGLDoViewport(renderer->viewportWidth, renderer->viewportHeight, v);
66				renderer->player.windowUpdated = 0;
67			}
68		}
69		renderer->core->desiredVideoDimensions(renderer->core, &renderer->width, &renderer->height);
70		if (renderer->width != v->width || renderer->height != v->height) {
71			renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, renderer->width);
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}