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