mirror of
https://github.com/vlang/v.git
synced 2025-09-18 11:56:57 -04:00
examples: make the 2048 game update rate, independent from the frame rate as well
This commit is contained in:
parent
aafbda4bc4
commit
03d033fa4b
@ -31,6 +31,7 @@ mut:
|
|||||||
state GameState = .play
|
state GameState = .play
|
||||||
tile_format TileFormat = .normal
|
tile_format TileFormat = .normal
|
||||||
moves int
|
moves int
|
||||||
|
updates u64
|
||||||
|
|
||||||
is_ai_mode bool
|
is_ai_mode bool
|
||||||
ai_fpm u64 = 8
|
ai_fpm u64 = 8
|
||||||
@ -956,14 +957,23 @@ fn on_event(e &gg.Event, mut app App) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn frame(mut app App) {
|
fn frame(mut app App) {
|
||||||
|
mut do_update := false
|
||||||
|
if app.gg.timer.elapsed().milliseconds() > 15 {
|
||||||
|
app.gg.timer.restart()
|
||||||
|
do_update = true
|
||||||
|
app.updates++
|
||||||
|
}
|
||||||
app.gg.begin()
|
app.gg.begin()
|
||||||
app.update_tickers()
|
if do_update {
|
||||||
|
app.update_tickers()
|
||||||
|
}
|
||||||
app.draw()
|
app.draw()
|
||||||
app.gg.end()
|
app.gg.end()
|
||||||
if app.is_ai_mode && app.state in [.play, .freeplay] && app.gg.frame % app.ai_fpm == 0 {
|
if do_update && app.is_ai_mode && app.state in [.play, .freeplay]
|
||||||
|
&& app.updates % app.ai_fpm == 0 {
|
||||||
app.ai_move()
|
app.ai_move()
|
||||||
}
|
}
|
||||||
if app.gg.frame % 120 == 0 {
|
if app.updates % 120 == 0 {
|
||||||
// do GC once per 2 seconds
|
// do GC once per 2 seconds
|
||||||
// eprintln('> gc_memory_use: ${gc_memory_use()}')
|
// eprintln('> gc_memory_use: ${gc_memory_use()}')
|
||||||
if gc_is_enabled() {
|
if gc_is_enabled() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user