2020-01-16 20:45:47 +01:00
|
|
|
import sokol
|
|
|
|
import sokol.sapp
|
|
|
|
import sokol.gfx
|
|
|
|
import sokol.sgl
|
|
|
|
|
|
|
|
struct AppState {
|
2022-01-02 19:36:01 +01:00
|
|
|
pass_action gfx.PassAction
|
2020-01-16 20:45:47 +01:00
|
|
|
}
|
|
|
|
|
2020-01-17 20:05:45 +01:00
|
|
|
const (
|
|
|
|
used_import = sokol.used_import
|
|
|
|
)
|
|
|
|
|
2020-01-16 20:45:47 +01:00
|
|
|
fn main() {
|
|
|
|
state := &AppState{
|
|
|
|
pass_action: gfx.create_clear_pass(0.1, 0.1, 0.1, 1.0)
|
|
|
|
}
|
2020-01-17 20:05:45 +01:00
|
|
|
title := 'Sokol Drawing Template'
|
2021-12-26 12:02:51 +01:00
|
|
|
desc := sapp.Desc{
|
2022-04-10 09:39:55 +02:00
|
|
|
width: 640
|
|
|
|
height: 480
|
2020-01-16 20:45:47 +01:00
|
|
|
user_data: state
|
|
|
|
init_userdata_cb: init
|
|
|
|
frame_userdata_cb: frame
|
2020-01-17 20:05:45 +01:00
|
|
|
window_title: title.str
|
|
|
|
html5_canvas_name: title.str
|
2020-01-16 20:45:47 +01:00
|
|
|
}
|
|
|
|
sapp.run(&desc)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init(user_data voidptr) {
|
2022-01-02 19:36:01 +01:00
|
|
|
desc := sapp.create_desc() // gfx.Desc{
|
2020-01-16 20:45:47 +01:00
|
|
|
gfx.setup(&desc)
|
2022-01-03 14:05:24 +01:00
|
|
|
sgl_desc := sgl.Desc{}
|
2020-01-16 20:45:47 +01:00
|
|
|
sgl.setup(&sgl_desc)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn frame(user_data voidptr) {
|
2020-01-17 20:05:45 +01:00
|
|
|
// println('frame')
|
2020-01-16 20:45:47 +01: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)
|
2022-04-10 09:39:55 +02:00
|
|
|
draw_hollow_rect(220, 140, 200, 200)
|
|
|
|
sgl.c4b(25, 150, 255, 128)
|
|
|
|
draw_filled_rect(270, 190, 100, 100)
|
2020-01-17 20:05:45 +01:00
|
|
|
// line(0, 0, 500, 500)
|
2020-01-16 20:45:47 +01:00
|
|
|
}
|
|
|
|
|
2020-10-26 12:14:21 +01:00
|
|
|
fn draw_hollow_rect(x f32, y f32, w f32, h f32) {
|
2020-01-17 20:05:45 +01:00
|
|
|
sgl.begin_line_strip()
|
|
|
|
sgl.v2f(x, y)
|
2020-01-16 20:45:47 +01:00
|
|
|
sgl.v2f(x + w, y)
|
|
|
|
sgl.v2f(x + w, y + h)
|
|
|
|
sgl.v2f(x, y + h)
|
|
|
|
sgl.v2f(x, y)
|
2020-01-17 20:05:45 +01:00
|
|
|
sgl.end()
|
2020-01-16 20:45:47 +01:00
|
|
|
}
|
|
|
|
|
2020-10-26 12:14:21 +01:00
|
|
|
fn draw_filled_rect(x f32, y f32, w f32, h f32) {
|
2020-01-17 20:05:45 +01:00
|
|
|
sgl.begin_quads()
|
2020-03-26 08:54:33 +01:00
|
|
|
sgl.v2f(x, y)
|
|
|
|
sgl.v2f(x + w, y)
|
|
|
|
sgl.v2f(x + w, y + h)
|
|
|
|
sgl.v2f(x, y + h)
|
2020-01-17 20:05:45 +01:00
|
|
|
sgl.end()
|
2020-01-16 20:45:47 +01:00
|
|
|
}
|