v/vlib/gg
2025-03-09 08:45:21 +02: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 vlib: fix typos and formatting (#19649) 2023-10-25 11:19: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
draw.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
draw_fns_api_test.v vfmt: change all '$expr' to '${expr}' (#16428) 2022-11-15 16:53:13 +03:00
enums.v fmt: fix alignment of enumeration types (#21999) 2024-08-07 15:46:50 +03:00
gg.c.v gg,examples: use a timer to limit the rate of updates in breakout, instead of a separate thread, restore ability to run in a browser through emscripten 2025-03-09 08:45:21 +02:00
gg.js.v vlib: remove modules/functions/fields, deprecated in 2023 (#22750) 2024-11-17 20:09:21 +02:00
gg.v gg: change the type of PenConfig.thickness to f32 2024-09-08 12:05:04 +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 all: fix typos (#21089) 2024-03-25 12:18:27 +02: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 gg: mark create_image_with_size as deprecated (image resizing is done by stbi.resize_uint8/3, with a different fn signature) (#23580) 2025-01-26 08:04:17 +02:00
image.js.v all: update attributes to use new syntax 2023-11-15 16:16:01 +11:00
image.v fmt: fix alignment of struct init fields (#22025) 2024-08-11 09:11:24 +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
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.