fixed reset doing bad things to memory + added warp text entity

This commit is contained in:
Alexandre 2025-02-19 12:07:30 +01:00
parent bd87416496
commit a273c643c6
22 changed files with 194 additions and 68 deletions

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -195,7 +195,19 @@ void explodeOnHit(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret) {
}
// metai1 = id of the interface
// metach1 = text (unused outside of initialization)
// metach1 = text (stored here to free() easily)
void pop_text(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret) {
interface_set(ent->metai1);
}
// metai1 = id of the interface
// metai2 = room count
// metach1 = dest folder
// metach2 = text (stored here to free() easily)
void pop_and_tp(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret) {
interface_set(ent->metai1);
newRoomName = ent->metach1;
newRoomCount = ent->metai2;
//printf("%s %d\n", newRoomName, newRoomCount);
switchRoom = true;
}

View File

@ -22,5 +22,6 @@ void explodeOnHit(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret);
void translatePlayer(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret);
void translatePlayerLine(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret);
void pop_text(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret);
void pop_and_tp(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret);
#endif

View File

@ -28,6 +28,9 @@ int total_weight;
int coins;
int fct_entry_size;
static char** to_free;
static int to_length;
fct_entry* hashtbl_entities;
void init_ent_generator(int n) {
@ -80,11 +83,10 @@ void init_ent_generator(int n) {
hashtbl_entities[6].onHit = &pop_text;
hashtbl_entities[6].onDeath = NULL;
// NOT IMPLEMENTED YET //
hashtbl_entities[7].id = 7;
hashtbl_entities[7].name = "WarpBox";
hashtbl_entities[7].updatePos = NULL;
hashtbl_entities[7].onHit = &translatePlayerLine;
hashtbl_entities[7].onHit = &pop_and_tp;
hashtbl_entities[7].onDeath = NULL;
}
@ -148,6 +150,10 @@ void copy_room(room* src, room* dest, int chx, int chy) {
dest->ents[k]->metad9 = src->ents[k]->metad9;
dest->ents[k]->metach1 = src->ents[k]->metach1;
dest->ents[k]->metach2 = src->ents[k]->metach2;
//dest->ents[k]->metach1 = malloc(sizeof(char)*52);
//dest->ents[k]->metach2 = malloc(sizeof(char)*52);
//strcpy(dest->ents[k]->metach1, src->ents[k]->metach1);
//strcpy(dest->ents[k]->metach2, src->ents[k]->metach2);
*(dest->ents[k]->hitpoints) = *(src->ents[k]->hitpoints);
dest->ents[k]->pos = create_cube_0(
(*(src->ents[k]->pos)).x, (*(src->ents[k]->pos)).y, (*(src->ents[k]->pos)).z,
@ -209,10 +215,17 @@ void build_starting_chunk(int chx, int chy) {
}
void init_hashtbl() {
printf("+1\n"); fflush(stdout);
visited = hashtbl_generate(1789);
printf("+1\n"); fflush(stdout);
build_starting_chunk(player_chx, player_chy);
printf("+1\n"); fflush(stdout);
current_room = hashtbl_find_opt(visited, player_chx, player_chy);
printf("+1\n"); fflush(stdout);
total_weight = 0;
printf("+1\n"); fflush(stdout);
to_free = malloc(sizeof(char*)*100); // ------------------------------------------------------------------------------------------------- 100 max //
to_length = 0;
}
void get_number_blocks(int* ret_cubes, int* ret_tps, int* ret_ent, FILE* ptr) {
@ -275,9 +288,12 @@ int read_int(FILE* ptr, bool print) {
}
char* read_string(FILE* ptr) {
char* res0 = malloc(sizeof(char)*51);
char* res0 = malloc(sizeof(char)*52);
char c = fgetc(ptr);
int i = 0;
while(c != EOF && c == ' ') { // ignore initial spaces
c = fgetc(ptr);
}
while(c != EOF && c != ',') {
res0[i] = c;
i += 1;
@ -423,8 +439,6 @@ void parse_one_room(int id, char* filename) {
pool[id].area->ents[k]->metad7 = entry->metad7;
pool[id].area->ents[k]->metad8 = entry->metad8;
pool[id].area->ents[k]->metad9 = entry->metad9;
pool[id].area->ents[k]->metach1 = entry->metach1;
pool[id].area->ents[k]->metach2 = entry->metach2;
if(entry->id == 4) {
// sine platform
double ccw = read_float(ptr);
@ -465,12 +479,33 @@ void parse_one_room(int id, char* filename) {
} else if(entry->id == 6) {
// text box
char* msg = read_string(ptr);
to_free[to_length] = msg;
to_length += 1;
pool[id].area->ents[k]->metach1 = msg;
int ired = read_int(ptr, true);
int igreen = read_int(ptr, true);
int iblue = read_int(ptr, true);
pool[id].area->ents[k]->metai1 = build_text_box(msg, ired, igreen, iblue);
pool[id].area->ents[k]->metai2 = (-727); // random value to recognize
pool[id].area->ents[k]->metai3 = (-727); // random value to recognize
} else if(entry->id == 7) {
// warp text box
char* dest = read_string(ptr);
to_free[to_length] = dest;
to_length += 1;
int count = read_int(ptr, true);
char* msg = read_string(ptr);
to_free[to_length] = msg;
to_length += 1;
pool[id].area->ents[k]->metach1 = dest;
pool[id].area->ents[k]->metach2 = msg;
int ired = read_int(ptr, true);
int igreen = read_int(ptr, true);
int iblue = read_int(ptr, true);
pool[id].area->ents[k]->metai1 = build_text_box(msg, ired, igreen, iblue);
pool[id].area->ents[k]->metai2 = count;
pool[id].area->ents[k]->metai3 = (-72727); // random value to recognize
} else {
pool[id].area->ents[k]->metai3 = 0;
}
}
@ -513,32 +548,13 @@ char* get_name_and_i(char* folder, int nrooms, int* reti) {
}
void parse_rooms(int n_rooms, char* folder) {
/*char* name = malloc(sizeof(char)*19); // 1000 rooms max (not anymore xD)
name[0] = 't';
name[1] = 'e';
name[2] = 'm';
name[3] = 'p';
name[4] = 'l';
name[5] = 'a';
name[6] = 't';
name[7] = 'e';
name[8] = 's';
name[9] = '/';
name[10] = 'r';
name[11] = 'o';
name[12] = 'o';
name[13] = 'm';
name[14] = '_';
name[15] = '0';
name[16] = '\0';
name[17] = '\0';
name[18] = '\0';*/
int id = 0;
char* name = get_name_and_i(folder, n_rooms, &id);
pool = malloc(sizeof(entry)*n_rooms);
pool_size = n_rooms;
printf("<%s> with %d rooms\n", folder, n_rooms);
printf("Parsing...\n");
for(int k = 0; k < n_rooms; k++) {
@ -558,9 +574,8 @@ void parse_rooms(int n_rooms, char* folder) {
}
printf("\nDone.\n");
printf("Total sum : %d\n", total_weight);
free(name);
printf("Total sum : %d\n", total_weight);
}
// has to be a multiple of both room_width and room_depth
@ -622,6 +637,7 @@ void generate_nearby_chunks(int render_dist) {
void free_pool() {
for(int k0 = 0; k0 < pool_size; k0++) {
printf("%d/%d\n", 1+k0, pool_size);
for(int k = 0; k < pool[k0].area->map_size; k++) {
free(pool[k0].area->map[k]);
}
@ -630,8 +646,12 @@ void free_pool() {
free(pool[k0].area->tps[k]);
}
for(int k = 0; k < pool[k0].area->ent_memlen; k++) {
if(pool[k0].area->ents[k]->metai2 == -727) {
free(pool[k0].area->ents[k]->metach1);
if(pool[k0].area->ents[k]->metai3 == -727) { // 6 and 7 //
//free(pool[k0].area->ents[k]->metach1);
}
if(pool[k0].area->ents[k]->metai3 == -72727) { // 7 //
//free(pool[k0].area->ents[k]->metach1);
//free(pool[k0].area->ents[k]->metach2);
}
free(pool[k0].area->ents[k]->hitpoints);
free(pool[k0].area->ents[k]->pos);
@ -643,4 +663,16 @@ void free_pool() {
free(pool[k0].area);
}
free(pool);
}
void free_to_free() {
for(int k = 0; k < to_length; k++) {
free(to_free[k]);
}
free(to_free);
}
void free_ent_generator() {
free(hashtbl_entities);
}

View File

@ -55,5 +55,7 @@ void parse_rooms(int n_rooms, char* folder) ;
void generate_nearby_chunks(int render_dist) ;
void free_pool();
void free_ent_generator();
void free_to_free();
#endif

View File

@ -66,6 +66,8 @@ void free_all_cubes(room* r) {
free(r->tps[k]);
}
for(int k = 0; k < r->ent_memlen; k++) {
//free(r->ents[k]->metach1);
//free(r->ents[k]->metach2);
free(r->ents[k]->hitpoints);
free(r->ents[k]->pos);
free(r->ents[k]);

View File

@ -25,23 +25,74 @@ int triCount;
unsigned int fffff;
int gamemode;
char* newRoomName;
int newRoomCount;
bool switchRoom;
double jPress = false;
double gPress = false;
double rPress = false;
void reset_everything(GLFWwindow *window, int count, char* folder) {
hashtbl_free(visited);
/*hashtbl_free(visited);
free_proj();
free_interf();
free_pool();
//init_csts();
init_csts();
init_hashtbl();
init_interf(window);
//build_all_menus();
build_all_menus();
init_ent_generator(30);
init_proj();
parse_rooms(7, "templates/");
parse_rooms(count, folder);
interface_set(-1);*/
camx = /*2*room_width*player_chx+*/2.0;
camy = 5.0;
camz = /*2*room_depth*player_chy+*/2.0;
player_chx = 0;
player_chy = 0;
camvx = 0.0;
camvy = 0.0;
camvz = 0.0;
printf("-------------------------------- resetting hashtbl... --------------------------------\n");
fflush(stdout);
hashtbl_free(visited);
printf("-------------------------------- done 1 --------------------------------\n");
fflush(stdout);
printf("-------------------------------- resetting pool... --------------------------------\n");
fflush(stdout);
free_pool();
free_to_free();
printf("-------------------------------- done 2 --------------------------------\n");
fflush(stdout);
printf("-------------------------------- initializing hashtbl... --------------------------------\n");
fflush(stdout);
init_hashtbl();
printf("-------------------------------- done 3 --------------------------------\n");
fflush(stdout);
printf("-------------------------------- initializing rooms... --------------------------------\n");
fflush(stdout);
parse_rooms(count, folder);
printf("-------------------------------- Done 4 --------------------------------\n");
fflush(stdout);
interface_set(-1);
generate_nearby_chunks(1);
}
void processInput(GLFWwindow *window, float dtime) {
@ -177,9 +228,16 @@ void processInput(GLFWwindow *window, float dtime) {
rPress = true;
reset_everything(window, 7, "templates/");
}
}/*else {
} else {
rPress = false;
}*/
}
if(!isMenuOpen() && switchRoom) {
//printf("-----\n");
switchRoom = false;
reset_everything(window, newRoomCount, newRoomName);
free(newRoomName);
}
}
const char *vertexShaderSource = "#version 330 core\n"
@ -423,7 +481,7 @@ int main_alt() {
}
teleport_on_edge();
update_entities(delta);
updateProj(delta);
updateProj(delta);
}
usleep(max(0, interval-(int)(1000000*delta)));
@ -440,10 +498,18 @@ int main_alt() {
incr = 0.0f;
}
printf("| 1/5 |\n"); fflush(stdout);
hashtbl_free(visited);
printf("| 2/5 |\n"); fflush(stdout);
free_proj();
printf("| 3/5 |\n"); fflush(stdout);
free_interf();
printf("| 4/5 |\n"); fflush(stdout);
free_pool();
printf("| 5/5 |\n"); fflush(stdout);
free_to_free();
free_ent_generator();
printf("Done\n"); fflush(stdout);
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
@ -462,5 +528,8 @@ int main(int argc, char** argv) {
triCount = 0;
sim_time = 0.0;
gamemode = 0;
newRoomCount = 7;
switchRoom = false;
newRoomName = "templates/";
return main_alt();
}

View File

@ -631,6 +631,23 @@ void init_interf(GLFWwindow *window) {
glfwSetMouseButtonCallback(window, menu_actions);
}
void reset_interf() {
bListId = 0;
intListId = 0;
*current_interface = -1;
noMousePoll = false;
}
void free_interf() {
free(buttonList);
for(int k = 0; k < intListId; k++) {
free(interfaceList[k].buttons);
}
free(interfaceList);
free(current_interface);
free(numbers);
}
// 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
@ -769,11 +786,4 @@ int build_text_box(char* msg, int red, int green, int blue) {
interface_link_button(intf, button_ok);
return intf;
}
void free_interf() {
free(buttonList);
free(interfaceList);
free(current_interface);
free(numbers);
}

View File

@ -50,6 +50,8 @@ 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(GLFWwindow *window);
void reset_interf();
void free_interf();
bool isInMenu(GLFWwindow *win, unsigned int fragShader);
bool isMenuOpen();
@ -63,6 +65,4 @@ void interface_set(int interface_id);
void build_all_menus();
int build_text_box(char* msg, int red, int green, int blue);
void free_interf();
#endif

View File

@ -166,4 +166,8 @@ extern int gamemode;
extern float incr;
extern char* newRoomName;
extern int newRoomCount;
extern bool switchRoom;
#endif

View File

@ -34,10 +34,9 @@ else if entityType = 6 (text box)
[.. text] with
text = {char*}
// NOT IMPLEMENTED YET //
else if entityType = 7 (warp text box)
[.. text, r, g, b] with
text = {char*} (length <= 50)
[.. dest_folder, room_count, text, r, g, b] with
{dest_folder,text} = {char*} (length <= 50)
{r,g,b} = int[0-256]
else

View File

@ -20,6 +20,7 @@ Entities :
[0.0, 8.0, 1.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0]
[0.0, 9.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0]
[0.0, 10.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0]
[-1.0, -5.0, -1.0, 2.0, 2.0, 2.0, 1.0, 0.0, 16, 16, 16, 1, 0, 7, templates/, 7, level passed, 192, 192, 192]
Weight :
50
@ -43,10 +44,9 @@ else if entityType = 6 (text box)
[.. text] with
text = {char*}
// NOT IMPLEMENTED YET //
else if entityType = 7 (warp text box)
[.. text, r, g, b] with
text = {char*} (length <= 50)
[.. dest_folder, room_count, text, r, g, b] with
{dest_folder,text} = {char*} (length <= 50)
{r,g,b} = int[0-256]
else

View File

@ -41,10 +41,9 @@ else if entityType = 6 (text box)
[.. text] with
text = {char*}
// NOT IMPLEMENTED YET //
else if entityType = 7 (warp text box)
[.. text, r, g, b] with
text = {char*} (length <= 50)
[.. dest_folder, room_count, text, r, g, b] with
{dest_folder,text} = {char*} (length <= 50)
{r,g,b} = int[0-256]
else

View File

@ -35,10 +35,9 @@ else if entityType = 6 (text box)
[.. text] with
text = {char*}
// NOT IMPLEMENTED YET //
else if entityType = 7 (warp text box)
[.. text, r, g, b] with
text = {char*} (length <= 50)
[.. dest_folder, room_count, text, r, g, b] with
{dest_folder,text} = {char*} (length <= 50)
{r,g,b} = int[0-256]
else

View File

@ -34,10 +34,9 @@ else if entityType = 6 (text box)
[.. text] with
text = {char*}
// NOT IMPLEMENTED YET //
else if entityType = 7 (warp text box)
[.. text, r, g, b] with
text = {char*} (length <= 50)
[.. dest_folder, room_count, text, r, g, b] with
{dest_folder,text} = {char*} (length <= 50)
{r,g,b} = int[0-256]
else

View File

@ -35,10 +35,9 @@ else if entityType = 6 (text box)
[.. text] with
text = {char*}
// NOT IMPLEMENTED YET //
else if entityType = 7 (warp text box)
[.. text, r, g, b] with
text = {char*} (length <= 50)
[.. dest_folder, room_count, text, r, g, b] with
{dest_folder,text} = {char*} (length <= 50)
{r,g,b} = int[0-256]
else

View File

@ -35,10 +35,9 @@ else if entityType = 6 (text box)
[.. text] with
text = {char*}
// NOT IMPLEMENTED YET //
else if entityType = 7 (warp text box)
[.. text, r, g, b] with
text = {char*} (length <= 50)
[.. dest_folder, room_count, text, r, g, b] with
{dest_folder,text} = {char*} (length <= 50)
{r,g,b} = int[0-256]
else