diff --git a/game.c b/game.c index 73662ff..c9d9b0a 100644 --- a/game.c +++ b/game.c @@ -1,6 +1,7 @@ #include #include #include +#include #include int sleepNanos(long int ns) { @@ -10,24 +11,52 @@ int sleepNanos(long int ns) { return nanosleep(&time, NULL); } -#define MAX_STEP_CLOCKS 20000 +typedef struct { + GraphicsContext *gfx; +} GameState; + +int onFrame(VB *sim) { + static uint8_t leftEye[384*224]; + static uint8_t rightEye[384*224]; + GameState *state; + GraphicsContext *gfx; + + state = vbGetUserData(sim); + gfx = state->gfx; + + vbGetPixels(sim, leftEye, 1, 384, rightEye, 1, 384); + gfxUpdateLeftEye(gfx, leftEye); + gfxUpdateRightEye(gfx, rightEye); + gfxRender(gfx); + return 1; +} + +#define MAX_STEP_CLOCKS 20000000 int runGame(VB *sim, GraphicsContext *gfx) { uint32_t clocks; SDL_Event event; + GameState state; + uint64_t ticks, prevTicks; + + state.gfx = gfx; + vbSetUserData(sim, &state); + vbSetFrameCallback(sim, &onFrame); gfxUpdateLeftEye(gfx, LEFT_EYE_DEFAULT); gfxUpdateRightEye(gfx, RIGHT_EYE_DEFAULT); + prevTicks = SDL_GetTicks64(); while (1) { clocks = MAX_STEP_CLOCKS; vbEmulate(sim, &clocks); - gfxRender(gfx); + ticks = SDL_GetTicks64(); while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { return 0; } } - sleepNanos((MAX_STEP_CLOCKS - clocks) * 50); + sleepNanos(((MAX_STEP_CLOCKS - clocks) * 50) - (ticks - prevTicks) * 1000); + prevTicks = ticks; } } diff --git a/graphics.c b/graphics.c index 6c3c0eb..4eb15b7 100644 --- a/graphics.c +++ b/graphics.c @@ -40,14 +40,14 @@ int gfxInit(GraphicsContext *gfx) { fprintf(stderr, "Error creating left eye texture: %s\n", SDL_GetError()); goto cleanup_window; } - SDL_SetTextureColorMod(gfx->leftEye, 255, 0, 0); + SDL_SetTextureColorMod(gfx->leftEye, 0xff, 0, 0); gfx->rightEye = SDL_CreateTexture(gfx->renderer, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING, 384, 224); if (!gfx->rightEye) { fprintf(stderr, "Error creating left eye texture: %s\n", SDL_GetError()); goto cleanup_left_eye; } - SDL_SetTextureColorMod(gfx->rightEye, 0, 0, 255); + SDL_SetTextureColorMod(gfx->rightEye, 0, 0xc6, 0xf0); SDL_SetTextureBlendMode(gfx->rightEye, SDL_BLENDMODE_ADD); return 0; diff --git a/makefile b/makefile index 62f6b92..ab15d20 100644 --- a/makefile +++ b/makefile @@ -15,4 +15,5 @@ build: $(SHROOMSFLAGS) $(SDL2FLAGS) \ -D POSIX_C_SOURCE=199309L \ -o shrooms-vb \ + -O3 -fno-strict-aliasing \ -Werror -std=c90 -Wall -Wextra -Wpedantic \ No newline at end of file diff --git a/shrooms-vb-core b/shrooms-vb-core index 767675e..37b9a94 160000 --- a/shrooms-vb-core +++ b/shrooms-vb-core @@ -1 +1 @@ -Subproject commit 767675eb42254613170e937b5b67bd9c109a9cf8 +Subproject commit 37b9a943384a230b93089b7617d1b3f5344480d7