Skip to content

gg #

Description

gg is V's simple graphics module. It is currently implemented using sokol, and makes easy creating apps that just need a way to draw simple 2D shapes, and to react to user's keyboard/mouse input.

Example

module main

import gg
import gx

fn main() {
    mut context := gg.new_context(
        bg_color:     gx.rgb(174, 198, 255)
        width:        600
        height:       400
        window_title: 'Polygons'
        frame_fn:     frame
    )
    context.run()
}

fn frame(mut ctx gg.Context) {
    ctx.begin()
    ctx.draw_convex_poly([f32(100.0), 100.0, 200.0, 100.0, 300.0, 200.0, 200.0, 300.0, 100.0, 300.0],
        gx.blue)
    ctx.draw_poly_empty([f32(50.0), 50.0, 70.0, 60.0, 90.0, 80.0, 70.0, 110.0], gx.black)
    ctx.draw_triangle_filled(450, 142, 530, 280, 370, 280, gx.red)
    ctx.end()
}

fn create_default_pass #

fn create_default_pass(action gfx.PassAction) gfx.Pass

fn dpi_scale #

fn dpi_scale() f32

dpi_scale returns the DPI scale coefficient for the screen. Do not use for Android development, use Context.scale instead.

fn high_dpi #

fn high_dpi() bool

high_dpi returns true if gg is running on a high DPI monitor or screen.

fn is_fullscreen #

fn is_fullscreen() bool

is it fullscreen

fn new_context #

fn new_context(cfg Config) &Context

new_context returns an initialized Context allocated on the heap.

fn screen_size #

fn screen_size() Size

screen_size returns the size of the active screen.

fn set_window_title #

fn set_window_title(title string)

set_window_title sets main window's title

fn start #

fn start(cfg Config)

start creates a new context and runs it right away. It is a convenient way to start short/throwaway gg based prototypes, that do not need to keep and update their own state, like simple animations/visualisations that depend only on the time, or the ctx.frame counter. Use gg.new_context() for more complex ones.

fn toggle_fullscreen #

fn toggle_fullscreen()

toggle fullscreen

fn window_size #

fn window_size() Size

window_size returns the Size of the active window. Do not use for Android development, use Context.window_size() instead.

fn window_size_real_pixels #

fn window_size_real_pixels() Size

window_size_real_pixels returns the Size of the active window without scale

type Color #

type Color = gx.Color

type FNCb #

type FNCb = fn (data voidptr)

type FNChar #

type FNChar = fn (c u32, data voidptr)

type FNClick #

type FNClick = fn (x f32, y f32, button MouseButton, data voidptr)

type FNEvent #

type FNEvent = fn (e &Event, data voidptr)

type FNEvent2 #

type FNEvent2 = fn (data voidptr, e &Event)

type FNFail #

type FNFail = fn (msg string, data voidptr)

type FNKeyDown #

type FNKeyDown = fn (c KeyCode, m Modifier, data voidptr)

type FNKeyUp #

type FNKeyUp = fn (c KeyCode, m Modifier, data voidptr)

type FNMove #

type FNMove = fn (x f32, y f32, data voidptr)

type FNUnClick #

type FNUnClick = fn (x f32, y f32, button MouseButton, data voidptr)

fn (FT) flush #

fn (ft &FT) flush()

flush prepares the font for use.

type TouchPoint #

type TouchPoint = C.sapp_touchpoint

fn C.WaitMessage()

enum EndEnum #

enum EndEnum {
	clear
	passthru
}

enum ImageEffect #

enum ImageEffect {
	// TODO(FireRedz): Add more effects
	alpha
	add
}

enum KeyCode #

