Switch to SDL3

This commit is contained in:
Simon Gellis 2024-10-21 23:31:55 -04:00
parent 8b31e76655
commit cd29cca495
10 changed files with 40 additions and 42 deletions

View File

@ -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

17
audio.c
View File

@ -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;
}

View File

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

View File

@ -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;

View File

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

12
game.c
View File

@ -2,7 +2,7 @@
#include <assets.h>
#include <controller.h>
#include <game.h>
#include <SDL2/SDL.h>
#include <SDL3/SDL.h>
#include <stdbool.h>
#include <time.h>
@ -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));

View File

@ -1,4 +1,5 @@
#include <graphics.h>
#include <stdio.h>
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);
}

View File

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

5
main.c
View File

@ -1,9 +1,10 @@
#include <cli.h>
#include <game.h>
#include <graphics.h>
#include <SDL2/SDL.h>
#include <SDL3/SDL.h>
#include "shrooms-vb-core/core/vb.h"
#include <stdio.h>
#include <stdlib.h>
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;
}

View File

@ -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