diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2025-08-11 12:21:24 +0200 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2025-08-11 12:21:24 +0200 |
| commit | fa1aa7793bbe2a3647435798c56f43945278bcd0 (patch) | |
| tree | b8f31d4731ebac875b72104b27aeb1b9c36fa8b0 | |
| parent | 4b38f7f3a34dd696320f61d1fe67e6e1ff7d6795 (diff) | |
| download | bidi-fa1aa7793bbe2a3647435798c56f43945278bcd0.tar.gz | |
Added load, play and stop sound functions
| -rw-r--r-- | README.md | 22 | ||||
| -rw-r--r-- | main.c | 98 | ||||
| -rw-r--r-- | tests/sounds/effect1.wav | bin | 0 -> 26182 bytes | |||
| -rw-r--r-- | tests/sounds/song1.ogg | bin | 0 -> 103277 bytes | |||
| -rw-r--r-- | tests/test.lua | 23 |
5 files changed, 126 insertions, 17 deletions
@@ -27,6 +27,7 @@ open_window(800, 800, "My Game") set_fps(60) axe_image = load_image("images/axe.png") +slash_effect = load_sound("sounds/slash.wav") while window_running() do start_drawing() @@ -45,6 +46,10 @@ while window_running() do draw_text("Pad Up", 10, 10, 20, color.VIOLET) end + if button_pressed(button.A) then + play_sound(slash_effect) + end + draw_image(axe_image, 100, 200) draw_info() @@ -80,11 +85,13 @@ close_window() | `draw_circle` | `number center_x`, `number center_y`, `number radius`, `color color` | | | `draw_ellipse` | `number center_x`, `number center_y`, `number radius_h`, `number radius_v`, `color color` | | | `draw_triangle` | `number x1`, `number y1`, `number x2`, `number y2`, `number x3`, `number y3`, `color color` | | -| `load_image` | `string filepath` | `string` | -| `draw_image` | `string uid`, `number x`, `number y` | | -| `load_audio` | `TODO` | | +| `load_image` | `string filepath` | `uid` | +| `draw_image` | `uid uid`, `number x`, `number y` | | | `button_down` | `button button` | `bool` | | `button_pressed` | `button button` | `bool` | +| `load_sound` | `string filepath` | `uid` | +| `play_sound` | `uid uid` | | +| `stop_sound` | `uid uid` | | ### Controller mappings @@ -138,11 +145,16 @@ close_window() - https://github.com/raysan5/raylib - https://github.com/rxi/json.lua - https://dejavu-fonts.github.io/ +- https://freetestdata.com/audio-files/ ## Cool tools -- https://hardwaretester.com/gamepad -- https://www.raylib.com/examples/core/loader.html?name=core_input_gamepad +- Controller testing: + - https://hardwaretester.com/gamepad + - https://www.raylib.com/examples/core/loader.html?name=core_input_gamepad +- Sound effect generators: + - https://www.bfxr.net + - https://sfxr.me ## Interesting games @@ -19,7 +19,7 @@ #define VERSION "x.x" #define DEBUG_LEVEL LOG_DEBUG #define FONT_IMPORT_SIZE 30 -#define UID_LENGTH 36 +#define UID_LENGTH 64 #define GAMEPAD_INDEX 0 #define XBOX_ALIAS_1 "xbox" #define XBOX_ALIAS_2 "x-box" @@ -37,11 +37,24 @@ typedef struct { int count; } ImageList; +typedef struct ExternalSound { + char uid[UID_LENGTH + 1]; + Sound sound; + struct ExternalSound *next; +} ExternalSound; + +typedef struct { + ExternalSound *head; + ExternalSound *tail; + int count; +} SoundList; + typedef struct { Font font; int font_size; Camera2D camera; ImageList images; + SoundList sounds; } Context; // Setting up global context. @@ -64,6 +77,12 @@ void init_image_list(ImageList *list) { list->count = 0; } +void init_sound_list(SoundList *list) { + list->head = NULL; + list->tail = NULL; + list->count = 0; +} + static int lua_getfield_int(lua_State *L, int index, const char *key) { lua_getfield(L, index, key); int val = (int)luaL_checknumber(L, -1); @@ -107,6 +126,8 @@ static int l_open_window(lua_State *L) { ctx.camera.rotation = 0.0f; ctx.camera.zoom = 1.0f; + InitAudioDevice(); + return 0; } @@ -423,16 +444,12 @@ static int l_load_image(lua_State *L) { } ctx.images.count++; - TraceLog(LOG_WARNING, "[adding]\t %s (%d)", img->uid, strlen(img->uid)); + TraceLog(LOG_WARNING, "[add_img] %s (%d)", img->uid, strlen(img->uid)); lua_pushstring(L, img->uid); return 1; } -static int l_load_audio(lua_State *L) { - return 0; -} - static int l_draw_image(lua_State *L) { const char *uid = luaL_checkstring(L, 1); int x = luaL_checknumber(L, 2); @@ -441,7 +458,6 @@ static int l_draw_image(lua_State *L) { ExternalImage *current = ctx.images.head; while (current) { if (strncmp(current->uid, uid, UID_LENGTH) == 0) { - /* TraceLog(LOG_WARNING, "[match]\t %s (%d)", uid, strlen(uid)); */ DrawTexture(current->texture, x, y, WHITE); break; } @@ -451,6 +467,64 @@ static int l_draw_image(lua_State *L) { return 0; } +static int l_load_sound(lua_State *L) { + const char *filepath = luaL_checkstring(L, 1); + + ExternalSound *snd = malloc(sizeof(ExternalSound)); + if (!snd) { + TraceLog(LOG_FATAL, "Out of memory!"); + return 0; + } + + snd->sound = LoadSound(filepath); + generate_uid(snd->uid, UID_LENGTH); + snd->next = NULL; + + if (ctx.sounds.tail) { + ctx.sounds.tail->next = snd; + ctx.sounds.tail = snd; + } else { + ctx.sounds.head = snd; + ctx.sounds.tail = snd; + } + ctx.sounds.count++; + + TraceLog(LOG_WARNING, "[add_snd] %s (%d)", snd->uid, strlen(snd->uid)); + + lua_pushstring(L, snd->uid); + return 1; +} + +static int l_play_sound(lua_State *L) { + const char *uid = luaL_checkstring(L, 1); + + ExternalSound *current = ctx.sounds.head; + while (current) { + if (strncmp(current->uid, uid, UID_LENGTH) == 0) { + PlaySound(current->sound); + break; + } + current = current->next; + } + + return 0; +} + +static int l_stop_sound(lua_State *L) { + const char *uid = luaL_checkstring(L, 1); + + ExternalSound *current = ctx.sounds.head; + while (current) { + if (strncmp(current->uid, uid, UID_LENGTH) == 0) { + StopSound(current->sound); + break; + } + current = current->next; + } + + return 0; +} + static void help(const char *argv0) { printf("Usage: %s [options]\n" "\nAvailable options:\n" @@ -469,6 +543,7 @@ static void version(const char *argv0) { int main(int argc, char *argv[]) { srand(time(NULL)); init_image_list(&ctx.images); + init_sound_list(&ctx.sounds); TraceLogLevel debug_level = LOG_WARNING; const char *run_file = NULL; @@ -537,9 +612,6 @@ int main(int argc, char *argv[]) { lua_register(L, "get_width", l_get_width); lua_register(L, "get_height", l_get_height); - lua_register(L, "load_image", l_load_image); - lua_register(L, "load_audio", l_load_audio); - lua_register(L, "button_down", l_button_down); lua_register(L, "button_pressed", l_button_pressed); @@ -551,8 +623,14 @@ int main(int argc, char *argv[]) { lua_register(L, "draw_circle", l_draw_circle); lua_register(L, "draw_ellipse", l_draw_ellipse); lua_register(L, "draw_triangle", l_draw_triangle); + + lua_register(L, "load_image", l_load_image); lua_register(L, "draw_image", l_draw_image); + lua_register(L, "load_sound", l_load_sound); + lua_register(L, "play_sound", l_play_sound); + lua_register(L, "stop_sound", l_play_sound); + // Interpreting and running input file Lua script. if (luaL_loadfile(L, run_file) || lua_pcall(L, 0, 0, 0)) { TraceLog(LOG_FATAL, "Error: %s\n", lua_tostring(L, -1)); diff --git a/tests/sounds/effect1.wav b/tests/sounds/effect1.wav Binary files differnew file mode 100644 index 0000000..209c609 --- /dev/null +++ b/tests/sounds/effect1.wav diff --git a/tests/sounds/song1.ogg b/tests/sounds/song1.ogg Binary files differnew file mode 100644 index 0000000..4a47527 --- /dev/null +++ b/tests/sounds/song1.ogg diff --git a/tests/test.lua b/tests/test.lua index 372b221..67df27a 100644 --- a/tests/test.lua +++ b/tests/test.lua @@ -10,6 +10,9 @@ test_camera_position = { x = 0, y = 0 } test_camera_speed = 200 test_images_asset1 = load_image("tests/icons/icon_1.png") test_images_asset2 = load_image("tests/icons/icon_2.png") +test_sounds_sound1 = load_sound("tests/sounds/effect1.wav") +test_sounds_sound2 = load_sound("tests/sounds/song1.ogg") +test_sounds_volume = 100 function test_api() draw_rect(100, 100, 300, 200, color.YELLOW) @@ -140,13 +143,28 @@ function test_camera() end function test_images() - draw_text(string.format("uid: %s", test_images_asset1), 50, 50, 20, color.VIOLET) - draw_text(string.format("uid: %s", test_images_asset2), 50, 80, 20, color.VIOLET) + draw_text(string.format("uid: %s", test_images_asset1), 30, 30, 20, color.VIOLET) + draw_text(string.format("uid: %s", test_images_asset2), 30, 55, 20, color.VIOLET) draw_image(test_images_asset1, 150, 150) draw_image(test_images_asset2, 200, 200) end +function test_sounds() + draw_text("Press button A to play a short sound effect.", 30, 30, 20, color.BLUE) + draw_text("Press button B to play a longer sound effect.", 30, 55, 20, color.BLUE) + + if button_pressed(button.A) then + draw_text("Button A pressed", 30, 85, 20, color.YELLOW) + play_sound(test_sounds_sound1) + end + + if button_pressed(button.B) then + draw_text("Button B pressed", 30, 85, 20, color.YELLOW) + play_sound(test_sounds_sound2) + end +end + while window_running() do start_drawing() clear_window(color.BLACK) @@ -156,6 +174,7 @@ while window_running() do -- test_buttons() -- test_camera() -- test_images() + -- test_sounds() draw_info() stop_drawing() |