enum KeyCode {
	invalid       = 0
	space         = 32
	apostrophe    = 39 //'
	comma         = 44 //,
	minus         = 45 //-
	period        = 46 //.
	slash         = 47 ///
	_0            = 48
	_1            = 49
	_2            = 50
	_3            = 51
	_4            = 52
	_5            = 53
	_6            = 54
	_7            = 55
	_8            = 56
	_9            = 57
	semicolon     = 59 //;
	equal         = 61 //=
	a             = 65
	b             = 66
	c             = 67
	d             = 68
	e             = 69
	f             = 70
	g             = 71
	h             = 72
	i             = 73
	j             = 74
	k             = 75
	l             = 76
	m             = 77
	n             = 78
	o             = 79
	p             = 80
	q             = 81
	r             = 82
	s             = 83
	t             = 84
	u             = 85
	v             = 86
	w             = 87
	x             = 88
	y             = 89
	z             = 90
	left_bracket  = 91  //[
	backslash     = 92  //\
	right_bracket = 93  //]
	grave_accent  = 96  //`
	world_1       = 161 // non-us #1
	world_2       = 162 // non-us #2
	escape        = 256
	enter         = 257
	tab           = 258
	backspace     = 259
	insert        = 260
	delete        = 261
	right         = 262
	left          = 263
	down          = 264
	up            = 265
	page_up       = 266
	page_down     = 267
	home          = 268
	end           = 269
	caps_lock     = 280
	scroll_lock   = 281
	num_lock      = 282
	print_screen  = 283
	pause         = 284
	f1            = 290
	f2            = 291
	f3            = 292
	f4            = 293
	f5            = 294
	f6            = 295
	f7            = 296
	f8            = 297
	f9            = 298
	f10           = 299
	f11           = 300
	f12           = 301
	f13           = 302
	f14           = 303
	f15           = 304
	f16           = 305
	f17           = 306
	f18           = 307
	f19           = 308
	f20           = 309
	f21           = 310
	f22           = 311
	f23           = 312
	f24           = 313
	f25           = 314
	kp_0          = 320
	kp_1          = 321
	kp_2          = 322
	kp_3          = 323
	kp_4          = 324
	kp_5          = 325
	kp_6          = 326
	kp_7          = 327
	kp_8          = 328
	kp_9          = 329
	kp_decimal    = 330
	kp_divide     = 331
	kp_multiply   = 332
	kp_subtract   = 333
	kp_add        = 334
	kp_enter      = 335
	kp_equal      = 336
	left_shift    = 340
	left_control  = 341
	left_alt      = 342
	left_super    = 343
	right_shift   = 344
	right_control = 345
	right_alt     = 346
	right_super   = 347
	menu          = 348
}

enum Modifier #

@[flag]
enum Modifier {
	shift // (1<<0)
	ctrl  // (1<<1)
	alt   // (1<<2)
	super // (1<<3)
}

enum MouseButton #

enum MouseButton {
	left    = 0
	right   = 1
	middle  = 2
	invalid = 256
}

enum MouseButtons #

@[flag]
enum MouseButtons {
	left
	right
	middle
}

Note: unlike the MouseButton enum from above,the [flag]-ed enum here can have combined states, representing several pressed buttons at once.

enum PaintStyle #

enum PaintStyle {
	fill
	stroke
}

enum PenLineType #

enum PenLineType {
	solid
	dashed
	dotted
}

struct Config #

struct Config {
pub:
	width         int     // desired start width of the window
	height        int     // desired start height of the window
	retina        bool    // TODO: implement or deprecate
	resizable     bool    // TODO: implement or deprecate
	user_data     voidptr // a custom pointer to the application data/instance
	font_size     int     // TODO: implement or deprecate
	create_window bool    // TODO: implement or deprecate
	// window_user_ptr voidptr
	window_title      string // the desired title of the window
	html5_canvas_name string = 'canvas'
	borderless_window bool     // TODO: implement or deprecate
	always_on_top     bool     // TODO: implement or deprecate
	bg_color          gx.Color // The background color of the window. By default, the first thing gg does in ctx.begin(), is clear the whole buffer with that color.
	init_fn           FNCb   = unsafe { nil } // Called once, after Sokol has finished its setup. Some gg and Sokol functions have to be called *in this* callback, or after this callback, but not before
	frame_fn          FNCb   = unsafe { nil } // Called once per frame, usually 60 times a second (depends on swap_interval). See also https://dri.freedesktop.org/wiki/ConfigurationOptions/#synchronizationwithverticalrefreshswapintervals
	native_frame_fn   FNCb   = unsafe { nil }
	cleanup_fn        FNCb   = unsafe { nil } // Called once, after Sokol determines that the application is finished/closed. Put your app specific cleanup/free actions here.
	fail_fn           FNFail = unsafe { nil } // Called once per Sokol error/log message. TODO: currently it does nothing with latest Sokol, reimplement using Sokol's new sapp_logger APIs.

	event_fn FNEvent  = unsafe { nil } // Called once per each user initiated event, received by Sokol/GG.
	on_event FNEvent2 = unsafe { nil } // Called once per each user initiated event, received by Sokol/GG. Same as event_fn, just the parameter order is different. TODO: deprecate this, in favor of event_fn
	quit_fn  FNEvent  = unsafe { nil } // Called when the user closes the app window.

