This commit is contained in:
Alexandre 2025-01-23 18:00:26 +01:00
parent 0c3e582cd2
commit e265292843
20 changed files with 182 additions and 50 deletions

View File

@ -7,7 +7,7 @@ all: bin/back
test: bin/back test: bin/back
bin/back bin/back
bin/back: obj/main.o obj/generation.o obj/display.o obj/entities.o obj/triangles.o obj/move.o obj/base.o obj/hash.o bin/back: obj/main.o obj/generation.o obj/display.o obj/entities.o obj/menus.o obj/triangles.o obj/move.o obj/base.o obj/hash.o
mkdir -p bin mkdir -p bin
$(CC) $(FLAGS) $^ $(LFLAGS) -o $@ $(CC) $(FLAGS) $^ $(LFLAGS) -o $@
@ -22,6 +22,7 @@ obj/entities.o: src/entities.c
obj/triangles.o: src/triangles.c obj/triangles.o: src/triangles.c
obj/move.o: src/move.c obj/move.o: src/move.c
obj/base.o: src/base.c obj/base.o: src/base.c
obj/hash.o: src/menus.c
obj/hash.o: src/hash.c obj/hash.o: src/hash.c
.PHONY: clean mrproper .PHONY: clean mrproper

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
obj/menus.o Normal file

Binary file not shown.

Binary file not shown.

View File

@ -779,7 +779,7 @@ void renderTriangleFull(SDL_Renderer* renderer, int k) {
} }
void remove_hidden(SDL_Renderer* renderer) { void remove_hidden(SDL_Renderer* renderer) {
printf("%d --> ", triangles_i); //printf("%d --> ", triangles_i);
for(int k = 0; k < triangles_i; k++) { for(int k = 0; k < triangles_i; k++) {
int halt = 1 ; int halt = 1 ;
bool fst = false ; bool fst = false ;
@ -804,8 +804,9 @@ void remove_hidden(SDL_Renderer* renderer) {
} }
} }
} }
halt = 1 ;
} }
printf("%d\n", triangles_i); //printf("%d\n", triangles_i);
} }
void visit(int k, bool vflag) { void visit(int k, bool vflag) {

View File

@ -71,8 +71,40 @@ void detectHit(float dtime, int* hp, int* dmg, cube_0* ret) {
ret->green = 192; ret->green = 192;
ret->blue = 0; ret->blue = 0;
coins += *hp; coins += *hp;
player_hp -= 10*(*hp); player_hp -= (*dmg);
fade_dmg = 255 ; if(*dmg != 0) {
fade_dmg = 255 ;
}
*hp = 0 ; *hp = 0 ;
} }
} }
void go_to_player(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret) {
double dx = (x+w/2 - camx) ;
double dy = (y+h/2 - camy) ;
double dz = (z+d/2 - camz);
dx = 10.0*dx/absf(dx);
dy = 10.0*dy/absf(dy);
dz = 10.0*dz/absf(dz);
ret->x -=12.0*dtime*dx ;
if(is_colliding_with_map(*ret) || is_colliding_with_tp(*ret)) {
ret->x +=12.0*dtime*dx ;
}
ret->y -=12.0*dtime*dy ;
if(is_colliding_with_map(*ret) || is_colliding_with_tp(*ret)) {
ret->y +=12.0*dtime*dy ;
}
ret->z -=12.0*dtime*dz ;
if(is_colliding_with_map(*ret) || is_colliding_with_tp(*ret)) {
ret->z +=12.0*dtime*dz ;
}
}
void explodeOnHit(float dtime, int* hp, int* dmg, cube_0* ret) {
player_hp -= (*dmg);
if(*dmg != 0) {
fade_dmg = 255 ;
}
*hp = 0 ;
}

View File

@ -8,9 +8,11 @@ void update_entity(entity* ent, float dtime);
void update_entities(float dtime); void update_entities(float dtime);
void speen(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret); void speen(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret);
void speen2(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret); void speen2(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret);
void go_to_player(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); void detectHit(float dtime, int* hp, int* dmg, cube_0* ret);
void explodeOnHit(float dtime, int* hp, int* dmg, cube_0* ret);
#endif #endif

View File

