working on interfaces and buttons

This commit is contained in:
Alexandre 2025-02-16 22:28:07 +01:00
parent 04d782f55a
commit cc7f169340
9 changed files with 271 additions and 25 deletions

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -63,6 +63,20 @@ int max(int a, int b) {
return a;
}
float minf(float a, float b) {
if(a > b) {
return b;
};
return a;
}
float maxf(float a, float b) {
if(a < b) {
return b;
};
return a;
}
double mind(double a, double b) {
if(a > b) {
return b;

View File

@ -9,6 +9,8 @@ int min(int a, int b);
int max(int a, int b);
double mind(double a, double b);
double maxd(double a, double b);
float minf(float a, float b);
float maxf(float a, float b);
double absf(double n);
int convex_seg(int x1, int x2, double theta);
double convex_tri(double a, double tha, double b, double thb, double c, double thc);

View File

@ -29,7 +29,7 @@ double jPress = false;
double gPress = false;
double rPress = false;
void reset_everything(char* folder) {
void reset_everything(int count, char* folder) {
hashtbl_free(visited);
free_proj();
free_interf();
@ -40,7 +40,7 @@ void reset_everything(char* folder) {
init_ent_generator(10);
init_proj();
init_interf();
parse_rooms(1, folder);
parse_rooms(count, folder);
}
void processInput(GLFWwindow *window, float dtime) {
@ -174,11 +174,11 @@ void processInput(GLFWwindow *window, float dtime) {
if(glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
if(!rPress) {
rPress = true;
reset_everything("levels/level_00/");
reset_everything(7, "templates/");
}
} else {
}/*else {
rPress = false;
}
}*/
}
const char *vertexShaderSource = "#version 330 core\n"
@ -412,19 +412,20 @@ int main_alt() {
gl_drawInteger(shaderProgramR, triCount, 0.0f, 0.92f, 0.04f, 128, 128, 128, 0.005f, 1);
gl_drawData(shaderProgramR);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
processInput(window, delta);
if(gamemode == 0) {
movePlayerG(delta);
if(!isInMenu(shaderProgramR)) {
processInput(window, delta);
if(gamemode == 0) {
movePlayerG(delta);
}
teleport_on_edge();
update_entities(delta);
updateProj(delta);
}
teleport_on_edge();
update_entities(delta);
updateProj(delta);
usleep(max(0, interval-(int)(1000000*delta)));
sim_time += deltad;
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
glfwSwapBuffers(window);
glfwPollEvents();

View File

@ -10,7 +10,7 @@
#include <time.h>
#include <glad/glad.h>
#include "../include/glad/glad.h"
//#include <GLFW/glfw3.h>
#include <GLFW/glfw3.h>
#include <cglm/cglm.h>
#include "hash.h"
@ -106,6 +106,7 @@ void gl_drawDigit(unsigned int fragShader, int n, float x, float y, float size,
}
// 7-segment display
// y is the middle of the text
// side can be -1 (aligned right) or 1 (aligned left)
void gl_drawInteger(unsigned int fragShader, int n, float x, float y, float size, int r, int g, int b, float width, int side) {
int left = n;
@ -277,6 +278,7 @@ int get_string_len(char* s) {
}
// 7-segment display
// y is the middle of the text
// side can be -1 (aligned right) 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) {
float curx = x;
@ -343,15 +345,232 @@ void gl_initDrawRect(unsigned int shaderProgram) {
glUseProgram(shaderProgram);
}
// ------------------------------------------------------------------------------------------------------------------------------------------------ //
static onoff_button* buttonList;
static interface* interfaceList;
static int MAX_BUTTON_SIZE = 1024;
static int MAX_INTERFACE_SIZE = 192;
static int bListId;
static int intListId;
static int* current_interface = NULL;
void display_button(int but_id, unsigned int fragShader) {
if(but_id < 0 || but_id > bListId) {
fprintf(stderr, "ERROR : attempting to display a non-existing button (%d)\n", but_id);
} else {
float size = minf(buttonList[but_id].h/2.1f, (buttonList[but_id].w)/get_string_len(buttonList[but_id].text));
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);
}
}
void display_interface(int int_id, unsigned int fragShader) {
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) {
fprintf(stderr, "ERROR : attempting to update a non-existing button (%d)\n", but_id);
return false;
} else {
return (mx >= buttonList[but_id].x && my >= buttonList[but_id].y && mx <= buttonList[but_id].x+buttonList[but_id].w && my <= buttonList[but_id].y+buttonList[but_id].h);
}
}
void menu_actions(int int_id) {
if(int_id < 0 || int_id > intListId) {
fprintf(stderr, "ERROR : attempting to update a non-existing interface (%d)\n", int_id);
} else if(false /* TODO : mouse is clicked */) {
float mx = 0.0f;
float my = 0.0f;
/* TODO : get mouse position and state */
for(int k = 0; k < interfaceList[int_id].nButtons; k++) {
if(is_button_pressed(interfaceList[int_id].buttons[k], mx, my)) {
switch (buttonList[interfaceList[int_id].buttons[k]].type) {
case WARP:
*current_interface = *buttonList[interfaceList[int_id].buttons[k]].metadata;
break;
case SET_VAR:
/* TODO */
break;
case EXIT:
current_interface = NULL;
break;
default:
break;
}
}
}
}
}
bool isInMenu(unsigned int fragShader) {
if(current_interface == NULL) {
return false;
} else {
display_interface(*current_interface, fragShader);
menu_actions(*current_interface);
return true;
}
}
void init_interf() {
buttonList = malloc(sizeof(onoff_button)*256);
interfaceList = malloc(sizeof(interface)*64);
buttonList = malloc(sizeof(onoff_button)*MAX_BUTTON_SIZE);
for(int k = 0; k < MAX_BUTTON_SIZE; k++) {
buttonList[k].id = -1;
}
interfaceList = malloc(sizeof(interface)*MAX_INTERFACE_SIZE);
for(int k = 0; k < MAX_INTERFACE_SIZE; k++) {
interfaceList[k].intfid = -1;
}
bListId = 0;
intListId = 0;
}
// returns the ID of the new button (-1 if error)
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) {
if(bListId < MAX_BUTTON_SIZE) {
buttonList[bListId].id = bListId;
buttonList[bListId].text = text;
buttonList[bListId].red = red;
buttonList[bListId].green = green;
buttonList[bListId].blue = blue;
buttonList[bListId].x = x;
buttonList[bListId].y = y;
buttonList[bListId].w = w;
buttonList[bListId].h = h;
buttonList[bListId].type = actn;
buttonList[bListId].metadata = val;
bListId += 1;
return (bListId-1);
} else {
return -1;
}
}
// returns the ID of the new interface (-1 if error)
int interface_create(char* title, int red, int green, int blue, float x, float y, float w, float h) {
if(intListId < MAX_INTERFACE_SIZE) {
interfaceList[intListId].intfid = intListId;
interfaceList[intListId].title = title;
interfaceList[intListId].red = red;
interfaceList[intListId].green = green;
interfaceList[intListId].blue = blue;
interfaceList[intListId].x = x;
interfaceList[intListId].y = y;
interfaceList[intListId].w = w;
interfaceList[intListId].h = h;
interfaceList[intListId].buttons = malloc(sizeof(int*)*16);
interfaceList[intListId].nButtons = 0;
interfaceList[intListId].nMemButtons = 16;
intListId += 1;
return (intListId-1);
} else {
return -1;
}
}
void interface_link_button(int interface_id, int button_id) {
if(interface_id < 0 || interface_id >= intListId) {
fprintf(stderr, "ERROR : attempting to link non-existing interface (%d)\n", interface_id);
} else if(button_id < 0 || button_id >= bListId) {
fprintf(stderr, "ERROR : attempting to link non-existing button (%d)\n", button_id);
} else {
if(interfaceList[interface_id].nMemButtons == interfaceList[interface_id].nButtons) {
int* newI = malloc(sizeof(int)*2*interfaceList[interface_id].nMemButtons);
for(int k = 0; k < interfaceList[interface_id].nMemButtons; k++) {
newI[k] = interfaceList[interface_id].buttons[k];
}
free(interfaceList[interface_id].buttons);
interfaceList[interface_id].buttons = newI;
interfaceList[interface_id].nMemButtons *= 2;
}
interfaceList[interface_id].buttons[interfaceList[interface_id].nButtons] = button_id;
interfaceList[interface_id].nButtons += 1;
}
}
void interface_unlink_button(int interface_id, int button_id) {
if(interface_id < 0 || interface_id >= intListId) {
fprintf(stderr, "ERROR : attempting to unlink non-existing interface (%d)\n", interface_id);
} else if(button_id < 0 || button_id >= bListId) {
fprintf(stderr, "ERROR : attempting to unlink non-existing button (%d)\n", button_id);
} else {
int not_found = 1;
for(int k = 0; k < not_found*interfaceList[interface_id].nButtons; k++) {
if(interfaceList[interface_id].buttons[k] == button_id) {
not_found = 0;
interfaceList[interface_id].buttons[k] = interfaceList[interface_id].buttons[interfaceList[interface_id].nButtons-1];
interfaceList[interface_id].nButtons -= 1;
}
if(not_found) {
fprintf(stderr, "WARNING : interface %d does not link to buttton %d\n", interface_id, button_id);
}
}
}
}
void interface_set(int interface_id) {
*current_interface = interface_id;
}
void free_interf() {
free(buttonList);
free(interfaceList);
}
/*
typedef struct onoff_button {
int id;
char* text;
float x; float y; float w; float h;
int red;
int green;
int blue;
// {WARP, SET_VAR}
button_action type;
// the value to change (if SET_VAR) or the destination interface (if WARP)
int* metadata;
} onoff_button;
typedef struct interface {
int intfid;
char* title;
float x; float y; float w; float h;
int red;
int green;
int blue;
int* buttons;
int nButtons;
int nMemButtons;
} interface;
*/

View File

@ -1,18 +1,16 @@
#ifndef MENUS_H
#define MENUS_H
typedef enum button_action {WARP, SET_VAR} button_action ;
typedef enum button_action {WARP, SET_VAR, EXIT} button_action ;
typedef struct onoff_button {
int id;
char* text;
double x; double y; double w; double h;
int red;
int green;
int blue;
float x; float y; float w; float h;
int red; int green; int blue;
// {WARP, SET_VAR}
// {WARP, SET_VAR, EXIT}
button_action type;
// the value to change (if SET_VAR) or the destination interface (if WARP)
@ -23,9 +21,12 @@ typedef struct interface {
int intfid;
char* title;
float x; float y; float w; float h;
int red; int green; int blue;
onoff_button* buttons;
int nbuttons;
int* buttons;
int nButtons;
int nMemButtons;
} interface;
void initMenus();
@ -38,6 +39,15 @@ void gl_drawString(unsigned int fragShader, char* str, float x, float y, float s
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();
bool isInMenu(unsigned int fragShader);
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);
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_unlink_button(int interface_id, int button_id);
void interface_set(int interface_id);
void free_interf();
#endif