	keydown_fn FNKeyDown = unsafe { nil } // Called once per key press, no matter how long the key is held down. Note that here you can access the scan code/physical key, but not the logical character.
	keyup_fn   FNKeyUp   = unsafe { nil } // Called once per key press, when the key is released.
	char_fn    FNChar    = unsafe { nil } // Called once per character (after the key is pressed down, and then released). Note that you can access the character/utf8 rune here, not just the scan code.

	move_fn    FNMove    = unsafe { nil } // Called while the mouse/touch point is moving.
	click_fn   FNClick   = unsafe { nil } // Called once when the mouse/touch button is clicked.
	unclick_fn FNUnClick = unsafe { nil } // Called once when the mouse/touch button is released.
	leave_fn   FNEvent   = unsafe { nil } // Called once when the mouse/touch point leaves the window.
	enter_fn   FNEvent   = unsafe { nil } // Called once when the mouse/touch point enters again the window.
	resized_fn FNEvent   = unsafe { nil } // Called once when the window has changed its size.
	scroll_fn  FNEvent   = unsafe { nil } // Called while the user is scrolling. The direction of scrolling is indicated by either 1 or -1.
	// wait_events       bool // set this to true for UIs, to save power
	fullscreen    bool // set this to true, if you want your window to start in fullscreen mode (suitable for games/demos/screensavers)
	scale         f32 = 1.0
	sample_count  int // bigger values usually have performance impact, but can produce smoother/antialiased lines, if you draw lines or polygons (2 is usually good enough)
	swap_interval int = 1 // 1 = 60fps, 2 = 30fps etc. The preferred swap interval (ignored on some platforms)
	// ved needs this
	// init_text bool
	font_path             string
	custom_bold_font_path string
	ui_mode               bool // refreshes only on events to save CPU usage
	// font bytes for embedding
	font_bytes_normal []u8
	font_bytes_bold   []u8
	font_bytes_mono   []u8
	font_bytes_italic []u8
	native_rendering  bool // Cocoa on macOS/iOS, GDI+ on Windows
	// drag&drop
	enable_dragndrop             bool // enable file dropping (drag'n'drop), default is false
	max_dropped_files            int = 1    // max number of dropped files to process (default: 1)
	max_dropped_file_path_length int = 2048 // max length in bytes of a dropped UTF-8 file path (default: 2048)

	min_width  int // desired minimum width of the window
	min_height int // desired minimum height of the window
}

struct Context #

@[heap]
struct Context {
mut:
	render_text bool = true
	// a cache with all images created by the user. used for sokol image init and to save space
	// (so that the user can store image ids, not entire Image objects)
	image_cache   []Image
	needs_refresh bool = true
	ticks         int // for ui mode only
pub:
	native_rendering bool
pub mut:
	scale       f32 = 1.0 // will get set to 2.0 for retina, will remain 1.0 for normal
	width       int
	height      int
	clear_pass  gfx.PassAction
	window      sapp.Desc
	pipeline    &PipelineContainer = unsafe { nil }
	config      Config
	user_data   voidptr
	ft          &FT = unsafe { nil }
	font_inited bool
	ui_mode     bool // do not redraw everything 60 times/second, but only when the user requests
	frame       u64  // the current frame counted from the start of the application; always increasing

	mbtn_mask     u8
	mouse_buttons MouseButtons // typed version of mbtn_mask; easier to use for user programs
	mouse_pos_x   int
	mouse_pos_y   int
	mouse_dx      int
	mouse_dy      int
	scroll_x      int
	scroll_y      int

	key_modifiers     Modifier           // the current key modifiers
	key_repeat        bool               // whether the pressed key was an autorepeated one
	pressed_keys      [key_code_max]bool // an array representing all currently pressed keys
	pressed_keys_edge [key_code_max]bool // true when the previous state of pressed_keys,
	// *before* the current event was different
	fps FPSConfig
}

fn (Context) begin #

fn (ctx &Context) begin()

begin prepares the context for drawing.

fn (Context) cache_image #

fn (mut ctx Context) cache_image(img Image) int

cache_image caches the image img in memory for later reuse. cache_image returns the cache index of the cached image.

See also: get_cached_image_by_idx See also: remove_cached_image_by_idx

fn (Context) create_image #

fn (mut ctx Context) create_image(file string) !Image

create_image creates an Image from file.

fn (Context) create_image_from_byte_array #

fn (mut ctx Context) create_image_from_byte_array(b []u8) !Image

create_image_from_byte_array creates an Image from the byte array b.

See also: create_image_from_memory

fn (Context) create_image_from_memory #

