all repos — mgba @ 36ad461ee002b199111baa68b57122679c443894

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.filter = renderer->filter;
39	renderer->gl.d.swap = mSDLGLCommonSwap;
40	renderer->gl.d.init(&renderer->gl.d, 0);
41	renderer->gl.d.setDimensions(&renderer->gl.d, renderer->width, renderer->height);
42
43	mSDLGLDoViewport(renderer->viewportWidth, renderer->viewportHeight, &renderer->gl.d);
44	return true;
45}
46
47void mSDLGLRunloop(struct mSDLRenderer* renderer, void* user) {
48	struct mCoreThread* context = user;
49	SDL_Event event;
50	struct VideoBackend* v = &renderer->gl.d;
51
52	while (mCoreThreadIsActive(context)) {
53		while (SDL_PollEvent(&event)) {
54			mSDLHandleEvent(context, &renderer->player, &event);
55			// Event handling can change the size of the screen
56			if (renderer->player.windowUpdated) {
57#if SDL_VERSION_ATLEAST(2, 0, 0)
58				SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
59#else
60				renderer->viewportWidth = renderer->player.newWidth;
61				renderer->viewportHeight = renderer->player.newHeight;
62				mSDLGLCommonInit(renderer);
63#endif
64				mSDLGLDoViewport(renderer->viewportWidth, renderer->viewportHeight, v);
65				renderer->player.windowUpdated = 0;
66			}
67		}
68		renderer->core->desiredVideoDimensions(renderer->core, &renderer->width, &renderer->height);
69		if (renderer->width != v->width || renderer->height != v->height) {
70			renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, renderer->width);
71			v->setDimensions(v, renderer->width, renderer->height);
72		}
73
74		if (mCoreSyncWaitFrameStart(&context->impl->sync)) {
75			v->postFrame(v, renderer->outputBuffer);
76		}
77		mCoreSyncWaitFrameEnd(&context->impl->sync);
78		v->drawFrame(v);
79		v->swap(v);
80	}
81}
82
83void mSDLGLDeinit(struct mSDLRenderer* renderer) {
84	if (renderer->gl.d.deinit) {
85		renderer->gl.d.deinit(&renderer->gl.d);
86	}
87	free(renderer->outputBuffer);
88#if SDL_VERSION_ATLEAST(2, 0, 0)
89	SDL_GL_DeleteContext(renderer->glCtx);
90#endif
91}