Revert "Switch to SDL3"

This reverts commit cd29cca495.
This commit is contained in:
Simon Gellis 2024-10-23 19:01:54 -04:00
parent cd29cca495
commit 4e3495948c
10 changed files with 42 additions and 40 deletions

View File

@ -7,7 +7,7 @@ An SDL-based implementation of shrooms-vb.
Install the following dependencies: Install the following dependencies:
- `gcc` (or MinGW on Windows) (or whatever, just set `CC`) - `gcc` (or MinGW on Windows) (or whatever, just set `CC`)
- `pkg-config` - `pkg-config`
- sdl3 - sdl2
Run Run
```sh ```sh

17
audio.c
View File

@ -4,28 +4,31 @@
int audioInit(AudioContext *aud) { int audioInit(AudioContext *aud) {
SDL_AudioSpec spec; SDL_AudioSpec spec;
spec.freq = 41700; spec.freq = 41700;
spec.format = SDL_AUDIO_S16; spec.format = AUDIO_S16;
spec.channels = 2; 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; aud->paused = true;
if (!aud->stream) { if (!aud->id) {
fprintf(stderr, "could not open audio device stream: %s\n", SDL_GetError()); fprintf(stderr, "could not open audio device: %s\n", SDL_GetError());
return -1; return -1;
} }
return 0; return 0;
} }
int audioUpdate(AudioContext *aud, void *data, uint32_t bytes) { 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()); fprintf(stderr, "could not write audio: %s\n", SDL_GetError());
return -1; return -1;
} }
if (aud->paused) { if (aud->paused) {
SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(aud->stream)); SDL_PauseAudioDevice(aud->id, false);
aud->paused = false; aud->paused = false;
} }

View File

@ -1,11 +1,11 @@
#ifndef SHROOMS_VB_NATIVE_AUDIO_ #ifndef SHROOMS_VB_NATIVE_AUDIO_
#define SHROOMS_VB_NATIVE_AUDIO_ #define SHROOMS_VB_NATIVE_AUDIO_
#include <SDL3/SDL.h> #include <SDL2/SDL.h>
#include <stdbool.h> #include <stdbool.h>
typedef struct { typedef struct {
SDL_AudioStream *stream; SDL_AudioDeviceID id;
bool paused; bool paused;
} AudioContext; } AudioContext;

View File