fn (mut ctx Context) create_image_from_memory(buf &u8, bufsize int) !Image

create_image_from_memory creates an Image from the memory buffer buf of size bufsize.

See also: create_image_from_byte_array

fn (Context) create_image_with_size #

fn (mut ctx Context) create_image_with_size(file string, width int, height int) Image

create_image_with_size creates an Image from file in the given width x height dimension.

Todo: copypasta

fn (Context) draw_arc_empty #

fn (ctx &Context) draw_arc_empty(x f32, y f32, inner_radius f32, thickness f32, start_angle f32, end_angle f32,
	segments int, c gx.Color)

draw_arc_empty draws the outline of an arc. x,y defines the end point of the arc (center of the circle that the arc is part of). inner_radius defines the radius of the arc (length from the center point where the arc is drawn). thickness defines how wide the arc is drawn. start_angle is the angle in radians at which the arc starts. end_angle is the angle in radians at which the arc ends. segments affects how smooth/round the arc is. c is the color of the arc outline.

fn (Context) draw_arc_filled #

fn (ctx &Context) draw_arc_filled(x f32, y f32, inner_radius f32, thickness f32, start_angle f32, end_angle f32,
	segments int, c gx.Color)

draw_arc_filled draws a filled arc. x,y defines the central point of the arc (center of the circle that the arc is part of). inner_radius defines the radius of the arc (length from the center point where the arc is drawn). thickness defines how wide the arc is drawn. start_angle is the angle in radians at which the arc starts. end_angle is the angle in radians at which the arc ends. segments affects how smooth/round the arc is. c is the fill color of the arc.

fn (Context) draw_arc_line #

fn (ctx Context) draw_arc_line(x f32, y f32, radius f32, start_angle f32, end_angle f32, segments int,
	c gx.Color)

draw_arc_line draws a line arc. x,y defines the end point of the arc (center of the circle that the arc is part of). radius defines the radius of the arc (length from the center point where the arc is drawn). start_angle is the angle in radians at which the arc starts. end_angle is the angle in radians at which the arc ends. segments affects how smooth/round the arc is. c is the color of the arc/outline.

fn (Context) draw_circle_empty #

fn (ctx &Context) draw_circle_empty(x f32, y f32, radius f32, c gx.Color)

draw_circle_empty draws the outline of a circle. x,y defines the center of the circle. radius defines the radius of the circle. c is the color of the outline.

fn (Context) draw_circle_filled #

fn (ctx &Context) draw_circle_filled(x f32, y f32, radius f32, c gx.Color)

draw_circle_filled draws a filled circle. x,y defines the center of the circle. radius defines the radius of the circle. c is the fill color.

fn (Context) draw_circle_line #

fn (ctx &Context) draw_circle_line(x f32, y f32, radius int, segments int, c gx.Color)

draw_circle_line draws the outline of a circle with a specific number of segments. x,y defines the center of the circle. radius defines the radius of the circle. segments affects how smooth/round the circle is. c is the color of the outline.

fn (Context) draw_circle_with_segments #

fn (ctx &Context) draw_circle_with_segments(x f32, y f32, radius f32, segments int, c gx.Color)

draw_circle_with_segments draws a filled circle with a specific number of segments. x,y defines the center of the circle. radius defines the radius of the circle. segments affects how smooth/round the circle is. c is the fill color.

fn (Context) draw_convex_poly #

fn (ctx &Context) draw_convex_poly(points []f32, c gx.Color)

draw_convex_poly draws a convex polygon, given an array of points, and a color. NOTE that the points must be given in clockwise winding order. The contents of the points array should be x and y coordinate pairs.

fn (Context) draw_cubic_bezier #

fn (ctx &Context) draw_cubic_bezier(points []f32, c gx.Color)

draw_cubic_bezier draws a cubic Bézier curve, also known as a spline, from four points. The four points is provided as one points array which contains a stream of point pairs (x and y coordinates). Thus a cubic Bézier could be declared as: points := [x1, y1, control_x1, control_y1, control_x2, control_y2, x2, y2]. Please see draw_cubic_bezier_in_steps to control the amount of steps (segments) used to draw the curve.

fn (Context) draw_cubic_bezier_in_steps #

fn (ctx &Context) draw_cubic_bezier_in_steps(points []f32, steps u32, c gx.Color)

draw_cubic_bezier_in_steps draws a cubic Bézier curve, also known as a spline, from four points. The smoothness of the curve can be controlled with the steps parameter. steps determines how many iterations is taken to draw the curve. The four points is provided as one points array which contains a stream of point pairs (x and y coordinates). Thus a cubic Bézier could be declared as: points := [x1, y1, control_x1, control_y1, control_x2, control_y2, x2, y2].

