From 5496a5d47e4574770d532602ec8f4e4ceba218ec Mon Sep 17 00:00:00 2001 From: onezplpl Date: Sat, 20 Jul 2024 16:52:03 -0400 Subject: [PATCH] save inventory --- inv.h | 5 +++++ main.c | 23 +++++++++++------------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/inv.h b/inv.h index 94b35b9..0bdac80 100644 --- a/inv.h +++ b/inv.h @@ -1,8 +1,13 @@ #ifndef V_INV_H #define V_INV_H +#include "linmath.h" struct item { int id; int count; }; +struct player { + struct item inv[10]; + vec3 pos; +}; #endif \ No newline at end of file diff --git a/main.c b/main.c index 264786b..f70d13c 100644 --- a/main.c +++ b/main.c @@ -56,8 +56,7 @@ int isCursor = 0; double cx = 0; double cy = 0; int fr = 0; -vec3 pos; -struct item *inv; +struct player player; int slot; static void key_callback(GLFWwindow *window, int key, int scancode, int action, @@ -82,11 +81,11 @@ void mouse_button_callback(GLFWwindow *window, int button, int action, int mods) { if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) { fr = 1; - control_rclick(cx, cy, pos, inv, slot); + control_rclick(cx, cy, player.pos, player.inv, slot); } if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) { fr = 1; - control_lclick(cx, cy, pos, inv); + control_lclick(cx, cy, player.pos, player.inv); } } @@ -136,8 +135,6 @@ void *render_chunks(void *args) { #define WinMain main int main(void) { - inv = calloc(10, sizeof(struct item)); - if (!glfwInit()) exit(EXIT_FAILURE); @@ -215,12 +212,14 @@ int main(void) { glfwSetMouseButtonCallback(window, mouse_button_callback); glfwSetCursorPosCallback(window, cursor_position_callback); + float *pos = player.pos; + pos[0] = 10240; pos[1] = 48; pos[2] = 10240; - FILE *fp = fopen("./db/position.dat", "rb"); - int code = (fp) ? fread(&pos, sizeof(struct v3f), 1, fp) : 0; + FILE *fp = fopen("./db/player.dat", "rb"); + int code = (fp) ? fread(&player, sizeof(struct player), 1, fp) : 0; int cube_count = cubes_vert(cube, text, cubeO, textO, pos[0] / CHUNK_LENGTH, @@ -370,7 +369,7 @@ int main(void) { for (int i = 0; i < 10; i++) { for (int x = 0; x < 3; x++) { - int dig = (int)(inv[i].count / (pow(10, x))) % 10; + int dig = (int)(player.inv[i].count / (pow(10, x))) % 10; dig = (dig + 9) % 10; @@ -387,7 +386,7 @@ int main(void) { iz += 6; cubes_rect(cubeO, &iz, ((i - 4.5) * 2.5) - 0.5, ((i - 4.5) * 2.5) + 0.5, -10.5, -9.5, 0, 0, 0); - cubes_rect(textO, &iz, 0, 0, 0, 0, 1, inv[i].id - 1, 0); + cubes_rect(textO, &iz, 0, 0, 0, 0, 1, player.inv[i].id - 1, 0); } iz += 6; @@ -413,9 +412,9 @@ int main(void) { if (fp) { fclose(fp); } - fp = fopen("./db/position.dat", "wb"); + fp = fopen("./db/player.dat", "wb"); if (fp) { - fwrite(&pos, sizeof(struct v3f), 1, fp); + fwrite(&player, sizeof(struct player), 1, fp); fclose(fp); } free(cube);