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 return 1;
58 }
59
60 renderer.viewportWidth = graphicsOpts.width;
61 renderer.viewportHeight = graphicsOpts.height;
62
63 if (!_GBASDLInit(&renderer)) {
64 return 1;
65 }
66
67 struct GBAThread context = {
68 .renderer = &renderer.d.d,
69 .startCallback = _GBASDLStart,
70 .cleanCallback = _GBASDLClean,
71 .sync.videoFrameWait = 0,
72 .sync.audioWait = 1,
73 .userData = &renderer
74 };
75
76 context.debugger = createDebugger(&opts);
77
78 GBAMapOptionsToContext(&opts, &context);
79
80#if SDL_VERSION_ATLEAST(2, 0, 0)
81 renderer.events.fullscreen = graphicsOpts.fullscreen;
82 renderer.window = SDL_CreateWindow("GBAc", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, renderer.viewportWidth, renderer.viewportHeight, SDL_WINDOW_OPENGL | (SDL_WINDOW_FULLSCREEN_DESKTOP * renderer.events.fullscreen));
83 SDL_GetWindowSize(renderer.window, &renderer.viewportWidth, &renderer.viewportHeight);
84 renderer.events.window = renderer.window;
85 renderer.sdlRenderer = SDL_CreateRenderer(renderer.window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
86#ifdef COLOR_16_BIT
87#ifdef COLOR_5_6_5
88 renderer.tex = SDL_CreateTexture(renderer.sdlRenderer, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STREAMING, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
89#else
90 renderer.tex = SDL_CreateTexture(renderer.sdlRenderer, SDL_PIXELFORMAT_ABGR1555, SDL_TEXTUREACCESS_STREAMING, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
91#endif
92#else
93 renderer.tex = SDL_CreateTexture(renderer.sdlRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
94#endif
95
96 SDL_LockTexture(renderer.tex, 0, &renderer.d.outputBuffer, &renderer.d.outputBufferStride);
97#ifdef COLOR_16_BIT
98 renderer.d.outputBufferStride /= 2;
99#else
100 renderer.d.outputBufferStride /= 4;
101#endif
102#else
103 SDL_Surface* surface = SDL_GetVideoSurface();
104 SDL_LockSurface(surface);
105
106 renderer.ratio = graphicsOpts.multiplier;
107 if (renderer.ratio == 1) {
108 renderer.d.outputBuffer = surface->pixels;
109#ifdef COLOR_16_BIT
110 renderer.d.outputBufferStride = surface->pitch / 2;
111#else
112 renderer.d.outputBufferStride = surface->pitch / 4;
113#endif
114 } else {
115#ifdef COLOR_16_BIT
116 renderer.d.outputBuffer = malloc(240 * 160 * 2);
117#else
118 renderer.d.outputBuffer = malloc(240 * 160 * 4);
119#endif
120 renderer.d.outputBufferStride = 240;
121 }
122#endif
123
124 GBAThreadStart(&context);
125
126 _GBASDLRunloop(&context, &renderer);
127
128#if !SDL_VERSION_ATLEAST(2, 0, 0)
129 SDL_UnlockSurface(surface);
130#endif
131 GBAThreadJoin(&context);
132 close(opts.fd);
133 if (opts.biosFd >= 0) {
134 close(opts.biosFd);
135 }
136 free(context.debugger);
137
138 _GBASDLDeinit(&renderer);
139
140 return 0;
141}
142
143static int _GBASDLInit(struct SoftwareRenderer* renderer) {
144 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
145 return 0;
146 }
147
148 GBASDLInitEvents(&renderer->events);
149 GBASDLInitAudio(&renderer->audio);
150
151#if !SDL_VERSION_ATLEAST(2, 0, 0)
152#ifdef COLOR_16_BIT
153 SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 16, SDL_DOUBLEBUF | SDL_HWSURFACE);
154#else
155 SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 32, SDL_DOUBLEBUF | SDL_HWSURFACE);
156#endif
157#endif
158
159 return 1;
160}
161
162static void _GBASDLRunloop(struct GBAThread* context, struct SoftwareRenderer* renderer) {
163 SDL_Event event;
164#if !SDL_VERSION_ATLEAST(2, 0, 0)
165 SDL_Surface* surface = SDL_GetVideoSurface();
166#endif
167
168 while (context->state < THREAD_EXITING) {
169 if (GBASyncWaitFrameStart(&context->sync, context->frameskip)) {
170#if SDL_VERSION_ATLEAST(2, 0, 0)
171 SDL_UnlockTexture(renderer->tex);
172 SDL_RenderCopy(renderer->sdlRenderer, renderer->tex, 0, 0);
173 SDL_RenderPresent(renderer->sdlRenderer);
174 SDL_LockTexture(renderer->tex, 0, &renderer->d.outputBuffer, &renderer->d.outputBufferStride);
175#ifdef COLOR_16_BIT
176 renderer->d.outputBufferStride /= 2;
177#else
178 renderer->d.outputBufferStride /= 4;
179#endif
180#else
181 switch (renderer->ratio) {
182#if defined(__ARM_NEON) && COLOR_16_BIT
183 case 2:
184 _neon2x(surface->pixels, renderer->d.outputBuffer, 240, 160);
185 break;
186 case 4:
187 _neon4x(surface->pixels, renderer->d.outputBuffer, 240, 160);
188 break;
189#endif
190 case 1:
191 break;
192 default:
193 abort();
194 }
195 SDL_UnlockSurface(surface);
196 SDL_Flip(surface);
197 SDL_LockSurface(surface);
198#endif
199 }
200 GBASyncWaitFrameEnd(&context->sync);
201
202 while (SDL_PollEvent(&event)) {
203 GBASDLHandleEvent(context, &renderer->events, &event);
204 }
205 }
206}
207
208static void _GBASDLDeinit(struct SoftwareRenderer* renderer) {
209 GBASDLDeinitEvents(&renderer->events);
210 GBASDLDeinitAudio(&renderer->audio);
211 SDL_Quit();
212}
213
214static void _GBASDLStart(struct GBAThread* threadContext) {
215 struct SoftwareRenderer* renderer = threadContext->userData;
216 renderer->audio.audio = &threadContext->gba->audio;
217}
218
219static void _GBASDLClean(struct GBAThread* threadContext) {
220 struct SoftwareRenderer* renderer = threadContext->userData;
221 renderer->audio.audio = 0;
222}