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