fixed music still playing upon closing application + started working on math module

This commit is contained in:
Alexandre 2025-02-26 10:59:27 +01:00
parent 8f0f033e81
commit 62ba393a15
15 changed files with 217 additions and 16 deletions

View File

@ -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/music.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/maeth.o obj/move.o obj/base.o obj/hash.o
mkdir -p bin
$(CC) $(FLAGS) $^ $(LFLAGS) -o $@
@ -27,6 +27,7 @@ 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/maeth.o: src/maeth.c
obj/menus.o: src/menus.c
obj/hash.o: src/hash.c

BIN
bin/back

Binary file not shown.

BIN
obj/maeth.o Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -2,8 +2,16 @@
#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);}
@ -14,7 +22,13 @@ int main(int argc, char** argv) {
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));
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();

1
sound/sem.txt Normal file
View File

@ -0,0 +1 @@
1

134
src/maeth.c Normal file
View File

@ -0,0 +1,134 @@
#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 "../include/glad/glad.h"
#include <GLFW/glfw3.h>
#include <cglm/cglm.h>
#include "hash.h"
#include "base.h"
#include "menus.h"
#include "maeth.h"
// --------------------------------------------------------------------- //
// between 1 and 20
int mathDiff = 3;
// between 1.0 and 60.0
double mathAnsT = 10.0;
// 0 or 1
int mathDoDmg = 1;
// between 1 and 1000
int mathDmg = 500;
// --------------------------------------------------------------------- //
// min bound for number gen (disregarding difficulty)
int mthInf = 1;
// max bound for number gen (disregarding difficulty)
int mthSup = 100;
// whether or not to allow operations (SUM is always enabled)
bool mthAllow[3];
// --------------------------------------------------------------------- //
int mthDiffSetting, mthAnsTSetting, mthDoDmgSetting, mthDmgSetting;
int mthInfSetting, mthSupSetting;
int mthExitSetting;
int mthInterface;
void rebalance(void* arg) {
if(mthInf > mthSup) {
mthInf = 1;
mthSup = 100;
}
}
void init_math_interfaces() {
mthDiffSetting = button_create_onoff("difficulty", 128, 64, 64, -0.6f, 0.4f, 0.5f, 0.25f, SET_VAR, &mathDiff, 1, 20, INT, NULL, NULL);
mthAnsTSetting = button_create_onoff("answer time", 255, 255, 64, -0.6f, 0.1f, 0.5f, 0.25f, SET_VAR, &mathAnsT, 1.0, 60.0, DOUBLE, NULL, NULL);
mthDoDmgSetting = button_create_onoff("damage if wrong", 255, 64, 64, -0.6f, -0.2f, 0.5f, 0.25f, SET_VAR, &mathDoDmg, 0, 1, INT, NULL, NULL);
mthDmgSetting = button_create_onoff("damage value", 128, 6, 6, -0.6f, -0.5f, 0.5f, 0.25f, SET_VAR, &mathDmg, 1, 1000, INT, NULL, NULL);
mthInfSetting = button_create_onoff("minimum val", 128, 128, 255, 0.1f, 0.4f, 0.5f, 0.25f, SET_VAR, &mthInf, 1, 32767, INT, NULL, NULL);
mthSupSetting = button_create_onoff("maximum val", 128, 128, 255, 0.1f, 0.1f, 0.5f, 0.25f, SET_VAR, &mthSup, 1, 32767, INT, NULL, NULL);
mthExitSetting = button_create_onoff("back", 64, 255, 64,-0.25f, -0.8f, 0.5f, 0.25f, WARP, &welcome_start_i, 0, 1, INT, &rebalance, NULL);
mthInterface = interface_create("math configs", 255, 192, 192, -0.4f , 0.7f, 0.8f, 0.25f);
interface_link_button(mthInterface, mthDiffSetting);
interface_link_button(mthInterface, mthAnsTSetting);
interface_link_button(mthInterface, mthDoDmgSetting);
interface_link_button(mthInterface, mthDmgSetting);
interface_link_button(mthInterface, mthInfSetting);
interface_link_button(mthInterface, mthSupSetting);
interface_link_button(mthInterface, mthExitSetting);
}
void init_maeth() {
mthAllow[0] = true;
mthAllow[1] = true;
mthAllow[2] = true;
}
ctype get_ctype() {
int r = rand()%4;
return (ctype)(1+r);
}
formula create_formula_aux(int depth, int max_depth) {
formula newF = malloc(sizeof(struct calcul));
if(depth == max_depth) {
newF->calc1 = NULL;
newF->calc2 = NULL;
newF->operation = VALUE;
newF->val = mthInf + rand()%(mthSup-mthInf);
} else {
newF->calc1 = create_formula_aux(depth+1, max_depth);
newF->calc2 = create_formula_aux(depth+1, max_depth);
newF->operation = get_ctype();
newF->val = 0; // irrelevant
}
return newF;
}
int solve_formula(formula fm) {
switch (fm->operation) {
case VALUE:
return fm->val;
case SUM:
return (solve_formula(fm->calc1) + solve_formula(fm->calc2));
case DIFF:
return (solve_formula(fm->calc1) - solve_formula(fm->calc2));
case MULT:
return (solve_formula(fm->calc1) * solve_formula(fm->calc2));
case MODULO:
return (solve_formula(fm->calc1) % solve_formula(fm->calc2));
default:
fprintf(stderr, "ERROR : unsupported operation type\n");
return -1;
}
}
void free_formula(formula fm) {
if(fm != NULL) {
free_formula(fm->calc1);
free_formula(fm->calc2);
free(fm);
}
}