@ -28,6 +28,44 @@ int pool_size ;
int total_weight ; int total_weight ;
int coins ; int coins ;
int fct_entry_size ;
fct_entry* hashtbl_entities ;
void init_ent_generator(int n) {
hashtbl_entities = malloc(sizeof(fct_entry)*n);
fct_entry_size = n;
for(int k = 0; k < 10; k++) {
hashtbl_entities[k].id = (-1) ;
}
hashtbl_entities[0].id = 0;
hashtbl_entities[0].name = "Coin"; // 0 = default
hashtbl_entities[0].updatePos = &speen2 ;
hashtbl_entities[0].onHit = &detectHit ;
hashtbl_entities[0].onDeath = NULL ;
hashtbl_entities[1].id = 1;
hashtbl_entities[1].name = "ExplosiveStill";
hashtbl_entities[1].updatePos = &speen2 ;
hashtbl_entities[1].onHit = &explodeOnHit ;
hashtbl_entities[1].onDeath = NULL ;
hashtbl_entities[2].id = 2;
hashtbl_entities[2].name = "ExplosiveSeek";
hashtbl_entities[2].updatePos = &go_to_player ;
hashtbl_entities[2].onHit = &explodeOnHit ;
hashtbl_entities[2].onDeath = NULL ;
}
fct_entry* get_entry(int k0) {
for(int k = 0; k < fct_entry_size; k++) {
if(hashtbl_entities[k].id == k0) {
return &(hashtbl_entities[k]);
}
}
return NULL;
}
void copy_room(room* src, room* dest, int chx, int chy) { void copy_room(room* src, room* dest, int chx, int chy) {
// considering dest has already been malloc'd // considering dest has already been malloc'd
@ -279,14 +317,20 @@ void parse_one_room(int id, char* filename) {
int blue = read_int(ptr, true); int blue = read_int(ptr, true);
int hp = read_int(ptr, true); int hp = read_int(ptr, true);
int dmg = read_int(ptr, true); int dmg = read_int(ptr, true);
int fid = read_int(ptr, true);
fct_entry* entry = get_entry(fid);
if(entry == NULL) {
entry = get_entry(0);
}
pool[id].area->ents[k].pos = malloc(sizeof(cube_0)); pool[id].area->ents[k].pos = malloc(sizeof(cube_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].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].damage = dmg ;
pool[id].area->ents[k].hitpoints = malloc(sizeof(int)); pool[id].area->ents[k].hitpoints = malloc(sizeof(int));
*(pool[id].area->ents[k].hitpoints) = hp ; *(pool[id].area->ents[k].hitpoints) = hp ;
pool[id].area->ents[k].updatePos = *speen ;
pool[id].area->ents[k].onHit = *detectHit ; pool[id].area->ents[k].updatePos = entry->updatePos ;
pool[id].area->ents[k].onDeath = NULL ; pool[id].area->ents[k].onHit = entry->onHit ;
pool[id].area->ents[k].onDeath = entry->onDeath ;
//printf("\n"); //printf("\n");
} }

View File

@ -6,6 +6,18 @@ typedef struct entry {
int weight ; int weight ;
} entry ; } entry ;
typedef struct fct_entry {
int id ;
char* name ;
void (*updatePos)(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret) ;
// act as velocity function
void (*onHit)(float dtime, int* hp, int* dmg, cube_0* ret) ;
// triggers when object is hit
void (*onDeath)(float dtime) ;
} fct_entry ;
void init_ent_generator(int n);
void copy_room(room* src, room* dest, int chx, int chy) ; void copy_room(room* src, room* dest, int chx, int chy) ;
void build_starting_chunk(int chx, int chy) ; void build_starting_chunk(int chx, int chy) ;

View File

