src/platform/sdl/sw-sdl2.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 <mgba/core/core.h>
9#include <mgba/core/thread.h>
10#include <mgba/core/version.h>
11
12static bool mSDLSWInit(struct mSDLRenderer* renderer);
13static void mSDLSWRunloop(struct mSDLRenderer* renderer, void* user);
14static void mSDLSWDeinit(struct mSDLRenderer* renderer);
15
16void mSDLSWCreate(struct mSDLRenderer* renderer) {
17 renderer->init = mSDLSWInit;
18 renderer->deinit = mSDLSWDeinit;
19 renderer->runloop = mSDLSWRunloop;
20}
21
22bool mSDLSWInit(struct mSDLRenderer* renderer) {
23 unsigned width, height;
24 renderer->core->desiredVideoDimensions(renderer->core, &width, &height);
25 renderer->window = SDL_CreateWindow(projectName, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, renderer->viewportWidth, renderer->viewportHeight, SDL_WINDOW_OPENGL | (SDL_WINDOW_FULLSCREEN_DESKTOP * renderer->player.fullscreen));
26 SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
27 renderer->player.window = renderer->window;
28 renderer->sdlRenderer = SDL_CreateRenderer(renderer->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
29#ifdef COLOR_16_BIT
30#ifdef COLOR_5_6_5
31 renderer->sdlTex = SDL_CreateTexture(renderer->sdlRenderer, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STREAMING, width, height);
32#else
33 renderer->sdlTex = SDL_CreateTexture(renderer->sdlRenderer, SDL_PIXELFORMAT_ABGR1555, SDL_TEXTUREACCESS_STREAMING, width, height);
34#endif
35#else
36 renderer->sdlTex = SDL_CreateTexture(renderer->sdlRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, width, height);
37#endif
38
39 int stride;
40 SDL_LockTexture(renderer->sdlTex, 0, (void**) &renderer->outputBuffer, &stride);
41 renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, stride / BYTES_PER_PIXEL);
42
43 return true;
44}
45
46void mSDLSWRunloop(struct mSDLRenderer* renderer, void* user) {
47 struct mCoreThread* context = user;
48 SDL_Event event;
49
50 while (mCoreThreadIsActive(context)) {
51 while (SDL_PollEvent(&event)) {
52 mSDLHandleEvent(context, &renderer->player, &event);
53 }
54
55 if (mCoreSyncWaitFrameStart(&context->impl->sync)) {
56 SDL_UnlockTexture(renderer->sdlTex);
57 SDL_RenderCopy(renderer->sdlRenderer, renderer->sdlTex, 0, 0);
58 SDL_RenderPresent(renderer->sdlRenderer);
59 int stride;
60 SDL_LockTexture(renderer->sdlTex, 0, (void**) &renderer->outputBuffer, &stride);
61 renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, stride / BYTES_PER_PIXEL);
62 }
63 mCoreSyncWaitFrameEnd(&context->impl->sync);
64 }
65}
66
67void mSDLSWDeinit(struct mSDLRenderer* renderer) {
68 if (renderer->ratio > 1) {
69 free(renderer->outputBuffer);
70 }
71}