Compare commits
2 Commits
04d782f55a
...
cb5ee0de8a
Author | SHA1 | Date |
---|---|---|
|
cb5ee0de8a | |
|
cc7f169340 |
BIN
obj/base.o
BIN
obj/base.o
Binary file not shown.
BIN
obj/main.o
BIN
obj/main.o
Binary file not shown.
BIN
obj/menus.o
BIN
obj/menus.o
Binary file not shown.
14
src/base.c
14
src/base.c
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
32
src/main.c
32
src/main.c
|
@ -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) {
|
||||
|
@ -172,13 +172,13 @@ void processInput(GLFWwindow *window, float dtime) {
|
|||
|
||||
// reset
|
||||
if(glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
|
||||
if(!rPress) {
|
||||
if(!isMenuOpen() && !rPress) {
|
||||
rPress = true;
|
||||
reset_everything("levels/level_00/");
|
||||
reset_everything(7, "templates/");
|
||||
}
|
||||
} else {
|
||||
}/*else {
|
||||
rPress = false;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
const char *vertexShaderSource = "#version 330 core\n"
|
||||
|
@ -246,11 +246,14 @@ int main_alt() {
|
|||
glCullFace(GL_FRONT);
|
||||
glDepthFunc(GL_LESS);
|
||||
|
||||
//glfwSetMouseButtonCallback(window, mouse_button_callback);
|
||||
|
||||
init_csts();
|
||||
init_hashtbl();
|
||||
init_ent_generator(10);
|
||||
init_proj();
|
||||
init_interf();
|
||||
build_all_menus();
|
||||
parse_rooms(7, "templates/");
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------------------------------------- //
|
||||
|
@ -412,19 +415,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(window, 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();
|
||||
|
||||
|
|
268
src/menus.c
268
src/menus.c
|
@ -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,273 @@ 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;
|
||||
|
||||
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(GLFWwindow *window, int int_id) {
|
||||
printf("%d\n", glfwGetKey(window, GLFW_MOUSE_BUTTON_LEFT));
|
||||
if(int_id < 0 || int_id > intListId) {
|
||||
fprintf(stderr, "ERROR : attempting to update a non-existing interface (%d)\n", int_id);
|
||||
} else if(
|
||||
glfwGetKey(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS
|
||||
) {
|
||||
double mx = 0.0;
|
||||
double my = 0.0;
|
||||
glfwGetCursorPos(window, &mx, &my);
|
||||
mx = 2.0*mx/1500.0 -1.0;
|
||||
my = 2.0*my/1000.0 -1.0;
|
||||
//printf("EE %lf, %lf\n", mx, my);
|
||||
//fflush(stdout);
|
||||
for(int k = 0; k < interfaceList[int_id].nButtons; k++) {
|
||||
if(is_button_pressed(interfaceList[int_id].buttons[k], (float)mx, (float)my)) {
|
||||
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) {
|
||||
case WARP:
|
||||
*current_interface = *buttonList[interfaceList[int_id].buttons[k]].metadata;
|
||||
break;
|
||||
|
||||
case NONE:
|
||||
break;
|
||||
|
||||
case SET_VAR:
|
||||
/* TODO */
|
||||
break;
|
||||
|
||||
case EXIT:
|
||||
*current_interface = -1;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool isInMenu(GLFWwindow *win, unsigned int fragShader) {
|
||||
if(*current_interface == -1) {
|
||||
return false;
|
||||
} else {
|
||||
//printf("displaying %d\n", *current_interface);
|
||||
display_interface(*current_interface, fragShader);
|
||||
menu_actions(win, *current_interface);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool isMenuOpen() {
|
||||
return (*current_interface != -1);
|
||||
}
|
||||
|
||||
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;
|
||||
current_interface = malloc(sizeof(int));
|
||||
*current_interface = -1;
|
||||
}
|
||||
|
||||
// returns the ID of the new button (-1 if error)
|
||||
// actn can be within {NONE, WARP, SET_VAR, EXIT}
|
||||
// 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) {
|
||||
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;
|
||||
|
||||
buttonList[bListId].onClick = onClick;
|
||||
buttonList[bListId].arg = arg;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
*/
|
||||
// build and link everything here
|
||||
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_i = interface_create("Welcome", 128, 128, 128, -0.4f, 0.7f, 0.8f, 0.25f);
|
||||
//printf("%d %d\n", welcome_start_i, welcome_start_b);
|
||||
interface_link_button(welcome_start_i, welcome_start_b);
|
||||
interface_set(welcome_start_i);
|
||||
//printf("------------- %d\n", *current_interface);
|
||||
}
|
||||
|
||||
void free_interf() {
|
||||
free(buttonList);
|
||||
free(interfaceList);
|
||||
}
|
||||
free(current_interface);
|
||||
}
|
||||
|
||||
/*
|
||||
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;
|
||||
*/
|
34
src/menus.h
34
src/menus.h
|
@ -1,31 +1,37 @@
|
|||
#ifndef MENUS_H
|
||||
#define MENUS_H
|
||||
|
||||
typedef enum button_action {WARP, SET_VAR} button_action ;
|
||||
typedef enum button_action {NONE, 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)
|
||||
int* metadata;
|
||||
|
||||
// is called upon clicking button
|
||||
// this function is executed before metadata is used
|
||||
void (*onClick)(void* args);
|
||||
void* arg;
|
||||
} onoff_button;
|
||||
|
||||
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 +44,18 @@ 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(GLFWwindow *win, unsigned int fragShader);
|
||||
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 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 build_all_menus();
|
||||
|
||||
void free_interf();
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue