Skip to content

Commit 2860152

Browse files
committed
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
1 parent 77a9e6e commit 2860152

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

examples/breakout/breakout.v

+5-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import gg
22
import gx
33
import math
44
import rand
5-
import time
65
import sokol.audio
76
import os.asset
87
import sokol.sgl
@@ -295,16 +294,12 @@ fn main() {
295294
width: g.width
296295
height: g.height
297296
window_title: 'V Breakout'
298-
sample_count: 2
299-
init_fn: fn (mut g Game) {
300-
spawn fn (mut g Game) {
301-
for {
302-
g.update()
303-
time.sleep(16666 * time.microsecond)
304-
}
305-
}(mut g)
306-
}
307297
frame_fn: fn (mut g Game) {
298+
dt := g.ctx.timer.elapsed().milliseconds()
299+
if dt > 15 {
300+
g.update()
301+
g.ctx.timer.restart()
302+
}
308303
g.draw()
309304
}
310305
click_fn: fn (x f32, y f32, btn gg.MouseButton, mut g Game) {

vlib/gg/gg.c.v

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module gg
66
import os
77
import os.font
88
import gx
9+
import time
910
import sokol.sapp
1011
import sokol.sgl
1112
import sokol.gfx
@@ -202,6 +203,7 @@ pub mut:
202203
font_inited bool
203204
ui_mode bool // do not redraw everything 60 times/second, but only when the user requests
204205
frame u64 // the current frame counted from the start of the application; always increasing
206+
timer time.StopWatch
205207

206208
mbtn_mask u8
207209
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) {
285287
ctx.pipeline = &PipelineContainer{}
286288
ctx.pipeline.init_pipeline()
287289

290+
ctx.timer = time.new_stopwatch()
288291
if ctx.config.init_fn != unsafe { nil } {
289292
$if android {
290293
// NOTE on Android sokol can emit resize events *before* the init function is

0 commit comments

Comments
 (0)