changed type in room + fixed some segFaults

This commit is contained in:
Alexandre 2025-02-06 14:36:33 +01:00
parent dec1124c43
commit 381348a420
19 changed files with 256 additions and 190 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.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -357,21 +357,25 @@ void project_to_cube(double x0, double y0, double z0, double* rx, double* ry, do
void remove_entity(entity** arr, int* memlen, int* len, int index) {
if(*len > 0) {
(*arr)[index] = (*arr)[*len -1];
entity* temp = arr[index];
arr[index] = arr[*len -1];
arr[*len -1] = temp;
*len -= 1;
}
}
void add_entity(entity** arr, int* memlen, int* len, entity ent) {
void add_entity(entity** arr, int* memlen, int* len, entity* ent) {
if(*memlen == *len) {
entity* newarr = malloc(sizeof(entity)*2*(*memlen));
entity** newarr = malloc(sizeof(entity*)*2*(*memlen));
for(int k = 0; k < *len; k++) {
newarr[k] = (*arr)[k] ;
newarr[k] = malloc(sizeof(entity));
newarr[k] = arr[k] ;
free(arr[k]);
}
free(*arr);
*arr = newarr ;
arr = newarr ;
*memlen *= 2;
}
(*arr)[*len] = ent ;
arr[*len] = ent ;
*len += 1;
}

View File

@ -56,7 +56,7 @@ double distance_pt_cube_axis_max(double coord, double begin, double end);
double distance_pt_cube_aligned_3d_max(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd);
void remove_entity(entity** arr, int* memlen, int* len, int index);
void add_entity(entity** arr, int* memlen, int* len, entity ent);
void add_entity(entity** arr, int* memlen, int* len, entity* ent);
void project_to_camera(double x0, double y0, double z0, double* rx, double* ry, double* rz);
void project_to_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0* c);

View File

@ -27,6 +27,13 @@ int MAX_SIZE = 8192 ;
int* drawOrder ;
GLint loc_scale;
GLint loc_model;
GLint loc_view;
GLint loc_proj;
GLint loc_frag;
void init_draworder() {
drawOrder = malloc(sizeof(int)*6) ;
}
@ -128,23 +135,24 @@ void gl_renderCube(unsigned int shaderProgram, unsigned int fragmentShader, unsi
glm_rotate(model, (float)(-c->hz_angle), (vec3){0.0f, 1.0f, 0.0f});
glm_rotate(model, (float)(c->vt_angle), (vec3){1.0f, 0.0f, 0.0f});
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "scale"), 1, GL_FALSE, (float*)scale);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "model"), 1, GL_FALSE, (float*)model);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "view"), 1, GL_FALSE, (float*)view);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, (float*)projection);
glUniformMatrix4fv(loc_scale, 1, GL_FALSE, (float*)scale);
glUniformMatrix4fv(loc_model, 1, GL_FALSE, (float*)model);
glUniformMatrix4fv(loc_view, 1, GL_FALSE, (float*)view);
glUniformMatrix4fv(loc_proj, 1, GL_FALSE, (float*)projection);
glUniform4f(glGetUniformLocation(fragmentShader, "u_color"), c->red/255.0f, c->green/255.0f, c->blue/255.0f, 1.0f);
glUniform4f(loc_frag, c->red/255.0f, c->green/255.0f, c->blue/255.0f, 1.0f);
glDrawArrays(GL_TRIANGLES, 0, 36);
triCount += 12;
}
double px, py, pz, pz2;
double px, py, pz;
double px2, py2, pz2;
bool is_visible(cube_0* cb, double offx, double offy, double offz) {
for(int d = 0; d < 8; d++) {
project_to_cube(cb->x+cb->w*(d%2==0)+offx, cb->y+cb->h*((d/2)%2==0)+offy, cb->z+cb->d*((d/4)%2==0)+offz, &px, &py, &pz, cb);
project_to_camera(px, py, pz, NULL, NULL, &pz2);
if(pz2 > near) {
project_to_camera(px, py, pz, &px2, &py2, &pz2);
if(pz2 >= near) {
return true;
}
}
@ -164,8 +172,8 @@ void gl_renderAll(unsigned int shaderProgram, unsigned int fragmentShader, unsig
}
}
for(int k = 0; k < rtd->ent_len; k++) {
if(is_visible(rtd->ents[k].pos, offx, offy, offz)) {
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->ents[k].pos, offx, offy, offz);
if(is_visible(rtd->ents[k]->pos, offx, offy, offz)) {
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->ents[k]->pos, offx, offy, offz);
}
}
}
@ -193,6 +201,13 @@ void gl_initRender(unsigned int shaderProgram, unsigned int fragmentShader, unsi
glm_lookat((vec3){(float)camx, (float)camy, (float)camz}, direction, (vec3){0.0f, 1.0f, 0.0f}, view);
glm_perspective(glm_rad((float)fov), 1500.0f / 1000.0f, 0.1f, 100.0f, projection);
loc_scale = glGetUniformLocation(shaderProgram, "scale");
loc_model = glGetUniformLocation(shaderProgram, "model");
loc_view = glGetUniformLocation(shaderProgram, "view");
loc_proj = glGetUniformLocation(shaderProgram, "projection");
loc_frag = glGetUniformLocation(fragmentShader, "u_color");
}
void gl_drawData(unsigned int shaderProg) {

View File

@ -47,9 +47,9 @@ void update_entity(entity* ent, float dtime) {
void update_entities(float dtime) {
for(int k = 0; k < current_room->ent_len; k++) {
if(current_room->ents[k].updatePos != NULL) {
if(current_room->ents[k]->updatePos != NULL) {
//printf("e\n");
update_entity(&(current_room->ents[k]), dtime);
update_entity(current_room->ents[k], dtime);
}
}
}
@ -78,6 +78,10 @@ void speen3(double x, double y, double z, double w, double h, double d, double h
ret->vt_angle += ((double)dtime)*22.5;
}
void moving_x(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret) {
}
void detectHit(float dtime, int* hp, int* dmg, cube_0* ret) {
if(ret->red == 193) {
ret->red = 0;

View File

@ -97,21 +97,35 @@ void copy_room(room* src, room* dest, int chx, int chy) {
dest->tps[k]->dest_y = src->tps[k]->dest_y;
dest->tps[k]->dest_z = src->tps[k]->dest_z;
}
dest->ents = malloc(sizeof(entity)*src->ent_memlen);
dest->ents = malloc(sizeof(entity*)*src->ent_memlen);
dest->ent_memlen = src->ent_memlen;
dest->ent_len = src->ent_len;
for(int k = 0; k < src->ent_len; k++) {
dest->ents[k].damage = src->ents[k].damage ;
dest->ents[k].hitpoints = malloc(sizeof(int));
*(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,
(*(src->ents[k].pos)).w, (*(src->ents[k].pos)).h, (*(src->ents[k].pos)).d,
(*(src->ents[k].pos)).hz_angle, (*(src->ents[k].pos)).vt_angle, (*(src->ents[k].pos)).red, (*(src->ents[k].pos)).green, (*(src->ents[k].pos)).blue
dest->ents[k] = malloc(sizeof(entity));
dest->ents[k]->damage = src->ents[k]->damage;
dest->ents[k]->hitpoints = malloc(sizeof(int));
dest->ents[k]->meta1 = src->ents[k]->meta1;
dest->ents[k]->meta2 = src->ents[k]->meta2;
dest->ents[k]->meta3 = src->ents[k]->meta3;
dest->ents[k]->meta4 = src->ents[k]->meta4;
dest->ents[k]->meta5 = src->ents[k]->meta5;
dest->ents[k]->meta6 = src->ents[k]->meta6;
dest->ents[k]->meta7 = src->ents[k]->meta7;
dest->ents[k]->meta8 = src->ents[k]->meta8;
*(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,
(*(src->ents[k]->pos)).w, (*(src->ents[k]->pos)).h, (*(src->ents[k]->pos)).d,
(*(src->ents[k]->pos)).hz_angle, (*(src->ents[k]->pos)).vt_angle, (*(src->ents[k]->pos)).red, (*(src->ents[k]->pos)).green, (*(src->ents[k]->pos)).blue
);
dest->ents[k].updatePos = src->ents[k].updatePos;
dest->ents[k].onHit = src->ents[k].onHit;
dest->ents[k].onDeath = src->ents[k].onDeath;
dest->ents[k]->updatePos = src->ents[k]->updatePos;
dest->ents[k]->onHit = src->ents[k]->onHit;
dest->ents[k]->onDeath = src->ents[k]->onDeath;
}
for(int k = src->ent_len; k < src->ent_memlen; k++) {
dest->ents[k] = malloc(sizeof(entity));
dest->ents[k]->hitpoints = malloc(sizeof(int));
dest->ents[k]->pos = malloc(sizeof(cube_0));
}
dest->tps_size = src->tps_size;
}
@ -140,16 +154,20 @@ void build_starting_chunk(int chx, int chy) {
new->tps[2] = create_teleporter(5.0, 1.0, 2.0, 1.0, 2.0 + (double)(rand()%2), 1.0, 0.0, 0.0, 0, 255, 0 , chx+1, chy , 2.5, 2.5, 2.5);
new->tps[3] = create_teleporter(2.0, 1.0, 5.0, 1.0, 2.0 + (double)(rand()%2), 1.0, 0.0, 0.0, 0, 0, 255 , chx , chy+1, 2.5, 2.5, 2.5);
new->ents = malloc(sizeof(entity)*128);
new->ents = malloc(sizeof(entity*)*32);
for(int k = 0; k < 32; k++) {
new->ents[k] = malloc(sizeof(entity));
new->ents[k]->hitpoints = malloc(sizeof(int));
new->ents[k]->pos = malloc(sizeof(cube_0));
};
new->ent_len = 1;
new->ent_memlen = 128 ;
new->ents[0].pos = create_cube_0(-0.25, 8.25, -0.25, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 128, 0);
new->ents[0].damage = 0 ;
new->ents[0].hitpoints = malloc(sizeof(int));
*(new->ents[0].hitpoints) = 5 ;
new->ents[0].onDeath = NULL ;
new->ents[0].onHit = *detectHit ;
new->ents[0].updatePos = *speen2 ;
new->ent_memlen = 32;
fill_cube_0(new->ents[0]->pos, -0.25, 8.25, -0.25, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 128, 0);
new->ents[0]->damage = 0;
*(new->ents[0]->hitpoints) = 5;
new->ents[0]->onDeath = NULL;
new->ents[0]->onHit = *detectHit;
new->ents[0]->updatePos = *speen2;
hashtbl_add(visited, chx, chy, new);
}
@ -255,10 +273,7 @@ void parse_one_room(int id, char* filename) {
get_number_blocks(&ncubes, &ntps, &nent, ptr2);
printf("(%d, %d, %d)\n", ncubes, ntps, nent);
int nmemlen = 128 ;
while(nmemlen < nent) {
nmemlen *= 2.;
}
int nmemlen = maxd(nent, 64);
pool[id].area->map = malloc(sizeof(cube_0*)*ncubes);
pool[id].area->map_size = ncubes;
@ -267,7 +282,12 @@ void parse_one_room(int id, char* filename) {
pool[id].area->tps[k] = malloc(sizeof(teleporter));
}
pool[id].area->tps_size = ntps;
pool[id].area->ents = malloc(sizeof(entity)*nmemlen);
pool[id].area->ents = malloc(sizeof(entity*)*nmemlen);
for(int k = 0; k < nmemlen; k++) {
pool[id].area->ents[k] = malloc(sizeof(entity));
pool[id].area->ents[k]->hitpoints = malloc(sizeof(int));
pool[id].area->ents[k]->pos = malloc(sizeof(cube_0));
}
pool[id].area->ent_len = nent;
pool[id].area->ent_memlen = nmemlen;
@ -336,17 +356,23 @@ void parse_one_room(int id, char* filename) {
if(entry == NULL) {
entry = get_entry(0);
}
pool[id].area->ents[k].pos = create_cube_0(cx, cy, cz, cw, ch, cd, chz, cvt, red, green, blue);
pool[id].area->ents[k].damage = dmg ;
pool[id].area->ents[k].hitpoints = malloc(sizeof(int));
*(pool[id].area->ents[k].hitpoints) = hp ;
pool[id].area->ents[k].updatePos = entry->updatePos ;
pool[id].area->ents[k].onHit = entry->onHit ;
pool[id].area->ents[k].onDeath = entry->onDeath ;
//pool[id].area->ents[k].updatePos = &speen2 ;
//pool[id].area->ents[k].onHit = &detectHit ;
//pool[id].area->ents[k].onDeath = NULL ;
fill_cube_0(pool[id].area->ents[k]->pos, cx, cy, cz, cw, ch, cd, chz, cvt, red, green, blue);
pool[id].area->ents[k]->damage = dmg;
*(pool[id].area->ents[k]->hitpoints) = hp;
pool[id].area->ents[k]->updatePos = entry->updatePos;
pool[id].area->ents[k]->onHit = entry->onHit ;
pool[id].area->ents[k]->onDeath = entry->onDeath ;
pool[id].area->ents[k]->meta1 = 0;
pool[id].area->ents[k]->meta2 = 0;
pool[id].area->ents[k]->meta3 = 0;
pool[id].area->ents[k]->meta4 = 0;
pool[id].area->ents[k]->meta5 = 0.0;
pool[id].area->ents[k]->meta6 = 0.0;
pool[id].area->ents[k]->meta7 = 0.0;
pool[id].area->ents[k]->meta8 = 0.0;
//pool[id].area->ents[k]->updatePos = &speen2;
//pool[id].area->ents[k]->onHit = &detectHit ;
//pool[id].area->ents[k]->onDeath = NULL ;
//printf("\n");
}
@ -423,14 +449,14 @@ void parse_rooms(int n_rooms) {
free(name);
}
int divider = 8;
// has to be a multiple of both room_width and room_depth
int divider = 4;
void generate_terrain(room* r) {
int rsize = 4*(room_width/divider)*(room_width/divider)*(room_depth/divider)*(room_depth/divider); // floor size (with 1x1 cubes)
cube** newMap = malloc(sizeof(cube_0*)*(rsize+r->map_size));
cube_0** newMap = malloc(sizeof(cube_0*)*(rsize+r->map_size));
for(int k = 0; k < rsize+r->map_size; k++) {
newMap[k] = malloc(sizeof(cube_0));
}
for(int k = 0; k < r->map_size; k++) {
@ -443,7 +469,7 @@ void generate_terrain(room* r) {
int i = r->map_size;
for(int l = -room_width; l < room_width; l+=divider) {
for(int d = -room_depth; d < room_depth; d+=divider) {
newMap[i] = create_cube_0((double)l, -20.0, (double)d, (double)divider, 1.0, (double)divider, 0.0, 0.0,32, 128, 32);
fill_cube_0(newMap[i], (double)l, -20.0, (double)d, (double)divider, 1.0, (double)divider, 0.0, 0.0,32, 128, 32);
i+=1;
}
}
@ -466,11 +492,12 @@ void generate_nearby_chunks(int render_dist) {
printf("chose %d\n", k);
room* new = malloc(sizeof(room));
copy_room(pool[k].area, new, player_chx + w, player_chy + h);
generate_terrain(new);
//generate_terrain(new);
hashtbl_add(visited, player_chx + w, player_chy + h, new);
k = pool_size+1;
}
}
printf("Done\n");
}
}
}
@ -485,9 +512,10 @@ void free_pool() {
free(pool[k0].area->tps[k]->hitbox);
free(pool[k0].area->tps[k]);
}
for(int k = 0; k < pool[k0].area->ent_len; k++) {
free(pool[k0].area->ents[k].hitpoints);
free(pool[k0].area->ents[k].pos);
for(int k = 0; k < pool[k0].area->ent_memlen; k++) {
free(pool[k0].area->ents[k]->hitpoints);
free(pool[k0].area->ents[k]->pos);
free(pool[k0].area->ents[k]);
}
free(pool[k0].area->ents);
free(pool[k0].area->tps);

View File

@ -65,9 +65,10 @@ void free_all_cubes(room* r) {
free(r->tps[k]->hitbox);
free(r->tps[k]);
}
for(int k = 0; k < r->ent_len; k++) {
free(r->ents[k].hitpoints);
free(r->ents[k].pos);
for(int k = 0; k < r->ent_memlen; k++) {
free(r->ents[k]->hitpoints);
free(r->ents[k]->pos);
free(r->ents[k]);
}
free(r->ents);
free(r->tps);

View File

@ -88,10 +88,10 @@ void processInput(GLFWwindow *window, float dtime) {
}
if(glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) {
rot_vt -= sensitivity;
rot_vt = maxd(-3.14159/2.0, rot_vt-sensitivity);
}
if(glfwGetKey(window, GLFW_KEY_M) == GLFW_PRESS) {
rot_vt += sensitivity;
rot_vt = mind(3.14159/2.0, rot_vt+sensitivity);
}
}
@ -290,10 +290,12 @@ int main_alt() {
int fps = 60 ;
int interval = 1000000/fps ;
float delta;
clock_t finish = clock();
clock_t origin = clock();
while(!glfwWindowShouldClose(window) && player_hp >= 0) {
while(!glfwWindowShouldClose(window) && player_hp > 0) {
// input
// -----
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
@ -301,7 +303,9 @@ int main_alt() {
triCount = 0;
origin = clock();
fflush(stdout);
generate_nearby_chunks(1);
fflush(stdout);
// draw the map
glUseProgram(shaderProgram);
@ -316,20 +320,21 @@ int main_alt() {
glBindVertexArray(RVAO);
finish = clock();
gl_drawInteger(shaderProgramR, (int)(1.0f/(((float)finish - (float)origin)/CLOCKS_PER_SEC)), 0.9f, -0.85f, 0.05, 32, 255, 32, 0.005, -1);
delta = ((float)finish - (float)origin)/CLOCKS_PER_SEC;
gl_drawInteger(shaderProgramR, (int)(1.0f/(delta)), 0.9f, -0.85f, 0.05, 32, 255, 32, 0.005, -1);
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, ((float)finish - (float)origin)/CLOCKS_PER_SEC);
processInput(window, delta);
teleport_on_edge();
update_entities(((float)finish - (float)origin)/CLOCKS_PER_SEC);
updateProj(((float)finish - (float)origin)/CLOCKS_PER_SEC);
//printf("%f\n", 1.0f/(((float)finish - (float)origin)/CLOCKS_PER_SEC));
update_entities(delta);
updateProj(delta);
//printf("%f\n", 1.0f/(delta));
//printf("%lf, %lf, %lf\n", camx, camy, camz);
usleep(interval);
usleep(max(0, interval-(int)(1000000*delta)));
glfwSwapBuffers(window);
glfwPollEvents();

View File

@ -92,15 +92,15 @@ bool is_colliding(float dtime) {
}
}
for(int k = 0; k < current_room->ent_len; k++) {
double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->ents[k].pos);
double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->ents[k]->pos);
if(dist <= min_dist) {
if(current_room->ents[k].onHit != NULL) {
(*current_room->ents[k].onHit)(dtime, current_room->ents[k].hitpoints, &current_room->ents[k].damage, &(*(current_room->ents[k].pos)));
if(*(current_room->ents[k].hitpoints) <= 0) {
if(current_room->ents[k].onDeath != NULL) {
(*current_room->ents[k].onDeath)(dtime);
if(current_room->ents[k]->onHit != NULL) {
(*current_room->ents[k]->onHit)(dtime, current_room->ents[k]->hitpoints, &current_room->ents[k]->damage, &(*(current_room->ents[k]->pos)));
if(*(current_room->ents[k]->hitpoints) <= 0) {
if(current_room->ents[k]->onDeath != NULL) {
(*current_room->ents[k]->onDeath)(dtime);
}
remove_entity(&current_room->ents, &current_room->ent_memlen, &current_room->ent_len, k);
remove_entity(current_room->ents, &current_room->ent_memlen, &current_room->ent_len, k);
}
}
return true;

View File

@ -41,6 +41,15 @@ typedef struct entity {
// triggers when <hitpoints> goes negative (death)
void (*onDeath)(float dtime);
int meta1;
int meta2;
int meta3;
int meta4;
double meta5;
double meta6;
double meta7;
double meta8;
int damage;
int* hitpoints;
} entity;
@ -53,7 +62,7 @@ struct room {
int map_size;
teleporter** tps;
int tps_size;
entity* ents;
entity** ents;
int ent_len;
int ent_memlen;
};