$darkmode
Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
ui.c File Reference
#include "ui.h"
#include "glad/glad.h"
#include "io.h"
#include "log.h"
#include "render_utils.h"
#include "shader.h"
#include "utils.h"
#include <cglm/affine.h>
#include <cglm/cam.h>
#include <cglm/mat4.h>
#include <cglm/types.h>
#include <cglm/vec3.h>
#include <stb_truetype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for ui.c:

Data Structures

struct  UIQuad
 

Enumerations

enum  FontAtlasConfig { FONT_ATLAS_SIZE = 512 , FONT_FIRST_CHAR = 32 , FONT_CHAR_COUNT = 96 }
 
enum  VertexConfig { QUAD_VERTICES_COUNT = 6 , VERTEX_COMPONENTS = 12 , VERTICES_PER_QUAD = 6 , FLOATS_PER_VERTEX = 12 }
 

Functions

static void setup_ui_render_state (void)
 
static int create_font_atlas (unsigned char *font_buffer, float font_size, UIContext *ui_context)
 
static int setup_vertex_buffers (UIContext *ui_context)
 
static void prepare_batch (UIContext *ui_context, GLuint texture)
 
static UIQuad make_glyph_quad (const GlyphInfo *glyph, float render_x, float render_y, float scale, const vec3 color, float alpha)
 
int ui_init (UIContext *ui_context, const char *font_path, float font_size)
 Initializes the UI system. More...
 
void ui_begin (UIContext *ui_context, int screen_width, int screen_height)
 Begins a new UI batch. More...
 
void ui_flush (UIContext *ui_context)
 Flushes the current UI batch to the GPU. More...
 
void ui_end (UIContext *ui_context)
 Ends the current UI batch and restores OpenGL state. More...
 
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. More...
 
float ui_measure_text (const UIContext *ui_context, const char *text)
 Measures the width of a text string in pixels. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
void ui_destroy (UIContext *ui_context)
 Releases UI resources. More...
 
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. More...
 
void ui_layout_text (UILayout *layout, const char *text, const vec3 color)
 Draws text and advances the layout cursor. More...
 
void ui_layout_separator (UILayout *layout, float space)
 Adds vertical space to the layout. More...
 
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. More...
 
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. More...
 
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). More...
 
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). More...
 
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. More...
 

Variables

static const float FONT_ATLAS_SIZE_F = 512.0F
 
static const float FONT_BASELINE_OFFSET = 30.0F
 
static const size_t MAX_FONT_FILE_SIZE = 10 * 1024 * 1024
 
static const float UI_QUAD_POS_HALF = 0.5F
 
static const float UI_QUAD_TEX_MAX = 1.0F
 
static const float UI_QUAD_MIN = 0.0F
 
static GLStateBackup g_ui_saved_state
 

Enumeration Type Documentation

◆ FontAtlasConfig

Enumerator
FONT_ATLAS_SIZE 
FONT_FIRST_CHAR 
FONT_CHAR_COUNT 

◆ VertexConfig

Enumerator
QUAD_VERTICES_COUNT 
VERTEX_COMPONENTS 
VERTICES_PER_QUAD 
FLOATS_PER_VERTEX 

Function Documentation

◆ create_font_atlas()

static int create_font_atlas ( unsigned char *  font_buffer,
float  font_size,
UIContext ui_context 
)
static

◆ make_glyph_quad()

static UIQuad make_glyph_quad ( const GlyphInfo glyph,
float  render_x,
float  render_y,
float  scale,
const vec3  color,
float  alpha 
)
static

◆ prepare_batch()

static void prepare_batch ( UIContext ui_context,
GLuint  texture 
)
static
Here is the call graph for this function:

◆ setup_ui_render_state()

static void setup_ui_render_state ( void  )
static

◆ setup_vertex_buffers()

static int setup_vertex_buffers ( UIContext ui_context)
static
Here is the call graph for this function:

◆ ui_begin()

void ui_begin ( UIContext ui_context,
int  screen_width,
int  screen_height 
)

