Basic Unicode support, fix more Chunk bugs, add asserts

This commit is contained in:
2025-06-08 08:15:00 +02:00
parent d2ef7fc8de
commit 7f6b2f530c
12 changed files with 2065 additions and 47 deletions
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -9,5 +9,5 @@ varying vec2 f_uv;
void main ()
{
gl_FragColor = vec4 (color.xyz, 1.0);
gl_FragColor = color;
}
+1 -1
View File
@@ -11,7 +11,7 @@ varying vec2 f_uv;
void main ()
{
float tex_w = 128.0;
float tex_h = 128.0;
float tex_h = 256.0;
float char_w = 8.0;
float char_h = 16.0;
Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.
+6 -11
View File
@@ -117,12 +117,6 @@ chunkman_tick (vec3 position)
{
for (int x = cx - render_distance - 1; x <= cx + render_distance + 1; ++x)
{
/* Check in a circle instead of a square. */
if ((x - cx) * (x - cx) + (z - cz) * (z - cz) > (render_distance+1)*(render_distance+1))
{
continue;
}
uint64_t now = SDL_GetPerformanceCounter ();
elapsed_us = (double) (now - start) * 1000000.0 / (double) freq;
@@ -142,11 +136,6 @@ chunkman_tick (vec3 position)
{
for (int x = cx - render_distance; x <= cx + render_distance; ++x)
{
if ((x - cx) * (x - cx) + (z - cz) * (z - cz) > render_distance*render_distance)
{
continue;
}
uint64_t now = SDL_GetPerformanceCounter ();
elapsed_us = (double) (now - start) * 1000000.0 / (double) freq;
@@ -192,6 +181,7 @@ chunkman_render (vec3 position, vec4 planes[6])
{
for (int x = cx - render_distance; x <= cx + render_distance; ++x)
{
/* Render in a circle instead of a square. */
if ((x - cx) * (x - cx) + (z - cz) * (z - cz) > render_distance*render_distance)
{
continue;
@@ -309,6 +299,11 @@ chunkman_generate_ChunkMesh_at (int x, int z)
{
Chunk *nr, *nd, *nc;
chunkman_get_Chunk_neighbors_at (x, z, &nr, &nd, &nc);
g_assert (nr != NULL);
g_assert (nd != NULL);
g_assert (nc != NULL);
chunkman_set_ChunkMesh_at
(x, z, ChunkMesh_build (chunkman_get_Chunk_at (x, z), nr, nd, nc));
}
+1 -5
View File
@@ -1,9 +1,5 @@
#include "chunkmesh.h"
/* TODO: Store the position in an ivec2, and the height as a seperate
vertex attribute and as a float. Should be a bit faster. Since the
materials are ints, we could use a ivec4 there as well. */
ChunkMesh *
ChunkMesh_build (Chunk *chunk, Chunk *nr, Chunk *nd, Chunk *nc)
/* nd --- down neighbor; nr --- right neighbor; nc --- corner neighbor */
@@ -129,5 +125,5 @@ ChunkMesh_render (ChunkMesh *mesh, bool wireframe)
glEnableVertexAttribArray (2);
glVertexAttribPointer (2, 4, GL_INT, GL_FALSE, sizeof (ivec4), 0);
glDrawElements (wireframe ? GL_POINTS : GL_TRIANGLES, CHUNK_SIZE * 6, GL_UNSIGNED_INT, 0);
glDrawElements (wireframe ? GL_LINES : GL_TRIANGLES, CHUNK_SIZE * 6, GL_UNSIGNED_INT, 0);
}
+3 -3
View File
@@ -13,12 +13,12 @@
#define FRUSTUM_CULL 1
#define CHUNK_FRAME_BUDGET_US 5000
#define ENTITY_FRAME_BUDGET_US 5000
#define CHUNK_FRAME_BUDGET_US 500
#define ENTITY_FRAME_BUDGET_US 500
#define RENDER_FRAME_BUDGET_US 16666 /* 60 FPS */
#define RENDER_AVG_FREQ_S 10
#define RENDER_DISTANCE_MIN 1
#define RENDER_DISTANCE_MIN 2
#define RENDER_DISTANCE_MAX 12
void graphics_init ();
+13 -10
View File
@@ -1,8 +1,6 @@
#include "uielement.h"
/* TODO: Alpha blending. */
/* TODO: Texture support in the uielement shader. */
/* TODO: UTF-8 support */
static SimpleMesh quad;
static unsigned int last_program;
@@ -56,16 +54,16 @@ UIElement_set_on_tick (UIElement *element,
void *userdata)
{
element->on_tick = on_tick;
element->userdata = userdata;
element->tick_data = userdata;
}
void
UIElement_set_on_left_click (UIElement *element,
void (*on_left_click) (struct UIElement *self, void *userdata),
void (*on_left_click) (struct UIElement *, void *),
void *userdata)
{
element->on_left_click = on_left_click;
element->userdata = userdata;
element->left_click_data = userdata;
}
void
@@ -120,14 +118,19 @@ UIElement_label_render_text (UIElement *element, mat3 parent_model)
{
last_program = program_use (RESPRG ("uitext"));
}
for (guint i = 0; i < element->label.text->len; ++i)
const char *p = element->label.text->str;
while (*p)
{
gunichar ch = g_utf8_get_char (p);
program_set_mat3 (RESPRG ("uitext"), "model", model);
program_set_float (RESPRG ("uitext"), "character", element->label.text->str[i]);
program_set_float (RESPRG ("uitext"), "character", ch);
program_set_vec4 (RESPRG ("uitext"), "color", element->label.text_color);
SimpleMesh_render (&quad, false); /* UI always without wireframe. */
glm_translate2d (model, (vec2) { 1.5f, 0.0f });
p = g_utf8_next_char (p);
}
}
@@ -189,7 +192,7 @@ UIElement_render_with_model (UIElement *element, mat3 parent_model)
program_set_vec4 (RESPRG ("uielement"), "color",
element->hovered ? element->hover_background_color
: element->background_color);
glActiveTexture (GL_TEXTURE0);
texture_use (element->texture);
@@ -253,7 +256,7 @@ UIElement_tick (UIElement *element)
{
if (element->on_tick)
{
element->on_tick (element, element->userdata);
element->on_tick (element, element->tick_data);
}
for (guint i = 0; i < element->children->len; ++i)
+12 -8
View File
@@ -1,5 +1,7 @@
#pragma once
/* TODO: Seperate userdata for each callback. */
#include <stdbool.h>
#include <glib-2.0/glib.h>
@@ -27,6 +29,7 @@ typedef struct UILabel
{
vec4 text_color;
GString *text;
enum { ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT } alignment;
} UILabel;
typedef struct UIElement
@@ -42,13 +45,13 @@ typedef struct UIElement
bool visible;
bool hovered;
void *userdata;
bool ignores_events;
void (*on_left_click) (struct UIElement *, void *);
void *left_click_data;
void (*on_right_click) (struct UIElement *, void *);
void *right_click_data;
void (*on_tick) (struct UIElement *, void *);
void *tick_data;
void (*on_left_click) (struct UIElement *self, void *userdata);
void (*on_right_click) (struct UIElement *self, void *userdata);
void (*on_tick) (struct UIElement *self, void *userdata);
union {
UIContainer container;
UILabel label;
@@ -63,8 +66,9 @@ UIElement *UIElement_label_new (const char *text);
void UIElement_add_child (UIElement *parent, UIElement *child);
void UIElement_set_on_left_click (UIElement *element, void (*on_left_click) (struct UIElement *self, void *userdata), void *userdata);
void UIElement_set_on_tick (UIElement *element, void (*on_tick) (struct UIElement *self, void *userdata), void *userdata);
void UIElement_set_on_left_click (UIElement *element, void (*on_left_click) (struct UIElement *, void *), void *userdata);
void UIElement_set_on_tick (UIElement *element, void (*on_tick) (struct UIElement *, void *), void *userdata);
void UIElement_set_background_color (UIElement *element, float r, float g, float b, float a);
void UIElement_set_hover_background_color (UIElement *element, float r, float g, float b, float a);
void UIElement_set_position (UIElement *element, float x, float y);
+7 -7
View File
@@ -17,7 +17,7 @@ uiman_init ()
UIElement *container = UIElement_container_new (false);
UIElement_set_position (container, -0.5, 0.75);
UIElement_set_scale (container, 0.4, 0.15);
UIElement_set_background_color (container, 1.0, 0.0, 0.0, 1.0);
UIElement_set_background_color (container, 0.0, 0.0, 0.0, 0.8);
UIElement *frame_time = UIElement_label_new ("Frame time: ");
UIElement_set_on_tick (frame_time, uicb_label_show_float, &delta_time);
@@ -47,13 +47,13 @@ uiman_init ()
UIElement_add_child (container3, pos_z);
UIElement *wireframe_button = UIElement_label_new ("Toggle wireframe");
UIElement_set_background_color (wireframe_button, 0.2, 0.2, 0.2, 1.0);
UIElement_set_hover_background_color (wireframe_button, 0.3, 0.3, 0.3, 1.0);
UIElement_set_background_color (wireframe_button, 1.0, 1.0, 1.0, 0.2);
UIElement_set_hover_background_color (wireframe_button, 1.0, 1.0, 1.0, 0.3);
UIElement_set_on_left_click (wireframe_button, uicb_flag_switch, "wireframe_enabled");
UIElement *flight_button = UIElement_label_new ("Toggle flight");
UIElement_set_background_color (flight_button, 0.2, 0.2, 0.2, 1.0);
UIElement_set_hover_background_color (flight_button, 0.3, 0.3, 0.3, 1.0);
UIElement_set_background_color (flight_button, 1.0, 1.0, 1.0, 0.2);
UIElement_set_hover_background_color (flight_button, 1.0, 1.0, 1.0, 0.3);
UIElement_set_on_left_click (flight_button, uicb_flag_switch, "flight_enabled");
UIElement *container4 = UIElement_container_new (true);
@@ -161,13 +161,13 @@ uiman_cb_button_down (SDL_Event *event, void *data)
{
if (event->button.button == SDL_BUTTON_LEFT && element->on_left_click)
{
element->on_left_click (element, element->userdata);
element->on_left_click (element, element->left_click_data);
return true;
}
if (event->button.button == SDL_BUTTON_RIGHT && element->on_right_click)
{
element->on_right_click (element, element->userdata);
element->on_right_click (element, element->right_click_data);
return true;
}
}
+1 -1
View File
@@ -3,7 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#define GAME_NAME "Wschod"
#define GAME_NAME "Wschód"
#define GAME_VERSION "0.2"
#define COLOR_BLUE "\x1B[34m"