@ -48,13 +48,14 @@ int main(int argc, char** argv) {
SDL_SetRelativeMouseMode(true) ; SDL_SetRelativeMouseMode(true) ;
/* -------------------------------------------------------- */ /* -------------------------------------------------------- */
int fps = 60 ; int fps = 60;
int interval = 1000000/fps ; int interval = 1000000/fps;
double intervalf = 1.0/((double)(fps)) ; double intervalf = 1.0/((double)(fps));
init_csts() ; init_csts();
init_hashtbl() ; init_hashtbl();
init_draworder() ; init_draworder();
init_ent_generator(10);
trInit(); trInit();
parse_rooms(5); parse_rooms(5);
import_digits(rend) ; import_digits(rend) ;

18
src/menus.c Normal file
View File

@ -0,0 +1,18 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
#include <time.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "hash.h"
#include "structure.h"
#include "base.h"
#include "entities.h"
#include "menus.h"

6
src/menus.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef MENUS_H
#define MENUS_H
#endif

View File

@ -304,16 +304,21 @@ bool pt_equal(pt_2d p1, pt_2d p2, double epsilon) {
return (absf(p2.x - p1.x) <= epsilon && absf(p2.y - p1.y) <= epsilon && absf(p2.z - p1.z) <= epsilon) ; return (absf(p2.x - p1.x) <= epsilon && absf(p2.y - p1.y) <= epsilon && absf(p2.z - p1.z) <= epsilon) ;
} }
bool nonzero(double tha, double thb, double thc, double epsilon) {
return (absf(tha) > epsilon && absf(thb) > epsilon && absf(thc) > epsilon);
}
double u = 0.0 ;
double v = 0.0 ;
double w = 0.0 ;
double th1, th2;
double dist ;
bool is_hidden(SDL_Renderer* renderer, pt_2d p, pt_2d ogp, pt_2d* tri, pt_2d* og) { bool is_hidden(SDL_Renderer* renderer, pt_2d p, pt_2d ogp, pt_2d* tri, pt_2d* og) {
if(pt_equal(p, tri[0], 0.0001) || pt_equal(p, tri[1], 0.0001) || pt_equal(p, tri[2], 0.0001)) { if(pt_equal(p, tri[0], 0.0001) || pt_equal(p, tri[1], 0.0001) || pt_equal(p, tri[2], 0.0001)) {
return false; return false;
} }
double u = 0.0 ;
double v = 0.0 ;
double w = 0.0 ;
get_barycentric(p, tri, &u, &v, &w); get_barycentric(p, tri, &u, &v, &w);
pt_2d mid = convex_pt2d_tri(og[0], u, og[1], v, og[2], w); /*if(renderer != NULL && (u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) {
if(renderer != NULL && (u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) {
if(mid.z >= 0.4) { if(mid.z >= 0.4) {
SDL_Rect r; SDL_Rect r;
r.x = (int)(1500.0 * (1.0 + (mid.x / (1.5 * mid.z * tan_fov))) / 2.0) -2; r.x = (int)(1500.0 * (1.0 + (mid.x / (1.5 * mid.z * tan_fov))) / 2.0) -2;
@ -323,30 +328,32 @@ bool is_hidden(SDL_Renderer* renderer, pt_2d p, pt_2d ogp, pt_2d* tri, pt_2d* og
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &r); SDL_RenderFillRect(renderer, &r);
} }
} }*/
if((u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) { if(((u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) && nonzero(u, v, w, 0.0001)) {
return (proj_pt_distance_to_camera(mid) <= proj_pt_distance_to_camera(ogp)); pt_2d mid = convex_pt2d_tri(og[0], u, og[1], v, og[2], w);
dist = proj_pt_distance_to_camera(mid) - proj_pt_distance_to_camera(ogp);
if(absf(dist) >= 0.0001) {
//return (proj_pt_distance_to_camera(mid) <= proj_pt_distance_to_camera(ogp));
return (dist <= 0.0);
}
} }
return false; return false;
} }
bool nonzero(double tha, double thb, double thc, double epsilon) {
return (absf(tha) > epsilon && absf(thb) > epsilon && absf(thc) > epsilon);
}
bool is_in_front(pt_2d* tri1, pt_2d* og1, pt_2d* tri2, pt_2d* og2) { bool is_in_front(pt_2d* tri1, pt_2d* og1, pt_2d* tri2, pt_2d* og2) {
for(int k = 0; k < 3; k++) { for(int k = 0; k < 3; k++) {
pt_2d p = tri1[k]; pt_2d p = tri1[k];
if(pt_equal(p, tri2[0], 0.0001) || pt_equal(p, tri2[1], 0.0001) || pt_equal(p, tri2[2], 0.0001)) { if(pt_equal(p, tri2[0], 0.0001) || pt_equal(p, tri2[1], 0.0001) || pt_equal(p, tri2[2], 0.0001)) {
return false; return false;
} }
double u = 0.0 ;
double v = 0.0 ;
double w = 0.0 ;
get_barycentric(p, tri2, &u, &v, &w); get_barycentric(p, tri2, &u, &v, &w);
pt_2d mid = convex_pt2d_tri(og2[0], u, og2[1], v, og2[2], w); pt_2d mid = convex_pt2d_tri(og2[0], u, og2[1], v, og2[2], w);
if(((u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) && nonzero(u, v, w, 0.0001)) { if(((u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) && nonzero(u, v, w, 0.0001)) {
return !(proj_pt_distance_to_camera(mid) <= proj_pt_distance_to_camera(og1[k])); dist = proj_pt_distance_to_camera(mid) - proj_pt_distance_to_camera(og1[k]);
if(absf(dist) >= 0.0001) {
//return !(proj_pt_distance_to_camera(mid) <= proj_pt_distance_to_camera(og1[k]));
return !(dist <= 0.0);
}
} }
} }
for(int k = 0; k < 3; k++) { for(int k = 0; k < 3; k++) {
@ -354,22 +361,26 @@ bool is_in_front(pt_2d* tri1, pt_2d* og1, pt_2d* tri2, pt_2d* og2) {
if(pt_equal(p, tri1[0], 0.0001) || pt_equal(p, tri1[1], 0.0001) || pt_equal(p, tri1[2], 0.0001)) { if(pt_equal(p, tri1[0], 0.0001) || pt_equal(p, tri1[1], 0.0001) || pt_equal(p, tri1[2], 0.0001)) {
return false; return false;
} }
double u = 0.0 ;
double v = 0.0 ;
double w = 0.0 ;
get_barycentric(p, tri1, &u, &v, &w); get_barycentric(p, tri1, &u, &v, &w);
pt_2d mid = convex_pt2d_tri(og1[0], u, og1[1], v, og1[2], w); pt_2d mid = convex_pt2d_tri(og1[0], u, og1[1], v, og1[2], w);
if(((u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) && nonzero(u, v, w, 0.0001)) { if(((u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) && nonzero(u, v, w, 0.0001)) {
return !(proj_pt_distance_to_camera(mid) >= proj_pt_distance_to_camera(og2[k])); dist = proj_pt_distance_to_camera(mid) - proj_pt_distance_to_camera(og2[k]);
if(absf(dist) >= 0.0001) {
//return !(proj_pt_distance_to_camera(mid) >= proj_pt_distance_to_camera(og2[k]));
return !(dist >= 0.0) ;
}
} }
} }
for(int k1 = 0; k1 < 3; k1++) { for(int k1 = 0; k1 < 3; k1++) {
for(int k2 = 0; k2 < 3; k2++) { for(int k2 = 0; k2 < 3; k2++) {
double th1, th2;
if(seg_seg_inter(tri1[k1], tri1[(k1+1)%3], tri2[k2], tri2[(k2+1)%3], &th1, &th2)) { if(seg_seg_inter(tri1[k1], tri1[(k1+1)%3], tri2[k2], tri2[(k2+1)%3], &th1, &th2)) {
pt_2d mid1 = convex_pt2d(tri1[k1], tri1[(k1+1)%3], th1); pt_2d mid1 = convex_pt2d(tri1[k1], tri1[(k1+1)%3], th1);
pt_2d mid2 = convex_pt2d(tri2[k2], tri2[(k2+1)%3], th2); pt_2d mid2 = convex_pt2d(tri2[k2], tri2[(k2+1)%3], th2);
return (proj_pt_distance_to_camera(mid1) <= proj_pt_distance_to_camera(mid2)); dist = proj_pt_distance_to_camera(mid1) - proj_pt_distance_to_camera(mid2);
if(absf(dist) >= 0.0001) {
//return (proj_pt_distance_to_camera(mid1) <= proj_pt_distance_to_camera(mid2));
return (dist <= 0.0);
}
} }
} }
} }

View File

@ -12,14 +12,14 @@ Teleporters :
[7.0, 0.0, -1.0, 2.0, 1.0, 2.0, 0.0, 0.0, 0, 0, 255; 1, 0] [7.0, 0.0, -1.0, 2.0, 1.0, 2.0, 0.0, 0.0, 0, 0, 255; 1, 0]
Entities : Entities :
[0.0, 3.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] [0.0, 3.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0]
[0.0, 4.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] [0.0, 4.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0]
[0.0, 5.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] [0.0, 5.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0]
[0.0, 6.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] [0.0, 6.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0]
[0.0, 7.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] [0.0, 7.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0]
[0.0, 8.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] [0.0, 8.0, 0.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, 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.0, 10.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0]
Weight : Weight :
10 10

View File

@ -14,10 +14,10 @@ Teleporters :
[-5.0, 1.0, 9.0, 10.0, 2.0, 1.0, 0.0, 0.0, 0, 0, 255; 1, 0] [-5.0, 1.0, 9.0, 10.0, 2.0, 1.0, 0.0, 0.0, 0, 0, 255; 1, 0]
Entities : Entities :
[3.0, 3.0, 3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] [3.0, 3.0, 3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0]
[-3.0, 3.0, 3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] [-3.0, 3.0, 3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0]
[3.0, 3.0, -3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] [3.0, 3.0, -3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0]
[-3.0, 3.0, -3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] [-3.0, 3.0, -3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0, 0]
Weight : Weight :
20 20

View File

@ -8,7 +8,11 @@ Teleporters :
[9.0, 1.0, -5.0, 1.0, 2.0, 10.0, 0.0, 0.0, 0, 255, 0; 0, 1] [9.0, 1.0, -5.0, 1.0, 2.0, 10.0, 0.0, 0.0, 0, 255, 0; 0, 1]
[-5.0, 1.0, 9.0, 10.0, 2.0, 1.0, 0.0, 0.0, 0, 0, 255; 1, 0] [-5.0, 1.0, 9.0, 10.0, 2.0, 1.0, 0.0, 0.0, 0, 0, 255; 1, 0]
Entities :
[0.0, 3.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 255, 64, 0, 5, 100, 1]
[0.0, 40.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 255, 64, 0, 5, 100, 2]
Weight : Weight :
10 100
$ $