v/vlib/gg
Eliyaan (Nopana) bbb61ab368
Some checks failed
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
native backend CI / native-backend-ubuntu (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
native backend CI / native-backend-windows (push) Waiting to run
Shy and PV CI / v-compiles-puzzle-vibes (push) Waiting to run
Sanitized CI / sanitize-undefined-clang (push) Waiting to run
Sanitized CI / sanitize-undefined-gcc (push) Waiting to run
Sanitized CI / tests-sanitize-address-clang (push) Waiting to run
Sanitized CI / sanitize-address-msvc (push) Waiting to run
Sanitized CI / sanitize-address-gcc (push) Waiting to run
Sanitized CI / sanitize-memory-clang (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-macos (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
wasm backend CI / wasm-backend (ubuntu-22.04) (push) Waiting to run
wasm backend CI / wasm-backend (windows-2022) (push) Waiting to run
Workflow Lint / lint-yml-workflows (push) Has been cancelled
gg, gx: deprecate gx and replace all occurences with gg (which now contains all the functionality of gx) (#24966)
2025-08-14 19:53:56 +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, gx: deprecate gx and replace all occurences with gg (which now contains all the functionality of gx) (#24966) 2025-08-14 19:53:56 +03: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
color.v gg, gx: deprecate gx and replace all occurences with gg (which now contains all the functionality of gx) (#24966) 2025-08-14 19:53:56 +03:00
color_test.v gg, gx: deprecate gx and replace all occurences with gg (which now contains all the functionality of gx) (#24966) 2025-08-14 19:53:56 +03:00
draw.c.v gg, gx: deprecate gx and replace all occurences with gg (which now contains all the functionality of gx) (#24966) 2025-08-14 19:53:56 +03: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, gx: deprecate gx and replace all occurences with gg (which now contains all the functionality of gx) (#24966) 2025-08-14 19:53:56 +03:00
gg.js.v gg, gx: deprecate gx and replace all occurences with gg (which now contains all the functionality of gx) (#24966) 2025-08-14 19:53:56 +03:00
gg.v gg, gx: deprecate gx and replace all occurences with gg (which now contains all the functionality of gx) (#24966) 2025-08-14 19:53:56 +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, gx: deprecate gx and replace all occurences with gg (which now contains all the functionality of gx) (#24966) 2025-08-14 19:53:56 +03:00
gg_darwin.m gg, gx: deprecate gx and replace all occurences with gg (which now contains all the functionality of gx) (#24966) 2025-08-14 19:53:56 +03:00
gg_ui.c.v gg, gx: deprecate gx and replace all occurences with gg (which now contains all the functionality of gx) (#24966) 2025-08-14 19:53:56 +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 gg, gx: deprecate gx and replace all occurences with gg (which now contains all the functionality of gx) (#24966) 2025-08-14 19:53:56 +03:00
keyboard.v gg: add is_key_down/1 helper method 2025-03-13 11:40:39 +02:00
README.md gg, gx: deprecate gx and replace all occurences with gg (which now contains all the functionality of gx) (#24966) 2025-08-14 19:53:56 +03: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 gg, gx: deprecate gx and replace all occurences with gg (which now contains all the functionality of gx) (#24966) 2025-08-14 19:53:56 +03:00
text_rendering.js.v gg, gx: deprecate gx and replace all occurences with gg (which now contains all the functionality of gx) (#24966) 2025-08-14 19:53:56 +03:00
text_rendering.v gg, gx: deprecate gx and replace all occurences with gg (which now contains all the functionality of gx) (#24966) 2025-08-14 19:53:56 +03: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

fn main() {
	mut context := gg.new_context(
		bg_color:     gg.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],
		gg.blue)
	ctx.draw_poly_empty([f32(50.0), 50.0, 70.0, 60.0, 90.0, 80.0, 70.0, 110.0], gg.black)
	ctx.draw_triangle_filled(450, 142, 530, 280, 370, 280, gg.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.