1
1
Fork 0
Browse Source

basic sounds

emscriptem
user 3 years ago
parent
commit
4c00835537
  1. 1
      src/draw.c
  2. 80
      src/main.c

1
src/draw.c

@ -737,3 +737,4 @@ void redraw_buffer(SDL_Renderer * ren) { @@ -737,3 +737,4 @@ void redraw_buffer(SDL_Renderer * ren) {
/*}*/
}

80
src/main.c

@ -36,6 +36,81 @@ struct SDL_Window* make_window(void) { @@ -36,6 +36,81 @@ struct SDL_Window* make_window(void) {
SDL_WINDOW_FULLSCREEN_DESKTOP);
}
void callbackfn(void *unused, Uint8 *stream, int len);
void callbackfn2(void *unused, Uint8 *stream, int len);
void callbackfn3(void *unused, Uint8 *stream, int len);
void start_audio(void) {
SDL_AudioSpec fmt = {.freq = 8000, .format = AUDIO_U8,.channels = 1, .samples=512};
fmt.callback = callbackfn3;
if (SDL_OpenAudio(&fmt, NULL) < 0) {
fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError());
exit(1);
}
SDL_PauseAudio(0);
}
void stop_audio(void) {
SDL_CloseAudio();
}
unsigned int func1(int t) {
int note = 10;
if (!(t % 8000)) {
note = (1 + rand()) % 10 + 5;
printf("%d %d\n", t, note);
}
return ((t & 110) % 73 | (t % 1711));
}
unsigned int func0(int t) {
int note = 18;
if (t % 1240) {
note = (1 + rand()) % 18 + 1;
}
printf("%d %d\n", t, note);
fflush(stdout);
return (t % note);
}
void callbackfn3(void *unused, Uint8 *stream, int len) {
static int t = 1;
int i;
int yeet = rand();
for(i=0; i < len; i++,t++) {
stream[i] = func1(t);
}
}
void callbackfn2(void *unused, Uint8 *stream, int len) {
static int t = 1;
int i;
int yeet = rand();
for(i=0; i < len; i++,t++) {
stream[i] = func0(t);
}
}
void callbackfn(void *unused, Uint8 *stream, int len)
{
static int t = 0;
int i;
int yeet = rand();
for( i=0; i < len; i++,t++) {
/*
stream[i] = (t*5&(t>>7))|(t*3&(t>>8));
*/
//stream[i] = (( t >> 10 ) & 42) * t;
stream[i] = (t & yeet) % 73;
}
}
void redraw(struct SDL_Renderer * ren) {
SDL_RenderClear(ren);
@ -52,6 +127,7 @@ int physics_loop(void *ptr) { @@ -52,6 +127,7 @@ int physics_loop(void *ptr) {
// display end level screen
game_paused = true;
SDL_Delay(1000);
SDL_PauseAudio(1);
in_game = false;
SDL_SemWait(resume);
add_time(level_time);
@ -63,6 +139,7 @@ int physics_loop(void *ptr) { @@ -63,6 +139,7 @@ int physics_loop(void *ptr) {
SDL_UnlockMutex(player.physics->lock);
SDL_Delay(1000);
game_paused = 0;
SDL_PauseAudio(0);
}
}
}
@ -140,8 +217,9 @@ int game(void) { @@ -140,8 +217,9 @@ int game(void) {
int main (int argc, char** argv) {
int err = mkdirat(AT_FDCWD, "saves", 0777);
mkdirat(AT_FDCWD, "saves", 0777);
start_audio();
game();
SDL_Quit();
//empty_cleanup_queue();

Loading…
Cancel
Save