gg,examples: use a timer to limit the rate of updates in breakout, instead of a separate thread, restore ability to run in a browser through emscripten

This commit is contained in:
Delyan Angelov 2025-03-09 08:45:21 +02:00
parent 77a9e6e1b8
commit 2860152e82
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 8 additions and 10 deletions

View File

@ -2,7 +2,6 @@ import gg
import gx
import math
import rand
import time
import sokol.audio
import os.asset
import sokol.sgl
@ -295,16 +294,12 @@ fn main() {
width: g.width
height: g.height
window_title: 'V Breakout'
sample_count: 2
init_fn: fn (mut g Game) {
spawn fn (mut g Game) {
for {
g.update()
time.sleep(16666 * time.microsecond)
}
}(mut g)
}
frame_fn: fn (mut g Game) {
dt := g.ctx.timer.elapsed().milliseconds()
if dt > 15 {
g.update()
g.ctx.timer.restart()
}
g.draw()
}
click_fn: fn (x f32, y f32, btn gg.MouseButton, mut g Game) {

View File

@ -6,6 +6,7 @@ module gg
import os
import os.font
import gx
import time
import sokol.sapp
import sokol.sgl
import sokol.gfx
@ -202,6 +203,7 @@ pub mut:
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
timer time.StopWatch
mbtn_mask u8
mouse_buttons MouseButtons // typed version of mbtn_mask; easier to use for user programs
@ -285,6 +287,7 @@ fn gg_init_sokol_window(user_data voidptr) {
ctx.pipeline = &PipelineContainer{}
ctx.pipeline.init_pipeline()
ctx.timer = time.new_stopwatch()
if ctx.config.init_fn != unsafe { nil } {
$if android {
// NOTE on Android sokol can emit resize events *before* the init function is