17
src/maeth.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef MATH_H
#define MATH_H
typedef enum ctype {VALUE, SUM, DIFF, MULT, MODULO} ctype;
struct calcul {
int val;
ctype operation;
struct calcul* calc1;
struct calcul* calc2;
};
typedef struct calcul* formula;
void init_math_interfaces();
void init_maeth();
#endif

View File

@ -670,8 +670,8 @@ int main_alt() {
dmgCD = 0.5f;
}
finish = clock();
delta = slp_time+((float)finish - (float)origin)/CLOCKS_PER_SEC;
deltad = slp_time+((double)finish - (double)origin)/CLOCKS_PER_SEC;
delta = (float)slp_time;//+((float)finish - (float)origin)/CLOCKS_PER_SEC;
deltad = slp_time;//+((double)finish - (double)origin)/CLOCKS_PER_SEC;
origin = clock();
incr = 0.0f;
}
@ -708,6 +708,7 @@ int main_alt() {
int main(int argc, char** argv) {
srand(time(NULL));
init_music();
init_maeth();
triCount = 0;
sim_time = 0.0;
gamemode = 0;

View File

@ -16,6 +16,7 @@
#include "hash.h"
#include "base.h"
#include "entities.h"
#include "maeth.h"
#include "menus.h"
float rectDefault[18] ;
@ -173,7 +174,7 @@ void gl_drawFloat(unsigned int fragShader, float n, float x, float y, float size
left = left/10;
countf += 1;
if(countf == 3) {
gl_drawRect(fragShader, curx+size/2+3*width/2, y-size-width, width, 2*width, r, g, b);
gl_drawRect(fragShader, curx+size/2+width/2, y-size-width, 2*width, 2*width, r, g, b);
}
if(left == 0) {
acc -= 1;
@ -520,14 +521,22 @@ void menu_actions(GLFWwindow *window, int button, int action, int mods) {
pressed = true;
if(!clicked) {
clicked = true;
toad = (buttonList[but_id].sup-buttonList[but_id].inf)/50.0;
if(buttonList[but_id].cast != INT) {
toad = (buttonList[but_id].sup-buttonList[but_id].inf)/50.0;
} else {
toad = 1.0;
}
}
}
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
pressed = true;
if(!clicked) {
clicked = true;
toad = (buttonList[but_id].inf-buttonList[but_id].sup)/50.0;
if(buttonList[but_id].cast != INT) {
toad = (buttonList[but_id].inf-buttonList[but_id].sup)/50.0;
} else {
toad = -1.0;
}
}
}
if(glfwGetKey(window, GLFW_KEY_M) == GLFW_PRESS) {
@ -562,7 +571,7 @@ void menu_actions(GLFWwindow *window, int button, int action, int mods) {
switch (buttonList[but_id].cast) {
case INT:
int* valuei = (int*)buttonList[but_id].metadata;
*valuei = max(min(((int)toad) + ((int)mult)*(*valuei), (int)buttonList[but_id].sup), (int)buttonList[but_id].inf);
*valuei = (int)(maxf(minf(((float)toad) + ((float)mult)*(*valuei), (float)buttonList[but_id].sup), (float)buttonList[but_id].inf));
gl_drawInteger(fShader, *valuei, buttonList[but_id].x+buttonList[but_id].w/2.0f, buttonList[but_id].y+buttonList[but_id].h/2.0f, size*0.7f, retcol, retcol, retcol, size/10.0f*0.7f, 0);
break;
@ -671,6 +680,7 @@ void free_interf() {
// returns the ID of the new button (-1 if error)
// actn can be within {NONE, WARP, SET_VAR, EXIT}
// void_type can be within {INT, FLOAT, DOUBLE}
// if actn is WARP or SET_VAR then val shall not be NULL
// min and max dont matter is actn is not SET_VAR
int button_create_onoff(char* text, int red, int green, int blue, float x, float y, float w, float h, button_action actn, void* val, double min, double max, void_type cast, void (*onClick)(void*), void* arg) {
@ -779,20 +789,26 @@ void interface_set(int interface_id) {
// build and link everything here
int button_ok;
int button_pay;
int welcome_start_i, settings_i;
void build_all_menus() {
int welcome_start_go = button_create_onoff("start", 0, 255, 255, -0.25f, 0.05f, 0.5f, 0.3f, EXIT, NULL, 0.0, 0.0, INT, NULL, NULL);
int welcome_start_settings = button_create_onoff("config", 96, 96, 96, -0.25f, -0.35f, 0.5f, 0.3f, WARP, &(numbers[1]), INT, 0.0, 0.0, NULL, NULL);
int welcome_start_i = interface_create("Welcome", 255, 255, 255, -0.4f, 0.7f, 0.8f, 0.25f);
init_math_interfaces();
welcome_start_i = interface_create("Welcome", 255, 255, 255, -0.4f, 0.7f, 0.8f, 0.25f);
settings_i = interface_create("Settings", 128, 128, 128, -0.4f, 0.7f, 0.8f, 0.25f);
int welcome_start_go = button_create_onoff("start", 0, 255, 255, -0.25f, 0.05f, 0.5f, 0.3f, EXIT, NULL, 0.0, 0.0, INT, NULL, NULL);
int welcome_start_settings = button_create_onoff("config", 96, 96, 96 , -0.25f, -0.35f, 0.5f, 0.3f, WARP, &settings_i, 0.0, 0.0, INT, NULL, NULL);
int welcome_start_math = button_create_onoff("math config", 64, 64, 255, -0.25f, -0.75f, 0.5f, 0.3f, WARP, &mthInterface, 0.0, 0.0, INT, NULL, NULL);
interface_link_button(welcome_start_i, welcome_start_go);
interface_link_button(welcome_start_i, welcome_start_settings);
interface_link_button(welcome_start_i, welcome_start_math);
int settings_speed = button_create_onoff("speed", 0, 192, 192, -0.25f, 0.2f, 0.5f, 0.3f, SET_VAR, &speed, 0.1, 20.0, DOUBLE, NULL, NULL);
int settings_sensitivity = button_create_onoff("sensibility", 192, 192, 0, -0.25f, -0.15f, 0.5f, 0.3f, SET_VAR, &sensitivity, 0.01, 0.5, DOUBLE, NULL, NULL);
int settings_fov = button_create_onoff("fov", 192, 0, 192, -0.25f, -0.5f, 0.5f, 0.3f, SET_VAR, &fov, 30.0, 150.0, DOUBLE, NULL, NULL);
int settings_exit = button_create_onoff("back", 192, 64, 64, -0.25f, -0.85f, 0.5f, 0.3f, WARP, &(numbers[0]), INT, 0.0, 0.0, NULL, NULL);
int settings_i = interface_create("Settings", 128, 128, 128, -0.4f , 0.7f, 0.8f, 0.25f);
int settings_exit = button_create_onoff("back", 192, 64, 64, -0.25f, -0.85f, 0.5f, 0.3f, WARP, &welcome_start_i, INT, 0.0, 0.0, NULL, NULL);
interface_link_button(settings_i, settings_speed);
interface_link_button(settings_i, settings_sensitivity);
@ -801,6 +817,8 @@ void build_all_menus() {
button_ok = button_create_onoff("ok", 32, 255, 32, -0.2f, -0.9f, 0.4f, 0.3f, EXIT, NULL, 0.0, 0.0, INT, NULL, NULL);
button_pay = button_create_onoff("advance", 32, 255, 32, -0.2f, -0.9f, 0.4f, 0.3f, EXIT, NULL, 0.0, 0.0, INT, NULL, NULL);
//printf("> %d %d <------------------------------------>\n", welcome_start_i, settings_i);
interface_set(welcome_start_i);
}

View File

@ -32,6 +32,17 @@ void init_music() {
void end_music() {
pthread_mutex_destroy(&lock);
FILE* endptr = fopen("sound/sem.txt", "w");
fprintf(endptr, "0");
fclose(endptr);
usleep(500000);
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);

View File

@ -194,4 +194,8 @@ extern bool buttonSwitch[16];
extern float buttonTimes[16];
extern float buttonMaxT[16];
extern int mthInterface;
extern int welcome_start_i;
extern int settings_i;
#endif