2020-01-23 21:04:46 +01:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2020-07-06 20:29:05 +02:00
|
|
|
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
|
2019-06-22 20:20:28 +02:00
|
|
|
module gg
|
|
|
|
|
2020-04-26 11:42:44 +02:00
|
|
|
import gx
|
|
|
|
import os
|
2020-06-04 16:05:12 +02:00
|
|
|
import sokol
|
|
|
|
import sokol.sapp
|
|
|
|
import sokol.sgl
|
|
|
|
import sokol.gfx
|
2020-08-01 23:40:25 +02:00
|
|
|
import stbi
|
2019-06-22 20:20:28 +02:00
|
|
|
|
2020-08-01 23:40:25 +02:00
|
|
|
// import time
|
|
|
|
pub type FNCb = fn (x voidptr)
|
|
|
|
|
|
|
|
pub type FNEvent = fn (e, x voidptr)
|
|
|
|
|
|
|
|
pub type FNFail = fn (msg string, x voidptr)
|
|
|
|
|
|
|
|
pub type FNKeyDown = fn (c sapp.KeyCode, m sapp.Modifier, x voidptr)
|
|
|
|
|
|
|
|
pub type FNChar = fn (c u32, x voidptr)
|
2019-06-22 20:20:28 +02:00
|
|
|
|
2020-06-04 16:05:12 +02:00
|
|
|
pub struct Config {
|
2019-08-22 23:00:31 +02:00
|
|
|
pub:
|
2020-08-01 23:40:25 +02:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
use_ortho bool
|
|
|
|
retina bool
|
|
|
|
resizable bool
|
|
|
|
user_data voidptr
|
|
|
|
font_size int
|
|
|
|
create_window bool
|
2020-06-04 16:05:12 +02:00
|
|
|
// window_user_ptr voidptr
|
2020-08-01 23:40:25 +02:00
|
|
|
window_title string
|
2020-06-04 19:57:13 +02:00
|
|
|
borderless_window bool
|
2020-08-01 23:40:25 +02:00
|
|
|
always_on_top bool
|
|
|
|
bg_color gx.Color
|
|
|
|
init_fn FNCb = voidptr(0)
|
|
|
|
frame_fn FNCb = voidptr(0)
|
|
|
|
cleanup_fn FNCb = voidptr(0)
|
|
|
|
fail_fn FNFail = voidptr(0)
|
|
|
|
event_fn FNEvent = voidptr(0)
|
|
|
|
keydown_fn FNKeyDown = voidptr(0) // special case of event_fn
|
|
|
|
char_fn FNChar = voidptr(0) // special case of event_fn
|
|
|
|
wait_events bool // set this to true for UIs, to save power
|
|
|
|
fullscreen bool
|
|
|
|
scale f32 = 1.0 // vid needs this
|
|
|
|
// init_text bool
|
|
|
|
font_path string
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 20:26:18 +02:00
|
|
|
pub struct Context {
|
2020-07-06 19:45:00 +02:00
|
|
|
render_text bool
|
2019-08-22 23:00:31 +02:00
|
|
|
pub mut:
|
2020-08-01 23:40:25 +02:00
|
|
|
scale f32 = 1.0 // will get set to 2.0 for retina, will remain 1.0 for normal
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
clear_pass C.sg_pass_action
|
|
|
|
window C.sapp_desc
|
|
|
|
config Config
|
|
|
|
ft &FT
|
2020-07-06 19:45:00 +02:00
|
|
|
font_inited bool
|
2020-06-04 16:05:12 +02:00
|
|
|
}
|
|
|
|
|
2020-08-01 23:40:25 +02:00
|
|
|
pub struct Size {
|
|
|
|
pub:
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
}
|
2020-06-04 19:57:13 +02:00
|
|
|
|
2020-06-04 16:05:12 +02:00
|
|
|
fn gg_init_sokol_window(user_data voidptr) {
|
2020-06-04 20:26:18 +02:00
|
|
|
mut g := &Context(user_data)
|
2020-06-04 16:05:12 +02: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
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2020-06-04 16:05:12 +02:00
|
|
|
gfx.setup(&desc)
|
|
|
|
sgl_desc := C.sgl_desc_t{}
|
|
|
|
sgl.setup(&sgl_desc)
|
2020-06-04 20:09:46 +02:00
|
|
|
g.scale = sapp.dpi_scale()
|
|
|
|
// NB: on older X11, `Xft.dpi` from ~/.Xresources, that sokol uses,
|
|
|
|
// may not be set which leads to sapp.dpi_scale reporting incorrectly 0.0
|
|
|
|
if g.scale < 0.1 {
|
|
|
|
g.scale = 1.0
|
|
|
|
}
|
2020-08-01 23:40:25 +02:00
|
|
|
// is_high_dpi := sapp.high_dpi()
|
|
|
|
// fb_w := sapp.width()
|
|
|
|
// fb_h := sapp.height()
|
|
|
|
// println('g.scale=$g.scale is_high_dpi=$is_high_dpi fb_w=$fb_w fb_h=$fb_h')
|
|
|
|
// if g.config.init_text {
|
2020-07-06 19:45:00 +02:00
|
|
|
if g.config.font_path != '' {
|
2020-08-01 23:40:25 +02:00
|
|
|
// t := time.ticks()
|
|
|
|
g.ft = new_ft({
|
|
|
|
font_path: g.config.font_path
|
|
|
|
scale: sapp.dpi_scale()
|
|
|
|
}) or {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// println('FT took ${time.ticks()-t} ms')
|
2020-07-06 19:45:00 +02:00
|
|
|
g.font_inited = true
|
|
|
|
}
|
2020-06-04 16:05:12 +02:00
|
|
|
if g.config.init_fn != voidptr(0) {
|
2020-07-06 19:45:00 +02:00
|
|
|
g.config.init_fn(g.config.user_data)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 16:05:12 +02:00
|
|
|
fn gg_frame_fn(user_data voidptr) {
|
2020-06-04 20:26:18 +02:00
|
|
|
mut g := &Context(user_data)
|
2020-06-04 16:05:12 +02:00
|
|
|
if g.config.frame_fn != voidptr(0) {
|
2020-07-06 19:45:00 +02:00
|
|
|
g.config.frame_fn(g.config.user_data)
|
2019-08-22 23:00:31 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-19 13:27:44 +02:00
|
|
|
|
2020-06-05 10:46:58 +02:00
|
|
|
// TODO: remove this hacky workaround...
|
|
|
|
// NB: todo_remove_this is needed to workaround a v bug,
|
|
|
|
// where it thinks that &sapp.Event(x) is a function call,
|
|
|
|
// instead of a cast, if v has not yet seen &sapp.Event used
|
|
|
|
// as a parameter type.
|
2020-08-01 23:40:25 +02:00
|
|
|
fn todo_remove_this(e &sapp.Event) {
|
|
|
|
}
|
2020-06-05 10:46:58 +02:00
|
|
|
|
2020-08-01 23:40:25 +02:00
|
|
|
fn gg_event_fn(ce &C.sapp_event, user_data voidptr) {
|
2020-06-05 10:46:58 +02:00
|
|
|
e := &sapp.Event(ce)
|
2020-06-04 20:26:18 +02:00
|
|
|
mut g := &Context(user_data)
|
2020-06-04 16:05:12 +02:00
|
|
|
if g.config.event_fn != voidptr(0) {
|
|
|
|
g.config.event_fn(e, g.config.user_data)
|
2019-08-22 23:00:31 +02:00
|
|
|
}
|
2020-06-05 10:46:58 +02:00
|
|
|
match e.typ {
|
|
|
|
.key_down {
|
|
|
|
if g.config.keydown_fn != voidptr(0) {
|
|
|
|
kdfn := g.config.keydown_fn
|
|
|
|
kdfn(e.key_code, e.modifiers, g.config.user_data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.char {
|
|
|
|
if g.config.char_fn != voidptr(0) {
|
|
|
|
cfn := g.config.char_fn
|
|
|
|
cfn(e.char_code, g.config.user_data)
|
|
|
|
}
|
|
|
|
}
|
2020-08-01 23:40:25 +02:00
|
|
|
else {}
|
2020-06-05 10:46:58 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-08-01 23:40:25 +02:00
|
|
|
fn gg_cleanup_fn(user_data voidptr) {
|
2020-06-04 20:26:18 +02:00
|
|
|
mut g := &Context(user_data)
|
2020-06-04 16:05:12 +02:00
|
|
|
if g.config.cleanup_fn != voidptr(0) {
|
|
|
|
g.config.cleanup_fn(g.config.user_data)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 23:40:25 +02:00
|
|
|
fn gg_fail_fn(msg charptr, user_data voidptr) {
|
2020-06-04 20:26:18 +02:00
|
|
|
mut g := &Context(user_data)
|
2020-06-04 16:05:12 +02:00
|
|
|
vmsg := tos3(msg)
|
|
|
|
if g.config.fail_fn != voidptr(0) {
|
|
|
|
g.config.fail_fn(vmsg, g.config.user_data)
|
2020-08-01 23:40:25 +02:00
|
|
|
} else {
|
2020-06-04 16:05:12 +02:00
|
|
|
eprintln('gg error: $vmsg')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 16:05:12 +02:00
|
|
|
//
|
2020-08-01 23:40:25 +02:00
|
|
|
pub fn new_context(cfg Config) &Context {
|
2020-06-04 20:26:18 +02:00
|
|
|
mut g := &Context{
|
2020-06-04 16:05:12 +02:00
|
|
|
width: cfg.width
|
|
|
|
height: cfg.height
|
2020-08-01 23:40:25 +02:00
|
|
|
clear_pass: gfx.create_clear_pass(f32(cfg.bg_color.r) / 255.0, f32(cfg.bg_color.g) / 255.0,
|
|
|
|
f32(cfg.bg_color.b) / 255.0, 1.0)
|
2020-06-04 16:05:12 +02:00
|
|
|
config: cfg
|
2020-07-06 19:45:00 +02:00
|
|
|
render_text: cfg.font_path != ''
|
|
|
|
ft: 0
|
2020-06-04 16:05:12 +02:00
|
|
|
}
|
2020-08-01 23:40:25 +02:00
|
|
|
// C.printf('new_context() %p\n', cfg.user_data)
|
2020-06-04 16:05:12 +02:00
|
|
|
window := C.sapp_desc{
|
|
|
|
user_data: g
|
|
|
|
init_userdata_cb: gg_init_sokol_window
|
|
|
|
frame_userdata_cb: gg_frame_fn
|
|
|
|
event_userdata_cb: gg_event_fn
|
|
|
|
fail_userdata_cb: gg_fail_fn
|
|
|
|
cleanup_userdata_cb: gg_cleanup_fn
|
|
|
|
window_title: cfg.window_title.str
|
|
|
|
html5_canvas_name: cfg.window_title.str
|
|
|
|
width: cfg.width
|
|
|
|
height: cfg.height
|
2020-06-04 20:09:46 +02:00
|
|
|
high_dpi: true
|
2020-06-04 23:51:54 +02:00
|
|
|
fullscreen: cfg.fullscreen
|
2020-06-04 16:05:12 +02:00
|
|
|
}
|
2020-08-01 23:40:25 +02:00
|
|
|
if cfg.use_ortho {
|
|
|
|
} else {
|
|
|
|
}
|
2020-06-04 16:05:12 +02:00
|
|
|
g.window = window
|
|
|
|
return g
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 20:26:18 +02:00
|
|
|
pub fn (gg &Context) run() {
|
2020-06-04 16:05:12 +02:00
|
|
|
sapp.run(&gg.window)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 20:26:18 +02:00
|
|
|
pub fn (ctx &Context) draw_rect(x, y, w, h f32, c gx.Color) {
|
2020-06-04 16:05:12 +02:00
|
|
|
sgl.c4b(c.r, c.g, c.b, 128)
|
|
|
|
sgl.begin_quads()
|
|
|
|
sgl.v2f(x * ctx.scale, y * ctx.scale)
|
|
|
|
sgl.v2f((x + w) * ctx.scale, y * ctx.scale)
|
|
|
|
sgl.v2f((x + w) * ctx.scale, (y + h) * ctx.scale)
|
|
|
|
sgl.v2f(x * ctx.scale, (y + h) * ctx.scale)
|
|
|
|
sgl.end()
|
|
|
|
}
|
|
|
|
|
2020-06-04 23:51:54 +02:00
|
|
|
pub fn (ctx &Context) draw_empty_rect(x, y, w, h f32, c gx.Color) {
|
2020-06-04 16:05:12 +02:00
|
|
|
sgl.c4b(c.r, c.g, c.b, 128)
|
|
|
|
sgl.begin_line_strip()
|
2020-06-04 23:51:54 +02:00
|
|
|
if ctx.scale == 1 {
|
|
|
|
sgl.v2f(x, y)
|
|
|
|
sgl.v2f(x + w, y)
|
|
|
|
sgl.v2f(x + w, y + h)
|
|
|
|
sgl.v2f(x, y + h)
|
|
|
|
sgl.v2f(x, y)
|
2020-08-01 23:40:25 +02:00
|
|
|
} else {
|
2020-06-04 23:51:54 +02:00
|
|
|
sgl.v2f(x * ctx.scale, y * ctx.scale)
|
|
|
|
sgl.v2f((x + w) * ctx.scale, y * ctx.scale)
|
|
|
|
sgl.v2f((x + w) * ctx.scale, (y + h) * ctx.scale)
|
|
|
|
sgl.v2f(x * ctx.scale, (y + h) * ctx.scale)
|
2020-08-01 23:40:25 +02:00
|
|
|
sgl.v2f(x * ctx.scale, y * ctx.scale)
|
2020-06-04 23:51:54 +02:00
|
|
|
}
|
2020-06-04 16:05:12 +02:00
|
|
|
sgl.end()
|
2020-01-15 22:17:40 +01:00
|
|
|
}
|
|
|
|
|
2020-07-13 01:02:47 +02:00
|
|
|
pub fn (ctx &Context) draw_circle(x, y, r f32, c gx.Color) {
|
|
|
|
}
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
pub fn create_image(file string) u32 {
|
2020-06-04 16:05:12 +02:00
|
|
|
// println('gg create image "$file"')
|
2019-12-04 21:03:12 +01:00
|
|
|
if !os.exists(file) {
|
2019-06-22 20:20:28 +02:00
|
|
|
println('gg create image no such file "$file"')
|
|
|
|
return u32(0)
|
|
|
|
}
|
2020-06-04 16:05:12 +02:00
|
|
|
// img := stbi.load(file)
|
|
|
|
// img.free()
|
|
|
|
return 0 // texture
|
2020-01-09 12:00:39 +01:00
|
|
|
}
|
|
|
|
|
2020-08-01 23:40:25 +02:00
|
|
|
pub struct Image {
|
|
|
|
pub mut:
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
nr_channels int
|
|
|
|
ok bool
|
|
|
|
data voidptr
|
|
|
|
ext string
|
|
|
|
sokol_img C.sg_image
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_image2(file string) Image {
|
|
|
|
if !os.exists(file) {
|
|
|
|
println('gg create image no such file "$file"')
|
|
|
|
return Image{} // none
|
|
|
|
}
|
|
|
|
stb_img := stbi.load(file)
|
|
|
|
mut img := Image{
|
|
|
|
width: stb_img.width
|
|
|
|
height: stb_img.height
|
|
|
|
nr_channels: stb_img.nr_channels
|
|
|
|
ok: stb_img.ok
|
|
|
|
data: stb_img.data
|
|
|
|
ext: stb_img.ext
|
|
|
|
}
|
|
|
|
mut img_desc := C.sg_image_desc{
|
|
|
|
width: img.width
|
|
|
|
height: img.height
|
|
|
|
num_mipmaps: 0
|
|
|
|
wrap_u: .clamp_to_edge
|
|
|
|
wrap_v: .clamp_to_edge
|
|
|
|
label: &byte(0)
|
|
|
|
d3d11_texture: 0
|
|
|
|
}
|
|
|
|
img_desc.content.subimage[0][0] = C.sg_subimage_content{
|
|
|
|
ptr: img.data
|
|
|
|
size: img.nr_channels * img.width * img.height
|
|
|
|
}
|
|
|
|
the_sokol_image := C.sg_make_image(&img_desc)
|
|
|
|
img.sokol_img = the_sokol_image
|
|
|
|
return img
|
|
|
|
}
|
|
|
|
|
2020-01-09 12:00:39 +01:00
|
|
|
pub fn create_image_from_memory(buf byteptr) u32 {
|
2020-06-04 16:05:12 +02:00
|
|
|
// texture := gl.gen_texture()
|
|
|
|
// img := stbi.load_from_memory(buf)
|
|
|
|
// img.free()
|
|
|
|
return 0 // texture
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 20:26:18 +02:00
|
|
|
pub fn (gg &Context) begin() {
|
2020-07-06 19:45:00 +02:00
|
|
|
if gg.render_text && gg.font_inited {
|
|
|
|
gg.ft.flush()
|
|
|
|
}
|
2020-06-04 16:05:12 +02:00
|
|
|
sgl.defaults()
|
|
|
|
sgl.matrix_mode_projection()
|
|
|
|
sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
|
2020-01-27 20:42:32 +01:00
|
|
|
}
|
|
|
|
|
2020-06-04 20:26:18 +02:00
|
|
|
pub fn (gg &Context) end() {
|
2020-06-04 16:05:12 +02:00
|
|
|
gfx.begin_default_pass(gg.clear_pass, sapp.width(), sapp.height())
|
|
|
|
sgl.draw()
|
|
|
|
gfx.end_pass()
|
|
|
|
gfx.commit()
|
|
|
|
if gg.config.wait_events {
|
2020-08-01 23:40:25 +02:00
|
|
|
// println('gg: waiting')
|
2020-06-04 16:05:12 +02:00
|
|
|
wait_events()
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-20 17:38:00 +02:00
|
|
|
|
2020-06-05 15:43:44 +02:00
|
|
|
pub fn (ctx &Context) draw_line(x, y, x2, y2 f32, c gx.Color) {
|
|
|
|
sgl.c4b(c.r, c.g, c.b, 128)
|
|
|
|
sgl.begin_line_strip()
|
|
|
|
sgl.v2f(x * ctx.scale, y * ctx.scale)
|
|
|
|
sgl.v2f(x2 * ctx.scale, y2 * ctx.scale)
|
|
|
|
sgl.end()
|
2020-08-01 23:40:25 +02:00
|
|
|
}
|
2020-06-05 15:43:44 +02:00
|
|
|
|
2020-08-01 23:40:25 +02:00
|
|
|
pub fn (ctx &Context) draw_image(x, y, width, height f32, img u32) {
|
2020-06-05 15:43:44 +02:00
|
|
|
}
|
2020-08-01 23:40:25 +02:00
|
|
|
|
|
|
|
pub fn (ctx &Context) draw_image2(x, y, width, height f32, img Image) {
|
2020-08-01 23:52:15 +02:00
|
|
|
C.sgl_enable_texture()
|
2020-08-01 23:45:36 +02:00
|
|
|
C.sgl_texture(img.sokol_img)
|
2020-08-01 23:40:25 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
sgl.c4b(c.r, c.g, c.b, 128)
|
|
|
|
sgl.begin_quads()
|
|
|
|
sgl.v2f(x * ctx.scale, y * ctx.scale)
|
|
|
|
sgl.v2f((x + w) * ctx.scale, y * ctx.scale)
|
|
|
|
sgl.v2f((x + w) * ctx.scale, (y + h) * ctx.scale)
|
|
|
|
sgl.v2f(x * ctx.scale, (y + h) * ctx.scale)
|
|
|
|
sgl.end()
|
|
|
|
*/
|
2020-06-07 15:20:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (ctx &Context) draw_rounded_rect(x, y, width, height, radius f32, color gx.Color) {
|
2020-07-06 19:45:00 +02:00
|
|
|
}
|
|
|
|
|
2020-06-07 15:20:32 +02:00
|
|
|
pub fn (ctx &Context) draw_empty_rounded_rect(x, y, width, height, radius f32, border_color gx.Color) {
|
|
|
|
}
|
|
|
|
|
2020-06-04 16:05:12 +02:00
|
|
|
fn C.WaitMessage()
|
2020-01-15 22:17:40 +01:00
|
|
|
|
2020-06-04 16:05:12 +02:00
|
|
|
pub fn wait_events() {
|
|
|
|
unsafe {
|
2020-08-01 23:40:25 +02:00
|
|
|
$if macos {
|
|
|
|
#NSEvent *event = [NSApp nextEventMatchingMask:NSEventMaskAny
|
|
|
|
#untilDate:[NSDate distantFuture]
|
|
|
|
#inMode:NSDefaultRunLoopMode
|
|
|
|
#dequeue:YES];
|
|
|
|
#[NSApp sendEvent:event];
|
2020-06-04 16:05:12 +02:00
|
|
|
}
|
|
|
|
$if windows {
|
|
|
|
C.WaitMessage()
|
|
|
|
}
|
|
|
|
}
|
2020-02-03 04:01:39 +01:00
|
|
|
}
|