summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpryazha <pryadeiniv@mail.ru>2025-02-20 15:10:11 +0500
committerpryazha <pryadeiniv@mail.ru>2025-02-20 15:10:11 +0500
commit34821e9fefb0d7cbf9e72a2457b2901edbbe03bb (patch)
tree59ec69f7d19aa48091b3d39ad0dfd6b26bacb88b
initial
-rw-r--r--prge.h17
-rw-r--r--prge_input.c45
-rw-r--r--prge_input.h10
-rw-r--r--prge_mesh.c65
-rw-r--r--prge_mesh.h9
-rw-r--r--prge_shader.c65
-rw-r--r--prge_shader.h8
-rw-r--r--prge_types.h50
8 files changed, 269 insertions, 0 deletions
diff --git a/prge.h b/prge.h
new file mode 100644
index 0000000..066f1f5
--- /dev/null
+++ b/prge.h
@@ -0,0 +1,17 @@
+#ifndef PRGE_H
+#define PRGE_H
+
+#include "prb.h"
+
+#include "prge_types.h"
+
+#include "prge_input.h"
+#include "prge_input.c"
+
+#include "prge_shader.h"
+#include "prge_shader.c"
+
+#include "prge_mesh.h"
+#include "prge_mesh.c"
+
+#endif /* PRGE_H */
diff --git a/prge_input.c b/prge_input.c
new file mode 100644
index 0000000..e295ebc
--- /dev/null
+++ b/prge_input.c
@@ -0,0 +1,45 @@
+Input input_init()
+{
+ Input input = {0};
+ input.first_mouse = 1;
+ input.is_running = 1;
+ return(input);
+}
+
+void input_update(Input *input)
+{
+ input->mouse_offset = V2_ZERO;
+
+ input->move_right.last = input->move_right.state;
+ input->move_forward.last = input->move_forward.state;
+ input->move_left.last = input->move_left.state;
+ input->move_backward.last = input->move_backward.state;
+ input->move_up.last = input->move_up.state;
+ input->move_down.last = input->move_down.state;
+ input->jump.last = input->jump.state;
+ input->action_right.last = input->action_right.state;
+ input->action_up.last = input->action_up.state;
+ input->action_left.last = input->action_left.state;
+ input->action_down.last = input->action_down.state;
+ input->exit.last = input->exit.state;
+}
+
+B32 key_is_pressed(Key key)
+{
+ B32 result = (key.state == KeyState_PRESS);
+ return(result);
+}
+
+B32 key_first_press(Key key)
+{
+ B32 result = ((key.last == KeyState_RELEASE) &&
+ (key.state == KeyState_PRESS));
+ return(result);
+}
+
+B32 key_was_pressed(Key key)
+{
+ B32 result = ((key.last == KeyState_PRESS) &&
+ (key.state == KeyState_RELEASE));
+ return(result);
+}
diff --git a/prge_input.h b/prge_input.h
new file mode 100644
index 0000000..a5a77c7
--- /dev/null
+++ b/prge_input.h
@@ -0,0 +1,10 @@
+#ifndef PRGE_INPUT_H
+#define PRGE_INPUT_H
+
+Input input_init();
+void input_update(Input *input);
+B32 key_is_pressed(Key key);
+B32 key_first_press(Key key);
+B32 key_was_pressed(Key key);
+
+#endif /* PRGE_INPUT_H */
diff --git a/prge_mesh.c b/prge_mesh.c
new file mode 100644
index 0000000..dc2c2da
--- /dev/null
+++ b/prge_mesh.c
@@ -0,0 +1,65 @@
+Vertex vertex(V3 pos)
+{
+ Vertex v = { pos };
+ return v;
+}
+
+void mesh_init_buffers(Mesh *mesh)
+{
+ Assert(mesh->verts);
+ Assert(mesh->nverts > 0);
+
+ glGenVertexArrays(1, &mesh->vao);
+ glBindVertexArray(mesh->vao);
+
+ glGenBuffers(1, &mesh->vbo);
+ glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo);
+ glBufferData(GL_ARRAY_BUFFER, mesh->nverts*sizeof(Vertex), mesh->verts, GL_STATIC_DRAW);
+
+ if (mesh->indices && (mesh->nindices > 0)) {
+ glGenBuffers(1, &mesh->ebo);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->ebo);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh->nindices*sizeof(U32), mesh->indices, GL_STATIC_DRAW);
+ }
+
+ glEnableVertexAttribArray(PRGE_SHADER_POS_LOC);
+ glVertexAttribPointer(PRGE_SHADER_POS_LOC, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
+ glBindVertexArray(0);
+}
+
+Mesh *mesh(Arena *arena, Vertex *verts, U32 nverts, U32 *indices, U32 nindices)
+{
+ Mesh *mesh = arena_push(arena, sizeof(Mesh));
+ mesh->verts = verts;
+ mesh->nverts = nverts;
+ mesh->indices = indices;
+ mesh->nindices = nindices;
+ mesh_init_buffers(mesh);
+ return mesh;
+}
+
+void mesh_clear(Mesh *mesh)
+{
+ glBindVertexArray(mesh->vao);
+ glDisableVertexAttribArray(PRGE_SHADER_POS_LOC);
+
+ glBindVertexArray(0);
+ glDeleteVertexArrays(1, &mesh->vao);
+
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ glDeleteBuffers(1, &mesh->vbo);
+
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+ glDeleteBuffers(1, &mesh->ebo);
+}
+
+void mesh_draw(Mesh mesh)
+{
+ Assert(mesh.vao);
+ glBindVertexArray(mesh.vao);
+ if (mesh.ebo)
+ glDrawElements(GL_TRIANGLES, mesh.nindices, GL_UNSIGNED_INT, 0);
+ else
+ glDrawArrays(GL_TRIANGLES, 0, mesh.nverts);
+ glBindVertexArray(0);
+}
diff --git a/prge_mesh.h b/prge_mesh.h
new file mode 100644
index 0000000..1f70809
--- /dev/null
+++ b/prge_mesh.h
@@ -0,0 +1,9 @@
+#ifndef PRGE_MESH_H
+#define PRGE_MESH_H
+
+#define PRGE_SHADER_POS_LOC 0
+
+Vertex vertex(V3 pos);
+Mesh *mesh(Arena *arena, Vertex *verts, U32 nverts, U32 *indices, U32 nindices);
+
+#endif /* PRGE_MESH_H */
diff --git a/prge_shader.c b/prge_shader.c
new file mode 100644
index 0000000..21a91f3
--- /dev/null
+++ b/prge_shader.c
@@ -0,0 +1,65 @@
+U32 compile_shader(GLenum type, Str8 filename)
+{
+ Arena *tmp = arena_alloc(0);
+
+ Str8 src = str8_read_entire_file(tmp, filename);
+ if (!src.ptr || !src.length) {
+ str8print(str8_pushf(tmp, "[ERROR] : Failed to read \"%.*s\"\n", str8expand(filename)));
+ arena_release(tmp);
+ return 0;
+ }
+
+ const char *csrc = str8_to_cstr(tmp, src);
+
+ U32 id = glCreateShader(type);
+ glShaderSource(id, 1, &csrc, 0);
+ glCompileShader(id);
+
+ S32 status;
+ glGetShaderiv(id, GL_COMPILE_STATUS, &status);
+ if (status == GL_FALSE) {
+ char log[512];
+ glGetShaderInfoLog(id, 512, 0, log);
+ str8print(str8_pushf(tmp, "[ERROR] : Failed to compile : \"%.*s\"\n%s", str8expand(filename), log));
+ } else {
+ str8print(str8_pushf(tmp, "[INFO] : \"%.*s\" compiled successfully.\n", str8expand(filename)));
+ }
+
+ arena_release(tmp);
+
+ return id;
+}
+
+U32 create_shader_program(Str8 vert_filename, Str8 frag_filename)
+{
+ U32 vert = compile_shader(GL_VERTEX_SHADER, vert_filename);
+ U32 frag = compile_shader(GL_FRAGMENT_SHADER, frag_filename);
+
+ Arena *tmp = arena_alloc(0);
+
+ U32 id = glCreateProgram();
+ glAttachShader(id, vert);
+ glAttachShader(id, frag);
+ glLinkProgram(id);
+ S32 success;
+ glGetProgramiv(id, GL_LINK_STATUS, &success);
+ if (success == GL_FALSE) {
+ char log[512];
+ glGetProgramInfoLog(id, 512, 0, log);
+ str8print(str8_pushf(tmp, "[ERROR]: Failed to link shader program:\n%s", log));
+ } else {
+ str8print(str8_pushf(tmp, "[INFO]: Shader program linked successfuly.\n\n"));
+ }
+ glDeleteShader(vert);
+ glDeleteShader(frag);
+
+ arena_release(tmp);
+
+ return id;
+}
+
+void shader_set_mat4fv(U32 id, char *name, Mat4 m)
+{
+ S32 loc = glGetUniformLocation(id, name);
+ glUniformMatrix4fv(loc, 1, 0, (F32 *)&m);
+}
diff --git a/prge_shader.h b/prge_shader.h
new file mode 100644
index 0000000..1e065a5
--- /dev/null
+++ b/prge_shader.h
@@ -0,0 +1,8 @@
+#ifndef PRGE_SHADER_H
+#define PRGE_SHADER_H
+
+U32 compile_shader(GLenum type, Str8 filename);
+U32 create_shader_program(Str8 vert_filename, Str8 frag_filename);
+void shader_set_mat4fv(U32 id, char *name, Mat4 m);
+
+#endif /* PRGE_SHADER_H */
diff --git a/prge_types.h b/prge_types.h
new file mode 100644
index 0000000..ff5b860
--- /dev/null
+++ b/prge_types.h
@@ -0,0 +1,50 @@
+#ifndef PRGE_TYPES_H
+#define PRGE_TYPES_H
+
+enum KeyState_Enum {
+ KeyState_RELEASE = 0,
+ KeyState_PRESS = 1
+};
+
+typedef struct {
+ enum KeyState_Enum last;
+ enum KeyState_Enum state;
+} Key;
+
+typedef struct {
+ V2 mouse_pos;
+ V2 last_mouse_pos;
+ V2 mouse_offset;
+ B32 first_mouse;
+
+ B32 is_running;
+
+ F32 dt;
+
+ Key move_right;
+ Key move_forward;
+ Key move_left;
+ Key move_backward;
+ Key move_up;
+ Key move_down;
+ Key jump;
+ Key action_right;
+ Key action_up;
+ Key action_left;
+ Key action_down;
+ Key exit;
+} Input;
+
+typedef struct {
+ V3 pos;
+} Vertex;
+
+typedef struct {
+ Vertex *verts;
+ U32 nverts;
+ U32 *indices;
+ U32 nindices;
+ U32 vao, vbo, ebo;
+} Mesh;
+
+#endif /* PRGE_TYPES_H */