fn (Context) draw_cubic_bezier_recursive #

fn (ctx &Context) draw_cubic_bezier_recursive(points []f32, c Color)

draw_cubic_bezier_recursive draws a cubic Bézier curve, also known as a spline, from four points, where the first and the last points, will be part of the curve, and the middle 2 points are control ones. Unlike draw_cubic_bezier_in_steps, this method does not use a fixed number of steps for the whole curve, but tries to produce more tesselation points dynamically for the curvier parts.

fn (Context) draw_cubic_bezier_recursive_scalar #

fn (ctx &Context) draw_cubic_bezier_recursive_scalar(x1 f32, y1 f32, x2 f32, y2 f32, x3 f32, y3 f32,
	x4 f32, y4 f32, c Color)

draw_cubic_bezier_recursive_scalar is the same as draw_cubic_bezier_recursive, except that the points are given as indiviual x,y f32 scalar parameters, and not in a single dynamic array parameter.

fn (Context) draw_ellipse_empty #

fn (ctx &Context) draw_ellipse_empty(x f32, y f32, rw f32, rh f32, c gx.Color)

draw_ellipse_empty draws the outline of an ellipse. x,y defines the center of the ellipse. rw defines the width radius of the ellipse. rh defines the height radius of the ellipse. c is the color of the outline.

fn (Context) draw_ellipse_filled #

fn (ctx &Context) draw_ellipse_filled(x f32, y f32, rw f32, rh f32, c gx.Color)

draw_ellipse_filled draws an opaque ellipse. x,y defines the center of the ellipse. rw defines the width radius of the ellipse. rh defines the height radius of the ellipse. c is the fill color.

fn (Context) draw_image #

fn (ctx &Context) draw_image(x f32, y f32, width f32, height f32, img_ &Image)

draw_image draws the provided image onto the screen.

fn (Context) draw_image_3d #

fn (ctx &Context) draw_image_3d(x f32, y f32, z f32, width f32, height f32, img_ &Image)

draw_image_3d draws an image with a z depth

fn (Context) draw_image_by_id #

fn (ctx &Context) draw_image_by_id(x f32, y f32, width f32, height f32, id int)

draw_image_by_id draws an image by its id

fn (Context) draw_image_flipped #

fn (ctx &Context) draw_image_flipped(x f32, y f32, width f32, height f32, img_ &Image)

draw_image_flipped draws the provided image flipped horizontally (use draw_image_with_config to flip vertically)

fn (Context) draw_image_part #

fn (ctx &Context) draw_image_part(img_rect Rect, part_rect Rect, img_ &Image)

Draw part of an image using uv coordinates img_rect is the size and position (in pixels on screen) of the displayed rectangle (ie the draw_image args) part_rect is the size and position (in absolute pixels in the image) of the wanted part eg. On a 600600 context, to display only the first 400400 pixels of a 2000*2000 image on the entire context surface, call : draw_image_part(Rect{0, 0, 600, 600}, Rect{0, 0, 400, 400}, img)

fn (Context) draw_image_with_config #

fn (ctx &Context) draw_image_with_config(config DrawImageConfig)

draw_image_with_config takes in a config that details how the provided image should be drawn onto the screen

fn (Context) draw_line #

fn (ctx &Context) draw_line(x f32, y f32, x2 f32, y2 f32, c gx.Color)

draw_line draws a line between the points x,y and x2,y2 in color c.

fn (Context) draw_line_with_config #

fn (ctx &Context) draw_line_with_config(x f32, y f32, x2 f32, y2 f32, config PenConfig)

draw_line_with_config draws a line between the points x,y and x2,y2 using PenConfig.

fn (Context) draw_pixel #

fn (ctx &Context) draw_pixel(x f32, y f32, c gx.Color, params DrawPixelConfig)

draw_pixel draws one pixel on the screen.

NOTE calling this function frequently is very inefficient, for drawing shapes it's recommended to draw whole primitives with functions like draw_rect_empty or draw_triangle_empty etc.

fn (Context) draw_pixels #

fn (ctx &Context) draw_pixels(points []f32, c gx.Color, params DrawPixelConfig)

draw_pixels draws pixels from an array of points [x, y, x2, y2, etc...]

NOTE calling this function frequently is very inefficient, for drawing shapes it's recommended to draw whole primitives with functions like draw_rect_empty or draw_triangle_empty etc.

