Draw

#import "extra/draw"

Collection of simple 2D primitive rendering functions. Internally the module uses OpenGL core profile for rendering and is supposed to be used for simple games or quick prototyping.

The draw module is not supposed to be an OpenGL replacement, if you want to do some advanced rendering, use OpenGL directly.

Features

Notes

General desing

The draw API is based on switching the global context of rendering primitives (rectangles). In example to draw bunch of colored rectangles we must set the propper shader first:

using draw;
using glm;
set_shader_color(); // Prepare renderer for rendering of colored rectangles.

rect(10.f, 10.f, 100.f, 100.f, v4.{ 1.f, 0.f, 0.f, 1.f });
rect(200.f, 10.f, 100.f, 100.f, v4.{ 0.f, 1.f, 0.f, 1.f });
rect(400.f, 10.f, 100.f, 100.f, v4.{ 0.f, 0.f, 1.f, 1.f });

Internally each call to rect just appends the geometry caches; actual rendering is done when currenty used shader is changed or flush is called explicitly. This approach reduces count of required draw calls a bit.

See the example.

draw.init

init :: fn (viewport_width: s32, viewport_height: s32, initialize_opengl :: true) Error

Draw library initialization must be called once before the module is used. The viewport_width and viewport_height defines the viewport size in current opengl context. These values are usually the same as the window size.

The terminate must be called when draw module is not needed anymore.

The current OpenGL context must be aready set, the minimal required OpenGL version is 3.3 "Core Profile". You can use i.e. glfw or similar tool to create window and propper OpenGL context.

The gl_init function is called internally.

Note: Window resizing is not supported right now.

File: draw.bl

draw.set_viewport_size

set_viewport_size :: fn (viewport_width: s32, viewport_height: s32) 

Set the size of the viewport, this is done automatically in the init function call, however this one can be used e.g. in case of the window resize.

File: draw.bl

draw.toggle_gamma_correction

toggle_gamma_correction :: fn (v: bool)  #inline

File: draw.bl

draw.terminate

terminate :: fn (terminate_opengl :: true) 

Release all resources used by the module. This should be called when the module is not needed anymore.

The gl_terminate function is called internally.

File: draw.bl

draw.clear_color

clear_color :: fn (color :: glm.v4_zero)  #inline

Fill the current frame buffer with color. This is usually called each frame.

File: draw.bl

draw.set_shader_color

set_shader_color :: fn ()  #inline

Prepare renderer for rendering colored rectangles. Each rectangle can use different color. This method is supposed to be called before rect or rect_centered.

File: draw.bl

draw.set_shader_texture

set_shader_texture :: fn (texture: *Texture)  #inline

Prepare renderer for rendering textured rectangles. This method is supposed to be called before rect or rect_centered (all rectangles will use the same texture).

File: draw.bl

draw.set_shader_font

set_shader_font :: fn (font: *Font)  #inline

Prepare the renderer for rendering of a text.

File: draw.bl

draw.rect

rect :: fn { 
    fn (position_x: f32, position_y: f32, width: f32, height: f32, color :: glm.v4_one) ; 
    fn (position_x: f32, position_y: f32, width: f32, height: f32, colors: []glm.v4) ; 
} #inline

Draw a single colored rectangle or texture into the frame buffer.

File: draw.bl

draw.rect_centered

rect_centered :: fn (center_x: f32, center_y: f32, width: f32, height: f32, color :: glm.v4_one)  #inline

Draw a single colored rectangle or texture into the frame buffer.

File: draw.bl

draw.rect_rounded

rect_rounded :: fn (position_x: f32, position_y: f32, width: f32, height: f32, r: f32, inner_color :: glm.v4_one, outer_color :: glm.v4_one)  #inline

File: draw.bl

draw.rect_centered_rotated

rect_centered_rotated :: fn (center_x: f32, center_y: f32, width: f32, height: f32, angle_in_radians: f32, color :: glm.v4_one)  #inline

Draw a single colored rectangle or texture with rotation into the frame buffer.

File: draw.bl

draw.text

text :: fn (position_x: f32, position_y: f32, text: string_view, text_color : glm.v4: glm.v4_one)  #inline

