added all free() functions to (?) prevent SDL from crashing sometimes

This commit is contained in:
Alexandre 2025-01-23 23:01:11 +01:00
parent 6c8b04de69
commit 1578d6682d
23 changed files with 248 additions and 158 deletions

View File

@ -7,7 +7,7 @@ all: bin/back
test: bin/back
bin/back
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
bin/back: obj/main.o obj/generation.o obj/display.o obj/entities.o obj/bullets.o obj/menus.o obj/triangles.o obj/move.o obj/base.o obj/hash.o
mkdir -p bin
$(CC) $(FLAGS) $^ $(LFLAGS) -o $@
@ -20,9 +20,10 @@ obj/generation.o: src/generation.c
obj/display.o: src/display.c
obj/entities.o: src/entities.c
obj/triangles.o: src/triangles.c
obj/bullets.o: src/bullets.c
obj/move.o: src/move.c
obj/base.o: src/base.c
obj/hash.o: src/menus.c
obj/menus.o: src/menus.c
obj/hash.o: src/hash.c
.PHONY: clean mrproper

BIN
bin/back

Binary file not shown.

Binary file not shown.

BIN
obj/bullets.o Normal file

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

@ -182,7 +182,7 @@ bool str_equal(char* s1, char* s2) {
// ------------------------------------------------------------------------------------------------ //
cube_0 create_cube_0(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b) {
cube_0* create_cube_0(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b) {
cube_0* cb = malloc(sizeof(cube_0)) ;
cb->red = r ;
cb->green = g ;
@ -195,12 +195,12 @@ cube_0 create_cube_0(double x, double y, double z, double w, double h, double d,
cb->d = d ;
cb->hz_angle = hz_a ;
cb->vt_angle = vt_a ;
return *cb ;
return cb ;
}
cube create_cube(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b) {
cube cb = malloc(sizeof(cube_0));
*cb = create_cube_0(x, y, z, w, h, d, hz_a, vt_a, r, g, b) ;
cb = create_cube_0(x, y, z, w, h, d, hz_a, vt_a, r, g, b) ;
return cb;
}
@ -276,23 +276,23 @@ double distance_pt_cube_aligned_3d_max(double x0, double y0, double z0, double c
return (distance_pt_cube_axis_max(x0, cx, cx+cw)+distance_pt_cube_axis_max(y0, cy, cy+ch)+distance_pt_cube_axis_max(z0, cz, cz+cd)) ;
}
double distance_pt_cube_0_3d(double x0, double y0, double z0, cube_0 c) {
double distance_pt_cube_0_3d(double x0, double y0, double z0, cube_0* c) {
// places the origin at the center of the cube
double x = x0 - (c.x + c.w/2.0) ;
double y = y0 - (c.y + c.h/2.0) ;
double z = z0 - (c.z + c.d/2.0) ;
double x = x0 - (c->x + c->w/2.0) ;
double y = y0 - (c->y + c->h/2.0) ;
double z = z0 - (c->z + c->d/2.0) ;
// rotate the point : y then x
double xry = x*cos(c.hz_angle) + z*sin(c.hz_angle) ;
double xry = x*cos(c->hz_angle) + z*sin(c->hz_angle) ;
double yry = y ;
double zry = z*cos(c.hz_angle) - x*sin(c.hz_angle) ;
double zry = z*cos(c->hz_angle) - x*sin(c->hz_angle) ;
double xrx = xry ;
double yrx = yry*cos(c.vt_angle) - zry*sin(c.vt_angle) ;
double zrx = zry*cos(c.vt_angle) + yry*sin(c.vt_angle) ;
double yrx = yry*cos(c->vt_angle) - zry*sin(c->vt_angle) ;
double zrx = zry*cos(c->vt_angle) + yry*sin(c->vt_angle) ;
// now the cube and pt are aligned, and (0, 0, 0) is at the cube's (bary)center
return distance_pt_cube_aligned_3d(xrx, yrx, zrx, -c.w/2.0, -c.h/2.0, -c.d/2.0, c.w, c.h, c.d) ;
return distance_pt_cube_aligned_3d(xrx, yrx, zrx, -c->w/2.0, -c->h/2.0, -c->d/2.0, c->w, c->h, c->d) ;
}
double distance_pt_cube_0_3d_max(double x0, double y0, double z0, cube_0 c) {
@ -338,7 +338,7 @@ double distance_pt_cube_0_3d_weighted(double x0, double y0, double z0, double mx
}
double distance_pt_cube_3d(double x0, double y0, double z0, cube cb) {
return distance_pt_cube_0_3d(x0, y0, z0, *cb) ;
return distance_pt_cube_0_3d(x0, y0, z0, cb) ;
}
double zdepth_of_pt(double x0, double y0, double z0) {

View File

@ -29,7 +29,7 @@ pt_2d vect_diff(pt_2d p1, pt_2d p2);
double dot2D(pt_2d p1, pt_2d p2);
double dot3D(pt_2d p1, pt_2d p2);
cube_0 create_cube_0(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b);
cube_0* create_cube_0(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b);
cube create_cube(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b);
void free_cube(cube c);
teleporter create_teleporter(
@ -48,7 +48,7 @@ double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1,
double distance_pt_seg_3d(double x, double y, double z, double sx, double sy, double sz, double ex, double ey, double ez);
double distance_pt_cube_axis(double coord, double begin, double end);
double distance_pt_cube_aligned_3d(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd);
double distance_pt_cube_0_3d(double x0, double y0, double z0, cube_0 c);
double distance_pt_cube_0_3d(double x0, double y0, double z0, cube_0* c);
double distance_pt_cube_aligned_3d_weighted(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd, double mx, double my, double mz);
double distance_pt_cube_0_3d_weighted(double x0, double y0, double z0, double mx, double my, double mz, cube_0 c);

19
src/bullets.c Normal file
View File

@ -0,0 +1,19 @@
#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 "move.h"
#include "entities.h"
#include "bullets.h"

5
src/bullets.h Normal file
View File

@ -0,0 +1,5 @@
#ifndef BULLETS_H
#define BULLETS_H
#endif

View File

@ -82,8 +82,8 @@ void placeRectToRenderer(SDL_Renderer* renderer, int X, int Y, int W, int H, int
rect.y = Y;
rect.w = W;
rect.h = H;
SDL_SetRenderDrawColor(renderer, R, G, B, A);
SDL_RenderFillRect(renderer, &rect);
assert(SDL_SetRenderDrawColor(renderer, R, G, B, A) == 0);
assert(SDL_RenderFillRect(renderer, &rect) == 0);
}
void placeRectToRendererNoColor(SDL_Renderer* renderer, int X, int Y, int W, int H) {
@ -225,59 +225,23 @@ double square_z_distance_to_camera(
) {
return maxd(segment_z_distance_to_camera(x0, y0, z0, x1, y1, z1), segment_z_distance_to_camera(x0, y0, z0, x2, y2, z2));
}
double cube_z_distance_to_camera(cube_0 cb) {
double dist_0 = square_z_distance_to_camera(
cb.x + cb.w, cb.y, cb.z,
cb.x + cb.w, cb.y + cb.h, cb.z,
cb.x + cb.w, cb.y, cb.z + cb.d
);
double dist_1 = square_z_distance_to_camera(
cb.x, cb.y, cb.z,
cb.x, cb.y + cb.h, cb.z,
cb.x, cb.y, cb.z + cb.d
);
double dist_2 = square_z_distance_to_camera(
cb.x, cb.y + cb.h, cb.z,
cb.x + cb.w, cb.y + cb.h, cb.z,
cb.x, cb.y + cb.h, cb.z + cb.d
);
double dist_3 = square_z_distance_to_camera(
cb.x, cb.y, cb.z,
cb.x + cb.w, cb.y, cb.z,
cb.x, cb.y, cb.z + cb.d
);
double dist_4 = square_z_distance_to_camera(
cb.x, cb.y, cb.z + cb.d,
cb.x + cb.w, cb.y, cb.z + cb.d,
cb.x, cb.y + cb.h, cb.z + cb.d
);
double dist_5 = square_z_distance_to_camera(
cb.x, cb.y, cb.z,
cb.x + cb.w, cb.y, cb.z,
cb.x, cb.y + cb.h, cb.z
);
return maxd(maxd(dist_0, dist_1), maxd(maxd(dist_2, dist_3), maxd(dist_4, dist_5)));
}
// ---------------------------------------------------------------------------------------------------------------------------------- //
void rotate_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0 cb) {
void rotate_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0* cb) {
// align pt to (0, 0, 0)
double x = x0 - (cb.x + cb.w/2) ;
double y = y0 - (cb.y + cb.h/2) ;
double z = z0 - (cb.z + cb.d/2) ;
double x = x0 - (cb->x + cb->w/2) ;
double y = y0 - (cb->y + cb->h/2) ;
double z = z0 - (cb->z + cb->d/2) ;
// rotate (y)
double xry = x*cos(cb.hz_angle) - z*sin(cb.hz_angle) ;
double xry = x*cos(cb->hz_angle) - z*sin(cb->hz_angle) ;
double yry = y ;
double zry = z*cos(cb.hz_angle) + x*sin(cb.hz_angle) ;
double zry = z*cos(cb->hz_angle) + x*sin(cb->hz_angle) ;
// rotate (x)
*rx = (cb.x + cb.w/2) + xry ;
*ry = (cb.y + cb.h/2) + yry*cos(cb.vt_angle) - zry*sin(cb.vt_angle) ;
*rz = (cb.z + cb.d/2) + zry*cos(cb.vt_angle) + yry*sin(cb.vt_angle) ;
*rx = (cb->x + cb->w/2) + xry ;
*ry = (cb->y + cb->h/2) + yry*cos(cb->vt_angle) - zry*sin(cb->vt_angle) ;
*rz = (cb->z + cb->d/2) + zry*cos(cb->vt_angle) + yry*sin(cb->vt_angle) ;
}
void draw_segment(SDL_Renderer* renderer, double sx, double sy, double sz, double ex, double ey, double ez) {
@ -401,44 +365,44 @@ void drawOutlineOfCube_0(SDL_Renderer* renderer, cube_0 c) {
// -------------------------------------------------------------------------------------------------------------------------------- //
// this is where the fun begins
int surfaceDrawOrder(double x0, double y0, double z0, cube_0 cb) {
int surfaceDrawOrder(double x0, double y0, double z0, cube_0* cb) {
// returns the number of surfaces that should be drawn, as well as filling drawOrder for said surfaces :
// 0 = +x ; 1 = -x
// 2 = +y ; 3 = -y
// 4 = +z ; 5 = -z
// align cube center to (0, 0, 0)
double x = x0 - (cb.x + cb.w/2.0) ;
double y = y0 - (cb.y + cb.h/2.0) ;
double z = z0 - (cb.z + cb.d/2.0) ;
double x = x0 - (cb->x + cb->w/2.0) ;
double y = y0 - (cb->y + cb->h/2.0) ;
double z = z0 - (cb->z + cb->d/2.0) ;
// rotate (y)
double xry = x*cos(cb.hz_angle) + z*sin(cb.hz_angle) ;
double xry = x*cos(cb->hz_angle) + z*sin(cb->hz_angle) ;
double yry = y ;
double zry = z*cos(cb.hz_angle) - x*sin(cb.hz_angle) ;
double zry = z*cos(cb->hz_angle) - x*sin(cb->hz_angle) ;
// rotate (x)
double xrx = xry ;
double yrx = yry*cos(cb.vt_angle) + zry*sin(cb.vt_angle) ;
double zrx = zry*cos(cb.vt_angle) - yry*sin(cb.vt_angle) ;
double yrx = yry*cos(cb->vt_angle) + zry*sin(cb->vt_angle) ;
double zrx = zry*cos(cb->vt_angle) - yry*sin(cb->vt_angle) ;
// cube is centered and aligned
int id = 0 ;
if(xrx > cb.w/2.0) {
if(xrx > cb->w/2.0) {
drawOrder[id] = 0 ;
id += 1 ;
} else if(xrx < -cb.w/2.0) {
} else if(xrx < -cb->w/2.0) {
drawOrder[id] = 1 ;
id += 1 ;
}
if(yrx > cb.h/2.0) {
if(yrx > cb->h/2.0) {
drawOrder[id] = 2 ;
id += 1 ;
} else if(yrx < -cb.h/2.0) {
} else if(yrx < -cb->h/2.0) {
drawOrder[id] = 3 ;
id += 1 ;
}
if(zrx > cb.d/2.0) {
if(zrx > cb->d/2.0) {
drawOrder[id] = 4 ;
id += 1 ;
} else if(zrx < -cb.d/2.0) {
} else if(zrx < -cb->d/2.0) {
drawOrder[id] = 5 ;
id += 1 ;
}
@ -481,7 +445,7 @@ void renderTriangleNoProject(
if(debug) {
printf("P[*] : (%f, %f), (%f, %f), (%f, %f)\n", vtxs[0].position.x, vtxs[0].position.y, vtxs[1].position.x, vtxs[1].position.y, vtxs[2].position.x, vtxs[2].position.y);
}
SDL_RenderGeometry(renderer, NULL, vtxs, 3, NULL, 0);
assert(SDL_RenderGeometry(renderer, NULL, vtxs, 3, NULL, 0) == 0);
}
double near = -1.0 ;
@ -691,7 +655,7 @@ void addTriangleRotated(
double x1, double y1, double z1,
double x2, double y2, double z2,
int red, int green, int blue,
cube_0 cb
cube_0* cb
) {
double px0; double py0; double pz0;
double px1; double py1; double pz1;
@ -704,54 +668,54 @@ void addTriangleRotated(
// -------------------------------------------------------------------------------------------------------------------------------- //
void add_single(cube_0 c) { // x
void add_single(cube_0* c) { // x
int leng = surfaceDrawOrder(camx, camy, camz, c);
for(int sf0 = 0; sf0 < leng; sf0++) {
int sf = drawOrder[sf0];
if(sf == 0 || sf == 1) {
addTriangleRotated(
c.x + c.w*(sf==0), c.y , c.z,
c.x + c.w*(sf==0), c.y + c.h, c.z,
c.x + c.w*(sf==0), c.y + c.h, c.z + c.d,
c.red, c.green, c.blue, c
c->x + c->w*(sf==0), c->y , c->z,
c->x + c->w*(sf==0), c->y + c->h, c->z,
c->x + c->w*(sf==0), c->y + c->h, c->z + c->d,
c->red, c->green, c->blue, c
);
addTriangleRotated(
c.x + c.w*(sf==0), c.y, c.z,
c.x + c.w*(sf==0), c.y, c.z + c.d,
c.x + c.w*(sf==0), c.y + c.h, c.z + c.d,
c.red, c.green, c.blue, c
c->x + c->w*(sf==0), c->y, c->z,
c->x + c->w*(sf==0), c->y, c->z + c->d,
c->x + c->w*(sf==0), c->y + c->h, c->z + c->d,
c->red, c->green, c->blue, c
);
} else if(sf == 2 || sf == 3) {
addTriangleRotated(
c.x , c.y + c.h*(sf==2), c.z,
c.x + c.w, c.y + c.h*(sf==2), c.z,
c.x + c.w, c.y + c.h*(sf==2), c.z + c.d,
c.red, c.green, c.blue, c
c->x , c->y + c->h*(sf==2), c->z,
c->x + c->w, c->y + c->h*(sf==2), c->z,
c->x + c->w, c->y + c->h*(sf==2), c->z + c->d,
c->red, c->green, c->blue, c
);
addTriangleRotated(
c.x , c.y + c.h*(sf==2), c.z,
c.x , c.y + c.h*(sf==2), c.z + c.d,
c.x + c.w, c.y + c.h*(sf==2), c.z + c.d,
c.red, c.green, c.blue, c
c->x , c->y + c->h*(sf==2), c->z,
c->x , c->y + c->h*(sf==2), c->z + c->d,
c->x + c->w, c->y + c->h*(sf==2), c->z + c->d,
c->red, c->green, c->blue, c
);
} else { // z
addTriangleRotated(
c.x , c.y , c.z + c.d*(sf==4),
c.x + c.w, c.y , c.z + c.d*(sf==4),
c.x + c.w, c.y + c.h, c.z + c.d*(sf==4),
c.red, c.green, c.blue, c
c->x , c->y , c->z + c->d*(sf==4),
c->x + c->w, c->y , c->z + c->d*(sf==4),
c->x + c->w, c->y + c->h, c->z + c->d*(sf==4),
c->red, c->green, c->blue, c
);
addTriangleRotated(
c.x , c.y , c.z + c.d*(sf==4),
c.x , c.y + c.h, c.z + c.d*(sf==4),
c.x + c.w, c.y + c.h, c.z + c.d*(sf==4),
c.red, c.green, c.blue, c
c->x , c->y , c->z + c->d*(sf==4),
c->x , c->y + c->h, c->z + c->d*(sf==4),
c->x + c->w, c->y + c->h, c->z + c->d*(sf==4),
c->red, c->green, c->blue, c
);
}
}
}
void add_triangles_cb(cube_0* arr, int len) {
void add_triangles_cb(cube_0** arr, int len) {
for(int k = 0; k < len; k++) {
add_single(arr[k]);
}
@ -765,7 +729,7 @@ void add_triangles_tp(teleporter* arr, int len) {
void add_triangles_ent(entity* arr, int len) {
for(int k = 0; k < len; k++) {
add_single(*(arr[k].pos));
add_single(arr[k].pos);
}
}

View File

@ -28,7 +28,6 @@ double square_z_distance_to_camera(
double x1, double y1, double z1,
double x2, double y2, double z2
);
double cube_z_distance_to_camera(cube_0 cb);
void axialRotation_X0(double* y, double* z, double theta);
void axialRotation_X(double* y, double* z, double theta, double cst_y, double cst_z);

View File

@ -102,7 +102,6 @@ void go_to_player(double x, double y, double z, double w, double h, double d, do
}
void explodeOnHit(float dtime, int* hp, int* dmg, cube_0* ret) {
player_hp -= (*dmg);
if(*dmg != 0) {
fade_dmg = 255 ;

View File

@ -71,21 +71,21 @@ void copy_room(room* src, room* dest, int chx, int chy) {
// considering dest has already been malloc'd
dest->chunk_x = chx ;
dest->chunk_y = chy ;
dest->map = malloc(sizeof(cube_0)*src->map_size);
dest->map = malloc(sizeof(cube_0*)*src->map_size);
for(int k = 0; k < src->map_size; k++) {
dest->map[k] = create_cube_0(
src->map[k].x, src->map[k].y, src->map[k].z,
src->map[k].w, src->map[k].h, src->map[k].d,
src->map[k].hz_angle, src->map[k].vt_angle, src->map[k].red, src->map[k].green, src->map[k].blue
src->map[k]->x, src->map[k]->y, src->map[k]->z,
src->map[k]->w, src->map[k]->h, src->map[k]->d,
src->map[k]->hz_angle, src->map[k]->vt_angle, src->map[k]->red, src->map[k]->green, src->map[k]->blue
);
}
dest->map_size = src->map_size ;
dest->tps = malloc(sizeof(teleporter)*src->tps_size);
for(int k = 0; k < src->tps_size; k++) {
dest->tps[k].hitbox = create_cube_0(
src->tps[k].hitbox.x, src->tps[k].hitbox.y, src->tps[k].hitbox.z,
src->tps[k].hitbox.w, src->tps[k].hitbox.h, src->tps[k].hitbox.d,
src->tps[k].hitbox.hz_angle, src->tps[k].hitbox.vt_angle, src->tps[k].hitbox.red, src->tps[k].hitbox.green, src->tps[k].hitbox.blue
src->tps[k].hitbox->x, src->tps[k].hitbox->y, src->tps[k].hitbox->z,
src->tps[k].hitbox->w, src->tps[k].hitbox->h, src->tps[k].hitbox->d,
src->tps[k].hitbox->hz_angle, src->tps[k].hitbox->vt_angle, src->tps[k].hitbox->red, src->tps[k].hitbox->green, src->tps[k].hitbox->blue
);
dest->tps[k].dest_chx = src->tps[k].dest_chx + chx;
dest->tps[k].dest_chy = src->tps[k].dest_chy + chy;
@ -100,8 +100,7 @@ void copy_room(room* src, room* dest, int chx, int chy) {
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 = malloc(sizeof(cube_0));
*(dest->ents[k].pos) = create_cube_0(
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
@ -117,7 +116,7 @@ void build_starting_chunk(int chx, int chy) {
room* new = malloc(sizeof(room));
new->chunk_x = chx ;
new->chunk_y = chy ;
new->map = malloc(sizeof(cube_0)*8);
new->map = malloc(sizeof(cube_0)*9);
new->map_size = 9 ;
new->tps = malloc(sizeof(teleporter)*4);
new->tps_size = 4 ;
@ -140,8 +139,7 @@ void build_starting_chunk(int chx, int chy) {
new->ents = malloc(sizeof(entity)*128);
new->ent_len = 1 ;
new->ent_memlen = 128 ;
new->ents[0].pos = malloc(sizeof(cube_0));
*(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].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 ;
@ -322,8 +320,7 @@ void parse_one_room(int id, char* filename) {
if(entry == NULL) {
entry = get_entry(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].hitpoints = malloc(sizeof(int));
*(pool[id].area->ents[k].hitpoints) = hp ;
@ -331,23 +328,26 @@ void parse_one_room(int id, char* filename) {
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 ;
//printf("\n");
}
// debug
for(int k = 0; k < ncubes; k++) {
printf("(%lf, %lf, %lf), (%lf, %lf, %lf), (%lf, %lf), (%d, %d, %d)\n",
pool[id].area->map[k].x,
pool[id].area->map[k].y,
pool[id].area->map[k].z,
pool[id].area->map[k].w,
pool[id].area->map[k].h,
pool[id].area->map[k].d,
pool[id].area->map[k].hz_angle,
pool[id].area->map[k].vt_angle,
pool[id].area->map[k].red,
pool[id].area->map[k].green,
pool[id].area->map[k].blue
pool[id].area->map[k]->x,
pool[id].area->map[k]->y,
pool[id].area->map[k]->z,
pool[id].area->map[k]->w,
pool[id].area->map[k]->h,
pool[id].area->map[k]->d,
pool[id].area->map[k]->hz_angle,
pool[id].area->map[k]->vt_angle,
pool[id].area->map[k]->red,
pool[id].area->map[k]->green,
pool[id].area->map[k]->blue
);
}
printf("\n\n");
@ -423,3 +423,22 @@ void generate_nearby_chunks(int render_dist) {
}
}
}
void free_pool() {
for(int k0 = 0; k0 < pool_size; k0++) {
for(int k = 0; k < pool[k0].area->map_size; k++) {
free(pool[k0].area->map[k]);
}
for(int k = 0; k < pool[k0].area->tps_size; k++) {
free(pool[k0].area->tps[k].hitbox);
}
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);
}
free(pool[k0].area->ents);
free(pool[k0].area->tps);
free(pool[k0].area->map);
}
free(pool);
}

View File

@ -32,4 +32,6 @@ void parse_rooms(int n_rooms) ;
void generate_nearby_chunks(int render_dist) ;
void free_pool();
#endif

View File

@ -60,9 +60,27 @@ void linkedList_add(linkedList lst, int chx, int chy, room* area) {
}
}
void free_all_cubes(room* r) {
for(int k = 0; k < r->map_size; k++) {
free(r->map[k]);
}
for(int k = 0; k < r->tps_size; k++) {
free(r->tps[k].hitbox);
}
for(int k = 0; k < r->ent_len; k++) {
free(r->ents[k].hitpoints);
free(r->ents[k].pos);
}
free(r->ents);
free(r->tps);
free(r->map);
}
void linkedList_free(linkedList lst) {
// frees rooms as well (require all created rooms to be there)
if(lst != NULL) {
printf("freeing (%d, %d)\n", lst->chx, lst->chy);
free_all_cubes(lst->area);
linkedList_free(lst->next);
free(lst->area);
free(lst);
@ -133,7 +151,8 @@ void hashtbl_add(hashtbl tbl, int chx, int chy, room* area) {
void hashtbl_free(hashtbl tbl) {
for(int k = 0; k < tbl->tabLength; k++) {
free(tbl->tab[k]);
linkedList_free(tbl->tab[k]);
}
free(tbl->tab);
free(tbl);
}

View File

@ -28,7 +28,7 @@ int main(int argc, char** argv) {
//-------------------------------------------------------------------------------//
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf("error initializing SDL: %s\n", SDL_GetError());
printf( "error initializing SDL: %s\n", SDL_GetError());
}
SDL_Window* win = SDL_CreateWindow("Game",
SDL_WINDOWPOS_CENTERED,
@ -37,7 +37,11 @@ int main(int argc, char** argv) {
Uint32 render_flags = SDL_RENDERER_ACCELERATED;
SDL_Renderer* rend = SDL_CreateRenderer(win, -1, render_flags);
SDL_SetRenderDrawBlendMode(rend, SDL_BLENDMODE_BLEND);
if(rend == NULL) {
printf( "ERROR : cannot initialize SDL renderer\n");
exit(1);
}
printf( "%d\n", SDL_SetRenderDrawBlendMode(rend, SDL_BLENDMODE_BLEND));
//-------------------------------------------------------------------------------//
@ -52,6 +56,8 @@ int main(int argc, char** argv) {
int interval = 1000000/fps;
double intervalf = 1.0/((double)(fps));
bool debug_main = true ;
init_csts();
init_hashtbl();
init_draworder();
@ -67,45 +73,91 @@ int main(int argc, char** argv) {
clock_t entstart = clock();
clock_t entend = clock();
float delta;
while(true) {
while(!stop_evetything) {
resetRenderer(rend) ;
origin = clock();
SDL_SetRenderDrawColor(rend, 255, 255, 255, SDL_ALPHA_OPAQUE) ;
entend = clock();
//printf("00\n");
//printf("%s\n", SDL_GetError());
fflush(stdout);
playerActions(((float)entend - (float)entstart)/CLOCKS_PER_SEC) ;
//printf("01\n");
fflush(stdout);
generate_nearby_chunks(1);
//printf("02\n");
fflush(stdout);
entend = clock();
update_entities(((float)entend - (float)entstart)/CLOCKS_PER_SEC);
//printf("03\n");
fflush(stdout);
entstart = clock();
drawCurrentRoom(rend);
//printf("04\n");
//printf("%s\n", SDL_GetError());
fflush(stdout);
drawData(rend) ;
//printf("05\n");
fflush(stdout);
drawHPbar(rend);
//printf("06\n");
fflush(stdout);
finish = clock();
fade_dmg = max(fade_dmg-5, 0);
delta = ((float)finish - (float)origin)/CLOCKS_PER_SEC;
//printf("07\n");
fflush(stdout);
drawNumberToRenderer(rend, digits, (int)(1.0f/delta), 720, 60, 75/2, 105/2, 0);
drawNumberToRenderer(rend, digits, (int)(10*sim_time), 720, 110, 75/2, 105/2, 0);
drawNumberToRenderer(rend, digits, coins, 1500/2-55, 1000 - 70, 75/3, 105/3, 0);
//printf("08\n");
fflush(stdout);
//printf("%s\n", SDL_GetError());
updateRenderer(rend) ;
sim_time += delta + intervalf ;
//printf("09\n");
fflush(stdout);
usleep(interval) ;
}
//printf("GPNE\n");
fflush(stdout);
free_digits(digits) ;
for(int k = 0; k < MAX_SIZE; k++) {
free(triangles_to_render[k]);
free(triangles_og_coords[k]);
}
free(drawOrder);
free(triangles_to_render);
free(triangles_og_coords);
free(reds);
free(greens);
free(blues);
free(triangles_order);
free(visited_tri);
//printf("10\n");
fflush(stdout);
hashtbl_free(visited);
free_pool();
/* -------------------------------------------------------- */
SDL_DestroyRenderer(rend);
//printf("10\n");
fflush(stdout);
SDL_DestroyWindow(win);
//printf("11\n");
fflush(stdout);
SDL_Quit();
//printf("12\n");
fflush(stdout);
/* -------------------------------------------------------- */
printf("Done\n") ;
//printf("Done\n") ;
fflush(stdout);
return 0;
}

View File

@ -26,6 +26,8 @@ double min_dist = 0.7 ;
int player_hp ;
bool stop_evetything;
double camx ;
double camy ;
double camz ;
@ -45,10 +47,11 @@ void init_csts() {
camy = 3.0 ;
camz = 3.0 ;
rot_hz = 0.0 ;
rot_vt = 180.0 ;
rot_vt = 180.0*3.14159/180.0 ;
draw_type = 0 ;
player_hp = 1000 ;
fade_dmg = 0;
stop_evetything = false ;
tan_fov = tan((fov * 3.14159 / 180.0) / 2.0) ;
}
@ -56,9 +59,9 @@ void set_player_coords(int old_chx, int old_chy) {
for(int k = 0; k < current_room->tps_size; k++) {
if(current_room->tps[k].dest_chx == old_chx && current_room->tps[k].dest_chy == old_chy) {
if(true) {
camx = current_room->tps[k].hitbox.x + current_room->tps[k].hitbox.w/2.0;
camy = current_room->tps[k].hitbox.y + current_room->tps[k].hitbox.h +1.5;
camz = current_room->tps[k].hitbox.z + current_room->tps[k].hitbox.d/2.0;
camx = current_room->tps[k].hitbox->x + current_room->tps[k].hitbox->w/2.0;
camy = current_room->tps[k].hitbox->y + current_room->tps[k].hitbox->h +1.5;
camz = current_room->tps[k].hitbox->z + current_room->tps[k].hitbox->d/2.0;
}
return ;
}
@ -85,7 +88,7 @@ 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)));
@ -178,10 +181,7 @@ void playerActions(float dtime) {
}
if(state[SDL_SCANCODE_SPACE] == 1) {
if(pass) {
pass = false ;
draw_type = (1+draw_type)%2 ;
}
stop_evetything = true ;
} else {
pass = true ;
}

View File

@ -27,7 +27,7 @@ typedef struct cube_0 cube_0 ;
typedef cube_0* cube ;
typedef struct teleporter {
cube_0 hitbox ;
cube_0* hitbox ;
int dest_chx ; // in the pool, these are offsets
int dest_chy ;
double dest_x ;
@ -54,7 +54,7 @@ struct room {
// (0, 0, 0) = bottom, left and down
int chunk_x ;
int chunk_y ;
cube_0* map ;
cube_0** map ;
int map_size ;
teleporter* tps ;
int tps_size ;
@ -124,4 +124,15 @@ extern int player_hp ;
extern int fade_dmg ;
extern bool stop_evetything ;
extern int* reds ;
extern int* greens ;
extern int* blues ;
extern int* triangles_order ;
extern bool* visited_tri ;
extern int MAX_SIZE ;
#endif