From cd29cca495652492bde2819b6625d232f02d8737 Mon Sep 17 00:00:00 2001 From: Simon Gellis Date: Mon, 21 Oct 2024 23:31:55 -0400 Subject: [PATCH] Switch to SDL3 --- README.md | 2 +- audio.c | 17 +++++++---------- audio.h | 4 ++-- controller.c | 22 +++++++++++----------- controller.h | 2 +- game.c | 12 ++++++------ graphics.c | 10 +++++----- graphics.h | 2 +- main.c | 5 +++-- makefile | 6 +++--- 10 files changed, 40 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 35758eb..6720e87 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ An SDL-based implementation of shrooms-vb. Install the following dependencies: - `gcc` (or MinGW on Windows) (or whatever, just set `CC`) - `pkg-config` - - sdl2 + - sdl3 Run ```sh diff --git a/audio.c b/audio.c index a9faba1..b36deda 100644 --- a/audio.c +++ b/audio.c @@ -4,31 +4,28 @@ int audioInit(AudioContext *aud) { SDL_AudioSpec spec; spec.freq = 41700; - spec.format = AUDIO_S16; + spec.format = SDL_AUDIO_S16; spec.channels = 2; - spec.samples = 1024; - spec.callback = NULL; - spec.userdata = NULL; - aud->id = SDL_OpenAudioDevice(NULL, 0, &spec, NULL, 0); + aud->stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, NULL, NULL); aud->paused = true; - if (!aud->id) { - fprintf(stderr, "could not open audio device: %s\n", SDL_GetError()); + if (!aud->stream) { + fprintf(stderr, "could not open audio device stream: %s\n", SDL_GetError()); return -1; } return 0; } int audioUpdate(AudioContext *aud, void *data, uint32_t bytes) { - if (!aud->id) return -1; + if (!aud->stream) return -1; - if (SDL_QueueAudio(aud->id, data, bytes)) { + if (!SDL_PutAudioStreamData(aud->stream, data, bytes)) { fprintf(stderr, "could not write audio: %s\n", SDL_GetError()); return -1; } if (aud->paused) { - SDL_PauseAudioDevice(aud->id, false); + SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(aud->stream)); aud->paused = false; } diff --git a/audio.h b/audio.h index 075239a..ebbc1d7 100644 --- a/audio.h +++ b/audio.h @@ -1,11 +1,11 @@ #ifndef SHROOMS_VB_NATIVE_AUDIO_ #define SHROOMS_VB_NATIVE_AUDIO_ -#include +#include #include typedef struct { - SDL_AudioDeviceID id; + SDL_AudioStream *stream; bool paused; } AudioContext; diff --git a/controller.c b/controller.c index 54979a4..3dcda5a 100644 --- a/controller.c +++ b/controller.c @@ -17,28 +17,28 @@ #define VB_RL 0x4000 #define VB_RD 0x8000 -static uint16_t symToMask(SDL_KeyCode sym) { +static uint16_t symToMask(SDL_Keycode sym) { switch (sym) { default: return 0; - case SDLK_a: + case SDLK_A: return VB_SEL; - case SDLK_s: + case SDLK_S: return VB_STA; - case SDLK_d: + case SDLK_D: return VB_B; - case SDLK_f: + case SDLK_F: return VB_A; - case SDLK_e: + case SDLK_E: return VB_LT; - case SDLK_r: + case SDLK_R: return VB_RT; - case SDLK_i: + case SDLK_I: return VB_RU; - case SDLK_j: + case SDLK_J: return VB_RL; - case SDLK_k: + case SDLK_K: return VB_RD; - case SDLK_l: + case SDLK_L: return VB_RR; case SDLK_UP: return VB_LU; diff --git a/controller.h b/controller.h index fe9dabe..181f580 100644 --- a/controller.h +++ b/controller.h @@ -1,7 +1,7 @@ #ifndef SHROOMS_VB_NATIVE_CONTROLLER_ #define SHROOMS_VB_NATIVE_CONTROLLER_ -#include +#include #include typedef struct { diff --git a/game.c b/game.c index b818d75..f5ed2c5 100644 --- a/game.c +++ b/game.c @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include #include @@ -73,14 +73,14 @@ int runGame(VB *sim, GraphicsContext *gfx) { sleepNanos(((MAX_STEP_CLOCKS - clocks) * 50) - (ticks - prevTicks)); while (SDL_PollEvent(&event)) { - if (event.type == SDL_QUIT) { + if (event.type == SDL_EVENT_QUIT) { return 0; } - if (event.type == SDL_KEYDOWN) { - ctrlKeyDown(&ctrl, event.key.keysym.sym); + if (event.type == SDL_EVENT_KEY_DOWN) { + ctrlKeyDown(&ctrl, event.key.key); } - if (event.type == SDL_KEYUP) { - ctrlKeyUp(&ctrl, event.key.keysym.sym); + if (event.type == SDL_EVENT_KEY_UP) { + ctrlKeyUp(&ctrl, event.key.key); } } vbSetKeys(sim, ctrlKeys(&ctrl)); diff --git a/graphics.c b/graphics.c index 4eb15b7..10df940 100644 --- a/graphics.c +++ b/graphics.c @@ -1,4 +1,5 @@ #include +#include static void copyScreenTexture(uint8_t *dst, const uint8_t *src, int pitch) { int x, y, i; @@ -16,8 +17,7 @@ static void copyScreenTexture(uint8_t *dst, const uint8_t *src, int pitch) { int gfxInit(GraphicsContext *gfx) { gfx->window = SDL_CreateWindow("Shrooms VB", - SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, - 1536, 896, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI); + 1536, 896, SDL_WINDOW_HIGH_PIXEL_DENSITY); if (!gfx->window) { fprintf(stderr, "Error creating window: %s\n", SDL_GetError()); return 1; @@ -68,7 +68,7 @@ void gfxDestroy(GraphicsContext *gfx) { static void gfxUpdateEye(SDL_Texture *eye, const uint8_t *bytes) { void *target; int pitch; - if (SDL_LockTexture(eye, NULL, &target, &pitch)) { + if (!SDL_LockTexture(eye, NULL, &target, &pitch)) { fprintf(stderr, "Error locking buffer for eye: %s\n", SDL_GetError()); return; } @@ -85,8 +85,8 @@ void gfxUpdateRightEye(GraphicsContext *gfx, const uint8_t *bytes) { void gfxRender(GraphicsContext *gfx) { SDL_RenderClear(gfx->renderer); - SDL_RenderCopy(gfx->renderer, gfx->leftEye, NULL, NULL); - SDL_RenderCopy(gfx->renderer, gfx->rightEye, NULL, NULL); + SDL_RenderTexture(gfx->renderer, gfx->leftEye, NULL, NULL); + SDL_RenderTexture(gfx->renderer, gfx->rightEye, NULL, NULL); SDL_RenderPresent(gfx->renderer); } diff --git a/graphics.h b/graphics.h index aa4485b..df3adcb 100644 --- a/graphics.h +++ b/graphics.h @@ -1,7 +1,7 @@ #ifndef SHROOMS_VB_NATIVE_GRAPHICS_ #define SHROOMS_VB_NATIVE_GRAPHICS_ -#include +#include typedef struct { SDL_Window *window; diff --git a/main.c b/main.c index eb7eedf..2fe48e4 100644 --- a/main.c +++ b/main.c @@ -1,9 +1,10 @@ #include #include #include -#include +#include #include "shrooms-vb-core/core/vb.h" #include +#include uint8_t *readROM(char *filename, uint32_t *size) { FILE *file = fopen(filename, "rb"); @@ -69,7 +70,7 @@ int main(int argc, char **argv) { vbInit(sim); vbSetCartROM(sim, rom, romSize); - if (SDL_Init(SDL_INIT_EVERYTHING)) { + if (!SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO)) { fprintf(stderr, "Error initializing SDL: %s\n", SDL_GetError()); return 1; } diff --git a/makefile b/makefile index 85f346f..dc56768 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,6 @@ CC?=gcc SHROOMSFLAGS=shrooms-vb-core/core/vb.c -I shrooms-vb-core/core -SDL2FLAGS=$(shell pkg-config sdl2 --cflags --libs) +SDL3FLAGS=$(shell pkg-config sdl3 --cflags --libs) .PHONY: clean clean: @@ -12,8 +12,8 @@ endif build: @$(CC) audio.c cli.c controller.c game.c graphics.c main.c assets/assets.s -I . \ - $(SHROOMSFLAGS) $(SDL2FLAGS) \ - -D POSIX_C_SOURCE=199309L \ + $(SHROOMSFLAGS) $(SDL3FLAGS) \ + -D _POSIX_C_SOURCE=199309L \ -o shrooms-vb \ -O3 -fno-strict-aliasing \ -Werror -std=c90 -Wall -Wextra -Wpedantic \ No newline at end of file