Compare commits

..

No commits in common. "dec1124c4380c62c019f731cf06050375ff04f00" and "f2c45b45cd0595d12d933aee61dcf65a80cb528a" have entirely different histories.

27 changed files with 179 additions and 419 deletions

View File

@ -14,8 +14,6 @@
"move.h": "c",
"hash.h": "c",
"proj.h": "c",
"entities.h": "c",
"menus.h": "c",
"structure.h": "c"
"entities.h": "c"
}
}

View File

@ -1,6 +1,6 @@
CC = gcc
FLAGS = -O2 -Wall -Wextra -g
LFLAGS = -lm src/glad.c -ldl -lglfw -lcglm
LFLAGS = -lm -lncurses src/glad.c -ldl -lglfw -lcglm
all: bin/back

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.

Binary file not shown.

View File

@ -3,6 +3,7 @@
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
@ -230,20 +231,6 @@ teleporter* create_teleporter(
return tp ;
}
void copy_cube(cube_0* src, cube_0* dest) {
dest->red = src->red;
dest->green = src->green;
dest->blue = src->blue;
dest->hz_angle = src->hz_angle;
dest->vt_angle = src->vt_angle;
dest->x = src->x;
dest->y = src->y;
dest->z = src->z;
dest->w = src->w;
dest->h = src->h;
dest->d = src->d;
}
// ------------------------------------------------------------------------------------------------ //
double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1) {
@ -326,31 +313,54 @@ void project_to_camera(double x0, double y0, double z0, double* rx, double* ry,
double z = z0 - camz ;
// rotate (y)
double xry = x*cos(rot_hz) - z*sin(rot_hz) ;
double xry = x*cos(rot_hz) + z*sin(rot_hz) ;
double yry = y ;
double zry = z*cos(rot_hz) + x*sin(rot_hz) ;
double zry = z*cos(rot_hz) - x*sin(rot_hz) ;
// rotate (x)
if(rx != NULL) {*rx = xry ;}
if(ry != NULL) {*ry = yry*cos(rot_vt) + zry*sin(rot_vt) ;}
if(rz != NULL) {*rz = zry*cos(rot_vt) - yry*sin(rot_vt) ;}
if(ry != NULL) {*ry = yry*cos(rot_vt) - zry*sin(rot_vt) ;}
if(rz != NULL) {*rz = zry*cos(rot_vt) + yry*sin(rot_vt) ;}
}
void project_to_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0* c) {
void project_to_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0 c) {
// align pt to (0, 0, 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) ;
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 (y)
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) ;
// rotate (x)
if(rx != NULL) {*rx = c->x + c->w/2.0 + xry ;}
if(ry != NULL) {*ry = c->y + c->h/2.0 + yry*cos(c->vt_angle) - zry*sin(c->vt_angle);}
if(rz != NULL) {*rz = c->z + c->d/2.0 + zry*cos(c->vt_angle) + yry*sin(c->vt_angle);}
if(rx != NULL) {*rx = xry ;}
if(ry != NULL) {*ry = yry*cos(c.vt_angle) - zry*sin(c.vt_angle);}
if(rz != NULL) {*rz = zry*cos(c.vt_angle) + yry*sin(c.vt_angle);}
}
double dist_to_cam_cube(double x0, double y0, double z0, cube_0 c) {
double px0, py0, pz0;
project_to_cube(x0, y0, z0, &px0, &py0, &pz0, c);
return distance_pt_pt_3d(px0 + c.x + c.w/2.0, py0 + c.y + c.h/2.0, pz0 + c.z + c.d/2.0, camx, camy, camz);
}
bool pt_equal_3D(pt_2d p1, pt_2d p2) {
return (p1.x == p2.x && p1.y == p2.y && p1.z == p2.z);
}
bool pt_equal_2D(pt_2d p1, pt_2d p2) {
return (p1.x == p2.x && p1.y == p2.y);
}
bool pt_equal_3D_eps(pt_2d p1, pt_2d p2, double epsilon) {
return (absf(p1.x - p2.x) <= epsilon && absf(p1.y - p2.y) <= epsilon && absf(p1.z - p2.z) <= epsilon);
}
bool pt_equal_2D_eps(pt_2d p1, pt_2d p2, double epsilon, bool debug0) {
if(debug0) {printf("(%lf, %lf)\n", absf(p1.x - p2.x), absf(p1.y - p2.y));}
return (absf(p1.x - p2.x) <= epsilon && absf(p1.y - p2.y) <= epsilon);
}
// ------------------------------------------------------------------------------------------------ //

View File

