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