]> git.lizzy.rs Git - dragonblocks_alpha.git/commitdiff
Add HUD and crosshair
authorElias Fleckenstein <eliasfleckenstein@web.de>
Mon, 12 Jul 2021 15:43:08 +0000 (17:43 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Mon, 12 Jul 2021 15:43:08 +0000 (17:43 +0200)
src/Makefile
src/camera.c
src/camera.h
src/client.c
src/hud.c [new file with mode: 0644]
src/hud.h [new file with mode: 0644]
textures/crosshair.png [new file with mode: 0644]

index 519c5f63568a4500ae1e5af4b45ee66dd9681c9d..cdb6c042338b5d0f2802d695aa5e08dff34a5786 100644 (file)
@@ -1,6 +1,6 @@
 COMMON = array.o bintree.o list.o map.o signal.o util.o types.o node.o queue.o
 SERVER = $(COMMON) server.o servercommands.o servermap.o perlin.o facecache.o mapgen.o mapdb.o
-CLIENT = $(COMMON) camera.o client.o clientcommands.o clientmap.o clientnode.o clientplayer.o cube.o input.o mesh.o scene.o shaders.o blockmesh.o texture.o
+CLIENT = $(COMMON) camera.o client.o clientcommands.o clientmap.o clientnode.o clientplayer.o cube.o hud.o input.o mesh.o scene.o shaders.o blockmesh.o texture.o
 LIBRARIES = -lpthread -lm -lz
 FLAGS = -g -fmax-errors=4
 
index f534ff307ff16a0d473efb685df63e03af6bb6f7..4ce9f3b655bac9c94d442f6ea81cae43460e5c28 100644 (file)
@@ -21,7 +21,12 @@ static void update_camera()
        vec3_add(center, camera.eye, camera.front);
 
        mat4x4_look_at(camera.view, camera.eye, center, camera.up);
+}
+
+void camera_enable()
+{
        glUniformMatrix4fv(camera.prog->loc_view, 1, GL_FALSE, camera.view[0]);
+       glUniformMatrix4fv(camera.prog->loc_projection, 1, GL_FALSE, camera.projection[0]);
 }
 
 void set_camera_position(v3f pos)
@@ -58,7 +63,6 @@ void set_camera_angle(f32 yaw, f32 pitch)
 void set_window_size(int width, int height)
 {
        mat4x4_perspective(camera.projection, 86.1f / 180.0f * M_PI, (float) width / (float) height, 0.01f, 1000.0f);
-       glUniformMatrix4fv(camera.prog->loc_projection, 1, GL_FALSE, camera.projection[0]);
 }
 
 static void framebuffer_size_callback(__attribute__((unused)) GLFWwindow* window, int width, int height)
index d0725a6e42c6c8a8fd935fd5403428b6b5f5bbf4..b03b1ab6503285b44426787680542912d1220469 100644 (file)
@@ -11,6 +11,7 @@ void init_camera(GLFWwindow *window, ShaderProgram *prog);
 void set_camera_position(v3f pos);
 void set_camera_angle(f32 yaw, f32 pitch);
 void set_window_size(int width, int height);
+void camera_enable();
 
 extern struct movement_dirs
 {
index 61349813669023c2afcf98daa1dc0d8fdec162c9..230f729b5655e45c9249ed0f6a7aac2cffbc9292 100644 (file)
@@ -13,6 +13,8 @@
 #include "client.h"
 #include "clientmap.h"
 #include "clientnode.h"
+#include "cube.h"
+#include "hud.h"
 #include "input.h"
 #include "signal.h"
 #include "shaders.h"
@@ -101,6 +103,11 @@ static void client_loop()
 
        clientplayer_add_to_scene(&client.player);
 
+       hud_init(prog);
+       hud_rescale(width, height);
+
+       hud_add(RESSOURCEPATH "textures/crosshair.png", (v2f) {0.0f, 0.0f}, (v2f) {1.0f, 1.0f});
+
        struct timespec ts, ts_old;
        clock_gettime(CLOCK_REALTIME, &ts_old);
 
@@ -116,13 +123,17 @@ static void client_loop()
                process_input();
                clientplayer_tick(&client.player, dtime);
 
+               camera_enable();
                scene_render(client.scene, prog);
 
+               hud_render();
+
                glfwSwapBuffers(window);
                glfwPollEvents();
        }
 
        delete_shader_program(prog);
+       hud_deinit();
 }
 
 static bool client_name_prompt()
diff --git a/src/hud.c b/src/hud.c
new file mode 100644 (file)
index 0000000..9535d5c
--- /dev/null
+++ b/src/hud.c
@@ -0,0 +1,101 @@
+#include <string.h>
+#include <stdlib.h>
+#include <GL/glew.h>
+#include <GL/gl.h>
+#include "client.h"
+#include "cube.h"
+#include "hud.h"
+
+static struct
+{
+       MeshObject *obj;
+       List elements;
+       ShaderProgram *prog;
+       mat4x4 view, projection;
+       int width, height;
+} hud;
+
+void hud_init(ShaderProgram *prog)
+{
+       hud.prog = prog;
+       hud.elements = list_create(NULL);
+
+       Texture *texture = get_texture(RESSOURCEPATH "textures/invalid.png");
+       VertexBuffer buffer = vertexbuffer_create();
+       vertexbuffer_set_texture(&buffer, texture);
+
+       for (int v = 0; v < 6; v++) {
+               Vertex vertex = cube_vertices[0][v];
+               vertex.z = 0.0f;
+               vertexbuffer_add_vertex(&buffer, &vertex);
+       }
+
+       hud.obj = meshobject_create(buffer, NULL, (v3f) {0.0f, 0.0f, 0.0f});
+
+       mat4x4_identity(hud.view);
+}
+
+static void free_element(void *key, __attribute__((unused)) void *value, __attribute__((unused)) void *arg)
+{
+       free(key);
+}
+
+void hud_deinit()
+{
+       meshobject_delete(hud.obj);
+       list_clear_func(&hud.elements, &free_element, NULL);
+}
+
+static void element_transform(HUDElement *element)
+{
+       mat4x4_translate(element->transform, (1.0f + element->pos.x) / 2.0f * (f32) hud.width, (1.0f + element->pos.y) / 2.0f * (f32) hud.height, 0.0f);
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wpedantic"
+       mat4x4_scale_aniso(element->transform, element->transform, element->scale.x, element->scale.y, 1.0f);
+#pragma GCC diagnostic pop
+}
+
+void hud_rescale(int width, int height)
+{
+       hud.width = width;
+       hud.height = height;
+       mat4x4_ortho(hud.projection, 0, width, height, 0, -1.0f, 1.0f);
+
+       for (ListPair *pair = hud.elements.first; pair != NULL; pair = pair->next)
+               element_transform(pair->key);
+}
+
+void hud_render()
+{
+       glDisable(GL_DEPTH_TEST);
+
+       glUniformMatrix4fv(hud.prog->loc_view, 1, GL_FALSE, hud.view[0]);
+       glUniformMatrix4fv(hud.prog->loc_projection, 1, GL_FALSE, hud.projection[0]);
+
+       for (ListPair *pair = hud.elements.first; pair != NULL; pair = pair->next) {
+               HUDElement *element = pair->key;
+
+               if (element->visible) {
+                       memcpy(hud.obj->transform, element->transform, sizeof(mat4x4));
+                       hud.obj->meshes[0]->texture = element->texture->id;
+                       meshobject_render(hud.obj, hud.prog);
+               }
+       }
+}
+
+HUDElement *hud_add(char *texture, v2f pos, v2f scale)
+{
+       HUDElement *element = malloc(sizeof(HUDElement));
+       element->texture = get_texture(texture);
+       element->visible = true;
+       element->pos = pos;
+
+       element->scale = scale;
+       element->scale.x *= (f32) element->texture->width;
+       element->scale.y *= (f32) element->texture->height;
+
+       element_transform(element);
+
+       list_set(&hud.elements, element, NULL);
+       return element;
+}
diff --git a/src/hud.h b/src/hud.h
new file mode 100644 (file)
index 0000000..b963dee
--- /dev/null
+++ b/src/hud.h
@@ -0,0 +1,25 @@
+#ifndef _HUD_H_
+#define _HUD_H_
+
+#include <stdbool.h>
+#include <linmath.h/linmath.h>
+#include "shaders.h"
+#include "texture.h"
+#include "types.h"
+
+typedef struct
+{
+       Texture *texture;
+       bool visible;
+       v2f pos;
+       v2f scale;
+       mat4x4 transform;
+} HUDElement;
+
+void hud_init(ShaderProgram *prog);
+void hud_deinit();
+void hud_rescale(int width, int height);
+void hud_render();
+HUDElement *hud_add(char *texture, v2f pos, v2f scale);
+
+#endif
diff --git a/textures/crosshair.png b/textures/crosshair.png
new file mode 100644 (file)
index 0000000..8e94dcc
Binary files /dev/null and b/textures/crosshair.png differ