Draw Things

This commit is contained in:
Simon Gellis 2024-10-19 18:04:15 -04:00
parent a4b81c2603
commit addff55c24
4 changed files with 36 additions and 6 deletions

35
game.c
View File

@ -1,6 +1,7 @@
#include <assets.h> #include <assets.h>
#include <game.h> #include <game.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <stdbool.h>
#include <time.h> #include <time.h>
int sleepNanos(long int ns) { int sleepNanos(long int ns) {
@ -10,24 +11,52 @@ int sleepNanos(long int ns) {
return nanosleep(&time, NULL); 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) { int runGame(VB *sim, GraphicsContext *gfx) {
uint32_t clocks; uint32_t clocks;
SDL_Event event; SDL_Event event;
GameState state;
uint64_t ticks, prevTicks;
state.gfx = gfx;
vbSetUserData(sim, &state);
vbSetFrameCallback(sim, &onFrame);
gfxUpdateLeftEye(gfx, LEFT_EYE_DEFAULT); gfxUpdateLeftEye(gfx, LEFT_EYE_DEFAULT);
gfxUpdateRightEye(gfx, RIGHT_EYE_DEFAULT); gfxUpdateRightEye(gfx, RIGHT_EYE_DEFAULT);
prevTicks = SDL_GetTicks64();
while (1) { while (1) {
clocks = MAX_STEP_CLOCKS; clocks = MAX_STEP_CLOCKS;
vbEmulate(sim, &clocks); vbEmulate(sim, &clocks);
gfxRender(gfx); ticks = SDL_GetTicks64();
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) { if (event.type == SDL_QUIT) {
return 0; return 0;
} }
} }
sleepNanos((MAX_STEP_CLOCKS - clocks) * 50); sleepNanos(((MAX_STEP_CLOCKS - clocks) * 50) - (ticks - prevTicks) * 1000);
prevTicks = ticks;
} }
} }

View File

@ -40,14 +40,14 @@ int gfxInit(GraphicsContext *gfx) {
fprintf(stderr, "Error creating left eye texture: %s\n", SDL_GetError()); fprintf(stderr, "Error creating left eye texture: %s\n", SDL_GetError());
goto cleanup_window; 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); gfx->rightEye = SDL_CreateTexture(gfx->renderer, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING, 384, 224);
if (!gfx->rightEye) { if (!gfx->rightEye) {
fprintf(stderr, "Error creating left eye texture: %s\n", SDL_GetError()); fprintf(stderr, "Error creating left eye texture: %s\n", SDL_GetError());
goto cleanup_left_eye; 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); SDL_SetTextureBlendMode(gfx->rightEye, SDL_BLENDMODE_ADD);
return 0; return 0;

View File

@ -15,4 +15,5 @@ build:
$(SHROOMSFLAGS) $(SDL2FLAGS) \ $(SHROOMSFLAGS) $(SDL2FLAGS) \
-D POSIX_C_SOURCE=199309L \ -D POSIX_C_SOURCE=199309L \
-o shrooms-vb \ -o shrooms-vb \
-O3 -fno-strict-aliasing \
-Werror -std=c90 -Wall -Wextra -Wpedantic -Werror -std=c90 -Wall -Wextra -Wpedantic

@ -1 +1 @@
Subproject commit 767675eb42254613170e937b5b67bd9c109a9cf8 Subproject commit 37b9a943384a230b93089b7617d1b3f5344480d7