v/examples/gg/worker_thread.v
Eliyaan (Nopana) bbb61ab368
Some checks failed
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
native backend CI / native-backend-ubuntu (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
native backend CI / native-backend-windows (push) Waiting to run
Shy and PV CI / v-compiles-puzzle-vibes (push) Waiting to run
Sanitized CI / sanitize-undefined-clang (push) Waiting to run
Sanitized CI / sanitize-undefined-gcc (push) Waiting to run
Sanitized CI / tests-sanitize-address-clang (push) Waiting to run
Sanitized CI / sanitize-address-msvc (push) Waiting to run
Sanitized CI / sanitize-address-gcc (push) Waiting to run
Sanitized CI / sanitize-memory-clang (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-macos (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
wasm backend CI / wasm-backend (ubuntu-22.04) (push) Waiting to run
wasm backend CI / wasm-backend (windows-2022) (push) Waiting to run
Workflow Lint / lint-yml-workflows (push) Has been cancelled
gg, gx: deprecate gx and replace all occurences with gg (which now contains all the functionality of gx) (#24966)
2025-08-14 19:53:56 +03:00

84 lines
2.1 KiB
V

// Copyright(C) 2019 Lars Pontoppidan. All rights reserved.
// Use of this source code is governed by an MIT license file distributed with this software package.
module main
// Example of how to send a value through a channel from a worker thread to the main/rendering thread.
// This can be useful to do long running computations while keeping your framerate high (60 fps in this example).
import gg
import math
import time
const win_width = 600
const win_height = 700
const bg_color = gg.white
const count_color = gg.black
struct App {
mut:
gg &gg.Context = unsafe { nil }
ch chan i64
counter i64
}
fn main() {
mut app := &App{}
app.gg = gg.new_context(
width: win_width
height: win_height
create_window: true
window_title: 'Counter'
user_data: app
bg_color: bg_color
frame_fn: frame
init_fn: init
)
app.gg.run()
}
fn init(mut app App) {
// Spawn a new worker thread.
spawn worker(mut app)
}
// worker simulates a workload. This should be run in a separate thread.
fn worker(mut app App) {
stopwatch := time.new_stopwatch()
mut elapsed := stopwatch.elapsed()
// Do heavy operations here - like invoking a path finding algorithm, load an image or similar.
for {
now := stopwatch.elapsed()
// When done - send the result through a channel to the main/rendering thread.
app.ch <- (now - elapsed)
elapsed = now
time.sleep(1 * time.second)
}
}
fn frame(mut app App) {
app.gg.begin()
size := gg.window_size()
mut scale_factor := math.round(f32(size.width) / win_width)
if scale_factor <= 0 {
scale_factor = 1
}
text_cfg := gg.TextCfg{
size: 64 * int(scale_factor)
}
// Try a pop from the channel
mut count := i64(0)
if app.ch.try_pop(mut count) == .success {
// A value was assigned - increase the counter
app.counter += i64(f64(count) / time.second)
}
label := '${app.counter}'
label_width := (f64(label.len * text_cfg.size) / 4.0)
label_height := (f64(1 * text_cfg.size) / 2.0)
mut x := f32(size.width) * 0.5 - label_width
mut y := f32(size.height) * 0.5 - label_height
app.gg.draw_text(int(x), int(y), label, text_cfg)
app.gg.end()
}