preparing to add inventories

This commit is contained in:
Alexandre 2025-05-11 22:31:35 +02:00
parent b18b1dc5ed
commit 34d70154e0
14 changed files with 101 additions and 17 deletions

View File

@ -10,7 +10,7 @@ test: bin/back
mem: bin/back mem: bin/back
valgrind --leak-check=full ./bin/back valgrind --leak-check=full ./bin/back
bin/back: obj/main.o obj/generation.o obj/display.o obj/proj.o obj/entities.o obj/bullets.o obj/menus.o obj/music.o obj/maeth.o obj/move.o obj/base.o obj/hash.o bin/back: obj/main.o obj/generation.o obj/display.o obj/proj.o obj/entities.o obj/bullets.o obj/menus.o obj/inventory.o obj/music.o obj/maeth.o obj/move.o obj/base.o obj/hash.o
mkdir -p bin mkdir -p bin
$(CC) $(FLAGS) $^ $(LFLAGS) -o $@ $(CC) $(FLAGS) $^ $(LFLAGS) -o $@
@ -26,6 +26,7 @@ obj/bullets.o: src/bullets.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/proj.o: src/proj.c obj/proj.o: src/proj.c
obj/inventory.o: src/inventory.c
obj/music.o: src/music.c obj/music.o: src/music.c
obj/maeth.o: src/maeth.c obj/maeth.o: src/maeth.c
obj/menus.o: src/menus.c obj/menus.o: src/menus.c

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
obj/inventory.o Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -232,8 +232,8 @@ void gl_renderNearbyChunks(int render_distance) {
gl_renderAll(hashtbl_find_opt(visited, player_chx+w, player_chy+h), (2.0*room_width)*w, 0.0, (2.0*room_depth)*h); gl_renderAll(hashtbl_find_opt(visited, player_chx+w, player_chy+h), (2.0*room_width)*w, 0.0, (2.0*room_depth)*h);
} }
} }
if(F5 != 0) {
glBindTexture(GL_TEXTURE_2D, textures[0]); glBindTexture(GL_TEXTURE_2D, textures[0]);
if(F5 != 0) {
fill_cube_0(F5_renderer, camx-min_dist, camy-min_dist, camz-min_dist, 2*min_dist, 2*min_dist, 2*min_dist, 0.0, 0.0, 192, 192, 192); fill_cube_0(F5_renderer, camx-min_dist, camy-min_dist, camz-min_dist, 2*min_dist, 2*min_dist, 2*min_dist, 0.0, 0.0, 192, 192, 192);
gl_renderCube(F5_renderer, 0.0, 0.0, 0.0); gl_renderCube(F5_renderer, 0.0, 0.0, 0.0);
} }

42
src/inventory.c Normal file
View File

@ -0,0 +1,42 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
#include <time.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <cglm/cglm.h>
#include "hash.h"
#include "base.h"
#include "display.h"
#include "inventory.h"
inventory* playerInventory;
const int Lines = 9;
const int Cols = 3;
void inv_init() {
playerInventory = malloc(sizeof(inventory));
playerInventory->lines = Lines;
playerInventory->cols = Cols;
playerInventory->data = malloc(sizeof(stack*)*Lines);
for(int k = 0; k < Lines; k++) {
playerInventory->data[k] = malloc(sizeof(stack)*Cols);
for(int l = 0; l < Cols; l++) {
playerInventory->data[k][l] = NULL;
}
}
}
void inv_destroy() {
for(int k = 0; k < Lines; k++) {
free(playerInventory->data[k]);
}
free(playerInventory->data);
free(playerInventory);
}

29
src/inventory.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef INV_H
#define INV_H
typedef struct item {
const char* name;
int maxCount;
int texID;
void (*onCollect)(float dtime);
void (*onUse)(float dtime);
} item;
typedef struct stack_t {
item* itm;
int count;
} stack_t;
typedef stack_t* stack; // can be NULL
typedef struct inventory {
stack** data;
int lines;
int cols;
} inventory;
// -------------- //
void inv_init();
void inv_destroy();
#endif

View File

