added music cuz why not

This commit is contained in:
Alexandre 2025-05-20 22:18:12 +02:00
parent 32a72a2257
commit c9df3a30bc
32 changed files with 265 additions and 40 deletions

View File

@ -8,6 +8,8 @@
"base.h": "c",
"cars.h": "c",
"sdl_image.h": "c",
"collisions.h": "c"
"collisions.h": "c",
"stdbool.h": "c",
"music.h": "c"
}
}

View File

@ -25,7 +25,7 @@ road: bin/back
mem: bin/back
valgrind --leak-check=full ./bin/back levels/test.txt bots/dumb bots/dumb2 bots/dumb3 bots/dumb4
bin/back: obj/main.o obj/display.o obj/base.o obj/collisions.o obj/cars.o obj/rooms.o obj/structure.o
bin/back: obj/main.o obj/display.o obj/base.o obj/collisions.o obj/music.o obj/cars.o obj/rooms.o obj/structure.o
mkdir -p bin
$(CC) $(FLAGS) $^ $(LFLAGS) -o $@
@ -37,6 +37,7 @@ obj/main.o: src/main.c
obj/display.o: src/display.c
obj/base.o: src/base.c
obj/cars.o: src/cars.c
obj/music.o: src/music.c
obj/rooms.o: src/rooms.c
obj/collisions.o: src/collisions.c
obj/structure.o: src/structure.c

View File

@ -1 +1 @@
288 75
271 90

BIN
bin/back

Binary file not shown.

View File

@ -1,22 +1,53 @@
83.689682 166.568590
161.856726 -68.371906
-21.799328 154.573210
167.141051 -112.643060
23.867801 -137.063781
-134.600006 141.659189
-131.665050 -68.540697
-188.330352 56.382417
-44.777939 -151.693916
-157.607132 -25.718204
141.568869 35.871573
-11.482366 -141.473584
-161.353467 -52.403609
118.974318 -60.269145
142.449106 80.225227
-156.434592 120.574661
130.379231 139.672870
-107.308604 -82.900739
69.679734 -172.732818
150.627007 71.542801
46.785402 -141.274814
61.850213 -128.983670
-91.598146 124.327144
142.266681 -23.083685
80.023571 -110.291019
-209.326753 22.630339
68.505747 119.501330
107.060380 -114.069708
-70.233012 -101.577163
147.160673 -72.086463
-140.110322 106.226989
135.086455 98.172637
56.107652 105.827274
35.854738 -152.398792
132.320996 -67.163737
176.181947 66.489059
130.017074 -31.088727
66.243545 115.114005
145.260991 -91.403980
-69.982160 -182.672565
35.887790 -183.222296
108.756771 95.947835
-49.680253 -115.091637
58.894910 114.835148
99.088389 -156.633565
-187.977088 -51.719528
-104.449063 -91.836921
-107.925240 138.542331
-94.113722 79.680583
-97.214088 -68.731116
-100.166986 -61.714426
-115.272316 110.207498
-96.395609 -136.982500
-43.753892 -133.340136
-66.862857 105.898787
91.865015 121.247073
-95.201198 111.796966
37.801519 -111.152427
53.162045 174.621969
-83.439530 168.654458
86.164970 159.092863
-62.380427 -152.204942
17.433048 133.218210
-105.950486 88.326322
-47.503687 -175.362014
-63.992977 156.620065
-155.337407 -113.318321
114.800901 120.609239
-11.129331 -159.843313
79.955484 139.639897
-150.338137 27.027579
-165.852701 9.876618
-166.712365 85.606507
102.833309 109.163228
-16.456985 -187.091529

View File

@ -1,7 +1,7 @@
name, elo
./bots/dumb 1637
./bots/dumb2 1592
./bots/dumb3 1611
./bots/dumb4 1519
./bots/dumb 1656
./bots/dumb2 1542
./bots/dumb3 1633
./bots/dumb4 1481
./bots/dumb5 540
./bots/dumb6 562

Binary file not shown.

Binary file not shown.

BIN
obj/music.o Normal file

Binary file not shown.

View File

@ -1,12 +1,16 @@
4
0 (2 0) (10.04 51.32)
1 (2 0) (32.42 40.43)
2 (2 0) (72.81 52.36)
3 (0 0) (89.50 92.97)
0 (3 0) (22.26 39.65)
1 (1 3) (20.14 43.41)
2 (2 0) (27.37 58.70)
3 (2 0) (73.15 83.07)
[3]
1 3
S1E
5 5
..S14
....0
...35
.E15.
.....
100 200 5 0.90 0.80 0.20

BIN
sound/audio/kahoot-gong.wav Normal file

Binary file not shown.

BIN
sound/audio/kahoot_10s.mp3 Normal file

Binary file not shown.