@ -17,28 +17,28 @@
#define VB_RL 0x4000 #define VB_RL 0x4000
#define VB_RD 0x8000 #define VB_RD 0x8000
static uint16_t symToMask(SDL_Keycode sym) { static uint16_t symToMask(SDL_KeyCode sym) {
switch (sym) { switch (sym) {
default: return 0; default: return 0;
case SDLK_A: case SDLK_a:
return VB_SEL; return VB_SEL;
case SDLK_S: case SDLK_s:
return VB_STA; return VB_STA;
case SDLK_D: case SDLK_d:
return VB_B; return VB_B;
case SDLK_F: case SDLK_f:
return VB_A; return VB_A;
case SDLK_E: case SDLK_e:
return VB_LT; return VB_LT;
case SDLK_R: case SDLK_r:
return VB_RT; return VB_RT;
case SDLK_I: case SDLK_i:
return VB_RU; return VB_RU;
case SDLK_J: case SDLK_j:
return VB_RL; return VB_RL;
case SDLK_K: case SDLK_k:
return VB_RD; return VB_RD;
case SDLK_L: case SDLK_l:
return VB_RR; return VB_RR;
case SDLK_UP: case SDLK_UP:
return VB_LU; return VB_LU;

View File

@ -1,7 +1,7 @@
#ifndef SHROOMS_VB_NATIVE_CONTROLLER_ #ifndef SHROOMS_VB_NATIVE_CONTROLLER_
#define SHROOMS_VB_NATIVE_CONTROLLER_ #define SHROOMS_VB_NATIVE_CONTROLLER_
#include <SDL3/SDL.h> #include <SDL2/SDL.h>
#include <stdint.h> #include <stdint.h>
typedef struct { typedef struct {

12
game.c
View File

@ -2,7 +2,7 @@
#include <assets.h> #include <assets.h>
#include <controller.h> #include <controller.h>
#include <game.h> #include <game.h>
#include <SDL3/SDL.h> #include <SDL2/SDL.h>
#include <stdbool.h> #include <stdbool.h>
#include <time.h> #include <time.h>
@ -73,14 +73,14 @@ int runGame(VB *sim, GraphicsContext *gfx) {
sleepNanos(((MAX_STEP_CLOCKS - clocks) * 50) - (ticks - prevTicks)); sleepNanos(((MAX_STEP_CLOCKS - clocks) * 50) - (ticks - prevTicks));
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
if (event.type == SDL_EVENT_QUIT) { if (event.type == SDL_QUIT) {
return 0; return 0;
} }
if (event.type == SDL_EVENT_KEY_DOWN) { if (event.type == SDL_KEYDOWN) {
ctrlKeyDown(&ctrl, event.key.key); ctrlKeyDown(&ctrl, event.key.keysym.sym);
} }
if (event.type == SDL_EVENT_KEY_UP) { if (event.type == SDL_KEYUP) {
ctrlKeyUp(&ctrl, event.key.key); ctrlKeyUp(&ctrl, event.key.keysym.sym);
} }
} }
vbSetKeys(sim, ctrlKeys(&ctrl)); vbSetKeys(sim, ctrlKeys(&ctrl));

View File

@ -1,5 +1,4 @@
#include <graphics.h> #include <graphics.h>
#include <stdio.h>
static void copyScreenTexture(uint8_t *dst, const uint8_t *src, int pitch) { static void copyScreenTexture(uint8_t *dst, const uint8_t *src, int pitch) {
int x, y, i; int x, y, i;
@ -17,7 +16,8 @@ static void copyScreenTexture(uint8_t *dst, const uint8_t *src, int pitch) {
int gfxInit(GraphicsContext *gfx) { int gfxInit(GraphicsContext *gfx) {
gfx->window = SDL_CreateWindow("Shrooms VB", 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) { if (!gfx->window) {
fprintf(stderr, "Error creating window: %s\n", SDL_GetError()); fprintf(stderr, "Error creating window: %s\n", SDL_GetError());
return 1; return 1;
@ -68,7 +68,7 @@ void gfxDestroy(GraphicsContext *gfx) {
static void gfxUpdateEye(SDL_Texture *eye, const uint8_t *bytes) { static void gfxUpdateEye(SDL_Texture *eye, const uint8_t *bytes) {
void *target; void *target;
int pitch; 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()); fprintf(stderr, "Error locking buffer for eye: %s\n", SDL_GetError());
return; return;
} }
@ -85,8 +85,8 @@ void gfxUpdateRightEye(GraphicsContext *gfx, const uint8_t *bytes) {
void gfxRender(GraphicsContext *gfx) { void gfxRender(GraphicsContext *gfx) {
SDL_RenderClear(gfx->renderer); SDL_RenderClear(gfx->renderer);
SDL_RenderTexture(gfx->renderer, gfx->leftEye, NULL, NULL); SDL_RenderCopy(gfx->renderer, gfx->leftEye, NULL, NULL);
SDL_RenderTexture(gfx->renderer, gfx->rightEye, NULL, NULL); SDL_RenderCopy(gfx->renderer, gfx->rightEye, NULL, NULL);
SDL_RenderPresent(gfx->renderer); SDL_RenderPresent(gfx->renderer);
} }

View File

@ -1,7 +1,7 @@
#ifndef SHROOMS_VB_NATIVE_GRAPHICS_ #ifndef SHROOMS_VB_NATIVE_GRAPHICS_
#define SHROOMS_VB_NATIVE_GRAPHICS_ #define SHROOMS_VB_NATIVE_GRAPHICS_
#include <SDL3/SDL.h> #include <SDL2/SDL.h>
typedef struct { typedef struct {
SDL_Window *window; SDL_Window *window;

5
main.c
View File

@ -1,10 +1,9 @@
#include <cli.h> #include <cli.h>
#include <game.h> #include <game.h>
#include <graphics.h> #include <graphics.h>
#include <SDL3/SDL.h> #include <SDL2/SDL.h>
#include "shrooms-vb-core/core/vb.h" #include "shrooms-vb-core/core/vb.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
uint8_t *readROM(char *filename, uint32_t *size) { uint8_t *readROM(char *filename, uint32_t *size) {
FILE *file = fopen(filename, "rb"); FILE *file = fopen(filename, "rb");
@ -70,7 +69,7 @@ int main(int argc, char **argv) {
vbInit(sim); vbInit(sim);
vbSetCartROM(sim, rom, romSize); 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()); fprintf(stderr, "Error initializing SDL: %s\n", SDL_GetError());
return 1; return 1;
} }

View File

@ -1,6 +1,6 @@
CC?=gcc CC?=gcc
SHROOMSFLAGS=shrooms-vb-core/core/vb.c -I shrooms-vb-core/core 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 .PHONY: clean
clean: clean:
@ -12,8 +12,8 @@ endif
build: build:
@$(CC) audio.c cli.c controller.c game.c graphics.c main.c assets/assets.s -I . \ @$(CC) audio.c cli.c controller.c game.c graphics.c main.c assets/assets.s -I . \
$(SHROOMSFLAGS) $(SDL3FLAGS) \ $(SHROOMSFLAGS) $(SDL2FLAGS) \
-D _POSIX_C_SOURCE=199309L \ -D POSIX_C_SOURCE=199309L \
-o shrooms-vb \ -o shrooms-vb \
-O3 -fno-strict-aliasing \ -O3 -fno-strict-aliasing \
-Werror -std=c90 -Wall -Wextra -Wpedantic -Werror -std=c90 -Wall -Wextra -Wpedantic