src/platform/sdl/sw-main.c (view raw)
1#ifdef USE_CLI_DEBUGGER
2#include "debugger/cli-debugger.h"
3#endif
4
5#ifdef USE_GDB_STUB
6#include "debugger/gdb-stub.h"
7#endif
8
9#include "gba-thread.h"
10#include "gba.h"
11#include "renderers/video-software.h"
12#include "sdl-audio.h"
13#include "sdl-events.h"
14
15#include <SDL.h>
16
17#include <errno.h>
18#include <signal.h>
19#include <sys/time.h>
20
21#ifdef __ARM_NEON
22void _neon2x(void* dest, void* src, int width, int height);
23void _neon4x(void* dest, void* src, int width, int height);
24#endif
25
26struct SoftwareRenderer {
27 struct GBAVideoSoftwareRenderer d;
28 struct GBASDLAudio audio;
29 struct GBASDLEvents events;
30#if SDL_VERSION_ATLEAST(2, 0, 0)
31 SDL_Window* window;
32 SDL_Texture* tex;
33 SDL_Renderer* sdlRenderer;
34#else
35 int ratio;
36#endif
37 int viewportWidth;
38 int viewportHeight;
39};
40
41static int _GBASDLInit(struct SoftwareRenderer* renderer);
42static void _GBASDLDeinit(struct SoftwareRenderer* renderer);
43static void _GBASDLRunloop(struct GBAThread* context, struct SoftwareRenderer* renderer);
44static void _GBASDLStart(struct GBAThread* context);
45static void _GBASDLClean(struct GBAThread* context);
46
47int main(int argc, char** argv) {
48 struct SoftwareRenderer renderer;
49 GBAVideoSoftwareRendererCreate(&renderer.d);
50
51 struct StartupOptions opts;
52 struct SubParser subparser;
53 struct GraphicsOpts graphicsOpts;
54 initParserForGraphics(&subparser, &graphicsOpts);
55 if (!parseCommandArgs(&opts, argc, argv, &subparser)) {
56 usage(argv[0], subparser.usage);
57 freeOptions(&opts);
58 return 1;
59 }
60
61 renderer.viewportWidth = graphicsOpts.width;
62 renderer.viewportHeight = graphicsOpts.height;
63
64 if (!_GBASDLInit(&renderer)) {
65 freeOptions(&opts);
66 return 1;
67 }
68
69 struct GBAThread context = {
70 .renderer = &renderer.d.d,
71 .startCallback = _GBASDLStart,
72 .cleanCallback = _GBASDLClean,
73 .sync.videoFrameWait = 0,
74 .sync.audioWait = 1,
75 .userData = &renderer
76 };
77
78 context.debugger = createDebugger(&opts);
79
80 GBAMapOptionsToContext(&opts, &context);
81
82 renderer.audio.samples = context.audioBuffers;
83 GBASDLInitAudio(&renderer.audio);
84
85#if SDL_VERSION_ATLEAST(2, 0, 0)
86 renderer.events.fullscreen = graphicsOpts.fullscreen;
87 renderer.window = SDL_CreateWindow(PROJECT_NAME, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, renderer.viewportWidth, renderer.viewportHeight, SDL_WINDOW_OPENGL | (SDL_WINDOW_FULLSCREEN_DESKTOP * renderer.events.fullscreen));
88 SDL_GetWindowSize(renderer.window, &renderer.viewportWidth, &renderer.viewportHeight);
89 renderer.events.window = renderer.window;
90 renderer.sdlRenderer = SDL_CreateRenderer(renderer.window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
91#ifdef COLOR_16_BIT
92#ifdef COLOR_5_6_5
93 renderer.tex = SDL_CreateTexture(renderer.sdlRenderer, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STREAMING, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
94#else
95 renderer.tex = SDL_CreateTexture(renderer.sdlRenderer, SDL_PIXELFORMAT_ABGR1555, SDL_TEXTUREACCESS_STREAMING, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
96#endif
97#else
98 renderer.tex = SDL_CreateTexture(renderer.sdlRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
99#endif
100
101 SDL_LockTexture(renderer.tex, 0, &renderer.d.outputBuffer, &renderer.d.outputBufferStride);
102#ifdef COLOR_16_BIT
103 renderer.d.outputBufferStride /= 2;
104#else
105 renderer.d.outputBufferStride /= 4;
106#endif
107#else
108 SDL_Surface* surface = SDL_GetVideoSurface();
109 SDL_LockSurface(surface);
110
111 renderer.ratio = graphicsOpts.multiplier;
112 if (renderer.ratio == 1) {
113 renderer.d.outputBuffer = surface->pixels;
114#ifdef COLOR_16_BIT
115 renderer.d.outputBufferStride = surface->pitch / 2;
116#else
117 renderer.d.outputBufferStride = surface->pitch / 4;
118#endif
119 } else {
120#ifdef COLOR_16_BIT
121 renderer.d.outputBuffer = malloc(240 * 160 * 2);
122#else
123 renderer.d.outputBuffer = malloc(240 * 160 * 4);
124#endif
125 renderer.d.outputBufferStride = 240;
126 }
127#endif
128
129 GBAThreadStart(&context);
130
131 _GBASDLRunloop(&context, &renderer);
132
133#if !SDL_VERSION_ATLEAST(2, 0, 0)
134 SDL_UnlockSurface(surface);
135#endif
136 GBAThreadJoin(&context);
137 free(context.debugger);
138 freeOptions(&opts);
139
140 _GBASDLDeinit(&renderer);
141
142 return 0;
143}
144
145static int _GBASDLInit(struct SoftwareRenderer* renderer) {
146 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
147 return 0;
148 }
149
150 GBASDLInitEvents(&renderer->events);
151
152#if !SDL_VERSION_ATLEAST(2, 0, 0)
153#ifdef COLOR_16_BIT
154 SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 16, SDL_DOUBLEBUF | SDL_HWSURFACE);
155#else
156 SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 32, SDL_DOUBLEBUF | SDL_HWSURFACE);
157#endif
158#endif
159
160 return 1;
161}
162
163static void _GBASDLRunloop(struct GBAThread* context, struct SoftwareRenderer* renderer) {
164 SDL_Event event;
165#if !SDL_VERSION_ATLEAST(2, 0, 0)
166 SDL_Surface* surface = SDL_GetVideoSurface();
167#endif
168
169 while (context->state < THREAD_EXITING) {
170 if (GBASyncWaitFrameStart(&context->sync, context->frameskip)) {
171#if SDL_VERSION_ATLEAST(2, 0, 0)
172 SDL_UnlockTexture(renderer->tex);
173 SDL_RenderCopy(renderer->sdlRenderer, renderer->tex, 0, 0);
174 SDL_RenderPresent(renderer->sdlRenderer);
175 SDL_LockTexture(renderer->tex, 0, &renderer->d.outputBuffer, &renderer->d.outputBufferStride);
176#ifdef COLOR_16_BIT
177 renderer->d.outputBufferStride /= 2;
178#else
179 renderer->d.outputBufferStride /= 4;
180#endif
181#else
182 switch (renderer->ratio) {
183#if defined(__ARM_NEON) && COLOR_16_BIT
184 case 2:
185 _neon2x(surface->pixels, renderer->d.outputBuffer, 240, 160);
186 break;
187 case 4:
188 _neon4x(surface->pixels, renderer->d.outputBuffer, 240, 160);
189 break;
190#endif
191 case 1:
192 break;
193 default:
194 abort();
195 }
196 SDL_UnlockSurface(surface);
197 SDL_Flip(surface);
198 SDL_LockSurface(surface);
199#endif
200 }
201 GBASyncWaitFrameEnd(&context->sync);
202
203 while (SDL_PollEvent(&event)) {
204 GBASDLHandleEvent(context, &renderer->events, &event);
205 }
206 }
207}
208
209static void _GBASDLDeinit(struct SoftwareRenderer* renderer) {
210 GBASDLDeinitEvents(&renderer->events);
211 GBASDLDeinitAudio(&renderer->audio);
212 SDL_Quit();
213}
214
215static void _GBASDLStart(struct GBAThread* threadContext) {
216 struct SoftwareRenderer* renderer = threadContext->userData;
217 renderer->audio.audio = &threadContext->gba->audio;
218}
219
220static void _GBASDLClean(struct GBAThread* threadContext) {
221 struct SoftwareRenderer* renderer = threadContext->userData;
222 renderer->audio.audio = 0;
223}