BIN
sound/audio/kahoot_10s.wav Normal file

Binary file not shown.

BIN
sound/audio/kahoot_20s.mp3 Normal file

Binary file not shown.

BIN
sound/audio/kahoot_20s.wav Normal file

Binary file not shown.

BIN
sound/audio/kahoot_30s.mp3 Normal file

Binary file not shown.

BIN
sound/audio/kahoot_30s.wav Normal file

Binary file not shown.

BIN
sound/audio/kahoot_5s.mp3 Normal file

Binary file not shown.

BIN
sound/audio/kahoot_5s.wav Normal file

Binary file not shown.

BIN
sound/audio/kahoot_60s.mp3 Normal file

Binary file not shown.

BIN
sound/audio/kahoot_60s.wav Normal file

Binary file not shown.

BIN
sound/audio/smw_coin.wav Normal file

Binary file not shown.

BIN
sound/audio/tetris_2.wav Executable file

Binary file not shown.

1
sound/compilation.sh Normal file
View File

@ -0,0 +1 @@
gcc sound/playsound.c -lSDL2 -o sound/music

BIN
sound/music Executable file

Binary file not shown.

37
sound/playsound.c Normal file
View File

@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <time.h>
#include <SDL2/SDL.h>
int can_pass(char* filename) {
FILE* ptr = fopen(filename, "r");
char c = fgetc(ptr);
fclose(ptr);
return (c != '0');
}
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);
clock_t start = clock();
clock_t finish = clock();
while((double)finish - (double)start < (wavLength)/44100.0*100000.0 && can_pass("sound/sem.txt")) {
usleep(10000);
finish = clock();
}
//usleep((int)((wavLength)/44100.0*100000.0));
SDL_CloseAudioDevice(deviceId);
SDL_FreeWAV(wavBuffer);
SDL_Quit();
return 0;
}
// yummy

1
sound/sem.txt Normal file
View File

@ -0,0 +1 @@
1

View File

@ -12,8 +12,9 @@
#include <SDL2/SDL_image.h>
#include "structure.h"
#include "cars.h"
#include "base.h"
#include "cars.h"
#include "music.h"
#include "collisions.h"
// use after updating the position
@ -311,6 +312,7 @@ void hasWon(int nPl, int* ranks, int* incr) {
if(winners[nPl]==1 && ranks[nPl] == nPlayers) {
ranks[nPl] = nextRank;
*incr += 1;
play_sound("sound/audio/kahoot_10s.wav");
}
if(winners[nPl]==1 && remainingTurns == -1) {
remainingTurns = 4;

View File

@ -13,6 +13,7 @@
#include "structure.h"
#include "base.h"
#include "music.h"
#include "display.h"
#include "collisions.h"
#include "rooms.h"
@ -166,6 +167,7 @@ int main(int argc, char** argv) {
import_letters(rend);
import_digits(rend);
init_music();
// ---------------------------- //
//stage_file("levels/stage.txt");
level* stage = parse_level(argv[1]);
@ -255,6 +257,7 @@ int main(int argc, char** argv) {
free_level(stage);
freePath(pth);
fclose(save);
end_music();
for(int k = 0; k < N_PLAYERS; k++) {
free(execs[k]);
}

133
src/music.c Normal file
View File

@ -0,0 +1,133 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
#include <time.h>
#include <pthread.h>
#include <stdatomic.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "structure.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);
FILE* endptr = fopen("sound/sem.txt", "w");
fprintf(endptr, "0");
fclose(endptr);
usleep(100000);
FILE* stptr = fopen("sound/sem.txt", "w");
fprintf(stptr, "1");
fclose(stptr);
for(int k = 0; k < 32; k++) {
if(!threadIsFree[k]) {
printf("[pthread] killing %d\n", k);
pthread_cancel(threads[k]);
}
}
}
void reset_music() {
FILE* endptr = fopen("sound/sem.txt", "w");
fprintf(endptr, "0");
fclose(endptr);
usleep(100000);
FILE* stptr = fopen("sound/sem.txt", "w");
fprintf(stptr, "1");
fclose(stptr);
}
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(k < 99 &&filename[k-14] != '\0') {
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);
int i=0;
//while(name[i] != '\0') {
// printf("%c", name[i]);
// i +=1;
//}
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)) {
//printf("[music] %d\n");
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);
}

10
src/music.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef MUSIC_H
#define MUSIC_H
void init_music();
void reset_music();
void end_music();
void play_sound(char* filename);
#endif

View File

@ -1,7 +1,7 @@
name, elo
./bots/dumb 1637
./bots/dumb2 1592
./bots/dumb3 1611
./bots/dumb4 1519
./bots/dumb 1656
./bots/dumb2 1542
./bots/dumb3 1633
./bots/dumb4 1481
./bots/dumb5 540
./bots/dumb6 562