fn (Context) draw_poly_empty #

fn (ctx &Context) draw_poly_empty(points []f32, c gx.Color)

draw_poly_empty draws the outline of a polygon, given an array of points, and a color. NOTE that the points must be given in clockwise winding order.

fn (Context) draw_polygon_filled #

fn (ctx &Context) draw_polygon_filled(x f32, y f32, size f32, edges int, rotation f32, c gx.Color)

draw_polygon_filled draws a filled polygon. x,y defines the center of the polygon. size defines the size of the polygon. edges defines number of edges in the polygon. rotation defines rotation of the polygon. c is the fill color.

fn (Context) draw_rect #

fn (ctx &Context) draw_rect(p DrawRectParams)

fn (Context) draw_rect_empty #

fn (ctx &Context) draw_rect_empty(x f32, y f32, w f32, h f32, c gx.Color)

draw_rect_empty draws the outline of a rectangle. x,y is the top-left corner of the rectangle. w is the width, h is the height and c is the color of the outline.

fn (Context) draw_rect_filled #

fn (ctx &Context) draw_rect_filled(x f32, y f32, w f32, h f32, c gx.Color)

draw_rect_filled draws a filled rectangle. x,y is the top-left corner of the rectangle. w is the width, h is the height and c is the color of the fill.

fn (Context) draw_rounded_rect_empty #

fn (ctx &Context) draw_rounded_rect_empty(x f32, y f32, w f32, h f32, radius f32, c gx.Color)

draw_rounded_rect_empty draws the outline of a rounded rectangle with a thickness of 1 px. x,y is the top-left corner of the rectangle. w is the width, h is the height. radius is the radius of the corner-rounding in pixels. c is the color of the outline.

fn (Context) draw_rounded_rect_filled #

fn (ctx &Context) draw_rounded_rect_filled(x f32, y f32, w f32, h f32, radius f32, c gx.Color)

draw_rounded_rect_filled draws a filled rounded rectangle. x,y is the top-left corner of the rectangle. w is the width, h is the height . radius is the radius of the corner-rounding in pixels. c is the color of the filled.

fn (Context) draw_slice_empty #

fn (ctx &Context) draw_slice_empty(x f32, y f32, radius f32, start_angle f32, end_angle f32, segments int,
	c gx.Color)

draw_slice_empty draws the outline of a circle slice/pie

fn (Context) draw_slice_filled #

fn (ctx &Context) draw_slice_filled(x f32, y f32, radius f32, start_angle f32, end_angle f32, segments int,
	c gx.Color)

draw_slice_filled draws a filled circle slice/pie x,y defines the end point of the slice (center of the circle that the slice is part of). radius defines the radius ("length") of the slice. start_angle is the angle in radians at which the slice starts. end_angle is the angle in radians at which the slice ends. segments affects how smooth/round the slice is. c is the fill color.

fn (Context) draw_square_empty #

fn (ctx &Context) draw_square_empty(x f32, y f32, s f32, c gx.Color)

draw_square_empty draws the outline of a square. x,y is the top-left corner of the square. s is the length of each side of the square. c is the color of the outline.

fn (Context) draw_square_filled #

fn (ctx &Context) draw_square_filled(x f32, y f32, s f32, c gx.Color)

draw_square_filled draws a filled square. x,y is the top-left corner of the square. s is the length of each side of the square. c is the fill color.

fn (Context) draw_text #

fn (ctx &Context) draw_text(x int, y int, text_ string, cfg gx.TextCfg)

draw_text draws the string in text_ starting at top-left position x,y. Text settings can be provided with cfg.

fn (Context) draw_text2 #

fn (ctx &Context) draw_text2(p DrawTextParams)

fn (Context) draw_text_def #

fn (ctx &Context) draw_text_def(x int, y int, text string)

draw_text draws the string in text_ starting at top-left position x,y using default text settings.

fn (Context) draw_text_default #

fn (ctx &Context) draw_text_default(x int, y int, text string)

default draw_text (draw_text_def but without set_text_cfg)

fn (Context) draw_triangle_empty #

fn (ctx &Context) draw_triangle_empty(x f32, y f32, x2 f32, y2 f32, x3 f32, y3 f32, c gx.Color)

draw_triangle_empty draws the outline of a triangle. x,y defines the first point x2,y2 defines the second point x3,y3 defines the third point c is the color of the outline.

fn (Context) draw_triangle_filled #

fn (ctx &Context) draw_triangle_filled(x f32, y f32, x2 f32, y2 f32, x3 f32, y3 f32, c gx.Color)