Begins a new UI batch.

Parameters
ui_contextPointer to the UI context.
screen_widthCurrent window width.
screen_heightCurrent window height.
Here is the call graph for this function:

◆ ui_destroy()

void ui_destroy ( UIContext ui_context)

Releases UI resources.

Parameters
ui_contextPointer to the struct.
Here is the call graph for this function:

◆ ui_draw_bloom_quad()

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).

Uses GL_ONE, GL_ONE blending so dark pixels are invisible and bright pixels add light on top of existing content. The texture should be a radial bloom (bright center, black surround). The tint parameter allows coloring the bloom.

Parameters
textureOpenGL texture handle (bloom PNG, black background).
tintColor of the bloom light (e.g. white or cyan).
intensityBloom brightness (0.0 = invisible, 1.0 = full).
Here is the call graph for this function:

◆ ui_draw_glow_rect()

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.

Here is the call graph for this function:

◆ ui_draw_rect()

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.

Here is the call graph for this function:

◆ ui_draw_rect_ex()

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.

Here is the call graph for this function:

◆ ui_draw_rounded_rect()

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.

Here is the call graph for this function:

◆ ui_draw_spinner()

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.

Here is the call graph for this function:

◆ ui_draw_text()

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.

Here is the call graph for this function:

◆ ui_draw_text_ex()

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.

Here is the call graph for this function:

◆ ui_draw_text_scaled()

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.

Here is the call graph for this function:

◆ ui_draw_textured_quad()

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).

The PNG texture's RGB is multiplied by the tint color, and its alpha channel is scaled by the alpha parameter. Use for keycaps and panel frame.

Note
Flushes the current batch if the active texture differs.
Parameters
textureOpenGL texture handle (RGBA PNG).
tintTint color multiplied against the texture RGB.
alphaOverall opacity.
Here is the call graph for this function:

◆ ui_end()

void ui_end ( UIContext ui_context)

Ends the current UI batch and restores OpenGL state.

Parameters
ui_contextPointer to the UI context.
Here is the call graph for this function:

◆ ui_flush()

void ui_flush ( UIContext ui_context)

Flushes the current UI batch to the GPU.

Parameters
ui_contextPointer to the UI context.
Here is the call graph for this function:

◆ ui_init()

int ui_init ( UIContext ui_context,
const char *  font_path,
float  font_size 
)

Initializes the UI system.

Parameters
ui_contextPointer to the struct.
font_pathPath to the binary font metrics/texture.
font_sizeDefault scale.
Returns
0 on success, non-zero on error.
Here is the call graph for this function:

◆ ui_layout_init()

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.

Here is the call graph for this function:

◆ ui_layout_separator()

void ui_layout_separator ( UILayout layout,
float  space 
)

Adds vertical space to the layout.

Here is the call graph for this function:

◆ ui_layout_text()

void ui_layout_text ( UILayout layout,
const char *  text,
const vec3  color 
)

Draws text and advances the layout cursor.

Here is the call graph for this function:

◆ ui_measure_text()

float ui_measure_text ( const UIContext ui_context,
const char *  text 
)

Measures the width of a text string in pixels.

Parameters
ui_contextPointer to the context.
textThe text to measure.
Returns
Width in pixels.

Variable Documentation

◆ FONT_ATLAS_SIZE_F

const float FONT_ATLAS_SIZE_F = 512.0F
static

◆ FONT_BASELINE_OFFSET

const float FONT_BASELINE_OFFSET = 30.0F
static

◆ g_ui_saved_state

GLStateBackup g_ui_saved_state
static

◆ MAX_FONT_FILE_SIZE

const size_t MAX_FONT_FILE_SIZE = 10 * 1024 * 1024
static

◆ UI_QUAD_MIN

const float UI_QUAD_MIN = 0.0F
static

◆ UI_QUAD_POS_HALF

const float UI_QUAD_POS_HALF = 0.5F
static

◆ UI_QUAD_TEX_MAX

const float UI_QUAD_TEX_MAX = 1.0F
static