save inventory

This commit is contained in:
onezplpl 2024-07-20 16:52:03 -04:00
parent 8ccfe726fc
commit 5496a5d47e
No known key found for this signature in database
GPG key ID: 7EC026A136F9EEC3
2 changed files with 16 additions and 12 deletions

5
inv.h
View file

@ -1,8 +1,13 @@
#ifndef V_INV_H #ifndef V_INV_H
#define V_INV_H #define V_INV_H
#include "linmath.h"
struct item { struct item {
int id; int id;
int count; int count;
}; };
struct player {
struct item inv[10];
vec3 pos;
};
#endif #endif

23
main.c
View file

@ -56,8 +56,7 @@ int isCursor = 0;
double cx = 0; double cx = 0;
double cy = 0; double cy = 0;
int fr = 0; int fr = 0;
vec3 pos; struct player player;
struct item *inv;
int slot; int slot;
static void key_callback(GLFWwindow *window, int key, int scancode, int action, 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) { int mods) {
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) { if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) {
fr = 1; 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) { if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
fr = 1; 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 #define WinMain main
int main(void) { int main(void) {
inv = calloc(10, sizeof(struct item));
if (!glfwInit()) if (!glfwInit())
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -215,12 +212,14 @@ int main(void) {
glfwSetMouseButtonCallback(window, mouse_button_callback); glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetCursorPosCallback(window, cursor_position_callback); glfwSetCursorPosCallback(window, cursor_position_callback);
float *pos = player.pos;
pos[0] = 10240; pos[0] = 10240;
pos[1] = 48; pos[1] = 48;
pos[2] = 10240; pos[2] = 10240;
FILE *fp = fopen("./db/position.dat", "rb"); FILE *fp = fopen("./db/player.dat", "rb");
int code = (fp) ? fread(&pos, sizeof(struct v3f), 1, fp) : 0; int code = (fp) ? fread(&player, sizeof(struct player), 1, fp) : 0;
int cube_count = int cube_count =
cubes_vert(cube, text, cubeO, textO, pos[0] / CHUNK_LENGTH, 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 i = 0; i < 10; i++) {
for (int x = 0; x < 3; x++) { 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; dig = (dig + 9) % 10;
@ -387,7 +386,7 @@ int main(void) {
iz += 6; iz += 6;
cubes_rect(cubeO, &iz, ((i - 4.5) * 2.5) - 0.5, ((i - 4.5) * 2.5) + 0.5, 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); -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; iz += 6;
@ -413,9 +412,9 @@ int main(void) {
if (fp) { if (fp) {
fclose(fp); fclose(fp);
} }
fp = fopen("./db/position.dat", "wb"); fp = fopen("./db/player.dat", "wb");
if (fp) { if (fp) {
fwrite(&pos, sizeof(struct v3f), 1, fp); fwrite(&player, sizeof(struct player), 1, fp);
fclose(fp); fclose(fp);
} }
free(cube); free(cube);