resolved menus being bugged

This commit is contained in:
Alexandre 2025-02-17 16:46:50 +01:00
parent cb5ee0de8a
commit dab216810e
7 changed files with 124 additions and 75 deletions

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -29,7 +29,7 @@ double jPress = false;
double gPress = false; double gPress = false;
double rPress = false; double rPress = false;
void reset_everything(int count, char* folder) { void reset_everything(GLFWwindow *window, int count, char* folder) {
hashtbl_free(visited); hashtbl_free(visited);
free_proj(); free_proj();
free_interf(); free_interf();
@ -39,7 +39,7 @@ void reset_everything(int count, char* folder) {
init_hashtbl(); init_hashtbl();
init_ent_generator(10); init_ent_generator(10);
init_proj(); init_proj();
init_interf(); init_interf(window);
parse_rooms(count, folder); parse_rooms(count, folder);
} }
@ -174,7 +174,7 @@ void processInput(GLFWwindow *window, float dtime) {
if(glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) { if(glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
if(!isMenuOpen() && !rPress) { if(!isMenuOpen() && !rPress) {
rPress = true; rPress = true;
reset_everything(7, "templates/"); reset_everything(window, 7, "templates/");
} }
}/*else { }/*else {
rPress = false; rPress = false;
@ -252,7 +252,7 @@ int main_alt() {
init_hashtbl(); init_hashtbl();
init_ent_generator(10); init_ent_generator(10);
init_proj(); init_proj();
init_interf(); init_interf(window);
build_all_menus(); build_all_menus();
parse_rooms(7, "templates/"); parse_rooms(7, "templates/");
@ -436,6 +436,7 @@ int main_alt() {
delta = slp_time+((float)finish - (float)origin)/CLOCKS_PER_SEC; delta = slp_time+((float)finish - (float)origin)/CLOCKS_PER_SEC;
deltad = slp_time+((double)finish - (double)origin)/CLOCKS_PER_SEC; deltad = slp_time+((double)finish - (double)origin)/CLOCKS_PER_SEC;
origin = clock(); origin = clock();
incr = 0.0f;
} }
hashtbl_free(visited); hashtbl_free(visited);

View File

@ -20,16 +20,19 @@
#include "menus.h" #include "menus.h"
float rectDefault[18] ; float rectDefault[18] ;
float incr;
static mat4 scale, slide; static mat4 scale, slide;
void initMenus() { void initMenus() {
rectDefault[0] = -0.5f; rectDefault[1] = -0.5f; rectDefault[2] = -1.0f; rectDefault[0] = -0.5f; rectDefault[1] = -0.5f; rectDefault[2] = -0.99f;
rectDefault[3] = -0.5f; rectDefault[4] = 0.5f; rectDefault[5] = -1.0f; rectDefault[3] = -0.5f; rectDefault[4] = 0.5f; rectDefault[5] = -0.99f;
rectDefault[6] = 0.5f; rectDefault[7] = 0.5f; rectDefault[8] = -1.0f; rectDefault[6] = 0.5f; rectDefault[7] = 0.5f; rectDefault[8] = -0.99f;
rectDefault[9] = -0.5f; rectDefault[10] = -0.5f; rectDefault[11] = -1.0f; rectDefault[9] = -0.5f; rectDefault[10] = -0.5f; rectDefault[11] = -0.99f;
rectDefault[12] = 0.5f; rectDefault[13] = 0.5f; rectDefault[14] = -1.0f; rectDefault[12] = 0.5f; rectDefault[13] = 0.5f; rectDefault[14] = -0.99f;
rectDefault[15] = 0.5f; rectDefault[16] = -0.5f; rectDefault[17] = -1.0f; rectDefault[15] = 0.5f; rectDefault[16] = -0.5f; rectDefault[17] = -0.99f;
incr = 0.0f;
} }
void gl_drawRect(unsigned int fragShader, float x, float y, float w, float h, int r, int g, int b) { void gl_drawRect(unsigned int fragShader, float x, float y, float w, float h, int r, int g, int b) {
@ -38,7 +41,8 @@ void gl_drawRect(unsigned int fragShader, float x, float y, float w, float h, in
scale[0][0] = w; scale[0][0] = w;
scale[1][1] = h; scale[1][1] = h;
scale[2][2] = 0.5f; scale[2][2] = 0.5f+incr;
incr += 0.001f;
glm_translate(slide, (vec3){x+w/2, y+h/2, 0.0f}); glm_translate(slide, (vec3){x+w/2, y+h/2, 0.0f});
@ -279,12 +283,14 @@ int get_string_len(char* s) {
// 7-segment display // 7-segment display
// y is the middle of the text // y is the middle of the text
// side can be -1 (aligned right) or 1 (aligned left) // side can be -1 (aligned right), 0 (center) or 1 (aligned left)
void gl_drawString(unsigned int fragShader, char* str, float x, float y, float size, int r, int g, int b, float width, int side) { void gl_drawString(unsigned int fragShader, char* str, float x, float y, float size, int r, int g, int b, float width, int side) {
float curx = x; float curx = x;
int len = get_string_len(str); int len = get_string_len(str);
if(side == 1) { if(side == 1) {
curx += ((len)*(size+4*width)); curx += ((len-1)*(size+4*width));
} else if(side == 0) {
curx += ((len-1)*(size+4*width))/2.0f;
} }
char c = str[len-1]; char c = str[len-1];
int i = len-1; int i = len-1;
@ -358,32 +364,27 @@ static int intListId;
static int* current_interface; static int* current_interface;
int mn_get_color(int r, int g, int b) {
if(r+g+b <= 384) {
return 255;
} else {
return 0;
}
}
void display_button(int but_id, unsigned int fragShader) { void display_button(int but_id, unsigned int fragShader) {
if(but_id < 0 || but_id > bListId) { if(but_id < 0 || but_id > bListId) {
fprintf(stderr, "ERROR : attempting to display a non-existing button (%d)\n", but_id); fprintf(stderr, "ERROR : attempting to display a non-existing button (%d)\n", but_id);
} else { } else {
float size = minf(buttonList[but_id].h/2.1f, (buttonList[but_id].w)/get_string_len(buttonList[but_id].text)); float size = minf(buttonList[but_id].h/2.3f, (buttonList[but_id].w)/(get_string_len(buttonList[but_id].text)));
int retcol = mn_get_color(buttonList[but_id].red, buttonList[but_id].green, buttonList[but_id].blue);
gl_drawRect(fragShader, buttonList[but_id].x, buttonList[but_id].y, buttonList[but_id].w, buttonList[but_id].h, buttonList[but_id].red, buttonList[but_id].green, buttonList[but_id].blue); gl_drawRect(fragShader, buttonList[but_id].x, buttonList[but_id].y, buttonList[but_id].w, buttonList[but_id].h, buttonList[but_id].red, buttonList[but_id].green, buttonList[but_id].blue);
gl_drawString(fragShader, buttonList[but_id].text, buttonList[but_id].x+(size+size/10.0f)/2.0f, buttonList[but_id].y+buttonList[but_id].h/2.0f, size, 255-buttonList[but_id].red, 255-buttonList[but_id].green, 255-buttonList[but_id].blue, size/10.0f, 1); gl_drawString(fragShader, buttonList[but_id].text, buttonList[but_id].x+buttonList[but_id].w/2.0f, buttonList[but_id].y+buttonList[but_id].h/2.0f, 0.7f*size, retcol, retcol, retcol, 0.7f*size/10.0f, 0);
} }
} }
void display_interface(int int_id, unsigned int fragShader) { bool is_hovering_button(int but_id, float mx, float my) {
if(int_id < 0 || int_id > intListId) {
fprintf(stderr, "ERROR : attempting to display a non-existing interface (%d)\n", int_id);
} else {
float size = minf(interfaceList[int_id].h/2.1f, (interfaceList[int_id].w)/get_string_len(interfaceList[int_id].title));
gl_drawRect(fragShader, interfaceList[int_id].x, interfaceList[int_id].y, interfaceList[int_id].w, interfaceList[int_id].h, interfaceList[int_id].red, interfaceList[int_id].green, interfaceList[int_id].blue);
gl_drawString(fragShader, interfaceList[int_id].title, interfaceList[int_id].x+(size+size/10.0f)/2.0f, interfaceList[int_id].y+interfaceList[int_id].h/2.0f, size, 255-interfaceList[int_id].red, 255-interfaceList[int_id].green, 255-interfaceList[int_id].blue, size/10.0f, 1);
for(int k = 0; k < interfaceList[int_id].nButtons; k++) {
display_button(interfaceList[int_id].buttons[k], fragShader);
}
}
}
bool is_button_pressed(int but_id, float mx, float my) {
if(but_id < 0 || but_id > bListId) { if(but_id < 0 || but_id > bListId) {
fprintf(stderr, "ERROR : attempting to update a non-existing button (%d)\n", but_id); fprintf(stderr, "ERROR : attempting to update a non-existing button (%d)\n", but_id);
return false; return false;
@ -392,28 +393,44 @@ bool is_button_pressed(int but_id, float mx, float my) {
} }
} }
void menu_actions(GLFWwindow *window, int int_id) { void display_interface(int int_id, unsigned int fragShader, float mx, float my) {
printf("%d\n", glfwGetKey(window, GLFW_MOUSE_BUTTON_LEFT));
if(int_id < 0 || int_id > intListId) { if(int_id < 0 || int_id > intListId) {
fprintf(stderr, "ERROR : attempting to update a non-existing interface (%d)\n", int_id); fprintf(stderr, "ERROR : attempting to display a non-existing interface (%d)\n", int_id);
} else if( } else {
glfwGetKey(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS float size = minf(interfaceList[int_id].h/2.1f, (interfaceList[int_id].w)/(get_string_len(interfaceList[int_id].title)));
) { int retcol = mn_get_color(interfaceList[int_id].red, interfaceList[int_id].green, interfaceList[int_id].blue);
gl_drawRect(fragShader, interfaceList[int_id].x, interfaceList[int_id].y, interfaceList[int_id].w, interfaceList[int_id].h, interfaceList[int_id].red, interfaceList[int_id].green, interfaceList[int_id].blue);
gl_drawString(fragShader, interfaceList[int_id].title, interfaceList[int_id].x+interfaceList[int_id].w/2.0f, interfaceList[int_id].y+interfaceList[int_id].h/2.0f, 0.7f*size, retcol, retcol, retcol, 0.7f*size/10.0f, 0);
for(int k = 0; k < interfaceList[int_id].nButtons; k++) {
if(is_hovering_button(interfaceList[int_id].buttons[k], mx, my)) {
gl_drawRect(fragShader, buttonList[interfaceList[int_id].buttons[k]].x-0.015f, buttonList[interfaceList[int_id].buttons[k]].y-0.015f, buttonList[interfaceList[int_id].buttons[k]].w+0.03f, buttonList[interfaceList[int_id].buttons[k]].h+0.03f, 255, 255, 255);
}
display_button(interfaceList[int_id].buttons[k], fragShader);
}
}
}
bool some_bool = true;
void menu_actions(GLFWwindow *window, int button, int action, int mods) {
if(*current_interface < 0 || *current_interface > intListId) {
//fprintf(stderr, "ERROR : attempting to update a non-existing interface (%d)\n", *current_interface);
} else{
if(some_bool) {
some_bool = false;
double mx = 0.0; double mx = 0.0;
double my = 0.0; double my = 0.0;
glfwGetCursorPos(window, &mx, &my); glfwGetCursorPos(window, &mx, &my);
mx = 2.0*mx/1500.0 -1.0; mx = 2.0*mx/1500.0 -1.0;
my = 2.0*my/1000.0 -1.0; my = 1.0 -2.0*my/1000.0;
//printf("EE %lf, %lf\n", mx, my); for(int k = 0; k < interfaceList[*current_interface].nButtons; k++) {
//fflush(stdout); if(is_hovering_button(interfaceList[*current_interface].buttons[k], (float)mx, (float)my)) {
for(int k = 0; k < interfaceList[int_id].nButtons; k++) { if(buttonList[interfaceList[*current_interface].buttons[k]].onClick != NULL) {
if(is_button_pressed(interfaceList[int_id].buttons[k], (float)mx, (float)my)) { (*buttonList[interfaceList[*current_interface].buttons[k]].onClick)(buttonList[interfaceList[*current_interface].buttons[k]].arg);
if(buttonList[interfaceList[int_id].buttons[k]].onClick != NULL) {
(*buttonList[interfaceList[int_id].buttons[k]].onClick)(buttonList[interfaceList[int_id].buttons[k]].arg);
} }
switch (buttonList[interfaceList[int_id].buttons[k]].type) { switch (buttonList[interfaceList[*current_interface].buttons[k]].type) {
case WARP: case WARP:
*current_interface = *buttonList[interfaceList[int_id].buttons[k]].metadata; *current_interface = *((int*)buttonList[interfaceList[*current_interface].buttons[k]].metadata);
break; break;
case NONE: case NONE:
@ -430,8 +447,12 @@ void menu_actions(GLFWwindow *window, int int_id) {
default: default:
break; break;
} }
break;
} }
} }
} else {
some_bool = true;
}
} }
} }
@ -440,8 +461,13 @@ bool isInMenu(GLFWwindow *win, unsigned int fragShader) {
return false; return false;
} else { } else {
//printf("displaying %d\n", *current_interface); //printf("displaying %d\n", *current_interface);
display_interface(*current_interface, fragShader); double mx = 0.0;
menu_actions(win, *current_interface); double my = 0.0;
glfwGetCursorPos(win, &mx, &my);
mx = 2.0*mx/1500.0 -1.0;
my = 1.0 -2.0*my/1000.0;
display_interface(*current_interface, fragShader, (float)mx, (float)my);
//menu_actions(win, *current_interface);
return true; return true;
} }
} }
@ -450,7 +476,7 @@ bool isMenuOpen() {
return (*current_interface != -1); return (*current_interface != -1);
} }
void init_interf() { void init_interf(GLFWwindow *window) {
buttonList = malloc(sizeof(onoff_button)*MAX_BUTTON_SIZE); buttonList = malloc(sizeof(onoff_button)*MAX_BUTTON_SIZE);
for(int k = 0; k < MAX_BUTTON_SIZE; k++) { for(int k = 0; k < MAX_BUTTON_SIZE; k++) {
buttonList[k].id = -1; buttonList[k].id = -1;
@ -463,12 +489,13 @@ void init_interf() {
intListId = 0; intListId = 0;
current_interface = malloc(sizeof(int)); current_interface = malloc(sizeof(int));
*current_interface = -1; *current_interface = -1;
glfwSetMouseButtonCallback(window, menu_actions);
} }
// returns the ID of the new button (-1 if error) // returns the ID of the new button (-1 if error)
// actn can be within {NONE, WARP, SET_VAR, EXIT} // actn can be within {NONE, WARP, SET_VAR, EXIT}
// if actn is WARP or SET_VAR then val shall not be NULL // if actn is WARP or SET_VAR then val shall not be NULL
int button_create_onoff(char* text, int red, int green, int blue, float x, float y, float w, float h, button_action actn, int* val, void (*onClick)(void*), void* arg) { 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, void_type cast, void (*onClick)(void*), void* arg) {
if(bListId < MAX_BUTTON_SIZE) { if(bListId < MAX_BUTTON_SIZE) {
buttonList[bListId].id = bListId; buttonList[bListId].id = bListId;
@ -484,6 +511,7 @@ int button_create_onoff(char* text, int red, int green, int blue, float x, float
buttonList[bListId].type = actn; buttonList[bListId].type = actn;
buttonList[bListId].metadata = val; buttonList[bListId].metadata = val;
buttonList[bListId].cast = cast;
buttonList[bListId].onClick = onClick; buttonList[bListId].onClick = onClick;
buttonList[bListId].arg = arg; buttonList[bListId].arg = arg;
@ -569,13 +597,29 @@ void interface_set(int interface_id) {
*/ */
// build and link everything here // build and link everything here
int zero = 0;
int one = 1;
int two = 2;
int three = 3;
void build_all_menus() { void build_all_menus() {
int welcome_start_b = button_create_onoff("start", 32, 255, 255, -0.25f, -0.25f, 0.5f, 0.5f, EXIT, NULL, NULL, NULL); int welcome_start_go = button_create_onoff("start", 0, 255, 255, -0.25f, 0.05f, 0.5f, 0.3f, EXIT, NULL, INT, NULL, NULL);
int welcome_start_i = interface_create("Welcome", 128, 128, 128, -0.4f, 0.7f, 0.8f, 0.25f); int welcome_start_settings = button_create_onoff("config", 96, 96, 96, -0.25f, -0.35f, 0.5f, 0.3f, WARP, &one, INT, NULL, NULL);
//printf("%d %d\n", welcome_start_i, welcome_start_b); int welcome_start_i = interface_create("Welcome", 255, 255, 255, -0.4f, 0.7f, 0.8f, 0.25f);
interface_link_button(welcome_start_i, welcome_start_b);
interface_link_button(welcome_start_i, welcome_start_go);
interface_link_button(welcome_start_i, welcome_start_settings);
int settings_speed = button_create_onoff("speed", 0, 192, 192, -0.25f, 0.2f, 0.5f, 0.3f, SET_VAR, &speed, DOUBLE, NULL, NULL);
int settings_sensitivity = button_create_onoff("sensibility", 192, 192, 0, -0.25f, -0.15f, 0.5f, 0.3f, SET_VAR, &sensitivity, DOUBLE, NULL, NULL);
int settings_exit = button_create_onoff("back", 192, 64, 64, -0.25f, -0.5f, 0.5f, 0.3f, WARP, &zero, INT, NULL, NULL);
int settings_i = interface_create("Settings", 128, 128, 128, -0.4f , 0.7f, 0.8f, 0.25f);
interface_link_button(settings_i, settings_speed);
interface_link_button(settings_i, settings_sensitivity);
interface_link_button(settings_i, settings_exit);
interface_set(welcome_start_i); interface_set(welcome_start_i);
//printf("------------- %d\n", *current_interface);
} }
void free_interf() { void free_interf() {

View File

@ -2,6 +2,7 @@
#define MENUS_H #define MENUS_H
typedef enum button_action {NONE, WARP, SET_VAR, EXIT} button_action; typedef enum button_action {NONE, WARP, SET_VAR, EXIT} button_action;
typedef enum void_type {INT, FLOAT, DOUBLE} void_type;
typedef struct onoff_button { typedef struct onoff_button {
int id; int id;
@ -14,7 +15,8 @@ typedef struct onoff_button {
button_action type; button_action type;
// the value to change (if SET_VAR) or the destination interface (if WARP) // the value to change (if SET_VAR) or the destination interface (if WARP)
int* metadata; void* metadata;
void_type cast;
// is called upon clicking button // is called upon clicking button
// this function is executed before metadata is used // this function is executed before metadata is used
@ -43,12 +45,12 @@ void gl_drawInteger(unsigned int fragShader, int n, float x, float y, float size
void gl_drawString(unsigned int fragShader, char* str, float x, float y, float size, int r, int g, int b, float width, int side); void gl_drawString(unsigned int fragShader, char* str, float x, float y, float size, int r, int g, int b, float width, int side);
void gl_printf(unsigned int fragShader, float x, float y, float size, float width, int r, int g, int b, const char* str, ...); void gl_printf(unsigned int fragShader, float x, float y, float size, float width, int r, int g, int b, const char* str, ...);
void init_interf(); void init_interf(GLFWwindow *window);
bool isInMenu(GLFWwindow *win, unsigned int fragShader); bool isInMenu(GLFWwindow *win, unsigned int fragShader);
bool isMenuOpen(); bool isMenuOpen();
int button_create_onoff(char* text, int red, int green, int blue, float x, float y, float w, float h, button_action actn, int* val, void (*onClick)(void*), void* arg); 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, void_type cast, void (*onClick)(void*), void* arg);
int interface_create(char* title, int red, int green, int blue, float x, float y, float w, float h); int interface_create(char* title, int red, int green, int blue, float x, float y, float w, float h);
void interface_link_button(int interface_id, int button_id); void interface_link_button(int interface_id, int button_id);
void interface_unlink_button(int interface_id, int button_id); void interface_unlink_button(int interface_id, int button_id);

View File

@ -162,4 +162,6 @@ extern int njumps;
extern unsigned int fffff; extern unsigned int fffff;
extern int gamemode; extern int gamemode;
extern float incr;
#endif #endif