@ -37,7 +37,6 @@ teleporter* create_teleporter(
double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b,
int chx_dest, int chy_dest, double x_dest, double y_dest, double z_dest
);
void copy_cube(cube_0* src, cube_0* dest);
double convex_pt(double a, double b, double theta);
double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1);
@ -59,6 +58,7 @@ void remove_entity(entity** arr, int* memlen, int* len, int index);
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);
void project_to_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0 c);
double dist_to_cam_cube(double x0, double y0, double z0, cube_0 c);
#endif

View File

@ -3,6 +3,7 @@
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>

View File

@ -3,6 +3,7 @@
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
@ -15,7 +16,6 @@
#include "structure.h"
#include "base.h"
#include "move.h"
#include "menus.h"
#include "proj.h"
#include "entities.h"
#include "generation.h"
@ -136,37 +136,18 @@ void gl_renderCube(unsigned int shaderProgram, unsigned int fragmentShader, unsi
glUniform4f(glGetUniformLocation(fragmentShader, "u_color"), 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;
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) {
return true;
}
}
return false;
}
void gl_renderAll(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO, room* rtd, double offx, double offy, double offz) {
if(rtd != NULL) {
for(int k = 0; k < rtd->map_size; k++) {
if(is_visible(rtd->map[k], offx, offy, offz)) {
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->map[k], offx, offy, offz);
}
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->map[k], offx, offy, offz);
}
for(int k = 0; k < rtd->tps_size; k++) {
if(is_visible(rtd->tps[k]->hitbox, offx, offy, offz)) {
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->tps[k]->hitbox, offx, offy, offz);
}
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->tps[k]->hitbox, offx, offy, offz);
}
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);
}
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->ents[k].pos, offx, offy, offz);
}
}
}
@ -193,12 +174,12 @@ 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);
glUseProgram(shaderProgram);
//glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
}
void gl_drawData(unsigned int shaderProg) {
gl_drawInteger(shaderProg, (int)camx, 0.95f, 0.9f, 0.05f, 255, 255, 255, 0.005, -1);
gl_drawInteger(shaderProg, (int)camy, 0.95f, 0.75f, 0.05f, 255, 255, 255, 0.005, -1);
gl_drawInteger(shaderProg, (int)camz, 0.95f, 0.6f, 0.05f, 255, 255, 255, 0.005, -1);
void gl_drawTriangle() {
gl_drawInteger(shaderProg, player_hp, -0.95f, 0.9f, 0.05f, 255-player_hp/4, player_hp/4, 0, 0.005f, 1);
}

View File

@ -8,6 +8,4 @@ void gl_renderNearbyChunks(unsigned int shaderProgram, unsigned int fragmentShad
void gl_initRender(unsigned int shaderProgram, unsigned int fragmentShader, unsigned int VAO, unsigned int VBO);
void init_vertices();
void gl_drawData(unsigned int shaderProg);
#endif

View File

@ -3,6 +3,7 @@
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>

View File

@ -3,6 +3,7 @@
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
@ -423,35 +424,6 @@ void parse_rooms(int n_rooms) {
free(name);
}
int divider = 8;
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));
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++) {
copy_cube(r->map[k], newMap[k]);
free(r->map[k]);
}
free(r->map);
// use whatever noise funtcion here
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);
i+=1;
}
}
r->map = newMap;
r->map_size += rsize;
}
void generate_nearby_chunks(int render_dist) {
for(int w = -render_dist; w <= render_dist; w++) {
for(int h = -render_dist; h <= render_dist; h++) {
@ -466,7 +438,6 @@ 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);
hashtbl_add(visited, player_chx + w, player_chy + h, new);
k = pool_size+1;
}

View File

@ -3,6 +3,7 @@
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>

View File

@ -3,6 +3,7 @@
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
@ -14,14 +15,12 @@
#include "structure.h"
#include "base.h"
#include "move.h"
#include "menus.h"
#include "proj.h"
#include "entities.h"
#include "display.h"
#include "generation.h"
double sim_time ;
int triCount;
void processInput(GLFWwindow *window, float dtime) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
@ -113,21 +112,6 @@ const char *fragmentShaderSource = "#version 330 core\n"
" FragColor = u_color;\n"
"}\0";
const char *vertexShaderSourceR = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"uniform mat4 scale;\n"
"uniform mat4 slide;\n"
"void main() {\n"
" gl_Position = slide * scale * vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char *fragmentShaderSourceR = "#version 330 core\n"
"uniform vec4 u_color2;\n"
"out vec4 FragColor;\n"
"void main() {\n"
" FragColor = u_color2;\n"
"}\0";
int main_alt() {
// glfw: initialize and configure
// ------------------------------
@ -138,7 +122,7 @@ int main_alt() {
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(1500, 1000, "yahoo", NULL, NULL);
GLFWwindow* window = glfwCreateWindow(1500, 1000, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
fprintf(stderr, "ERROR : cannot initialize GL window");
@ -160,14 +144,7 @@ int main_alt() {
glCullFace(GL_FRONT);
glDepthFunc(GL_LESS);
init_csts();
init_hashtbl();
init_ent_generator(10);
init_proj();
parse_rooms(5);
// ---------------------------------------------------------------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------------------------------------------------------------- //
//printf("%f\n", glDepthRange);
// build and compile our shader program
// ------------------------------------
@ -184,7 +161,6 @@ int main_alt() {
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
fprintf(stderr, "ERROR : cannot initialize shader (1)");
}
// fragment shader
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
@ -194,98 +170,31 @@ int main_alt() {
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
fprintf(stderr, "ERROR : cannot initialize shader (3)");
fprintf(stderr, "ERROR : cannot initialize shader (2)");
}
// link shaders
unsigned int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// check for linking errors
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
fprintf(stderr, "ERROR : cannot link shaders");
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// main VAO/VBO
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(VAO);
// set up data (buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
init_vertices();
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// ---------------------------------------------------------------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------------------------------------------------------------- //
// build and compile our shader program
// ------------------------------------
// vertex shader
unsigned int vertexShaderR = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShaderR, 1, &vertexShaderSourceR, NULL);
glCompileShader(vertexShaderR);
// check for shader compile errors
glGetShaderiv(vertexShaderR, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShaderR, 512, NULL, infoLog);
//std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// fragment shader
unsigned int fragmentShaderR = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShaderR, 1, &fragmentShaderSourceR, NULL);
glCompileShader(fragmentShaderR);
// check for shader compile errors
glGetShaderiv(fragmentShaderR, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShaderR, 512, NULL, infoLog);
//std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// link shaders
unsigned int shaderProgramR = glCreateProgram();
glAttachShader(shaderProgramR, vertexShaderR);
glAttachShader(shaderProgramR, fragmentShaderR);
glLinkProgram(shaderProgramR);
// check for linking errors
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgramR, 512, NULL, infoLog);
//std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexShaderR);
glDeleteShader(fragmentShaderR);
// rectangle VAO/VBO
initMenus();
unsigned int RVBO, RVAO;
glGenVertexArrays(1, &RVAO);
glGenBuffers(1, &RVBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(RVAO);
glBindBuffer(GL_ARRAY_BUFFER, RVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(rectDefault), rectDefault, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
glBindVertexArray(0);
// ---------------------------------------------------------------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------------------------------------------------------------- //
init_csts();
init_hashtbl();
init_ent_generator(10);
init_proj();
parse_rooms(5);
int fps = 60 ;
int interval = 1000000/fps ;
@ -293,40 +202,45 @@ int main_alt() {
clock_t finish = clock();
clock_t origin = clock();
while(!glfwWindowShouldClose(window) && player_hp >= 0) {
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
init_vertices();
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
while(!glfwWindowShouldClose(window)) {
// input
// -----
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
triCount = 0;
origin = clock();
generate_nearby_chunks(1);
// draw the map
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
gl_initRender(shaderProgram, shaderProgram, VAO, VBO);
//gl_renderAll(shaderProgram, shaderProgram, VAO, VBO, current_room, 0.0, 0.0, 0.0);
gl_renderNearbyChunks(shaderProgram, shaderProgram, VAO, VBO, 1);
gl_renderProj(shaderProgram, shaderProgram, VAO, VBO);
// draw data
glUseProgram(shaderProgramR);
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);
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.)
// -------------------------------------------------------------------------------
finish = clock();
processInput(window, ((float)finish - (float)origin)/CLOCKS_PER_SEC);
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));
printf("%f\n", 1.0f/(((float)finish - (float)origin)/CLOCKS_PER_SEC));
//printf("%lf, %lf, %lf\n", camx, camy, camz);
usleep(interval);
@ -355,6 +269,5 @@ int main_alt() {
int main(int argc, char** argv) {
srand(time(NULL));
triCount = 0;
return main_alt();
}

View File

@ -3,14 +3,11 @@
#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 <glad/glad.h>
#include "../include/glad/glad.h"
//#include <GLFW/glfw3.h>
#include <cglm/cglm.h>
#include "hash.h"
#include "structure.h"
@ -18,117 +15,3 @@
#include "entities.h"
#include "menus.h"
float rectDefault[18] ;
static mat4 scale, slide;
void initMenus() {
rectDefault[0] = -0.5f; rectDefault[1] = -0.5f; rectDefault[2] = -1.0f;
rectDefault[3] = -0.5f; rectDefault[4] = 0.5f; rectDefault[5] = -1.0f;
rectDefault[6] = 0.5f; rectDefault[7] = 0.5f; rectDefault[8] = -1.0f;
rectDefault[9] = -0.5f; rectDefault[10] = -0.5f; rectDefault[11] = -1.0f;
rectDefault[12] = 0.5f; rectDefault[13] = 0.5f; rectDefault[14] = -1.0f;
rectDefault[15] = 0.5f; rectDefault[16] = -0.5f; rectDefault[17] = -1.0f;
}
void gl_drawRect(unsigned int fragShader, float x, float y, float w, float h, int r, int g, int b) {
glm_mat4_identity(scale);
glm_mat4_identity(slide);
scale[0][0] = w;
scale[1][1] = h;
scale[2][2] = 0.5f;
glm_translate(slide, (vec3){x+w/2, y+h/2, 0.0f});
glUniformMatrix4fv(glGetUniformLocation(fragShader, "scale"), 1, GL_FALSE, (float*)scale);
glUniformMatrix4fv(glGetUniformLocation(fragShader, "slide"), 1, GL_FALSE, (float*)slide);
glUniform4f(glGetUniformLocation(fragShader, "u_color2"), r/255.0f, g/255.0f, b/255.0f, 0.5f);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
void gl_drawDigit(unsigned int fragShader, int n, float x, float y, float size, int r, int g, int b, float width) {
assert(n >= 0 && n < 10);
if(n == 0) {
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
} else if(n == 1) {
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
} else if(n == 2) {
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b);
} else if(n == 3) {
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b);
} else if(n == 4) {
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, 2*width, size+2*width, r, g, b);
} else if(n == 5) {
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b);
} else if(n == 6) {
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b);
} else if(n == 7) {
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
} else if(n == 8) {
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b);
} else {
gl_drawRect(fragShader, x-size/2-width, y+size-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, 2*width, size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-width, size+2*width, 2*width, r, g, b);
gl_drawRect(fragShader, x+size/2-width, y-size-width, 2*width, 2*size+2*width, r, g, b);
gl_drawRect(fragShader, x-size/2-width, y-size-width, size+2*width, 2*width, r, g, b);
}
}
// 7-segment display
// 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;
float curx = x;
if(left < 0) {
left *= (-1);
}
if(side == 1) {
curx += ((ln_baseN(abs(n), 10)-1)*(size+4*width));
}
while(left > 0) {
gl_drawDigit(fragShader, left%10, curx, y, size, r, g, b, width);
curx -= (size+4*width);
left = left/10;
}
if(n == 0) {
gl_drawDigit(fragShader, 0, curx, y, size, r, g, b, width);
curx -= (size+4*width);
}
if(n < 0) {
gl_drawRect(fragShader, curx-size/2-width, y, size+2*width, 2*width, r, g, b);
}
}
void gl_initDrawRect(unsigned int shaderProgram) {
glUseProgram(shaderProgram);
}

