basic inventory (doesn't save)

This commit is contained in:
onezplpl 2024-07-19 21:14:40 -04:00
parent 2ce46f188b
commit 8ccfe726fc
No known key found for this signature in database
GPG key ID: 7EC026A136F9EEC3
6 changed files with 87 additions and 29 deletions

View file

@ -1,8 +1,8 @@
#include "cubes.h"
#include "gen.h"
#include "inv.h"
#include "linmath.h"
#include <GLFW/glfw3.h>
#include <stdio.h>
int is_space;
@ -66,9 +66,9 @@ struct v3f control_handler(double cx, double cy, vec3 pos, vec3 dir_temp,
for (int a = -2; a < 0; a++) {
off[1] = a;
for (int b = -1; b < 1; b++) {
off[0] = (b + 0.5)*0.95;
off[0] = (b + 0.5) * 0.95;
for (int c = -1; c < 1; c++) {
off[2] = (c + 0.5)*0.95;
off[2] = (c + 0.5) * 0.95;
col = col || gen_cube(pos[0] + off[0], pos[1] + off[1], pos[2] + off[2],
chunk, 0, -1);
}
@ -82,9 +82,9 @@ struct v3f control_handler(double cx, double cy, vec3 pos, vec3 dir_temp,
off[1] = -2;
for (int b = -1; b < 1; b++) {
off[0] = (b + 0.5)*0.95;
off[0] = (b + 0.5) * 0.95;
for (int c = -1; c < 1; c++) {
off[2] = (c + 0.5)*0.95;
off[2] = (c + 0.5) * 0.95;
col = col || gen_cube(pos[0] + off[0], pos[1] + off[1], pos[2] + off[2],
chunk, 0, -1);
}
@ -152,7 +152,7 @@ struct v3f control_rtrace(double cx, double cy, vec3 pos, int mode) {
return out_pos;
}
void control_lclick(double cx, double cy, vec3 pos) {
void control_lclick(double cx, double cy, vec3 pos, struct item *inv) {
struct v3f highlighted = control_rtrace(cx, cy, pos, 1);
struct chunk placeChunk;
int cube = gen_cube(highlighted.pos[0], highlighted.pos[1],
@ -163,16 +163,39 @@ void control_lclick(double cx, double cy, vec3 pos) {
return;
gen_cube(highlighted.pos[0], highlighted.pos[1], highlighted.pos[2],
placeChunk, 0, 0);
for (int i = 0; i < 10; i++) {
if (inv[i].id == 0) {
inv[i].id = cube;
inv[i].count = 0;
}
if (inv[i].id == cube) {
inv[i].count++;
break;
}
}
}
void control_rclick(double cx, double cy, vec3 pos) {
void control_rclick(double cx, double cy, vec3 pos, struct item *inv, int slot) {
struct v3f highlighted = control_rtrace(cx, cy, pos, 0);
struct chunk placeChunk;
int cube = gen_cube(highlighted.pos[0], highlighted.pos[1],
highlighted.pos[2], placeChunk, 0, -1);
if (cube != 0 ||
(fabs(highlighted.pos[0] - pos[0]) < 1 && fabs(highlighted.pos[1] - pos[1] + 0.5) < 1.5 && fabs(highlighted.pos[2] - pos[2]) < 1))
if (cube != 0 || (fabs(highlighted.pos[0] - pos[0]) < 1 &&
fabs(highlighted.pos[1] - pos[1] + 0.5) < 1.5 &&
fabs(highlighted.pos[2] - pos[2]) < 1))
return;
gen_cube(highlighted.pos[0], highlighted.pos[1], highlighted.pos[2],
placeChunk, 0, 1);
placeChunk, 0, inv[slot].id);
for (int i = 0; i < 10; i++) {
if (inv[i].id == inv[slot].id) {
inv[i].count--;
if (inv[i].count == 0) {
inv[i].id = 0;
inv[i].count = 0;
}
break;
}
}
}

View file

@ -2,9 +2,11 @@
#define V_CONTROL_H
#include "cubes.h"
#include "linmath.h"
#include "inv.h"
#include <GLFW/glfw3.h>
struct v3f control_handler(double cx, double cy, vec3 pos, vec3 dir_temp,
GLFWwindow *window, struct chunk chunk);
void control_lclick(double cx, double cy, vec3 pos);
void control_rclick(double cx, double cy, vec3 pos);
void control_lclick(double cx, double cy, vec3 pos, struct item *inv);
void control_rclick(double cx, double cy, vec3 pos, struct item *inv, int slot);
#endif

View file

@ -7,7 +7,7 @@
#define CHUNK_RADIUS_V 4 // 4
#define TEXT_GAP_H 1 / 6.0
#define TEXT_GAP_V 1 / 7.0
#define TEXT_GAP_V 1 / 9.0
#define CHUNK_DIAMETER_H (CHUNK_RADIUS_H * 2 + 1)
#define CHUNK_DIAMETER_V (CHUNK_RADIUS_V * 2 + 1)
@ -15,7 +15,7 @@
#define BLOCK_ALL CHUNK_LENGTH *CHUNK_LENGTH *CHUNK_LENGTH
#define CBLOCK_ALL BLOCK_ALL *CHUNK_ALL
#define CBUF_ALL 2
#define UI_ALL 1024
#define UI_ALL 20480
#define CTRI_ALL CBLOCK_ALL * 18 * sizeof(struct v3f)

8
inv.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef V_INV_H
#define V_INV_H
struct item {
int id;
int count;
};
#endif

55
main.c
View file

@ -13,6 +13,7 @@
#include "cubes.h"
#include "gen.h"
#include "gl.h"
#include "inv.h"
#include "linmath.h"
#include "stb_image.h"
@ -52,6 +53,12 @@ static const char *fragment_shader_text =
"}\n";
int isCursor = 0;
double cx = 0;
double cy = 0;
int fr = 0;
vec3 pos;
struct item *inv;
int slot;
static void key_callback(GLFWwindow *window, int key, int scancode, int action,
int mods) {
@ -63,22 +70,23 @@ static void key_callback(GLFWwindow *window, int key, int scancode, int action,
glfwSetInputMode(window, GLFW_CURSOR,
isCursor ? GLFW_CURSOR_NORMAL : GLFW_CURSOR_DISABLED);
}
if (key == GLFW_KEY_Q && action == GLFW_PRESS) {
slot = (slot + 9) % 10;
}
if (key == GLFW_KEY_E && action == GLFW_PRESS) {
slot = (slot + 1) % 10;
}
}
double cx = 0;
double cy = 0;
int fr = 0;
vec3 pos;
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);
control_rclick(cx, cy, pos, inv, slot);
}
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
fr = 1;
control_lclick(cx, cy, pos);
control_lclick(cx, cy, pos, inv);
}
}
@ -128,6 +136,8 @@ void *render_chunks(void *args) {
#define WinMain main
int main(void) {
inv = calloc(10, sizeof(struct item));
if (!glfwInit())
exit(EXIT_FAILURE);
@ -359,12 +369,27 @@ int main(void) {
cubes_rect(textO, &iz, -0.5, 0.5, -0.5, 0.5, 1, 0, 6);
for (int i = 0; i < 10; i++) {
for (int x = 0; x < 3; x++) {
int dig = (int)(inv[i].count / (pow(10, x))) % 10;
dig = (dig + 9) % 10;
iz += 6;
cubes_rect(cubeO, &iz, ((i - 4.5) * 2.5) + 0.5 - ((x + 1) * 0.5),
((i - 4.5) * 2.5) + 1 - ((x + 1) * 0.5), -10, -10.5, 0, 0,
0);
cubes_rect(textO, &iz, 0, 0, 0, 0, 1, (dig + 3) % 6, (dig + 3) / 6 + 6);
}
iz += 6;
cubes_rect(cubeO, &iz, ((i - 4.5) * 2.5) - 1, ((i - 4.5) * 2.5) + 1, -11,
-9, 0, 1, 6);
cubes_rect(textO, &iz, ((i - 4.5) * 2.5) - 1, ((i - 4.5) * 2.5) + 1, -11,
-9, 1, 1, 6);
-9, 0, 0, 0);
cubes_rect(textO, &iz, 0, 0, 0, 0, 1, (slot == i) ? 1 : 2, 6);
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);
}
iz += 6;
mat4x4_identity(v);
mat4x4_ortho(p, -ratio * 15, ratio * 15, -1.f * 15, 1.f * 15, 1.0f, -1.0f);
@ -374,15 +399,15 @@ int main(void) {
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat *)&mvp);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[1]);
glBufferSubData(GL_ARRAY_BUFFER, CTRI_ALL,
(long)6 * 11 * sizeof(struct v3f), &textO[iz2]);
glBufferSubData(GL_ARRAY_BUFFER, CTRI_ALL, (iz - iz2) * sizeof(struct v3f),
&textO[iz2]);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]);
glBufferSubData(GL_ARRAY_BUFFER, CTRI_ALL,
(long)6 * 11 * sizeof(struct v3f), &cubeO[iz2]);
glBufferSubData(GL_ARRAY_BUFFER, CTRI_ALL, (iz - iz2) * sizeof(struct v3f),
&cubeO[iz2]);
glBindVertexArray(vertex_array);
glDrawArrays(GL_TRIANGLES, CBLOCK_ALL * 18, 6 * 11);
glDrawArrays(GL_TRIANGLES, CBLOCK_ALL * 18, (iz - iz2));
}
if (fp) {

BIN
test.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.9 KiB

After

Width:  |  Height:  |  Size: 9.5 KiB

Before After
Before After