2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2021-08-12 22:31:04 +03:00
|
|
|
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
module gg
|
|
|
|
|
|
|
|
import os
|
2022-01-13 13:16:18 +03:00
|
|
|
import os.font
|
2021-08-12 22:31:04 +03:00
|
|
|
import gx
|
|
|
|
import sokol
|
|
|
|
import sokol.sapp
|
|
|
|
import sokol.sgl
|
|
|
|
import sokol.gfx
|
|
|
|
|
2022-01-21 15:43:12 +03:00
|
|
|
$if windows {
|
|
|
|
#flag -lgdi32
|
|
|
|
#include "windows.h"
|
|
|
|
}
|
|
|
|
|
2022-01-24 15:12:34 +03:00
|
|
|
// call Windows API to get screen size
|
|
|
|
fn C.GetSystemMetrics(int) int
|
|
|
|
|
|
|
|
// fn C.WaitMessage()
|
|
|
|
|
2021-12-26 14:02:51 +03:00
|
|
|
pub type TouchPoint = C.sapp_touchpoint
|
|
|
|
|
2021-08-12 22:31:04 +03:00
|
|
|
pub struct Event {
|
|
|
|
pub mut:
|
|
|
|
frame_count u64
|
|
|
|
typ sapp.EventType
|
|
|
|
key_code KeyCode
|
|
|
|
char_code u32
|
|
|
|
key_repeat bool
|
|
|
|
modifiers u32
|
|
|
|
mouse_button MouseButton
|
|
|
|
mouse_x f32
|
|
|
|
mouse_y f32
|
|
|
|
mouse_dx f32
|
|
|
|
mouse_dy f32
|
|
|
|
scroll_x f32
|
|
|
|
scroll_y f32
|
|
|
|
num_touches int
|
2021-12-26 14:02:51 +03:00
|
|
|
touches [8]TouchPoint
|
2021-08-12 22:31:04 +03:00
|
|
|
window_width int
|
|
|
|
window_height int
|
|
|
|
framebuffer_width int
|
|
|
|
framebuffer_height int
|
|
|
|
}
|
|
|
|
|
2021-12-20 16:18:21 +03:00
|
|
|
pub struct Config {
|
|
|
|
pub:
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
use_ortho bool // unused, still here just for backwards compatibility
|
|
|
|
retina bool
|
|
|
|
resizable bool
|
|
|
|
user_data voidptr
|
|
|
|
font_size int
|
|
|
|
create_window bool
|
|
|
|
// window_user_ptr voidptr
|
|
|
|
window_title string
|
|
|
|
borderless_window bool
|
|
|
|
always_on_top bool
|
|
|
|
bg_color gx.Color
|
2022-07-21 20:45:57 +03:00
|
|
|
init_fn FNCb = unsafe { nil }
|
|
|
|
frame_fn FNCb = unsafe { nil }
|
|
|
|
native_frame_fn FNCb = unsafe { nil }
|
|
|
|
cleanup_fn FNCb = unsafe { nil }
|
|
|
|
fail_fn FNFail = unsafe { nil }
|
2021-12-20 16:18:21 +03:00
|
|
|
//
|
2022-07-21 20:45:57 +03:00
|
|
|
event_fn FNEvent = unsafe { nil }
|
|
|
|
quit_fn FNEvent = unsafe { nil }
|
2021-12-20 16:18:21 +03:00
|
|
|
//
|
2022-07-21 20:45:57 +03:00
|
|
|
keydown_fn FNKeyDown = unsafe { nil }
|
|
|
|
keyup_fn FNKeyUp = unsafe { nil }
|
|
|
|
char_fn FNChar = unsafe { nil }
|
2021-12-20 16:18:21 +03:00
|
|
|
//
|
2022-07-21 20:45:57 +03:00
|
|
|
move_fn FNMove = unsafe { nil }
|
|
|
|
click_fn FNClick = unsafe { nil }
|
|
|
|
unclick_fn FNUnClick = unsafe { nil }
|
|
|
|
leave_fn FNEvent = unsafe { nil }
|
|
|
|
enter_fn FNEvent = unsafe { nil }
|
|
|
|
resized_fn FNEvent = unsafe { nil }
|
|
|
|
scroll_fn FNEvent = unsafe { nil }
|
2021-12-20 16:18:21 +03:00
|
|
|
// wait_events bool // set this to true for UIs, to save power
|
|
|
|
fullscreen bool
|
|
|
|
scale f32 = 1.0
|
|
|
|
sample_count int
|
|
|
|
swap_interval int = 1 // 1 = 60fps, 2 = 30fps etc. The preferred swap interval (ignored on some platforms)
|
|
|
|
// ved needs this
|
|
|
|
// init_text bool
|
|
|
|
font_path string
|
|
|
|
custom_bold_font_path string
|
|
|
|
ui_mode bool // refreshes only on events to save CPU usage
|
|
|
|
// font bytes for embedding
|
2022-04-15 15:35:35 +03:00
|
|
|
font_bytes_normal []u8
|
|
|
|
font_bytes_bold []u8
|
|
|
|
font_bytes_mono []u8
|
|
|
|
font_bytes_italic []u8
|
2021-12-20 16:18:21 +03:00
|
|
|
native_rendering bool // Cocoa on macOS/iOS, GDI+ on Windows
|
|
|
|
// drag&drop
|
|
|
|
enable_dragndrop bool // enable file dropping (drag'n'drop), default is false
|
|
|
|
max_dropped_files int = 1 // max number of dropped files to process (default: 1)
|
|
|
|
max_dropped_file_path_length int = 2048 // max length in bytes of a dropped UTF-8 file path (default: 2048)
|
|
|
|
}
|
|
|
|
|
2022-11-12 10:39:18 +03:00
|
|
|
[heap]
|
|
|
|
pub struct PipelineContainer {
|
|
|
|
pub mut:
|
|
|
|
alpha sgl.Pipeline
|
|
|
|
add sgl.Pipeline
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut container PipelineContainer) init_pipeline() {
|
|
|
|
// FIXME(FireRedz): this looks kinda funny, find a better way to initialize pipeline.
|
|
|
|
|
|
|
|
// Alpha
|
|
|
|
mut alpha_pipdesc := gfx.PipelineDesc{
|
|
|
|
label: c'alpha-pipeline'
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe { vmemset(&alpha_pipdesc, 0, int(sizeof(alpha_pipdesc))) }
|
|
|
|
|
|
|
|
alpha_pipdesc.colors[0] = gfx.ColorState{
|
|
|
|
blend: gfx.BlendState{
|
|
|
|
enabled: true
|
|
|
|
src_factor_rgb: .src_alpha
|
|
|
|
dst_factor_rgb: .one_minus_src_alpha
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
container.alpha = sgl.make_pipeline(&alpha_pipdesc)
|
|
|
|
|
|
|
|
// Add
|
|
|
|
mut add_pipdesc := gfx.PipelineDesc{
|
|
|
|
label: c'additive-pipeline'
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe { vmemset(&add_pipdesc, 0, int(sizeof(add_pipdesc))) }
|
|
|
|
|
|
|
|
add_pipdesc.colors[0] = gfx.ColorState{
|
|
|
|
blend: gfx.BlendState{
|
|
|
|
enabled: true
|
|
|
|
src_factor_rgb: .src_alpha
|
|
|
|
dst_factor_rgb: .one
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
container.add = sgl.make_pipeline(&add_pipdesc)
|
|
|
|
}
|
|
|
|
|
2021-08-12 22:31:04 +03:00
|
|
|
[heap]
|
|
|
|
pub struct Context {
|
|
|
|
mut:
|
|
|
|
render_text bool = true
|
|
|
|
// a cache with all images created by the user. used for sokol image init and to save space
|
|
|
|
// (so that the user can store image ids, not entire Image objects)
|
|
|
|
image_cache []Image
|
|
|
|
needs_refresh bool = true
|
|
|
|
ticks int // for ui mode only
|
|
|
|
pub:
|
|
|
|
native_rendering bool
|
|
|
|
pub mut:
|
2022-01-24 15:12:34 +03:00
|
|
|
scale f32 = 1.0 // will get set to 2.0 for retina, will remain 1.0 for normal
|
2021-08-12 22:31:04 +03:00
|
|
|
width int
|
|
|
|
height int
|
2022-01-02 21:36:01 +03:00
|
|
|
clear_pass gfx.PassAction
|
2021-12-26 14:02:51 +03:00
|
|
|
window sapp.Desc
|
2022-11-12 10:39:18 +03:00
|
|
|
timage_pip sgl.Pipeline [deprecated: 'Use `Context.pipeline.alpha` instead!']
|
|
|
|
pipeline &PipelineContainer = unsafe { nil }
|
2021-08-12 22:31:04 +03:00
|
|
|
config Config
|
2021-11-06 19:24:19 +03:00
|
|
|
user_data voidptr
|
2022-09-15 07:59:31 +03:00
|
|
|
ft &FT = unsafe { nil }
|
2021-08-12 22:31:04 +03:00
|
|
|
font_inited bool
|
|
|
|
ui_mode bool // do not redraw everything 60 times/second, but only when the user requests
|
|
|
|
frame u64 // the current frame counted from the start of the application; always increasing
|
|
|
|
//
|
2022-04-15 18:25:45 +03:00
|
|
|
mbtn_mask u8
|
2021-08-12 22:31:04 +03:00
|
|
|
mouse_buttons MouseButtons // typed version of mbtn_mask; easier to use for user programs
|
|
|
|
mouse_pos_x int
|
|
|
|
mouse_pos_y int
|
|
|
|
mouse_dx int
|
|
|
|
mouse_dy int
|
|
|
|
scroll_x int
|
|
|
|
scroll_y int
|
|
|
|
//
|
|
|
|
key_modifiers Modifier // the current key modifiers
|
|
|
|
key_repeat bool // whether the pressed key was an autorepeated one
|
|
|
|
pressed_keys [key_code_max]bool // an array representing all currently pressed keys
|
|
|
|
pressed_keys_edge [key_code_max]bool // true when the previous state of pressed_keys,
|
|
|
|
// *before* the current event was different
|
2022-06-12 14:20:26 +03:00
|
|
|
fps FPSConfig
|
2021-08-12 22:31:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn gg_init_sokol_window(user_data voidptr) {
|
2022-01-26 13:26:12 +03:00
|
|
|
mut ctx := unsafe { &Context(user_data) }
|
2021-08-12 22:31:04 +03:00
|
|
|
desc := sapp.create_desc()
|
|
|
|
/*
|
2022-01-02 21:36:01 +03:00
|
|
|
desc := gfx.Desc{
|
2021-08-12 22:31:04 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
gfx.setup(&desc)
|
2022-01-03 16:05:24 +03:00
|
|
|
sgl_desc := sgl.Desc{}
|
2021-08-12 22:31:04 +03:00
|
|
|
sgl.setup(&sgl_desc)
|
2022-02-21 13:12:21 +03:00
|
|
|
ctx.set_scale()
|
2021-08-12 22:31:04 +03:00
|
|
|
// is_high_dpi := sapp.high_dpi()
|
|
|
|
// fb_w := sapp.width()
|
|
|
|
// fb_h := sapp.height()
|
2022-01-26 13:26:12 +03:00
|
|
|
// println('ctx.scale=$ctx.scale is_high_dpi=$is_high_dpi fb_w=$fb_w fb_h=$fb_h')
|
|
|
|
// if ctx.config.init_text {
|
2021-08-12 22:31:04 +03:00
|
|
|
// `os.is_file()` won't work on Android if the font file is embedded into the APK
|
2022-01-26 13:26:12 +03:00
|
|
|
exists := $if !android { os.is_file(ctx.config.font_path) } $else { true }
|
|
|
|
if ctx.config.font_path != '' && !exists {
|
|
|
|
ctx.render_text = false
|
|
|
|
} else if ctx.config.font_path != '' && exists {
|
2021-08-12 22:31:04 +03:00
|
|
|
// t := time.ticks()
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.ft = new_ft(
|
|
|
|
font_path: ctx.config.font_path
|
|
|
|
custom_bold_font_path: ctx.config.custom_bold_font_path
|
2022-02-21 13:12:21 +03:00
|
|
|
scale: ctx.scale
|
2021-08-12 22:31:04 +03:00
|
|
|
) or { panic(err) }
|
|
|
|
// println('FT took ${time.ticks()-t} ms')
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.font_inited = true
|
2021-08-12 22:31:04 +03:00
|
|
|
} else {
|
2022-01-26 13:26:12 +03:00
|
|
|
if ctx.config.font_bytes_normal.len > 0 {
|
|
|
|
ctx.ft = new_ft(
|
|
|
|
bytes_normal: ctx.config.font_bytes_normal
|
|
|
|
bytes_bold: ctx.config.font_bytes_bold
|
|
|
|
bytes_mono: ctx.config.font_bytes_mono
|
|
|
|
bytes_italic: ctx.config.font_bytes_italic
|
2021-08-12 22:31:04 +03:00
|
|
|
scale: sapp.dpi_scale()
|
|
|
|
) or { panic(err) }
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.font_inited = true
|
2021-08-12 22:31:04 +03:00
|
|
|
} else {
|
2022-01-13 13:16:18 +03:00
|
|
|
sfont := font.default()
|
2022-01-26 13:26:12 +03:00
|
|
|
if ctx.config.font_path != '' {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('font file "${ctx.config.font_path}" does not exist, the system font (${sfont}) was used instead.')
|
2021-08-12 22:31:04 +03:00
|
|
|
}
|
|
|
|
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.ft = new_ft(
|
2021-08-12 22:31:04 +03:00
|
|
|
font_path: sfont
|
2022-01-26 13:26:12 +03:00
|
|
|
custom_bold_font_path: ctx.config.custom_bold_font_path
|
2021-08-12 22:31:04 +03:00
|
|
|
scale: sapp.dpi_scale()
|
|
|
|
) or { panic(err) }
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.font_inited = true
|
2021-08-12 22:31:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-12 10:39:18 +03:00
|
|
|
// Pipeline
|
|
|
|
ctx.pipeline = &PipelineContainer{}
|
|
|
|
ctx.pipeline.init_pipeline()
|
|
|
|
|
|
|
|
// Keep the old pipeline for now, cuz v ui used it.
|
|
|
|
ctx.timage_pip = ctx.pipeline.alpha
|
2021-08-12 22:31:04 +03:00
|
|
|
|
|
|
|
//
|
2022-07-21 20:45:57 +03:00
|
|
|
if ctx.config.init_fn != unsafe { nil } {
|
2022-06-08 21:43:20 +03:00
|
|
|
$if android {
|
|
|
|
// NOTE on Android sokol can emit resize events *before* the init function is
|
|
|
|
// called (Android has to initialize a lot more through the Activity system to
|
|
|
|
// reach a valid coontext) and thus the user's code will miss the resize event.
|
|
|
|
// To prevent this we emit a custom window resize event, if the screen size has
|
|
|
|
// changed meanwhile.
|
|
|
|
win_size := ctx.window_size()
|
|
|
|
if ctx.width != win_size.width || ctx.height != win_size.height {
|
|
|
|
ctx.width = win_size.width
|
|
|
|
ctx.height = win_size.height
|
2022-07-21 20:45:57 +03:00
|
|
|
if ctx.config.resized_fn != unsafe { nil } {
|
2022-06-08 21:43:20 +03:00
|
|
|
e := Event{
|
|
|
|
typ: .resized
|
|
|
|
window_width: ctx.width
|
|
|
|
window_height: ctx.height
|
|
|
|
}
|
|
|
|
ctx.config.resized_fn(&e, ctx.user_data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.config.init_fn(ctx.user_data)
|
2021-08-12 22:31:04 +03:00
|
|
|
}
|
|
|
|
// Create images now that we can do that after sg is inited
|
2022-01-26 13:26:12 +03:00
|
|
|
if ctx.native_rendering {
|
2021-08-12 22:31:04 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-26 13:26:12 +03:00
|
|
|
for i in 0 .. ctx.image_cache.len {
|
|
|
|
if ctx.image_cache[i].simg.id == 0 {
|
|
|
|
ctx.image_cache[i].init_sokol_image()
|
2021-08-12 22:31:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 17:25:39 +03:00
|
|
|
fn gg_frame_fn(mut ctx Context) {
|
2021-12-17 16:22:09 +03:00
|
|
|
ctx.frame++
|
2022-07-21 20:45:57 +03:00
|
|
|
if ctx.config.frame_fn == unsafe { nil } {
|
2021-12-17 16:22:09 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if ctx.native_rendering {
|
|
|
|
// return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.record_frame()
|
|
|
|
|
|
|
|
if ctx.ui_mode && !ctx.needs_refresh {
|
2022-12-21 19:33:19 +03:00
|
|
|
// println('ui mode, exiting')
|
2021-12-17 16:22:09 +03:00
|
|
|
// Draw 3 more frames after the "stop refresh" command
|
|
|
|
ctx.ticks++
|
|
|
|
if ctx.ticks > 3 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.config.frame_fn(ctx.user_data)
|
|
|
|
ctx.needs_refresh = false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn gg_event_fn(ce voidptr, user_data voidptr) {
|
|
|
|
// e := unsafe { &sapp.Event(ce) }
|
|
|
|
mut e := unsafe { &Event(ce) }
|
2022-01-26 13:26:12 +03:00
|
|
|
mut ctx := unsafe { &Context(user_data) }
|
|
|
|
if ctx.ui_mode {
|
|
|
|
ctx.refresh_ui()
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
if e.typ == .mouse_down {
|
|
|
|
bitplace := int(e.mouse_button)
|
2022-04-15 14:58:56 +03:00
|
|
|
ctx.mbtn_mask |= u8(1 << bitplace)
|
2022-10-02 22:39:11 +03:00
|
|
|
ctx.mouse_buttons = unsafe { MouseButtons(ctx.mbtn_mask) }
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
if e.typ == .mouse_up {
|
|
|
|
bitplace := int(e.mouse_button)
|
2022-04-15 14:58:56 +03:00
|
|
|
ctx.mbtn_mask &= ~(u8(1 << bitplace))
|
2022-10-02 22:39:11 +03:00
|
|
|
ctx.mouse_buttons = unsafe { MouseButtons(ctx.mbtn_mask) }
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
if e.typ == .mouse_move && e.mouse_button == .invalid {
|
2022-01-26 13:26:12 +03:00
|
|
|
if ctx.mbtn_mask & 0x01 > 0 {
|
2021-12-17 16:22:09 +03:00
|
|
|
e.mouse_button = .left
|
|
|
|
}
|
2022-01-26 13:26:12 +03:00
|
|
|
if ctx.mbtn_mask & 0x02 > 0 {
|
2021-12-17 16:22:09 +03:00
|
|
|
e.mouse_button = .right
|
|
|
|
}
|
2022-01-26 13:26:12 +03:00
|
|
|
if ctx.mbtn_mask & 0x04 > 0 {
|
2021-12-17 16:22:09 +03:00
|
|
|
e.mouse_button = .middle
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.mouse_pos_x = int(e.mouse_x / ctx.scale)
|
|
|
|
ctx.mouse_pos_y = int(e.mouse_y / ctx.scale)
|
|
|
|
ctx.mouse_dx = int(e.mouse_dx / ctx.scale)
|
|
|
|
ctx.mouse_dy = int(e.mouse_dy / ctx.scale)
|
|
|
|
ctx.scroll_x = int(e.scroll_x / ctx.scale)
|
|
|
|
ctx.scroll_y = int(e.scroll_y / ctx.scale)
|
2022-10-02 22:39:11 +03:00
|
|
|
ctx.key_modifiers = unsafe { Modifier(e.modifiers) }
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.key_repeat = e.key_repeat
|
2021-12-17 16:22:09 +03:00
|
|
|
if e.typ in [.key_down, .key_up] {
|
|
|
|
key_idx := int(e.key_code) % key_code_max
|
2022-01-26 13:26:12 +03:00
|
|
|
prev := ctx.pressed_keys[key_idx]
|
2021-12-17 16:22:09 +03:00
|
|
|
next := e.typ == .key_down
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.pressed_keys[key_idx] = next
|
|
|
|
ctx.pressed_keys_edge[key_idx] = prev != next
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
2022-07-21 20:45:57 +03:00
|
|
|
if ctx.config.event_fn != unsafe { nil } {
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.config.event_fn(e, ctx.config.user_data)
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
match e.typ {
|
|
|
|
.mouse_move {
|
2022-07-21 20:45:57 +03:00
|
|
|
if ctx.config.move_fn != unsafe { nil } {
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.config.move_fn(e.mouse_x / ctx.scale, e.mouse_y / ctx.scale, ctx.config.user_data)
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.mouse_down {
|
2022-07-21 20:45:57 +03:00
|
|
|
if ctx.config.click_fn != unsafe { nil } {
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.config.click_fn(e.mouse_x / ctx.scale, e.mouse_y / ctx.scale, e.mouse_button,
|
|
|
|
ctx.config.user_data)
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.mouse_up {
|
2022-07-21 20:45:57 +03:00
|
|
|
if ctx.config.unclick_fn != unsafe { nil } {
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.config.unclick_fn(e.mouse_x / ctx.scale, e.mouse_y / ctx.scale, e.mouse_button,
|
|
|
|
ctx.config.user_data)
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.mouse_leave {
|
2022-07-21 20:45:57 +03:00
|
|
|
if ctx.config.leave_fn != unsafe { nil } {
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.config.leave_fn(e, ctx.config.user_data)
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.mouse_enter {
|
2022-07-21 20:45:57 +03:00
|
|
|
if ctx.config.enter_fn != unsafe { nil } {
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.config.enter_fn(e, ctx.config.user_data)
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.mouse_scroll {
|
2022-07-21 20:45:57 +03:00
|
|
|
if ctx.config.scroll_fn != unsafe { nil } {
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.config.scroll_fn(e, ctx.config.user_data)
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.key_down {
|
2022-07-21 20:45:57 +03:00
|
|
|
if ctx.config.keydown_fn != unsafe { nil } {
|
2022-10-02 22:39:11 +03:00
|
|
|
ctx.config.keydown_fn(e.key_code, unsafe { Modifier(e.modifiers) }, ctx.config.user_data)
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.key_up {
|
2022-07-21 20:45:57 +03:00
|
|
|
if ctx.config.keyup_fn != unsafe { nil } {
|
2022-10-02 22:39:11 +03:00
|
|
|
ctx.config.keyup_fn(e.key_code, unsafe { Modifier(e.modifiers) }, ctx.config.user_data)
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.char {
|
2022-07-21 20:45:57 +03:00
|
|
|
if ctx.config.char_fn != unsafe { nil } {
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.config.char_fn(e.char_code, ctx.config.user_data)
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.resized {
|
2022-07-21 20:45:57 +03:00
|
|
|
if ctx.config.resized_fn != unsafe { nil } {
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.config.resized_fn(e, ctx.config.user_data)
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.quit_requested {
|
2022-07-21 20:45:57 +03:00
|
|
|
if ctx.config.quit_fn != unsafe { nil } {
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.config.quit_fn(e, ctx.config.user_data)
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// dump(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn gg_cleanup_fn(user_data voidptr) {
|
2022-01-26 13:26:12 +03:00
|
|
|
mut ctx := unsafe { &Context(user_data) }
|
2022-07-21 20:45:57 +03:00
|
|
|
if ctx.config.cleanup_fn != unsafe { nil } {
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.config.cleanup_fn(ctx.config.user_data)
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
gfx.shutdown()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn gg_fail_fn(msg &char, user_data voidptr) {
|
2022-01-26 13:26:12 +03:00
|
|
|
mut ctx := unsafe { &Context(user_data) }
|
2021-12-17 16:22:09 +03:00
|
|
|
vmsg := unsafe { tos3(msg) }
|
2022-07-21 20:45:57 +03:00
|
|
|
if ctx.config.fail_fn != unsafe { nil } {
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.config.fail_fn(vmsg, ctx.config.user_data)
|
2021-12-17 16:22:09 +03:00
|
|
|
} else {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('gg error: ${vmsg}')
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 15:12:34 +03:00
|
|
|
//---- public methods
|
2021-12-17 16:22:09 +03:00
|
|
|
|
2022-04-05 18:42:01 +03:00
|
|
|
// new_context returns an initialized `Context` allocated on the heap.
|
2022-01-24 15:12:34 +03:00
|
|
|
pub fn new_context(cfg Config) &Context {
|
2022-01-26 13:26:12 +03:00
|
|
|
mut ctx := &Context{
|
2022-01-24 15:12:34 +03:00
|
|
|
user_data: cfg.user_data
|
|
|
|
width: cfg.width
|
|
|
|
height: cfg.height
|
|
|
|
config: cfg
|
|
|
|
ft: 0
|
|
|
|
ui_mode: cfg.ui_mode
|
|
|
|
native_rendering: cfg.native_rendering
|
2023-01-31 19:22:20 +03:00
|
|
|
window: sapp.Desc{
|
|
|
|
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: &char(cfg.window_title.str)
|
|
|
|
html5_canvas_name: &char(cfg.window_title.str)
|
|
|
|
width: cfg.width
|
|
|
|
height: cfg.height
|
|
|
|
sample_count: cfg.sample_count
|
|
|
|
high_dpi: true
|
|
|
|
fullscreen: cfg.fullscreen
|
|
|
|
__v_native_render: cfg.native_rendering
|
|
|
|
// drag&drop
|
|
|
|
enable_dragndrop: cfg.enable_dragndrop
|
|
|
|
max_dropped_files: cfg.max_dropped_files
|
|
|
|
max_dropped_file_path_length: cfg.max_dropped_file_path_length
|
|
|
|
swap_interval: cfg.swap_interval
|
|
|
|
}
|
2022-01-24 15:12:34 +03:00
|
|
|
}
|
2022-01-26 13:26:12 +03:00
|
|
|
ctx.set_bg_color(cfg.bg_color)
|
2022-01-24 15:12:34 +03:00
|
|
|
// C.printf('new_context() %p\n', cfg.user_data)
|
2022-01-26 13:26:12 +03:00
|
|
|
return ctx
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
|
2022-04-05 18:42:01 +03:00
|
|
|
// run starts the main loop of the context.
|
2023-01-31 19:22:20 +03:00
|
|
|
pub fn (mut ctx Context) run() {
|
|
|
|
// set context late, in case it changed (e.g., due to embedding)
|
|
|
|
ctx.window = sapp.Desc{
|
|
|
|
...ctx.window
|
|
|
|
user_data: ctx
|
|
|
|
}
|
|
|
|
if ctx.user_data == unsafe { nil } {
|
|
|
|
ctx.user_data = ctx
|
|
|
|
}
|
2022-01-24 15:12:34 +03:00
|
|
|
sapp.run(&ctx.window)
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// quit closes the context window and exits the event loop for it
|
|
|
|
pub fn (ctx &Context) quit() {
|
|
|
|
sapp.request_quit() // does not require ctx right now, but sokol multi-window might in the future
|
|
|
|
}
|
|
|
|
|
2022-04-05 18:42:01 +03:00
|
|
|
// set_bg_color sets the color of the window background to `c`.
|
2021-12-17 16:22:09 +03:00
|
|
|
pub fn (mut ctx Context) set_bg_color(c gx.Color) {
|
|
|
|
ctx.clear_pass = gfx.create_clear_pass(f32(c.r) / 255.0, f32(c.g) / 255.0, f32(c.b) / 255.0,
|
|
|
|
f32(c.a) / 255.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resize the context's Window
|
|
|
|
pub fn (mut ctx Context) resize(width int, height int) {
|
|
|
|
ctx.width = width
|
|
|
|
ctx.height = height
|
|
|
|
}
|
|
|
|
|
2022-04-05 18:42:01 +03:00
|
|
|
// refresh_ui requests a complete re-draw of the window contents.
|
2022-01-24 15:12:34 +03:00
|
|
|
pub fn (mut ctx Context) refresh_ui() {
|
|
|
|
ctx.needs_refresh = true
|
|
|
|
ctx.ticks = 0
|
2022-01-23 19:18:17 +03:00
|
|
|
}
|
|
|
|
|
2022-04-05 18:42:01 +03:00
|
|
|
// begin prepares the context for drawing.
|
2022-01-26 13:26:12 +03:00
|
|
|
pub fn (ctx &Context) begin() {
|
|
|
|
if ctx.render_text && ctx.font_inited {
|
|
|
|
ctx.ft.flush()
|
2022-01-23 19:18:17 +03:00
|
|
|
}
|
2022-01-24 15:12:34 +03:00
|
|
|
sgl.defaults()
|
|
|
|
sgl.matrix_mode_projection()
|
|
|
|
sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
|
2022-01-23 19:18:17 +03:00
|
|
|
}
|
|
|
|
|
2022-04-05 18:42:01 +03:00
|
|
|
// end finishes drawing for the context.
|
2022-01-26 13:26:12 +03:00
|
|
|
pub fn (ctx &Context) end() {
|
2022-06-12 14:20:26 +03:00
|
|
|
$if show_fps ? {
|
|
|
|
ctx.show_fps()
|
|
|
|
} $else {
|
|
|
|
if ctx.fps.show {
|
|
|
|
ctx.show_fps()
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 13:26:12 +03:00
|
|
|
gfx.begin_default_pass(ctx.clear_pass, sapp.width(), sapp.height())
|
2022-01-24 15:12:34 +03:00
|
|
|
sgl.draw()
|
|
|
|
gfx.end_pass()
|
|
|
|
gfx.commit()
|
|
|
|
/*
|
|
|
|
if gg.config.wait_events {
|
|
|
|
// println('gg: waiting')
|
|
|
|
wait_events()
|
2022-01-23 19:18:17 +03:00
|
|
|
}
|
2022-01-24 15:12:34 +03:00
|
|
|
*/
|
2021-12-23 14:31:25 +03:00
|
|
|
}
|
|
|
|
|
2022-06-12 14:20:26 +03:00
|
|
|
pub struct FPSConfig {
|
2022-06-12 16:07:00 +03:00
|
|
|
pub mut:
|
2022-06-12 14:20:26 +03:00
|
|
|
x int
|
|
|
|
y int
|
2022-06-12 16:07:00 +03:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
show bool // do not show by default, use `-d show_fps` or set it manually in your app to override with: `app.gg.fps.show = true`
|
2022-06-12 14:20:26 +03:00
|
|
|
text_config gx.TextCfg = gx.TextCfg{
|
|
|
|
color: gx.yellow
|
2022-06-12 16:07:00 +03:00
|
|
|
size: 20
|
|
|
|
align: .center
|
|
|
|
vertical_align: .middle
|
2022-06-12 14:20:26 +03:00
|
|
|
}
|
|
|
|
background_color gx.Color = gx.Color{
|
|
|
|
r: 0
|
|
|
|
g: 0
|
|
|
|
b: 0
|
|
|
|
a: 128
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (ctx &Context) show_fps() {
|
|
|
|
if !ctx.font_inited {
|
|
|
|
return
|
|
|
|
}
|
2022-06-12 16:07:00 +03:00
|
|
|
frame_duration := sapp.frame_duration()
|
2022-06-12 15:15:53 +03:00
|
|
|
sgl.defaults()
|
|
|
|
sgl.matrix_mode_projection()
|
|
|
|
sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
|
2022-09-29 15:24:16 +03:00
|
|
|
ctx.set_text_cfg(ctx.fps.text_config)
|
2022-06-12 16:07:00 +03:00
|
|
|
if ctx.fps.width == 0 {
|
|
|
|
mut fps := unsafe { &ctx.fps }
|
|
|
|
fps.width, fps.height = ctx.text_size('00') // maximum size; prevents blinking on variable width fonts
|
|
|
|
}
|
|
|
|
fps_text := int(0.5 + 1.0 / frame_duration).str()
|
|
|
|
ctx.draw_rect_filled(ctx.fps.x, ctx.fps.y, ctx.fps.width + 2, ctx.fps.height + 4,
|
|
|
|
ctx.fps.background_color)
|
|
|
|
ctx.draw_text(ctx.fps.x + ctx.fps.width / 2 + 1, ctx.fps.y + ctx.fps.height / 2 + 2,
|
|
|
|
fps_text, ctx.fps.text_config)
|
2022-06-12 14:20:26 +03:00
|
|
|
}
|
|
|
|
|
2022-02-21 13:12:21 +03:00
|
|
|
fn (mut ctx Context) set_scale() {
|
|
|
|
mut s := sapp.dpi_scale()
|
|
|
|
$if android {
|
|
|
|
w := ctx.config.width
|
|
|
|
h := ctx.config.height
|
|
|
|
dw := sapp.width()
|
|
|
|
dh := sapp.height()
|
|
|
|
if dw <= dh {
|
|
|
|
if w <= 0 {
|
|
|
|
s = 1.0
|
|
|
|
} else {
|
|
|
|
s = f32(dw) / w
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if h <= 0 {
|
|
|
|
s = 1.0
|
|
|
|
} else {
|
|
|
|
s = f32(dh) / h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-06 20:01:22 +03:00
|
|
|
// Note: on older X11, `Xft.dpi` from ~/.Xresources, that sokol uses,
|
2022-02-21 13:12:21 +03:00
|
|
|
// may not be set which leads to sapp.dpi_scale reporting incorrectly 0.0
|
|
|
|
if s < 0.1 {
|
|
|
|
s = 1.0
|
|
|
|
}
|
|
|
|
ctx.scale = s
|
|
|
|
}
|
|
|
|
|
2022-04-05 18:42:01 +03:00
|
|
|
// window_size returns the current dimensions of the window.
|
2022-02-21 13:12:21 +03:00
|
|
|
pub fn (ctx Context) window_size() Size {
|
|
|
|
s := ctx.scale
|
|
|
|
return Size{int(sapp.width() / s), int(sapp.height() / s)}
|
|
|
|
}
|
|
|
|
|
2022-01-24 15:12:34 +03:00
|
|
|
//---- public module functions
|
2021-12-17 16:22:09 +03:00
|
|
|
|
2022-04-05 18:42:01 +03:00
|
|
|
// dpi_scale returns the DPI scale coefficient for the screen.
|
2022-02-21 13:12:21 +03:00
|
|
|
// Do not use for Android development, use `Context.scale` instead.
|
2022-01-24 15:12:34 +03:00
|
|
|
pub fn dpi_scale() f32 {
|
|
|
|
mut s := sapp.dpi_scale()
|
|
|
|
$if android {
|
|
|
|
s *= android_dpi_scale()
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
2022-03-06 20:01:22 +03:00
|
|
|
// Note: on older X11, `Xft.dpi` from ~/.Xresources, that sokol uses,
|
2022-01-24 15:12:34 +03:00
|
|
|
// may not be set which leads to sapp.dpi_scale reporting incorrectly 0.0
|
|
|
|
if s < 0.1 {
|
|
|
|
s = 1.0
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
2022-01-24 15:12:34 +03:00
|
|
|
return s
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
|
2022-04-05 18:42:01 +03:00
|
|
|
// high_dpi returns true if `gg` is running on a high DPI monitor or screen.
|
2022-01-24 15:12:34 +03:00
|
|
|
pub fn high_dpi() bool {
|
|
|
|
return C.sapp_high_dpi()
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
|
2022-04-05 18:42:01 +03:00
|
|
|
// screen_size returns the size of the active screen.
|
2022-01-24 15:12:34 +03:00
|
|
|
pub fn screen_size() Size {
|
|
|
|
$if macos {
|
|
|
|
return C.gg_get_screen_size()
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
2022-01-24 15:12:34 +03:00
|
|
|
$if windows {
|
|
|
|
return Size{
|
|
|
|
width: int(C.GetSystemMetrics(C.SM_CXSCREEN))
|
|
|
|
height: int(C.GetSystemMetrics(C.SM_CYSCREEN))
|
|
|
|
}
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
2022-01-24 15:12:34 +03:00
|
|
|
// TODO linux, etc
|
|
|
|
return Size{}
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
|
2022-04-05 18:42:01 +03:00
|
|
|
// window_size returns the `Size` of the active window.
|
2022-02-21 13:12:21 +03:00
|
|
|
// Do not use for Android development, use `Context.window_size()` instead.
|
2021-12-17 16:22:09 +03:00
|
|
|
pub fn window_size() Size {
|
|
|
|
s := dpi_scale()
|
|
|
|
return Size{int(sapp.width() / s), int(sapp.height() / s)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// window_size_real_pixels returns the `Size` of the active window without scale
|
|
|
|
pub fn window_size_real_pixels() Size {
|
|
|
|
return Size{sapp.width(), sapp.height()}
|
|
|
|
}
|
|
|
|
|
2022-01-24 15:12:34 +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()
|
|
|
|
}
|
2021-12-17 16:22:09 +03:00
|
|
|
}
|
|
|
|
}
|
2022-01-24 15:12:34 +03:00
|
|
|
*/
|