diff --git a/.vscode/settings.json b/.vscode/settings.json index 55f345d..84c97d3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -22,6 +22,7 @@ "stdlib.h": "c", "stb_image.h": "c", "string.h": "c", - "unistd.h": "c" + "unistd.h": "c", + "pthread.h": "c" } } \ No newline at end of file diff --git a/Makefile b/Makefile index c740955..4e65e2b 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC = gcc FLAGS = -Wall -Wextra -g -LFLAGS = -lm src/glad.c -ldl -lglfw -lcglm -lpng -lSOIL +LFLAGS = -lm src/glad.c -ldl -lglfw -lcglm -lpng -lSOIL -lpthread all: bin/back @@ -10,7 +10,7 @@ test: bin/back mem: bin/back valgrind --leak-check=full ./bin/back -bin/back: obj/main.o obj/generation.o obj/display.o obj/proj.o obj/entities.o obj/bullets.o obj/menus.o obj/move.o obj/base.o obj/hash.o +bin/back: obj/main.o obj/generation.o obj/display.o obj/proj.o obj/entities.o obj/bullets.o obj/menus.o obj/music.o obj/move.o obj/base.o obj/hash.o mkdir -p bin $(CC) $(FLAGS) $^ $(LFLAGS) -o $@ @@ -26,6 +26,7 @@ obj/bullets.o: src/bullets.c obj/move.o: src/move.c obj/base.o: src/base.c obj/proj.o: src/proj.c +obj/music.o: src/music.c obj/menus.o: src/menus.c obj/hash.o: src/hash.c diff --git a/bin/back b/bin/back index 225e69a..2758f06 100755 Binary files a/bin/back and b/bin/back differ diff --git a/obj/main.o b/obj/main.o index b0c3d2c..ef3c4ff 100644 Binary files a/obj/main.o and b/obj/main.o differ diff --git a/obj/move.o b/obj/move.o index faac48d..5309f2e 100644 Binary files a/obj/move.o and b/obj/move.o differ diff --git a/obj/music.o b/obj/music.o new file mode 100644 index 0000000..1b7f4a1 Binary files /dev/null and b/obj/music.o differ diff --git a/sound/audio/smw_coin.wav b/sound/audio/smw_coin.wav new file mode 100644 index 0000000..09f9ca0 Binary files /dev/null and b/sound/audio/smw_coin.wav differ diff --git a/sound/audio/tetris_2.wav b/sound/audio/tetris_2.wav new file mode 100755 index 0000000..ae56c39 Binary files /dev/null and b/sound/audio/tetris_2.wav differ diff --git a/sound/compilation.sh b/sound/compilation.sh new file mode 100644 index 0000000..6f6a086 --- /dev/null +++ b/sound/compilation.sh @@ -0,0 +1 @@ +gcc sound/playsound.c -lSDL2 -o sound/music diff --git a/sound/music b/sound/music new file mode 100755 index 0000000..c9194d6 Binary files /dev/null and b/sound/music differ diff --git a/sound/playsound.c b/sound/playsound.c new file mode 100644 index 0000000..a994ccf --- /dev/null +++ b/sound/playsound.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include + +int main(int argc, char** argv) { + assert(argc == 2); + if(SDL_Init(SDL_INIT_AUDIO)) {fprintf(stderr, "cannot initialize audio");exit(1);} + SDL_AudioSpec wavSpec; + Uint32 wavLength; + Uint8 *wavBuffer; + SDL_LoadWAV(argv[1], &wavSpec, &wavBuffer, &wavLength); + SDL_AudioDeviceID deviceId = SDL_OpenAudioDevice(NULL, 0, &wavSpec, NULL, 0); + int success = SDL_QueueAudio(deviceId, wavBuffer, wavLength); + SDL_PauseAudioDevice(deviceId, 0); + usleep((int)((wavLength)/44100.0*100000.0)); + SDL_CloseAudioDevice(deviceId); + SDL_FreeWAV(wavBuffer); + SDL_Quit(); + return 0; +} +// yummy \ No newline at end of file diff --git a/src/main.c b/src/main.c index c894d16..fa91efe 100644 --- a/src/main.c +++ b/src/main.c @@ -23,6 +23,7 @@ #include "entities.h" #include "display.h" #include "generation.h" +#include "music.h" double sim_time; int triCount; @@ -699,17 +700,20 @@ int main_alt() { // glfw: terminate, clearing all previously allocated GLFW resources. // ------------------------------------------------------------------ glfwTerminate(); + printf("\n<0>\n\n"); + end_music(); return 0; } int main(int argc, char** argv) { srand(time(NULL)); + init_music(); triCount = 0; sim_time = 0.0; gamemode = 0; newRoomCount = 7; switchRoom = false; newRoomName = "templates/"; - //read_png_file("res/question_block.png"); + //play_sound("sound/audio/tetris_2.wav"); return main_alt(); } \ No newline at end of file diff --git a/src/move.c b/src/move.c index 7aa68f7..4f5b4ff 100644 --- a/src/move.c +++ b/src/move.c @@ -13,6 +13,7 @@ #include "entities.h" #include "proj.h" #include "move.h" +#include "music.h" // ---------------------------------------------------------------------------------------------------- // double sensitivity = 0.06; @@ -286,6 +287,9 @@ bool is_colliding(float dtime) { if(vstd->ents[k]->onDeath != NULL) { (*vstd->ents[k]->onDeath)(dtime); } + if(vstd->ents[k]->entity_type == 0) { + play_sound("sound/audio/smw_coin.wav"); + } remove_entity(vstd->ents, &vstd->ent_memlen, &vstd->ent_len, k); is_clipping = false; exists = false; diff --git a/src/music.c b/src/music.c new file mode 100644 index 0000000..0fced10 --- /dev/null +++ b/src/music.c @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "hash.h" +#include "base.h" +#include "music.h" + +static pthread_t threads[32]; +static bool threadIsFree[32]; +static int threadID; + +static int thrID; + +pthread_mutex_t lock; + +void init_music() { + pthread_mutex_init(&lock, NULL); + threadID = 0; + for(int k = 0; k < 32; k++) { + threadIsFree[k] = true; + } +} + +void end_music() { + pthread_mutex_destroy(&lock); + for(int k = 0; k < 32; k++) { + if(!threadIsFree[k]) { + printf("[pthread] killing %d\n", k); + pthread_cancel(threads[k]); + } + } +} + +char* get_name(char* filename) { + char* res = malloc(sizeof(char)*100); + res[0] = '.'; + res[1] = '/'; + res[2] = 's'; + res[3] = 'o'; + res[4] = 'u'; + res[5] = 'n'; + res[6] = 'd'; + res[7] = '/'; + res[8] = 'm'; + res[9] = 'u'; + res[10] = 's'; + res[11] = 'i'; + res[12] = 'c'; + res[13] = ' '; + int k = 14; + while(filename[k-14] != '\0' && k < 99) { + res[k] = filename[k-14]; + k += 1; + } + res[k] = '\0'; + return res; +} + +void* mainMusic(void* arg) { + int localThreadID = thrID; + char* name = get_name((char*)arg); + system(name); + free(name); + threadIsFree[localThreadID] = true; + printf("thread %d is done\n", localThreadID); + return NULL; +} + +bool get_id(int* res) { + int lp = 0; + while(lp < 32) { + if(threadIsFree[(lp+threadID)%32]) { + *res = (lp+threadID)%32; + return true; + } + lp += 1; + } + return false; +} + +void play_sound(char* filename) { + pthread_mutex_lock(&lock); + int id; + if(get_id(&id)) { + threadIsFree[id] = false; + thrID = id; + threadID += 1; + pthread_create(&threads[id], NULL, mainMusic, filename); + } else { + fprintf(stderr, "WARNING : cannot play audio %s (out of space)\n", filename); + } + pthread_mutex_unlock(&lock); +} \ No newline at end of file diff --git a/src/music.h b/src/music.h new file mode 100644 index 0000000..7ac867c --- /dev/null +++ b/src/music.h @@ -0,0 +1,9 @@ +#ifndef MUSIC_H +#define MUSIC_H + +void init_music(); +void end_music(); + +void play_sound(char* filename); + +#endif \ No newline at end of file