v/vlib/gg
2025-07-23 08:50:31 +03:00
..
m4 examples, gg, gg.m4: fix VFLAGS='-no-skip-unused -cstrict -cc clang-18' v should-compile-all examples/sokol/ too 2025-03-06 19:58:25 +02:00
testdata gg: make draw_rect_empty/5 draw more exact borders, independent of the device, and fitting the draw_rect_filled/5 shapes (#24024) 2025-03-23 21:38:15 +02:00
bezier.c.v fmt: implement wrapping function's super long arguments (fix #15545, fix #21643) (#21782) 2024-07-02 23:10:00 +03:00
draw.c.v gg: make draw_rect_empty/5 draw more exact borders, independent of the device, and fitting the draw_rect_filled/5 shapes (#24024) 2025-03-23 21:38:15 +02:00
draw_fns_api_test.v tools: cleanup entries from the hardcoded skip_files list in common.v (used by v test, v test-self etc); use the new // vtest build: syntax to mark the tests instead (#23918) 2025-03-13 19:51:51 +02:00
enums.v fmt: fix alignment of enumeration types (#21999) 2024-08-07 15:46:50 +03:00
gg.c.v gg: allow for -d gg_memory_trace_frame to be used in combination with -prealloc -d prealloc_dump 2025-06-26 12:20:16 +03:00
gg.js.v vlib: remove modules/functions/fields, deprecated in 2023 (#22750) 2024-11-17 20:09:21 +02:00
gg.v markused: skip unused symbols, dump fns and generic specialization (fix #24921) (fix #24927) (#24924) 2025-07-23 08:50:31 +03:00
gg_android_outside_termux.c.v Revert "gg: fix android compilation for ~/.vmodules/ui/examples/rectangles.v" 2022-07-06 16:19:40 +03:00
gg_darwin.c.v gg: fix empty circle in native; http: post_form_with_cookies; veb: print veb action in html errors 2024-03-11 18:21:06 +03:00
gg_darwin.m gg: fix screen_size() on macos with multiple displays 2025-04-07 04:18:18 +03:00
gg_ui.c.v fmt: implement wrapping function's super long arguments (fix #15545, fix #21643) (#21782) 2024-07-02 23:10:00 +03:00
image.c.v markused: skip unused symbols, dump fns and generic specialization (fix #24921) (fix #24927) (#24924) 2025-07-23 08:50:31 +03:00
image.js.v all: update attributes to use new syntax 2023-11-15 16:16:01 +11:00
image.v all: remove deprecations made before 2024-11-06 2025-05-05 09:31:32 +03:00
import_gx.v gg: add a temporary import for gx + a gg.Color type alias for gx.Color, without using it 2022-01-29 21:42:19 +02:00
keyboard.v gg: add is_key_down/1 helper method 2025-03-13 11:40:39 +02:00
README.md gg,sokol,examples: add example of overriding _SGL_DEFAULT_MAX_VERTICES in code 2025-01-06 14:54:56 +02:00
recorder.c.v fmt: remove the prefixed module name of const names, that are in the same module (related #22183) (#22185) 2024-09-10 11:25:56 +03:00
recorder.js.v all: update attributes to use new syntax 2023-11-15 16:16:01 +11:00
recorder.v all: update attributes to use new syntax 2023-11-15 16:16:01 +11:00
text_rendering.c.v examples,gg: modify all remaining calls to fons.add_font_mem/3 to use an array.clone() 2025-02-07 14:39:36 +02:00
text_rendering.js.v gg: add text rendering, keyboard event handling for JS and other fixes (#12932) 2021-12-22 12:26:52 +02:00
text_rendering.v all: update copyright year (#20334) 2024-01-01 23:29:54 +02:00

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()
}

Troubleshooting

A common problem, if you draw a lot of primitive elements in the same frame, is that there is a chance that your program can exceed the maximum allowed amount of vertices and commands, imposed by sokol. The symptom is that your frame will be suddenly black, after it becomes more complex. Sokol's default for vertices is 131072. Sokol's default for commands is 32768.

To solve that, you can try adding these lines at the top of your program: #flag -D_SGL_DEFAULT_MAX_VERTICES=4194304 #flag -D_SGL_DEFAULT_MAX_COMMANDS=65536 You can see an example of that in: https://github.com/vlang/v/blob/master/examples/gg/many_thousands_of_circles_overriding_max_vertices.v

Another approach is to use several draw passes, and limit the amount of draw calls that you make in each, demonstrated in: https://github.com/vlang/v/blob/master/examples/gg/many_thousands_of_circles.v

Another approach to that problem, is to draw everything yourself in a streaming texture, then upload that streaming texture as a single draw command to the GPU. You can see an example of that done in: https://github.com/vlang/v/blob/master/examples/gg/random.v

A third approach, is to only upload your changing inputs to the GPU, and do all the calculations and drawing there in shaders.