Audio!
This commit is contained in:
parent
7ace73c587
commit
8b31e76655
|
@ -0,0 +1,36 @@
|
||||||
|
#include <audio.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int audioInit(AudioContext *aud) {
|
||||||
|
SDL_AudioSpec spec;
|
||||||
|
spec.freq = 41700;
|
||||||
|
spec.format = AUDIO_S16;
|
||||||
|
spec.channels = 2;
|
||||||
|
spec.samples = 1024;
|
||||||
|
spec.callback = NULL;
|
||||||
|
spec.userdata = NULL;
|
||||||
|
|
||||||
|
aud->id = SDL_OpenAudioDevice(NULL, 0, &spec, NULL, 0);
|
||||||
|
aud->paused = true;
|
||||||
|
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->id) return -1;
|
||||||
|
|
||||||
|
if (SDL_QueueAudio(aud->id, data, bytes)) {
|
||||||
|
fprintf(stderr, "could not write audio: %s\n", SDL_GetError());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (aud->paused) {
|
||||||
|
SDL_PauseAudioDevice(aud->id, false);
|
||||||
|
aud->paused = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
#ifndef SHROOMS_VB_NATIVE_AUDIO_
|
||||||
|
#define SHROOMS_VB_NATIVE_AUDIO_
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
SDL_AudioDeviceID id;
|
||||||
|
bool paused;
|
||||||
|
} AudioContext;
|
||||||
|
|
||||||
|
int audioInit(AudioContext *aud);
|
||||||
|
int audioUpdate(AudioContext *aud, void *data, uint32_t bytes);
|
||||||
|
|
||||||
|
#endif
|
19
game.c
19
game.c
|
@ -1,3 +1,4 @@
|
||||||
|
#include <audio.h>
|
||||||
#include <assets.h>
|
#include <assets.h>
|
||||||
#include <controller.h>
|
#include <controller.h>
|
||||||
#include <game.h>
|
#include <game.h>
|
||||||
|
@ -20,21 +21,27 @@ long int tickNs() {
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
GraphicsContext *gfx;
|
GraphicsContext *gfx;
|
||||||
|
AudioContext aud;
|
||||||
|
int16_t audioBuffer[834 * 2];
|
||||||
} GameState;
|
} GameState;
|
||||||
|
|
||||||
int onFrame(VB *sim) {
|
int onFrame(VB *sim) {
|
||||||
static uint8_t leftEye[384*224];
|
static uint8_t leftEye[384*224];
|
||||||
static uint8_t rightEye[384*224];
|
static uint8_t rightEye[384*224];
|
||||||
GameState *state;
|
GameState *state;
|
||||||
GraphicsContext *gfx;
|
void *samples;
|
||||||
|
uint32_t samplePairs;
|
||||||
|
|
||||||
state = vbGetUserData(sim);
|
state = vbGetUserData(sim);
|
||||||
gfx = state->gfx;
|
|
||||||
|
|
||||||
vbGetPixels(sim, leftEye, 1, 384, rightEye, 1, 384);
|
vbGetPixels(sim, leftEye, 1, 384, rightEye, 1, 384);
|
||||||
gfxUpdateLeftEye(gfx, leftEye);
|
gfxUpdateLeftEye(state->gfx, leftEye);
|
||||||
gfxUpdateRightEye(gfx, rightEye);
|
gfxUpdateRightEye(state->gfx, rightEye);
|
||||||
gfxRender(gfx);
|
|
||||||
|
samples = vbGetSamples(sim, NULL, &samplePairs);
|
||||||
|
audioUpdate(&state->aud, samples, samplePairs * 4);
|
||||||
|
vbSetSamples(sim, samples, 834);
|
||||||
|
gfxRender(state->gfx);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,6 +55,8 @@ int runGame(VB *sim, GraphicsContext *gfx) {
|
||||||
uint64_t ticks, prevTicks;
|
uint64_t ticks, prevTicks;
|
||||||
|
|
||||||
state.gfx = gfx;
|
state.gfx = gfx;
|
||||||
|
audioInit(&state.aud);
|
||||||
|
vbSetSamples(sim, &state.audioBuffer, 834);
|
||||||
vbSetUserData(sim, &state);
|
vbSetUserData(sim, &state);
|
||||||
vbSetFrameCallback(sim, &onFrame);
|
vbSetFrameCallback(sim, &onFrame);
|
||||||
|
|
||||||
|
|
2
makefile
2
makefile
|
@ -11,7 +11,7 @@ else
|
||||||
endif
|
endif
|
||||||
|
|
||||||
build:
|
build:
|
||||||
@$(CC) 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) $(SDL2FLAGS) \
|
$(SHROOMSFLAGS) $(SDL2FLAGS) \
|
||||||
-D POSIX_C_SOURCE=199309L \
|
-D POSIX_C_SOURCE=199309L \
|
||||||
-o shrooms-vb \
|
-o shrooms-vb \
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 50903c7513b00cc62cb20a1578e7f6484914a9da
|
Subproject commit c06da323218071719c1500379e84008c369169d7
|
Loading…
Reference in New Issue