Skip to content

Commit

Permalink
Make SDL single threaded (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
freshollie authored Dec 5, 2024
1 parent 5c5335f commit 3cc9f44
Showing 1 changed file with 19 additions and 41 deletions.
60 changes: 19 additions & 41 deletions src/platform/pret_sdl/sdl2.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ struct bgPriority {
char subPriority;
};

SDL_Thread *mainLoopThread;
SDL_Window *sdlWindow;
SDL_Renderer *sdlRenderer;
SDL_Texture *sdlTexture;
Expand All @@ -102,26 +101,24 @@ SDL_Texture *vramTexture;
#define INITIAL_VIDEO_SCALE 1
unsigned int videoScale = INITIAL_VIDEO_SCALE;
unsigned int preFullscreenVideoScale = INITIAL_VIDEO_SCALE;
SDL_sem *vBlankSemaphore;
SDL_atomic_t isFrameAvailable;

bool speedUp = false;
bool videoScaleChanged = false;
bool isRunning = true;
bool paused = false;
bool stepOneFrame = false;
double simTime = 0;

double lastGameTime = 0;
double curGameTime = 0;
double fixedTimestep = 1.0 / 60.0; // 16.666667ms
double timeScale = 1.0;
// struct SiiRtcInfo internalClock;
double accumulator = 0.0;

static FILE *sSaveFile = NULL;

extern void AgbMain(void);
void DoSoftReset(void) {};

int DoMain(void *param);
void ProcessSDLEvents(void);
void VDraw(SDL_Texture *texture);
void VramDraw(SDL_Texture *texture);
Expand Down Expand Up @@ -223,14 +220,6 @@ int main(int argc, char **argv)
}
#endif

simTime = curGameTime = lastGameTime = SDL_GetPerformanceCounter();

isFrameAvailable.value = 0;
vBlankSemaphore = SDL_CreateSemaphore(0);
if (vBlankSemaphore == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to create Semaphore:\n %s", SDL_GetError());
}

#if ENABLE_AUDIO
SDL_AudioSpec want;

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

mainLoopThread = SDL_CreateThread(DoMain, "AgbMain", NULL);

double accumulator = 0.0;
AgbMain();

#if 0
memset(&internalClock, 0, sizeof(internalClock));
internalClock.status = SIIRTCINFO_24HOUR;
UpdateInternalClock();
#endif
return 0;
}

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

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

while (accumulator >= dt) {
REG_KEYINPUT = KEYS_MASK ^ Platform_GetKeyInput();
if (SDL_AtomicGet(&isFrameAvailable)) {
if (frameAvailable) {
VDraw(sdlTexture);
SDL_AtomicSet(&isFrameAvailable, 0);
frameAvailable = FALSE;

REG_DISPSTAT |= INTR_FLAG_VBLANK;

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

SDL_SemPost(vBlankSemaphore);

accumulator -= dt;
} else {
// Get another frame
return;
}
}

Expand All @@ -333,12 +324,11 @@ int main(int argc, char **argv)
#endif
}

// StoreSaveFile();
CloseSaveFile();

SDL_DestroyWindow(sdlWindow);
SDL_Quit();
return 0;
exit(0);
}

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

int DoMain(void *data)
{
AgbMain();
return 0;
}

void VBlankIntrWait(void)
{
SDL_AtomicSet(&isFrameAvailable, 1);
SDL_SemWait(vBlankSemaphore);
}

u8 BinToBcd(u8 bin)
{
int placeCounter = 1;
Expand Down

0 comments on commit 3cc9f44

Please sign in to comment.