diff options
author | Indrajith K L | 2022-12-03 17:00:20 +0530 |
---|---|---|
committer | Indrajith K L | 2022-12-03 17:00:20 +0530 |
commit | f5c4671bfbad96bf346bd7e9a21fc4317b4959df (patch) | |
tree | 2764fc62da58f2ba8da7ed341643fc359873142f /v_windows/v/examples/hot_reload | |
download | cli-tools-windows-master.tar.gz cli-tools-windows-master.tar.bz2 cli-tools-windows-master.zip |
Diffstat (limited to 'v_windows/v/examples/hot_reload')
-rw-r--r-- | v_windows/v/examples/hot_reload/.gitignore | 1 | ||||
-rw-r--r-- | v_windows/v/examples/hot_reload/bounce.v | 84 | ||||
-rw-r--r-- | v_windows/v/examples/hot_reload/graph.v | 86 | ||||
-rw-r--r-- | v_windows/v/examples/hot_reload/message.v | 18 |
4 files changed, 189 insertions, 0 deletions
diff --git a/v_windows/v/examples/hot_reload/.gitignore b/v_windows/v/examples/hot_reload/.gitignore new file mode 100644 index 0000000..fc656b9 --- /dev/null +++ b/v_windows/v/examples/hot_reload/.gitignore @@ -0,0 +1 @@ +!glfw3.dll diff --git a/v_windows/v/examples/hot_reload/bounce.v b/v_windows/v/examples/hot_reload/bounce.v new file mode 100644 index 0000000..135ad52 --- /dev/null +++ b/v_windows/v/examples/hot_reload/bounce.v @@ -0,0 +1,84 @@ +// Build this example with +// v -live bounce.v +module main + +import gx +import gg +import time + +struct Game { +mut: + gg &gg.Context + x int + y int + dy int + dx int + height int + width int + draw_fn voidptr +} + +const ( + window_width = 400 + window_height = 300 + width = 50 +) + +fn main() { + mut game := &Game{ + gg: 0 + dx: 2 + dy: 2 + height: window_height + width: window_width + draw_fn: 0 + } + game.gg = gg.new_context( + width: window_width + height: window_height + font_size: 20 + user_data: game + window_title: 'Hot code reloading demo' + create_window: true + frame_fn: frame + bg_color: gx.white + font_path: gg.system_font_path() + ) + // window.onkeydown(key_down) + println('Starting the game loop...') + go game.run() + game.gg.run() +} + +// Try uncommenting or changing the lines inside the live functions. +// Guess what will happen: +[live] +fn frame(mut game Game) { + game.gg.begin() + game.gg.draw_text_def(10, 5, 'Modify examples/hot_reload/bounce.v to get instant updates') + game.gg.draw_rect(game.x, game.y, width, width, gx.blue) + game.gg.draw_rect(window_width - width - game.x + 10, 200 - game.y + width, width, + width, gx.rgb(228, 10, 55)) + game.gg.draw_rect(game.x - 25, 250 - game.y, width, width, gx.rgb(28, 240, 55)) + game.gg.end() +} + +[live] +fn (mut game Game) update_model() { + speed := 2 + game.x += speed * game.dx + game.y += speed * game.dy + if game.y >= game.height - width || game.y <= 0 { + game.dy = -game.dy + } + if game.x >= game.width - width || game.x <= 0 { + game.dx = -game.dx + } +} + +fn (mut game Game) run() { + for { + game.update_model() + time.sleep(16 * time.millisecond) // 60fps + } +} diff --git a/v_windows/v/examples/hot_reload/graph.v b/v_windows/v/examples/hot_reload/graph.v new file mode 100644 index 0000000..53228f1 --- /dev/null +++ b/v_windows/v/examples/hot_reload/graph.v @@ -0,0 +1,86 @@ +module main + +import gx +import gg +import time +import math + +const ( + size = 700 + scale = 50.0 +) + +struct Context { +mut: + gg &gg.Context +} + +fn main() { + mut context := &Context{ + gg: 0 + } + context.gg = gg.new_context( + width: size + height: size + font_size: 20 + user_data: context + window_title: 'Graph builder' + create_window: true + frame_fn: frame + resizable: true + bg_color: gx.white + font_path: gg.system_font_path() + ) + context.gg.run() +} + +fn frame(mut ctx Context) { + ctx.gg.begin() + ctx.draw() + ctx.gg.end() +} + +[live] +fn (ctx &Context) draw() { + s := gg.window_size() + mut w := s.width + mut h := s.height + if gg.high_dpi() { + w /= 2 + h /= 2 + } + ctx.gg.draw_line(0, h / 2, w, h / 2, gx.gray) // x axis + ctx.gg.draw_line(w / 2, 0, w / 2, h, gx.gray) // y axis + atime := f64(time.ticks() / 10) + stime := math.sin(2.0 * math.pi * f64(time.ticks() % 6000) / 6000) + mut y := 0.0 + blue := gx.Color{ + r: 100 + g: 100 + b: 200 + } + red := gx.Color{ + r: 200 + g: 100 + b: 100 + } + y = 1.0 + max := f32(w) / (2 * scale) + min := -max + for x := min; x <= max; x += 0.01 { + // y = x*x + 2 + // y = x * x + stime * stime + // y = stime + // y = stime * h + y = stime * 1.0 * math.sin(x + stime + atime / 32) * ((h / 256) + x) + // y = (stime * x) * x + stime + // y = (x + 3) * (x + 3) / stime + stime*2.5 + // y = math.sqrt(30.0 - x * x) * stime + // y -= (stime-0.5) + stime + // ctx.gg.draw_rect(f32((w/2) + x * scale), f32((h/2) - y * scale), 2, 2, blue) + ctx.gg.draw_rect(f32((w / 2) + x * scale), f32((h / 2) - y * scale), 2, (f32(y) * scale), + blue) + ctx.gg.draw_rect(f32((w / 2) + x * scale), f32((h / 2) + y * scale), 2, (f32(y) * scale) + + 32, red) + } +} diff --git a/v_windows/v/examples/hot_reload/message.v b/v_windows/v/examples/hot_reload/message.v new file mode 100644 index 0000000..1f9ec2a --- /dev/null +++ b/v_windows/v/examples/hot_reload/message.v @@ -0,0 +1,18 @@ +module main + +// Build this example with `v -live message.v` +import time +import v.live + +[live] +fn print_message() { + info := live.info() + println('OK reloads: ${info.reloads_ok:4d} | Total reloads: ${info.reloads:4d} | Hello! Modify this message while the program is running.') +} + +fn main() { + for { + print_message() + time.sleep(500 * time.millisecond) + } +} |