draw_triangle_filled draws a filled triangle. x,y defines the first point x2,y2 defines the second point x3,y3 defines the third point c is the color of the outline.

fn (Context) end #

fn (ctx &Context) end(options EndOptions)

end finishes all the drawing for the context ctx. All accumulated draw calls before ctx.end(), will be done in a separate Sokol pass.

Note: each Sokol pass, has a limit on the number of draw calls, that can be done in it.Once that limit is reached, the whole pass will not draw anything, which can be frustrating.

To overcome this limitation, you may use several passes, when you want to make thousands of draw calls (for example, if you need to draw thousands of circles/rectangles/sprites etc), where each pass will render just a limited amount of primitives.

In the context of the gg module (without dropping to using sgl and gfx directly), it means, that you will need a new pair of ctx.begin() and ctx.end() calls, surrounding all the draw calls, that should be done in each pass.

The default ctx.end() is equivalent to ctx.end(how:.clear). It will erase the existing rendered content with the background color, before drawing anything else. You can call ctx.end(how:.passthru) for a pass, that will not erase the previously rendered content in the context.

fn (Context) get_cached_image_by_idx #

fn (mut ctx Context) get_cached_image_by_idx(image_idx int) &Image

get_cached_image_by_idx returns a cached Image identified by image_idx.

See also: cache_image See also: remove_cached_image_by_idx

fn (Context) has_text_style #

fn (ctx &Context) has_text_style() bool

fn (Context) new_streaming_image #

fn (mut ctx Context) new_streaming_image(w int, h int, channels int, sicfg StreamingImageConfig) int

new_streaming_image returns a cached image_idx of a special image, that can be updated each frame by calling: gg.update_pixel_data(image_idx, buf) ... where buf is a pointer to the actual pixel data for the image.

Note: you still need to call app.gg.draw_image after that, to actually draw it.

Note: Sokol needs to be setup, before calling this function. In practice, this often means, that you have to call it once in the init_fn callback of gg.new_context, or gg.start, and then store the result in your app instance.

fn (Context) quit #

fn (ctx &Context) quit()

quit closes the context window and exits the event loop for it

fn (Context) record_frame #

fn (mut ctx Context) record_frame()

record_frame records the current frame to a file. record_frame acts according to settings specified in gg.recorder_settings.

fn (Context) refresh_ui #

fn (mut ctx Context) refresh_ui()

refresh_ui requests a complete re-draw of the window contents.

fn (Context) remove_cached_image_by_idx #

fn (mut ctx Context) remove_cached_image_by_idx(image_idx int)

remove_cached_image_by_idx removes an Image identified by image_idx from the image cache.

See also: cache_image See also: get_cached_image_by_idx

fn (Context) resize #

fn (mut ctx Context) resize(width int, height int)

Resize the context's Window

fn (Context) run #

fn (mut ctx Context) run()

run starts the main loop of the context.

fn (Context) scissor_rect #

fn (ctx &Context) scissor_rect(x int, y int, w int, h int)

required for ui.DrawDevice interface (with &gg.Context as an instance)

fn (Context) set_bg_color #

fn (mut ctx Context) set_bg_color(c gx.Color)

set_bg_color sets the color of the window background to c.

fn (Context) set_text_cfg #

fn (ctx &Context) set_text_cfg(cfg gx.TextCfg)

set_text_cfg sets the current text configuration

fn (Context) set_text_style #

fn (ctx &Context) set_text_style(font_name string, font_path string, size int, color gx.Color, align int,
	vertical_align int)

fn (Context) show_fps #

fn (ctx &Context) show_fps()

fn (Context) text_height #

fn (ctx &Context) text_height(s string) int

text_height returns the height of the string s in pixels.

fn (Context) text_size #

fn (ctx &Context) text_size(s string) (int, int)

text_size returns the width and height of the string s in pixels.

fn (Context) text_width #

fn (ctx &Context) text_width(s string) int

text_width returns the width of the string s in pixels.

fn (Context) update_pixel_data #

fn (mut ctx Context) update_pixel_data(cached_image_idx int, buf &u8)

update_pixel_data is a helper for working with image streams (i.e. images, that are updated dynamically by the CPU on each frame)

fn (Context) window_size #

fn (ctx Context) window_size() Size

window_size returns the current dimensions of the window.

struct DrawImageConfig #

