1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/examples/sokol/drawing.v

84 lines
1.8 KiB
V
Raw Normal View History

2020-01-16 22:45:47 +03:00
import sokol
import sokol.sapp
import sokol.gfx
import sokol.sgl
struct AppState {
2020-04-16 16:53:34 +03:00
pass_action C.sg_pass_action
2020-01-16 22:45:47 +03:00
}
const (
used_import = sokol.used_import
)
2020-01-16 22:45:47 +03:00
fn main() {
state := &AppState{
pass_action: gfx.create_clear_pass(0.1, 0.1, 0.1, 1.0)
}
title := 'Sokol Drawing Template'
2020-04-16 16:53:34 +03:00
desc := C.sapp_desc{
2020-01-16 22:45:47 +03:00
user_data: state
init_userdata_cb: init
frame_userdata_cb: frame
window_title: title.str
html5_canvas_name: title.str
2020-01-16 22:45:47 +03:00
}
sapp.run(&desc)
}
fn init(user_data voidptr) {
2020-04-16 16:53:34 +03:00
desc := C.sg_desc{
mtl_device: sapp.metal_get_device()
mtl_renderpass_descriptor_cb: sapp.metal_get_renderpass_descriptor
mtl_drawable_cb: sapp.metal_get_drawable
d3d11_device: sapp.d3d11_get_device()
d3d11_device_context: sapp.d3d11_get_device_context()
d3d11_render_target_view_cb: sapp.d3d11_get_render_target_view
d3d11_depth_stencil_view_cb: sapp.d3d11_get_depth_stencil_view
2020-01-16 22:45:47 +03:00
}
gfx.setup(&desc)
2020-04-16 16:53:34 +03:00
sgl_desc := C.sgl_desc_t{}
2020-01-16 22:45:47 +03:00
sgl.setup(&sgl_desc)
}
fn frame(user_data voidptr) {
// println('frame')
2020-01-16 22:45:47 +03:00
state := &AppState(user_data)
draw()
gfx.begin_default_pass(&state.pass_action, sapp.width(), sapp.height())
sgl.draw()
gfx.end_pass()
gfx.commit()
}
fn draw() {
// first, reset and setup ortho projection
sgl.defaults()
sgl.matrix_mode_projection()
sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
sgl.c4b(255, 0, 0, 128)
draw_hollow_rect(10, 10, 100, 30)
sgl.c4b(25, 150, 0, 128)
draw_filled_rect(10, 150, 80, 40)
// line(0, 0, 500, 500)
2020-01-16 22:45:47 +03:00
}
fn draw_hollow_rect(x, y, w, h f32) {
sgl.begin_line_strip()
sgl.v2f(x, y)
2020-01-16 22:45:47 +03:00
sgl.v2f(x + w, y)
sgl.v2f(x + w, y + h)
sgl.v2f(x, y + h)
sgl.v2f(x, y)
sgl.end()
2020-01-16 22:45:47 +03:00
}
fn draw_filled_rect(x, y, w, h f32) {
sgl.begin_quads()
sgl.v2f(x, y)
sgl.v2f(x + w, y)
sgl.v2f(x + w, y + h)
sgl.v2f(x, y + h)
sgl.end()
2020-01-16 22:45:47 +03:00
}