From 32b0225079b113b73d640c8acf8e908c6784b371 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 9 Dec 2019 17:53:17 +0200 Subject: [PATCH] remove os/time references from live programs, fix warnings --- examples/game_of_life/life_gg.v | 5 +---- examples/hot_reload/bounce.v | 38 +++++++++++++++++++++++---------- examples/hot_reload/graph.v | 35 ++++++++++++++++++------------ examples/hot_reload/message.v | 2 -- vlib/gg/gg.v | 2 +- vlib/glm/glm.v | 4 ++-- 6 files changed, 52 insertions(+), 34 deletions(-) diff --git a/examples/game_of_life/life_gg.v b/examples/game_of_life/life_gg.v index 89af96b927..55dee8ffd7 100644 --- a/examples/game_of_life/life_gg.v +++ b/examples/game_of_life/life_gg.v @@ -1,6 +1,5 @@ module main -import time import gg import glfw import gx @@ -50,9 +49,7 @@ fn main() { gg.post_empty_event() // needed so the animation does not stop /////////////////////////////////////////////// a.update() - print_automaton(a) - + print_automaton(a) graphics.render() - time.sleep_ms(1) // TODO: remove this when live reload depence on the time module is fixed } } diff --git a/examples/hot_reload/bounce.v b/examples/hot_reload/bounce.v index 2c1c3ff5e3..8833d033b0 100644 --- a/examples/hot_reload/bounce.v +++ b/examples/hot_reload/bounce.v @@ -31,6 +31,8 @@ fn main() { dy: 2 height: height width: width + main_wnd: 0 + draw_fn: 0 } window := glfw.create_window(glfw.WinCfg { width: width @@ -49,6 +51,7 @@ fn main() { height: height font_size: 20 use_ortho: true + window_user_ptr: 0 }) println('Starting the game loop...') go game.run() @@ -63,26 +66,39 @@ fn main() { const ( width = 50 + red = gx.rgb(255,0,0) + green = gx.rgb(0,255,0) + blue = gx.rgb(0,0,255) ) +// Try uncommenting or changing the lines inside the live functions. +// Guess what will happen: [live] fn (game &Game) draw() { - game.gg.draw_rect(game.x, game.y, width, width, gx.rgb(255, 0, 0)) + game.gg.draw_rect(game.x, game.y, width, width, blue) + // game.gg.draw_rect(game.x, game.y, width, width, gx.rgb(128,10,255)) +} + +[live] +fn (game mut Game) update_model() { +// game.x = 0 game.y = 0 game.dx = 1 game.dy = 1 +// game.dx = 3 game.dy = 3 + 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 (game mut Game) run() { for { - game.x += game.dx - game.y += 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 - } - // Refresh + game.update_model() + glfw.post_empty_event() // Refresh time.sleep_ms(17) - glfw.post_empty_event() } } diff --git a/examples/hot_reload/graph.v b/examples/hot_reload/graph.v index 79c9b08e59..dfd5d2e08b 100644 --- a/examples/hot_reload/graph.v +++ b/examples/hot_reload/graph.v @@ -4,12 +4,12 @@ import gx import gg import time import glfw -// import math -import os +import math const ( Size = 700 - Scale = 50.0 + Scale = 50.0 + pi = math.pi ) struct Context { @@ -17,7 +17,6 @@ struct Context { } fn main() { - os.clear() glfw.init_glfw() ctx:= &Context{ gg: gg.new_context(gg.Cfg { @@ -40,23 +39,31 @@ fn main() { [live] fn (ctx &Context) draw() { - ctx.gg.draw_line(0, Size / 2, Size, Size / 2) // x axis - ctx.gg.draw_line(Size / 2, 0, Size / 2, Size) // y axis center := f64(Size / 2) + ctx.gg.draw_line(0, center, Size, center) // x axis + ctx.gg.draw_line(center, 0, center, Size) // y axis + atime := f64( time.ticks() / 10 ) + stime := math.sin( 2.0 * pi * f64( time.ticks() % 6000 ) / 6000 ) mut y := 0.0 - for x := -10.0; x <= 10.0; x += 0.002 { - y = x * x - 1 - //y = (x + 3) * (x + 3) - 1 - //y = math.sqrt(30.0 - x * x) - ctx.gg.draw_rect(center + x * Scale, center - y * Scale, 1, 1, gx.Black) - //ctx.gg.draw_rect(center + x * Scale, center + y * Scale, 1, 1, gx.Black) + y = 1.0 + for x := -10.0; x <= 10.0; x += 0.02 { + //y = x*x + 2 + y = x*x + stime*stime + //y = stime + //y = stime * x + y = stime*1.0*math.sin(x + stime+atime/50) * 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(center + x * Scale, center - y * Scale, 1, 1, gx.Blue) + ctx.gg.draw_rect(center + x * Scale, center + y * Scale, 1, 1, gx.Red) } } fn update() { for { gg.post_empty_event() - time.sleep_ms(300) + time.sleep_ms(16) // 60 fps } - } diff --git a/examples/hot_reload/message.v b/examples/hot_reload/message.v index 63b09b31bb..0c88cc65e1 100644 --- a/examples/hot_reload/message.v +++ b/examples/hot_reload/message.v @@ -2,7 +2,6 @@ // v -live message.v module main -import os import time [live] @@ -11,7 +10,6 @@ fn print_message() { } fn main() { - os.clear() for { print_message() time.sleep_ms(500) diff --git a/vlib/gg/gg.v b/vlib/gg/gg.v index 21fbfdb833..98a6487141 100644 --- a/vlib/gg/gg.v +++ b/vlib/gg/gg.v @@ -277,7 +277,7 @@ fn todo_remove_me(cfg Cfg, scale int) { } width := cfg.width * scale height := cfg.height * scale - font_size := cfg.font_size * scale + //font_size := cfg.font_size * scale gl.enable(C.GL_BLEND) //# glBlendFunc(C.GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); shader := gl.new_shader('text') diff --git a/vlib/glm/glm.v b/vlib/glm/glm.v index cefca3436d..6bbd46d192 100644 --- a/vlib/glm/glm.v +++ b/vlib/glm/glm.v @@ -272,8 +272,8 @@ pub fn identity3() []f32 { // https://github.com/toji/gl-matrix/blob/1549cf21dfa14a2bc845993485343d519cf064fe/src/gl-matrix/mat4.js fn ortho_js(left, right, bottom, top f32) &f32 { - mynear := 1 - myfar := 1 +// mynear := 1 +// myfar := 1 lr := 1.0 / (left - right) bt := 1.0 / (bottom - top) nf := 1.0 / 1.0// (mynear -myfar)