struct DrawImageConfig {
pub mut:
	flip_x    bool // set to true, if you need to flip the image horizontally (around a vertical axis), <- will become ->
	flip_y    bool // set to true, if you need to flip the image vertically (around a horizontal axiz), -\/- will become -/\-
	img       &Image = unsafe { nil }
	img_id    int
	img_rect  Rect // defines the size and position on image when rendering to the screen
	part_rect Rect // defines the size and position of part of the image to use when rendering
	rotate    f32 @[deprecated: 'use `rotation` instead of `rotate`'; deprecated_after: '2024-07-30']
	z         f32
	color     gx.Color    = gx.white
	effect    ImageEffect = .alpha

	rotation f32 // the amount to rotate the image in degrees, counterclockwise. Use a negative value, to rotate it clockwise.
}

DrawImageConfig struct defines the various options that can be used to draw an image onto the screen

struct DrawPixelConfig #

@[params]
struct DrawPixelConfig {
pub mut:
	size f32 = 1.0
}

struct DrawRectParams #

@[params]
struct DrawRectParams {
pub:
	x          f32
	y          f32
	w          f32
	h          f32
	color      gx.Color   = gx.black
	style      PaintStyle = .fill
	is_rounded bool
	radius     f32
}

struct DrawTextParams #

@[params]
struct DrawTextParams {
pub:
	x    int
	y    int
	text string

	color          Color              = gx.black
	size           int                = 16
	align          gx.HorizontalAlign = .left
	vertical_align gx.VerticalAlign   = .top
	max_width      int
	family         string
	bold           bool
	mono           bool
	italic         bool
}

struct EndOptions #

@[params]
struct EndOptions {
pub:
	how EndEnum
}

struct Event #

struct Event {
pub mut:
	frame_count        u64
	typ                sapp.EventType
	key_code           KeyCode
	char_code          u32
	key_repeat         bool
	modifiers          u32
	mouse_button       MouseButton
	mouse_x            f32
	mouse_y            f32
	mouse_dx           f32
	mouse_dy           f32
	scroll_x           f32
	scroll_y           f32
	num_touches        int
	touches            [8]TouchPoint
	window_width       int
	window_height      int
	framebuffer_width  int
	framebuffer_height int
}

struct FPSConfig #

struct FPSConfig {
pub mut:
	x                int  // horizontal position on screen
	y                int  // vertical position on screen
	width            int  // minimum width
	height           int  // minimum height
	show             bool // do not show by default, use `-d show_fps` or set it manually in your app to override with: `app.gg.fps.show = true`
	text_config      gx.TextCfg = gx.TextCfg{
		color:          gx.yellow
		size:           20
		align:          .center
		vertical_align: .middle
	}
	background_color gx.Color = gx.Color{
		r: 0
		g: 0
		b: 0
		a: 128
	}
}

struct Image #

@[heap]
struct Image {
pub mut:
	id          int
	width       int
	height      int
	nr_channels int
	ok          bool
	data        voidptr
	ext         string
	simg_ok     bool
	simg        gfx.Image
	ssmp        gfx.Sampler
	path        string
}

Image holds the fields and data needed to represent a bitmap/pixel based image in memory.

fn (Image) init_sokol_image #

fn (mut img Image) init_sokol_image() &Image

init_sokol_image initializes this Image for use with the sokol graphical backend system.

fn (Image) update_pixel_data #

fn (mut img Image) update_pixel_data(buf &u8)

update_pixel_data updates the sokol specific pixel data associated with this Image.

struct PenConfig #

struct PenConfig {
pub:
	color     gx.Color
	line_type PenLineType = .solid
	thickness f32         = 1
}

struct PipelineContainer #

@[heap]
struct PipelineContainer {
pub mut:
	alpha sgl.Pipeline
	add   sgl.Pipeline
}

struct Rect #

struct Rect {
pub:
	x      f32
	y      f32
	width  f32
	height f32
}

Rect represents a rectangular shape in gg.

struct SSRecorderSettings #

@[heap]
struct SSRecorderSettings {
pub mut:
	stop_at_frame     i64 = -1
	screenshot_frames []u64
	screenshot_folder string
	screenshot_prefix string
}

struct Size #

struct Size {
pub mut:
	width  int
	height int
}

struct StreamingImageConfig #

struct StreamingImageConfig {
pub:
	pixel_format gfx.PixelFormat = .rgba8
	wrap_u       gfx.Wrap        = .clamp_to_edge
	wrap_v       gfx.Wrap        = .clamp_to_edge
	min_filter   gfx.Filter      = .linear
	mag_filter   gfx.Filter      = .linear
	num_mipmaps  int             = 1
	num_slices   int             = 1
}