[FEAT] Audio is working
This commit is contained in:
28
ascii.cpp
28
ascii.cpp
@@ -14,6 +14,9 @@ uint8_t screen_height = 30;
|
|||||||
uint8_t current_color = 0x1e;
|
uint8_t current_color = 0x1e;
|
||||||
uint8_t cursor_x = 0;
|
uint8_t cursor_x = 0;
|
||||||
uint8_t cursor_y = 0;
|
uint8_t cursor_y = 0;
|
||||||
|
bool sounding = false;
|
||||||
|
int audio_freq = 0;
|
||||||
|
uint32_t audio_len = 0;
|
||||||
|
|
||||||
#define CHRSCR(x, y) char_screen[x+y*screen_width]
|
#define CHRSCR(x, y) char_screen[x+y*screen_width]
|
||||||
#define COLSCR(x, y) color_screen[x+y*screen_width]
|
#define COLSCR(x, y) color_screen[x+y*screen_width]
|
||||||
@@ -21,6 +24,7 @@ uint8_t cursor_y = 0;
|
|||||||
SDL_Window *mini_win;
|
SDL_Window *mini_win;
|
||||||
SDL_Renderer *mini_ren;
|
SDL_Renderer *mini_ren;
|
||||||
SDL_Texture *mini_bak;
|
SDL_Texture *mini_bak;
|
||||||
|
SDL_AudioDeviceID mini_audio_device;
|
||||||
Uint32 *pixels;
|
Uint32 *pixels;
|
||||||
int pitch;
|
int pitch;
|
||||||
|
|
||||||
@@ -48,6 +52,19 @@ void reinit() {
|
|||||||
cursor_y = 0;
|
cursor_y = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void audioCallback(void * userdata, uint8_t * stream, int len) {
|
||||||
|
//if (audio_len <= 0) nosound();
|
||||||
|
static int period=0;
|
||||||
|
static int v = 16;
|
||||||
|
SDL_memset(stream, 0, len);
|
||||||
|
const int flen = min(audio_len, len);
|
||||||
|
audio_len -= flen;
|
||||||
|
for (int i=0; i<flen; ++i) {
|
||||||
|
stream[i] = v;
|
||||||
|
period++; if (period>=audio_freq) {period = 0; v=-v;}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc,char*argv[]) {
|
int main(int argc,char*argv[]) {
|
||||||
|
|
||||||
for (int i=0; i<debug_total_size;++i) debug_text[i] = 32;
|
for (int i=0; i<debug_total_size;++i) debug_text[i] = 32;
|
||||||
@@ -61,6 +78,10 @@ int main(int argc,char*argv[]) {
|
|||||||
bool exit = false;
|
bool exit = false;
|
||||||
SDL_Event mini_eve;
|
SDL_Event mini_eve;
|
||||||
|
|
||||||
|
SDL_AudioSpec audioSpec{44100, AUDIO_S8, 1, 0, 512, 0, 0, audioCallback, NULL};
|
||||||
|
mini_audio_device = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0);
|
||||||
|
SDL_PauseAudioDevice(mini_audio_device, 0);
|
||||||
|
|
||||||
reinit();
|
reinit();
|
||||||
debug("ASCII SYSTEM BOOTING...");
|
debug("ASCII SYSTEM BOOTING...");
|
||||||
lua_init();
|
lua_init();
|
||||||
@@ -308,8 +329,13 @@ void poke(uint16_t addr, uint8_t val) {
|
|||||||
|
|
||||||
void sound(float freq, uint32_t len) {
|
void sound(float freq, uint32_t len) {
|
||||||
// [TODO]
|
// [TODO]
|
||||||
|
audio_len = len*44.1f;
|
||||||
|
audio_freq = 44100.0f/freq/2.0f;
|
||||||
|
sounding = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void nosound() {
|
void nosound() {
|
||||||
// [TODO]
|
//SDL_PauseAudioDevice(mini_audio_device, 1);
|
||||||
|
audio_len = 0;
|
||||||
|
sounding = false;
|
||||||
}
|
}
|
||||||
|
|||||||
2
game.lua
2
game.lua
@@ -11,9 +11,11 @@ function update()
|
|||||||
x = x + dx
|
x = x + dx
|
||||||
y = y + dy
|
y = y + dy
|
||||||
if x == 39 or x == 0 then
|
if x == 39 or x == 0 then
|
||||||
|
sound(440, 50)
|
||||||
dx = -dx
|
dx = -dx
|
||||||
end
|
end
|
||||||
if y == 29 or y == 0 then
|
if y == 29 or y == 0 then
|
||||||
|
sound(880, 50)
|
||||||
dy = -dy
|
dy = -dy
|
||||||
end
|
end
|
||||||
locate(2, 2)
|
locate(2, 2)
|
||||||
|
|||||||
Reference in New Issue
Block a user