From 4e3495948cd6e7d8aafb3e9b2d2240dabe508829 Mon Sep 17 00:00:00 2001 From: Simon Gellis Date: Wed, 23 Oct 2024 19:01:54 -0400 Subject: [PATCH] Revert "Switch to SDL3" This reverts commit cd29cca495652492bde2819b6625d232f02d8737. --- 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, 42 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 6720e87..35758eb 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` - - sdl3 + - sdl2 Run ```sh diff --git a/audio.c b/audio.c index b36deda..a9faba1 100644 --- a/audio.c +++ b/audio.c @@ -4,28 +4,31 @@ int audioInit(AudioContext *aud) { SDL_AudioSpec spec; spec.freq = 41700; - spec.format = SDL_AUDIO_S16; + spec.format = AUDIO_S16; spec.channels = 2; + spec.samples = 1024; + spec.callback = NULL; + spec.userdata = NULL; - aud->stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, NULL, NULL); + aud->id = SDL_OpenAudioDevice(NULL, 0, &spec, NULL, 0); aud->paused = true; - if (!aud->stream) { - fprintf(stderr, "could not open audio device stream: %s\n", SDL_GetError()); + if (!aud->id) { + fprintf(stderr, "could not open audio device: %s\n", SDL_GetError()); return -1; } return 0; } int audioUpdate(AudioContext *aud, void *data, uint32_t bytes) { - if (!aud->stream) return -1; + if (!aud->id) return -1; - if (!SDL_PutAudioStreamData(aud->stream, data, bytes)) { + if (SDL_QueueAudio(aud->id, data, bytes)) { fprintf(stderr, "could not write audio: %s\n", SDL_GetError()); return -1; } if (aud->paused) { - SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(aud->stream)); + SDL_PauseAudioDevice(aud->id, false); aud->paused = false; } diff --git a/audio.h b/audio.h index ebbc1d7..075239a 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_AudioStream *stream; + SDL_AudioDeviceID id; bool paused; } AudioContext; diff --git a/controller.c b/controller.c index 3dcda5a..54979a4 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 181f580..fe9dabe 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 f5ed2c5..b818d75 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_EVENT_QUIT) { + if (event.type == SDL_QUIT) { return 0; } - if (event.type == SDL_EVENT_KEY_DOWN) { - ctrlKeyDown(&ctrl, event.key.key); + if (event.type == SDL_KEYDOWN) { + ctrlKeyDown(&ctrl, event.key.keysym.sym); } - if (event.type == SDL_EVENT_KEY_UP) { - ctrlKeyUp(&ctrl, event.key.key); + if (event.type == SDL_KEYUP) { + ctrlKeyUp(&ctrl, event.key.keysym.sym); } } vbSetKeys(sim, ctrlKeys(&ctrl)); diff --git a/graphics.c b/graphics.c index 10df940..4eb15b7 100644 --- a/graphics.c +++ b/graphics.c @@ -1,5 +1,4 @@ #include -#include static void copyScreenTexture(uint8_t *dst, const uint8_t *src, int pitch) { int x, y, i; @@ -17,7 +16,8 @@ static void copyScreenTexture(uint8_t *dst, const uint8_t *src, int pitch) { int gfxInit(GraphicsContext *gfx) { gfx->window = SDL_CreateWindow("Shrooms VB", - 1536, 896, SDL_WINDOW_HIGH_PIXEL_DENSITY); + SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, + 1536, 896, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI); 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_RenderTexture(gfx->renderer, gfx->leftEye, NULL, NULL); - SDL_RenderTexture(gfx->renderer, gfx->rightEye, NULL, NULL); + SDL_RenderCopy(gfx->renderer, gfx->leftEye, NULL, NULL); + SDL_RenderCopy(gfx->renderer, gfx->rightEye, NULL, NULL); SDL_RenderPresent(gfx->renderer); } diff --git a/graphics.h b/graphics.h index df3adcb..aa4485b 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 2fe48e4..eb7eedf 100644 --- a/main.c +++ b/main.c @@ -1,10 +1,9 @@ #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"); @@ -70,7 +69,7 @@ int main(int argc, char **argv) { vbInit(sim); vbSetCartROM(sim, rom, romSize); - if (!SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO)) { + if (SDL_Init(SDL_INIT_EVERYTHING)) { fprintf(stderr, "Error initializing SDL: %s\n", SDL_GetError()); return 1; } diff --git a/makefile b/makefile index dc56768..85f346f 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,6 @@ CC?=gcc SHROOMSFLAGS=shrooms-vb-core/core/vb.c -I shrooms-vb-core/core -SDL3FLAGS=$(shell pkg-config sdl3 --cflags --libs) +SDL2FLAGS=$(shell pkg-config sdl2 --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) $(SDL3FLAGS) \ - -D _POSIX_C_SOURCE=199309L \ + $(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