View File

@ -1,11 +1,5 @@
#ifndef MENUS_H
#define MENUS_H
void initMenus();
void gl_drawRect(unsigned int fragShader, float x, float y, float w, float h, int r, int g, int b);
void gl_initDrawRect(unsigned int shaderProgram);
void gl_drawInteger(unsigned int fragShader, int n, float x, float y, float size, int r, int g, int b, float width, int side);
#endif

View File

@ -3,6 +3,7 @@
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>

View File

@ -3,6 +3,7 @@
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <ncurses.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
@ -61,6 +62,20 @@ void appendProj(double x, double y, double z, double w, double h, double d, doub
}
}
void copy_cube(cube_0* src, cube_0* dest) {
dest->red = src->red;
dest->green = src->green;
dest->blue = src->blue;
dest->hz_angle = src->hz_angle;
dest->vt_angle = src->vt_angle;
dest->x = src->x;
dest->y = src->y;
dest->z = src->z;
dest->w = src->w;
dest->h = src->h;
dest->d = src->d;
}
void removeProj(int k) {
if(k >= 0 && k < bullets_id) {
copy_cube(bullets[bullets_id-1].pos, bullets[k].pos);
@ -98,10 +113,6 @@ void updateProj(float dtime) {
if(*(bullets[k].ttl) <= 0.0 || is_colliding_with_map(bullets[k].pos) || is_colliding_with_tp(bullets[k].pos)) {
removeProj(k);
k -= 1;
} else if(distance_pt_cube_0_3d(camx, camy, camz, bullets[k].pos) <= 0.1) {
player_hp -= bullets[k].damage;
removeProj(k);
k -= 1;
} else {
bullets[k].vel->x += ((double)dtime)*bullets[k].acc->x;
bullets[k].vel->y += ((double)dtime)*bullets[k].acc->y;

View File

@ -5,125 +5,122 @@ typedef struct pt_2d {
double x;
double y;
double z;
} pt_2d;
} pt_2d ;
struct cube_0 {
int red; int green; int blue;
int red; int green; int blue ;
double x;
double y;
double z;
double w;
double h;
double d;
double hz_angle;
double vt_angle;
};
typedef struct cube_0 cube_0;
typedef cube_0* cube;
double hz_angle ;
double vt_angle ;
} ;
typedef struct cube_0 cube_0 ;
typedef cube_0* cube ;
typedef struct teleporter {
cube_0* hitbox;
int dest_chx; // in the pool, these are offsets
int dest_chy;
double dest_x;
double dest_y;
double dest_z;
} teleporter;
cube_0* hitbox ;
int dest_chx ; // in the pool, these are offsets
int dest_chy ;
double dest_x ;
double dest_y ;
double dest_z ;
} teleporter ;
typedef struct entity {
cube_0* pos;
cube_0* pos ;
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 (*updatePos)(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret);
void (*onHit)(float dtime, int* hp, int* dmg, cube_0* ret) ;
// triggers when object is hit
void (*onHit)(float dtime, int* hp, int* dmg, cube_0* ret);
// triggers when <hitpoints> goes negative (death)
void (*onDeath)(float dtime);
void (*onDeath)(float dtime) ;
// triggers when <hitpoints> goes negative
int damage;
int* hitpoints;
} entity;
int damage ;
int* hitpoints ;
} entity ;
struct room {
// (0, 0, 0) = bottom, left and down
int chunk_x;
int chunk_y;
cube_0** map;
int map_size;
teleporter** tps;
int tps_size;
entity* ents;
int ent_len;
int ent_memlen;
};
typedef struct room room;
int chunk_x ;
int chunk_y ;
cube_0** map ;
int map_size ;
teleporter** tps ;
int tps_size ;
entity* ents ;
int ent_len ;
int ent_memlen ;
} ;
typedef struct room room ;
struct cell {
int chx;
int chy;
room* area;
struct cell* next;
};
int chx ;
int chy ;
room* area ;
struct cell* next ;
} ;
typedef struct cell cell;
typedef struct cell* linkedList;
typedef struct cell cell ;
typedef struct cell* linkedList ;
struct hashtbl_0 {
int tabLength;
int insertedElts;
int maxInserted;
linkedList* tab;
};
typedef struct hashtbl_0 hashtbl_0;
typedef hashtbl_0* hashtbl;
int tabLength ;
int insertedElts ;
int maxInserted ;
linkedList* tab ;
} ;
typedef struct hashtbl_0 hashtbl_0 ;
typedef hashtbl_0* hashtbl ;
// ------------------------------------------------ //
extern double sim_time;
extern double sim_time ;
extern double camx;
extern double camy;
extern double camz;
extern double camx ;
extern double camy ;
extern double camz ;
extern double rot_hz;
extern double rot_vt;
extern double rot_hz ;
extern double rot_vt ;
extern double tan_fov;
extern double tan_fov ;
extern bool has_changed;
extern bool has_changed ;
extern hashtbl visited;
extern room* current_room;
extern hashtbl visited ;
extern room* current_room ;
extern int player_chx;
extern int player_chy;
extern int player_chx ;
extern int player_chy ;
extern int* drawOrder;
extern int* drawOrder ;
extern int coins;
extern int coins ;
extern int draw_type;
extern int draw_type ;
extern int player_hp;
extern int player_hp ;
extern int fade_dmg;
extern int fade_dmg ;
extern bool stop_evetything;
extern double room_width;
extern bool stop_evetything ;
extern double room_width ;
// no height yet (requires a rewrite oh hashtbl)
extern double room_depth;
extern double room_depth ;
// ---------------------------------------------------------------------------------------------------- //
extern double sensitivity;
extern double fov;
extern double speed;
extern double min_dist;
extern double sensitivity ;
extern double fov ;
extern double speed ;
extern double min_dist ;
// ---------------------------------------------------------------------------------------------------- //
extern float vertices[108];
extern float rectDefault[18];
extern int triCount ;
#endif