Skip to content

Commit 3cc9f44

Browse files
authored
Make SDL single threaded (#173)
1 parent 5c5335f commit 3cc9f44

File tree

1 file changed

+19
-41
lines changed

1 file changed

+19
-41
lines changed

src/platform/pret_sdl/sdl2.c

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ struct bgPriority {
9090
char subPriority;
9191
};
9292

93-
SDL_Thread *mainLoopThread;
9493
SDL_Window *sdlWindow;
9594
SDL_Renderer *sdlRenderer;
9695
SDL_Texture *sdlTexture;
@@ -102,26 +101,24 @@ SDL_Texture *vramTexture;
102101
#define INITIAL_VIDEO_SCALE 1
103102
unsigned int videoScale = INITIAL_VIDEO_SCALE;
104103
unsigned int preFullscreenVideoScale = INITIAL_VIDEO_SCALE;
105-
SDL_sem *vBlankSemaphore;
106-
SDL_atomic_t isFrameAvailable;
104+
107105
bool speedUp = false;
108106
bool videoScaleChanged = false;
109107
bool isRunning = true;
110108
bool paused = false;
111109
bool stepOneFrame = false;
112-
double simTime = 0;
110+
113111
double lastGameTime = 0;
114112
double curGameTime = 0;
115113
double fixedTimestep = 1.0 / 60.0; // 16.666667ms
116114
double timeScale = 1.0;
117-
// struct SiiRtcInfo internalClock;
115+
double accumulator = 0.0;
118116

119117
static FILE *sSaveFile = NULL;
120118

121119
extern void AgbMain(void);
122120
void DoSoftReset(void) {};
123121

124-
int DoMain(void *param);
125122
void ProcessSDLEvents(void);
126123
void VDraw(SDL_Texture *texture);
127124
void VramDraw(SDL_Texture *texture);
@@ -223,14 +220,6 @@ int main(int argc, char **argv)
223220
}
224221
#endif
225222

226-
simTime = curGameTime = lastGameTime = SDL_GetPerformanceCounter();
227-
228-
isFrameAvailable.value = 0;
229-
vBlankSemaphore = SDL_CreateSemaphore(0);
230-
if (vBlankSemaphore == NULL) {
231-
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to create Semaphore:\n %s", SDL_GetError());
232-
}
233-
234223
#if ENABLE_AUDIO
235224
SDL_AudioSpec want;
236225

@@ -256,18 +245,19 @@ int main(int argc, char **argv)
256245
#endif
257246
// Prevent the multiplayer screen from being drawn ( see core.c:GameInit() )
258247
REG_RCNT = 0x8000;
248+
REG_KEYINPUT = 0x3FF;
259249

260-
mainLoopThread = SDL_CreateThread(DoMain, "AgbMain", NULL);
261-
262-
double accumulator = 0.0;
250+
AgbMain();
263251

264-
#if 0
265-
memset(&internalClock, 0, sizeof(internalClock));
266-
internalClock.status = SIIRTCINFO_24HOUR;
267-
UpdateInternalClock();
268-
#endif
252+
return 0;
253+
}
269254

270-
REG_KEYINPUT = 0x3FF;
255+
// Every GBA frame we process the SDL events and render the number of times
256+
// SDL requires us to for vsync. When we need another frame we break out of
257+
// the loop via a return
258+
void VBlankIntrWait(void)
259+
{
260+
bool frameAvailable = TRUE;
271261

272262
while (isRunning) {
273263
ProcessSDLEvents();
@@ -290,9 +280,9 @@ int main(int argc, char **argv)
290280

291281
while (accumulator >= dt) {
292282
REG_KEYINPUT = KEYS_MASK ^ Platform_GetKeyInput();
293-
if (SDL_AtomicGet(&isFrameAvailable)) {
283+
if (frameAvailable) {
294284
VDraw(sdlTexture);
295-
SDL_AtomicSet(&isFrameAvailable, 0);
285+
frameAvailable = FALSE;
296286

297287
REG_DISPSTAT |= INTR_FLAG_VBLANK;
298288

@@ -304,9 +294,10 @@ int main(int argc, char **argv)
304294
gIntrTable[INTR_INDEX_VBLANK]();
305295
REG_DISPSTAT &= ~INTR_FLAG_VBLANK;
306296

307-
SDL_SemPost(vBlankSemaphore);
308-
309297
accumulator -= dt;
298+
} else {
299+
// Get another frame
300+
return;
310301
}
311302
}
312303

@@ -333,12 +324,11 @@ int main(int argc, char **argv)
333324
#endif
334325
}
335326

336-
// StoreSaveFile();
337327
CloseSaveFile();
338328

339329
SDL_DestroyWindow(sdlWindow);
340330
SDL_Quit();
341-
return 0;
331+
exit(0);
342332
}
343333

344334
static void ReadSaveFile(char *path)
@@ -2021,18 +2011,6 @@ void VDraw(SDL_Texture *texture)
20212011
REG_VCOUNT = 161; // prep for being in VBlank period
20222012
}
20232013

2024-
int DoMain(void *data)
2025-
{
2026-
AgbMain();
2027-
return 0;
2028-
}
2029-
2030-
void VBlankIntrWait(void)
2031-
{
2032-
SDL_AtomicSet(&isFrameAvailable, 1);
2033-
SDL_SemWait(vBlankSemaphore);
2034-
}
2035-
20362014
u8 BinToBcd(u8 bin)
20372015
{
20382016
int placeCounter = 1;

0 commit comments

Comments
 (0)