$darkmode
Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
ui.h
Go to the documentation of this file.
1 
9 #ifndef UI_H
10 #define UI_H
11 
13 #define ASCII_CHAR_COUNT 96
14 
15 #include "shader.h"
16 #include <cglm/cglm.h>
17 
22 typedef struct {
23  float x0, y0, x1, y1;
24  float w, h;
25  float x_off, y_off;
26  float advance;
27 } GlyphInfo;
28 
30 #define UI_MAX_BATCH_VERTICES 8192
31 
36 typedef struct {
37  float x, y;
38  float u, v;
39  float r, g, b, a;
40  float mode; // 0=solid, 1=text, 2=rounded
41  float rect_size_x, rect_size_y;
42  float radius;
43 } UIVertex;
44 
49 typedef struct {
50  GLuint texture;
52  Shader*
54  GLuint vao, vbo;
56  float font_size;
57  UIVertex batch_vertices[UI_MAX_BATCH_VERTICES];
62  GLuint current_texture;
63 } UIContext;
64 
69 typedef struct {
71  float start_x;
72  float cursor_y;
73  float padding;
76 } UILayout;
77 
85 int ui_init(UIContext* ui_context, const char* font_path, float font_size);
86 
91 void ui_destroy(UIContext* ui_context);
92 
93 /* --- Batch API --- */
94 
101 void ui_begin(UIContext* ui_context, int screen_width, int screen_height);
102 
107 void ui_flush(UIContext* ui_context);
108 
113 void ui_end(UIContext* ui_context);
114 
115 /* --- Layout API --- */
116 
120 void ui_layout_init(UILayout* layout, UIContext* ui_ctx, float pos_x,
121  float pos_y, float padding, int screen_width,
122  int screen_height);
123 
125 void ui_layout_text(UILayout* layout, const char* text, const vec3 color);
126 
128 void ui_layout_separator(UILayout* layout, float space);
129 
130 /* --- Low-level API --- */
131 
133 void ui_draw_text(UIContext* ui_context, const char* text, float pos_x,
134  float pos_y, const vec3 color, int screen_width,
135  int screen_height);
136 
143 float ui_measure_text(const UIContext* ui_context, const char* text);
144 
146 void ui_draw_text_ex(UIContext* ui_context, const char* text, float pos_x,
147  float pos_y, const vec3 color, float alpha,
148  int screen_width, int screen_height);
149 
151 void ui_draw_text_scaled(UIContext* ui_context, const char* text, float pos_x,
152  float pos_y, const vec3 color, float alpha,
153  float scale, int screen_width, int screen_height);
154 
156 void ui_draw_rect(UIContext* ui_context, float rect_x, float rect_y,
157  float width, float height, const vec3 color, int screen_width,
158  int screen_height);
159 
161 void ui_draw_rect_ex(UIContext* ui_context, float rect_x, float rect_y,
162  float width, float height, const vec3 color, float alpha,
163  int screen_width, int screen_height);
164 
166 void ui_draw_spinner(UIContext* ui_context, float center_x, float center_y,
167  float size, float angle, const vec3 color,
168  int screen_width, int screen_height);
169 
171 void ui_draw_rounded_rect(UIContext* ui_context, float rect_x, float rect_y,
172  float width, float height, float radius,
173  const vec3 color, float alpha, int screen_width,
174  int screen_height);
175 
177 void ui_draw_glow_rect(UIContext* ui_context, float rect_x, float rect_y,
178  float width, float height, float radius,
179  const vec3 color, float alpha, int screen_width,
180  int screen_height);
181 
193 void ui_draw_textured_quad(UIContext* ui_context, GLuint texture, float rect_x,
194  float rect_y, float width, float height,
195  const vec3 tint, float alpha, int screen_width,
196  int screen_height);
197 
211 void ui_draw_bloom_quad(UIContext* ui_context, GLuint texture, float rect_x,
212  float rect_y, float width, float height,
213  const vec3 tint, float intensity, int screen_width,
214  int screen_height);
215 
216 #endif /* UI_H */
layout(location=0) in vec3 RayDir
High-level OpenGL shader management with metadata and uniform caching.
Metrics and texture coordinates for a single font character.
Definition: ui.h:22
float h
Definition: ui.h:24
float advance
Definition: ui.h:26
float x_off
Definition: ui.h:25
float x0
Definition: ui.h:23
Wrapper for an OpenGL program with uniform caching and automatic cleanup.
Definition: shader.h:71
Persistent state for the UI system.
Definition: ui.h:49
GLuint vao
Definition: ui.h:54
int batch_active
Definition: ui.h:61
GLuint current_texture
Definition: ui.h:62
Shader * spinner_shader
Definition: ui.h:53
int current_screen_width
Definition: ui.h:59
Shader * shader
Definition: ui.h:51
GLuint texture
Definition: ui.h:50
int current_screen_height
Definition: ui.h:60
float font_size
Definition: ui.h:56
int batch_count
Definition: ui.h:58
Helper for automatic vertical stacking of UI elements.
Definition: ui.h:69
float cursor_y
Definition: ui.h:72
int screen_height
Definition: ui.h:75
float start_x
Definition: ui.h:71
float padding
Definition: ui.h:73
int screen_width
Definition: ui.h:74
UIContext * ui
Definition: ui.h:70
Vertex data for the batch renderer.
Definition: ui.h:36
float rect_size_x
Definition: ui.h:41
float mode
Definition: ui.h:40
float u
Definition: ui.h:38
float a
Definition: ui.h:39
float radius
Definition: ui.h:42
float x
Definition: ui.h:37
void ui_layout_init(UILayout *layout, UIContext *ui_ctx, float pos_x, float pos_y, float padding, int screen_width, int screen_height)
Initializes a layout helper for a frame.
Definition: ui.c:554
void ui_draw_rect_ex(UIContext *ui_context, float rect_x, float rect_y, float width, float height, const vec3 color, float alpha, int screen_width, int screen_height)
Draws a solid color rectangle with custom alpha transparency.
Definition: ui.c:472
float ui_measure_text(const UIContext *ui_context, const char *text)
Measures the width of a text string in pixels.
Definition: ui.c:380
void ui_begin(UIContext *ui_context, int screen_width, int screen_height)
Begins a new UI batch.
Definition: ui.c:304
void ui_draw_glow_rect(UIContext *ui_context, float rect_x, float rect_y, float width, float height, float radius, const vec3 color, float alpha, int screen_width, int screen_height)
Draws an SDF neon glow border around a rounded rectangle area.
Definition: ui.c:828
void ui_destroy(UIContext *ui_context)
Releases UI resources.
Definition: ui.c:524
void ui_draw_text_ex(UIContext *ui_context, const char *text, float pos_x, float pos_y, const vec3 color, float alpha, int screen_width, int screen_height)
Draws text with custom alpha transparency.
Definition: ui.c:403
void ui_layout_text(UILayout *layout, const char *text, const vec3 color)
Draws text and advances the layout cursor.
Definition: ui.c:566
void ui_layout_separator(UILayout *layout, float space)
Adds vertical space to the layout.
Definition: ui.c:580
#define UI_MAX_BATCH_VERTICES
Maximum number of vertices per UI batch.
Definition: ui.h:30
#define ASCII_CHAR_COUNT
Number of supported glyphs in the font atlas.
Definition: ui.h:13
void ui_draw_text(UIContext *ui_context, const char *text, float pos_x, float pos_y, const vec3 color, int screen_width, int screen_height)
Draws a single text string at a specific coordinate.
Definition: ui.c:372
void ui_flush(UIContext *ui_context)
Flushes the current UI batch to the GPU.
Definition: ui.c:327
void ui_draw_bloom_quad(UIContext *ui_context, GLuint texture, float rect_x, float rect_y, float width, float height, const vec3 tint, float intensity, int screen_width, int screen_height)
Draws a textured quad with additive blending for bloom effects (mode 4).
Definition: ui.c:771
void ui_draw_textured_quad(UIContext *ui_context, GLuint texture, float rect_x, float rect_y, float width, float height, const vec3 tint, float alpha, int screen_width, int screen_height)
Draws a textured quad tinted by a color (mode 3).
Definition: ui.c:719
void ui_draw_text_scaled(UIContext *ui_context, const char *text, float pos_x, float pos_y, const vec3 color, float alpha, float scale, int screen_width, int screen_height)
Draws text with custom alpha transparency and a scale factor.
Definition: ui.c:411
int ui_init(UIContext *ui_context, const char *font_path, float font_size)
Initializes the UI system.
Definition: ui.c:237
void ui_draw_rect(UIContext *ui_context, float rect_x, float rect_y, float width, float height, const vec3 color, int screen_width, int screen_height)
Draws a solid color rectangle.
Definition: ui.c:464
void ui_draw_spinner(UIContext *ui_context, float center_x, float center_y, float size, float angle, const vec3 color, int screen_width, int screen_height)
Draws a rotating loading spinner.
Definition: ui.c:588
void ui_draw_rounded_rect(UIContext *ui_context, float rect_x, float rect_y, float width, float height, float radius, const vec3 color, float alpha, int screen_width, int screen_height)
Draws a rounded rectangle with custom alpha.
Definition: ui.c:661
void ui_end(UIContext *ui_context)
Ends the current UI batch and restores OpenGL state.
Definition: ui.c:361