Draw text into the frame buffer.

File: draw.bl

draw.code

code :: fn (position_x: f32, position_y: f32, code: string_view, character_color_indices: []u8, color_table: []glm.v4)  #inline

File: draw.bl

draw.flush

flush :: fn ()  #inline

Immediately render all cached stuff into the frame buffer.

File: draw.bl

draw.TextureFormat

TextureFormat :: enum u8 {
    RED = 1;
    RGB = 3;
    RGBA = 4;
}

Texture channel distribution format.

File: draw.bl

draw.Texture

Texture :: struct {
    format: TextureFormat;
    bit_depth: u8;
    width: u32;
    height: u32;
    handle: GLuint;
}

Texture representation in draw module.

Members

File: draw.bl

draw.texture_init

texture_init :: fn (texture: *Texture, directory: string_view, filename: string_view) Error

Load png texture from the directory/filename.

File: draw.bl

draw.texture_terminate

texture_terminate :: fn (texture: *Texture) 

Release texture data.

File: draw.bl

draw.png_load_from_file

png_load_from_file :: fn (filepath: string_view, out_data: **u8, out_width: *u32, out_height: *u32, out_bit_depth: *u8, out_format: *TextureFormat) Error

Loads png image from filepath into out_data. The out_data shoud be released by free call on caller side.

File: draw.bl

draw.png_load_from_memory

png_load_from_memory :: fn (data: []u8, out_data: **u8, out_width: *u32, out_height: *u32, out_bit_depth: *u8, out_format: *TextureFormat) Error

File: draw.bl

draw.png_load_from_stream

png_load_from_stream :: fn (stream: *std.Stream, out_data: **u8, out_width: *u32, out_height: *u32, out_bit_depth: *u8, out_format: *TextureFormat) Error

File: draw.bl

draw.Font

Font :: struct {
    char_table: ;
    atlas: Texture;
    size: f32;
    height: f32;
    base_offset_y: f32;
    tab_size: s32;
}

File: draw.bl

draw.GlyphInfo

GlyphInfo :: struct {
    advance_x: f32;
    advance_y: f32;
    width: f32;
    height: f32;
    left: f32;
    top: f32;
    atlas_u_offset: f32;
    atlas_v_offset: f32;
}

File: draw.bl

draw.font_default_char_set

font_default_char_set :: fn () []u32 #inline

Returns default ASCII character table.

File: draw.bl

draw.font_generate_char_set

font_generate_char_set :: fn (chars: string_view, out_set: *[..]u32) 

Generate new character table based on input chars.

File: draw.bl

draw.font_init_at_size

font_init_at_size :: fn (font: *Font, directory: string_view, filename: string_view, size: s32, char_set :: ) Error

Initialize new font from the 'TTF' file at directory/filename at required size. Custom char_set can be specified in case we need to render some non-ascii unicode characters. Each loaded font must be terminated by font_terminate.

File: draw.bl

draw.font_init_at_size_from_memory

font_init_at_size_from_memory :: fn (font: *Font, file_data: []u8, size: s32, char_set :: ) Error

File: draw.bl

draw.font_terminate

font_terminate :: fn (font: *Font) 

Terminate loaded font.

File: draw.bl

draw.font_set_tab_size

font_set_tab_size :: fn (font: *Font, space_count: s32) 

Set size of a tab character as space_count count of spaces.

File: draw.bl

draw.text_size

text_size :: fn (text: string_view, font: *Font, _len :: -1) (width: f32, height: f32)

File: draw.bl

draw.get_character_count_to_fit

get_character_count_to_fit :: fn (text: string_view, font: *Font, fit_to: f32, split_by_words :: false) s32

File: draw.bl

draw.split_text_to_fit

split_text_to_fit :: fn (text: string_view, font: *Font, fit_to: f32, split_by_words :: false) (left: string_view, right: string_view)

File: draw.bl

draw.set_scissor

set_scissor :: fn (position_x: f32, position_y: f32, width: f32, height: f32) 

File: draw.bl

draw.clear_scissor

clear_scissor :: fn () 

File: draw.bl