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:
- `gcc` (or MinGW on Windows) (or whatever, just set `CC`)
- `pkg-config`
- sdl3
- sdl2
Run
```sh

17
audio.c
View File

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

View File

@ -1,11 +1,11 @@
#ifndef SHROOMS_VB_NATIVE_AUDIO_
#define SHROOMS_VB_NATIVE_AUDIO_
#include <SDL3/SDL.h>
#include <SDL2/SDL.h>
#include <stdbool.h>
typedef struct {
SDL_AudioStream *stream;
SDL_AudioDeviceID id;
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 <SDL3/SDL.h>
#include <SDL2/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 <SDL3/SDL.h>
#include <SDL2/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_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));

View File

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

View File

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

5
main.c
View File

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

View File

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