2020-01-23 23:04:46 +03:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-06-23 05:21:30 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-06-22 21:20:28 +03:00
|
|
|
module gg
|
|
|
|
|
2020-04-26 12:42:44 +03:00
|
|
|
import gx
|
|
|
|
import os
|
2020-06-04 17:05:12 +03:00
|
|
|
import sokol
|
|
|
|
import sokol.sapp
|
|
|
|
import sokol.sgl
|
|
|
|
import sokol.gfx
|
2020-06-04 20:57:13 +03:00
|
|
|
import gg.ft
|
2019-06-22 21:20:28 +03:00
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
type FNvoidptr1 fn(voidptr)
|
|
|
|
type FNvoidptr2 fn(voidptr,voidptr)
|
|
|
|
type FNFail fn(string,voidptr)
|
2019-06-22 21:20:28 +03:00
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
pub struct Config {
|
2019-08-23 00:00:31 +03:00
|
|
|
pub:
|
2020-06-04 17:05:12 +03:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
use_ortho bool
|
|
|
|
retina bool
|
|
|
|
resizable bool
|
|
|
|
user_data voidptr
|
|
|
|
font_size int
|
2019-08-23 00:00:31 +03:00
|
|
|
create_window bool
|
2020-06-04 17:05:12 +03:00
|
|
|
// window_user_ptr voidptr
|
|
|
|
window_title string
|
2020-06-04 20:57:13 +03:00
|
|
|
borderless_window bool
|
2019-08-23 00:00:31 +03:00
|
|
|
always_on_top bool
|
2020-06-04 17:05:12 +03:00
|
|
|
scale f32 = 1.0
|
|
|
|
bg_color gx.Color
|
|
|
|
init_fn FNvoidptr1 = voidptr(0)
|
|
|
|
frame_fn FNvoidptr1 = voidptr(0)
|
|
|
|
event_fn FNvoidptr2 = voidptr(0)
|
|
|
|
cleanup_fn FNvoidptr1 = voidptr(0)
|
|
|
|
fail_fn FNFail = voidptr(0)
|
2020-06-04 20:57:13 +03:00
|
|
|
wait_events bool // set this to true for UIs, to save power
|
|
|
|
font_path string
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-10-24 14:35:11 +03:00
|
|
|
pub struct GG {
|
2020-06-04 17:05:12 +03:00
|
|
|
scale f32 = 1.0// retina = 2 , normal = 1
|
2019-08-23 00:00:31 +03:00
|
|
|
pub mut:
|
2020-06-04 17:05:12 +03:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
clear_pass C.sg_pass_action
|
|
|
|
window C.sapp_desc
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
2020-06-04 20:57:13 +03:00
|
|
|
pub struct Size { pub: width int height int }
|
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
fn gg_init_sokol_window(user_data voidptr) {
|
|
|
|
mut g := &GG(user_data)
|
|
|
|
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 21:20:28 +03:00
|
|
|
}
|
2020-06-04 17:05:12 +03:00
|
|
|
gfx.setup(&desc)
|
|
|
|
sgl_desc := C.sgl_desc_t{}
|
|
|
|
sgl.setup(&sgl_desc)
|
|
|
|
if g.config.init_fn != voidptr(0) {
|
|
|
|
g.config.init_fn( g.config.user_data )
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
fn gg_frame_fn(user_data voidptr) {
|
|
|
|
mut g := &GG(user_data)
|
|
|
|
if g.config.frame_fn != voidptr(0) {
|
|
|
|
g.config.frame_fn( g.config.user_data )
|
2019-08-23 00:00:31 +03:00
|
|
|
}
|
|
|
|
}
|
2019-07-19 14:27:44 +03:00
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
fn gg_event_fn(e &C.sapp_event, user_data voidptr){
|
|
|
|
mut g := &GG(user_data)
|
|
|
|
if g.config.event_fn != voidptr(0) {
|
|
|
|
g.config.event_fn(e, g.config.user_data)
|
2019-08-23 00:00:31 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
fn gg_cleanup_fn(user_data voidptr){
|
|
|
|
mut g := &GG(user_data)
|
|
|
|
if g.config.cleanup_fn != voidptr(0) {
|
|
|
|
g.config.cleanup_fn(g.config.user_data)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
fn gg_fail_fn(msg charptr, user_data voidptr){
|
|
|
|
mut g := &GG(user_data)
|
|
|
|
vmsg := tos3(msg)
|
|
|
|
if g.config.fail_fn != voidptr(0) {
|
|
|
|
g.config.fail_fn(vmsg, g.config.user_data)
|
|
|
|
}else{
|
|
|
|
eprintln('gg error: $vmsg')
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
//
|
2019-06-22 21:20:28 +03:00
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
pub fn new_context(cfg Config) &GG {
|
|
|
|
mut g := &GG{
|
|
|
|
width: cfg.width
|
|
|
|
height: cfg.height
|
|
|
|
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)
|
|
|
|
scale: cfg.scale //sapp.dpi_scale()// cfg.scale
|
|
|
|
config: cfg
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
//C.printf('new_context() %p\n', cfg.user_data)
|
|
|
|
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
|
|
|
|
high_dpi: cfg.scale > 1
|
|
|
|
}
|
|
|
|
//b := sapp.high_dpi()
|
|
|
|
//println('scale=$g.scale high_dpi=$b')
|
|
|
|
if cfg.use_ortho {}
|
|
|
|
else {}
|
|
|
|
g.window = window
|
|
|
|
return g
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
pub fn (gg &GG) run() {
|
|
|
|
sapp.run(&gg.window)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
pub fn (ctx &GG) draw_rect(x, y, w, h f32, c gx.Color) {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (gg &GG) draw_empty_rect(x, y, w, h f32, c gx.Color) {
|
|
|
|
sgl.c4b(c.r, c.g, c.b, 128)
|
|
|
|
sgl.begin_line_strip()
|
|
|
|
sgl.v2f(x, y)
|
|
|
|
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 00:17:40 +03:00
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
pub fn create_image(file string) u32 {
|
2020-06-04 17:05:12 +03:00
|
|
|
// println('gg create image "$file"')
|
2019-12-04 23:03:12 +03:00
|
|
|
if !os.exists(file) {
|
2019-06-22 21:20:28 +03:00
|
|
|
println('gg create image no such file "$file"')
|
|
|
|
return u32(0)
|
|
|
|
}
|
2020-06-04 17:05:12 +03:00
|
|
|
// img := stbi.load(file)
|
|
|
|
// img.free()
|
|
|
|
return 0 // texture
|
2020-01-09 14:00:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_image_from_memory(buf byteptr) u32 {
|
2020-06-04 17:05:12 +03:00
|
|
|
// texture := gl.gen_texture()
|
|
|
|
// img := stbi.load_from_memory(buf)
|
|
|
|
// img.free()
|
|
|
|
return 0 // texture
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
pub fn (gg &GG) begin() {
|
|
|
|
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 22:42:32 +03:00
|
|
|
}
|
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
pub fn (gg &GG) end() {
|
|
|
|
gfx.begin_default_pass(gg.clear_pass, sapp.width(), sapp.height())
|
|
|
|
sgl.draw()
|
|
|
|
gfx.end_pass()
|
|
|
|
gfx.commit()
|
|
|
|
if gg.config.wait_events {
|
2020-06-04 20:57:13 +03:00
|
|
|
//println('gg: waiting')
|
2020-06-04 17:05:12 +03:00
|
|
|
wait_events()
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-07-20 18:38:00 +03:00
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
pub fn (ctx &GG) draw_line(x, y, x2, y2 f32, color gx.Color) {}
|
2019-07-20 18:38:00 +03:00
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
fn C.WaitMessage()
|
2020-01-16 00:17:40 +03:00
|
|
|
|
2020-06-04 17:05:12 +03:00
|
|
|
pub fn wait_events() {
|
|
|
|
unsafe {
|
|
|
|
$if macos {
|
|
|
|
# NSEvent *event = [NSApp nextEventMatchingMask:NSEventMaskAny
|
|
|
|
# untilDate:[NSDate distantFuture]
|
|
|
|
# inMode:NSDefaultRunLoopMode
|
|
|
|
# dequeue:YES];
|
|
|
|
# [NSApp sendEvent:event];
|
|
|
|
}
|
|
|
|
$if windows {
|
|
|
|
C.WaitMessage()
|
|
|
|
}
|
|
|
|
}
|
2020-02-03 06:01:39 +03:00
|
|
|
}
|