all repos — mgba @ 4c38f769565e8ddd7d3a8eef1a41975206c129a0

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 "core/core.h"
11#include "core/thread.h"
12#include "platform/opengl/gl.h"
13
14static void _doViewport(int w, int h, struct VideoBackend* v) {
15	v->resized(v, w, h);
16	v->clear(v);
17	v->swap(v);
18	v->clear(v);
19}
20
21static bool mSDLGLInit(struct mSDLRenderer* renderer);
22static void mSDLGLRunloop(struct mSDLRenderer* renderer, void* user);
23static void mSDLGLDeinit(struct mSDLRenderer* renderer);
24
25void mSDLGLCreate(struct mSDLRenderer* renderer) {
26	renderer->init = mSDLGLInit;
27	renderer->deinit = mSDLGLDeinit;
28	renderer->runloop = mSDLGLRunloop;
29}
30
31bool mSDLGLInit(struct mSDLRenderer* renderer) {
32	mSDLGLCommonInit(renderer);
33
34	renderer->outputBuffer = malloc(renderer->width * renderer->height * BYTES_PER_PIXEL);
35	memset(renderer->outputBuffer, 0, renderer->width * renderer->height * BYTES_PER_PIXEL);
36	renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, renderer->width);
37
38	mGLContextCreate(&renderer->gl);
39	renderer->gl.d.user = renderer;
40	renderer->gl.d.lockAspectRatio = renderer->lockAspectRatio;
41	renderer->gl.d.filter = renderer->filter;
42	renderer->gl.d.swap = mSDLGLCommonSwap;
43	renderer->gl.d.init(&renderer->gl.d, 0);
44	renderer->gl.d.setDimensions(&renderer->gl.d, renderer->width, renderer->height);
45
46	_doViewport(renderer->viewportWidth, renderer->viewportHeight, &renderer->gl.d);
47	return true;
48}
49
50void mSDLGLRunloop(struct mSDLRenderer* renderer, void* user) {
51	struct mCoreThread* context = user;
52	SDL_Event event;
53	struct VideoBackend* v = &renderer->gl.d;
54
55	while (context->state < THREAD_EXITING) {
56		while (SDL_PollEvent(&event)) {
57			mSDLHandleEvent(context, &renderer->player, &event);
58#if SDL_VERSION_ATLEAST(2, 0, 0)
59			// Event handling can change the size of the screen
60			if (renderer->player.windowUpdated) {
61				SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
62				_doViewport(renderer->viewportWidth, renderer->viewportHeight, v);
63				renderer->player.windowUpdated = 0;
64			}
65#endif
66		}
67
68		if (mCoreSyncWaitFrameStart(&context->sync)) {
69			v->postFrame(v, renderer->outputBuffer);
70		}
71		mCoreSyncWaitFrameEnd(&context->sync);
72		v->drawFrame(v);
73		v->swap(v);
74	}
75}
76
77void mSDLGLDeinit(struct mSDLRenderer* renderer) {
78	if (renderer->gl.d.deinit) {
79		renderer->gl.d.deinit(&renderer->gl.d);
80	}
81	free(renderer->outputBuffer);
82#if SDL_VERSION_ATLEAST(2, 0, 0)
83	SDL_GL_DeleteContext(renderer->glCtx);
84#endif
85}