@ -22,6 +22,7 @@
#include "move.h" #include "move.h"
#include "menus.h" #include "menus.h"
#include "proj.h" #include "proj.h"
#include "inventory.h"
#include "entities.h" #include "entities.h"
#include "display.h" #include "display.h"
#include "generation.h" #include "generation.h"
@ -48,6 +49,7 @@ bool yPress = false;
bool P5ress = false; bool P5ress = false;
unsigned int textures[32]; unsigned int textures[32];
unsigned int itemTexts[32];
bool paused = false; bool paused = false;
bool hardReset = true; bool hardReset = true;
@ -417,6 +419,8 @@ const char *vertexShaderSourceR = "#version 330 core\n"
const char *fragmentShaderSourceR = "#version 330 core\n" const char *fragmentShaderSourceR = "#version 330 core\n"
"uniform vec4 u_color2;\n" "uniform vec4 u_color2;\n"
"out vec4 FragColor;\n" "out vec4 FragColor;\n"
"in vec2 texCoord;\n"
"uniform sampler2D tex0;\n"
"void main() {\n" "void main() {\n"
" FragColor = u_color2;\n" " FragColor = u_color2;\n"
"}\0"; "}\0";
@ -449,33 +453,32 @@ void generate_texture_2D(int id, char* filename, file_extension ext) {
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
} }
/*void generate_texture_3D(int id, char* filename, file_extension ext) { void generate_itemTexture_2D(int id, char* filename, file_extension ext) {
int widthImg, heightImg, depthImg, numColCh; int widthImg, heightImg, numColCh;
//unsigned char* bytes = stbi_load("res/container.jpg", &widthImg, &heightImg, &numColCh, 0); //unsigned char* bytes = stbi_load("res/container.jpg", &widthImg, &heightImg, &numColCh, 0);
unsigned char* bytes = stbi_load(filename, &widthImg, &heightImg, &numColCh, 0); unsigned char* bytes = stbi_load(filename, &widthImg, &heightImg, &numColCh, 0);
if(bytes == 0) {fprintf(stderr, "ERROR : cannot load texture\n"); exit(1);} if(bytes == 0) {fprintf(stderr, "ERROR : cannot load texture [%s]\n", filename); exit(1);}
glGenTextures(1, &textures[id]); glGenTextures(1, &itemTexts[id]);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_3D, textures[id]); glBindTexture(GL_TEXTURE_2D, itemTexts[id]);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
if(ext == JPG || ext == JPEG) { if(ext == JPG || ext == JPEG) {
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB, widthImg, heightImg, depthImg, 0, GL_RGB, GL_UNSIGNED_BYTE, bytes); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, widthImg, heightImg, 0, GL_RGB, GL_UNSIGNED_BYTE, bytes);
glGenerateMipmap(GL_TEXTURE_3D); glGenerateMipmap(GL_TEXTURE_2D);
} else if(ext == PNG) { } else if(ext == PNG) {
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, widthImg, heightImg, depthImg, 0, GL_RGBA, GL_UNSIGNED_BYTE, bytes); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, widthImg, heightImg, 0, GL_RGBA, GL_UNSIGNED_BYTE, bytes);
glGenerateMipmap(GL_TEXTURE_3D); glGenerateMipmap(GL_TEXTURE_2D);
} }
stbi_image_free(bytes); stbi_image_free(bytes);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
}*/ }
int main_alt() { int main_alt() {
// glfw: initialize and configure // glfw: initialize and configure
@ -515,6 +518,7 @@ int main_alt() {
//glfwSetMouseButtonCallback(window, mouse_button_callback); //glfwSetMouseButtonCallback(window, mouse_button_callback);
init_csts(); init_csts();
inv_init();
init_hashtbl(); init_hashtbl();
init_interf(window); init_interf(window);
build_all_menus(); build_all_menus();
@ -612,6 +616,11 @@ int main_alt() {
//generate_texture_2D(17, "res/selection-mod-flashlight.png", PNG); //generate_texture_2D(17, "res/selection-mod-flashlight.png", PNG);
//generate_texture_2D(18, "res/selection-mod-suddendeath.png", PNG); //generate_texture_2D(18, "res/selection-mod-suddendeath.png", PNG);
printf("-----------------------------------------------------------------------------------------------\n"); fflush(stdout);
// 32 max
generate_itemTexture_2D(0, "res/white.png", PNG);
printf("-----------------------------------------------------------------------------------------------\n"); fflush(stdout); printf("-----------------------------------------------------------------------------------------------\n"); fflush(stdout);
// ---------------------------------------------------------------------------------------------------------------------------------------------- // // ---------------------------------------------------------------------------------------------------------------------------------------------- //
@ -745,6 +754,7 @@ int main_alt() {
glUseProgram(shaderProgramR); glUseProgram(shaderProgramR);
glBindVertexArray(RVAO); glBindVertexArray(RVAO);
glBindTexture(GL_TEXTURE_2D, itemTexts[0]);
gl_drawInteger(shaderProgramR, (int)(1.0f/(real_T)), 0.0f, -0.92f, 0.05, 32, 255, 32, 0.005, -1); gl_drawInteger(shaderProgramR, (int)(1.0f/(real_T)), 0.0f, -0.92f, 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_drawInteger(shaderProgramR, triCount, 0.0f, 0.92f, 0.04f, 128, 128, 128, 0.005f, 1);
gl_drawData(shaderProgramR); gl_drawData(shaderProgramR);
@ -837,6 +847,7 @@ int main_alt() {
free_ent_generator(); free_ent_generator();
printf("Done\n"); fflush(stdout); printf("Done\n"); fflush(stdout);
free_maeth(); free_maeth();
inv_destroy();
free(F5_renderer); free(F5_renderer);
// optional: de-allocate all resources once they've outlived their purpose: // optional: de-allocate all resources once they've outlived their purpose:

View File

@ -193,6 +193,7 @@ extern int loc_tex;
extern bool is_one_room; extern bool is_one_room;
extern unsigned int textures[32]; extern unsigned int textures[32];
extern unsigned int itemTexts[32];
extern double choffx; extern double choffx;
extern double choffz; extern double choffz;