1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/examples/hot_reload/graph.v

72 lines
1.5 KiB
V
Raw Normal View History

module main
import gx
import gg
2019-09-23 13:42:20 +03:00
import time
import glfw
import math
2019-09-23 13:42:20 +03:00
const (
2020-05-22 18:36:09 +03:00
size = 700
scale = 50.0
pi = math.pi
)
struct Context {
2020-06-04 21:26:18 +03:00
gg &gg.Context
}
fn main() {
glfw.init_glfw()
2020-05-01 14:44:22 +03:00
gconfig := gg.Cfg {
2020-05-22 18:36:09 +03:00
width: size
height: size
2020-01-16 02:07:59 +03:00
use_ortho: true
create_window: true
window_title: 'Graph builder'
always_on_top: true
2020-01-16 02:07:59 +03:00
}
2020-05-01 14:44:22 +03:00
ctx := &Context{ gg: gg.new_context(gconfig) }
ctx.gg.window.set_user_ptr( ctx )
2020-01-16 02:07:59 +03:00
go update() // update the scene in the background in case the window isn't focused
for {
if ctx.gg.window.should_close() {
break
}
gg.clear(gx.white)
ctx.draw()
2020-01-16 02:07:59 +03:00
ctx.gg.render()
}
}
2020-01-16 02:07:59 +03:00
[live]
fn (ctx &Context) draw() {
center := f32(size / 2)
2020-05-22 18:36:09 +03:00
ctx.gg.draw_line(0, center, size, center, gx.gray) // x axis
ctx.gg.draw_line(center, 0, center, size, gx.gray) // y axis
atime := f64( time.ticks() / 10 )
stime := math.sin( 2.0 * pi * f64( time.ticks() % 6000 ) / 6000 )
mut y := 0.0
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(f32(center + x * scale), f32(center - y * scale), 1, 1, gx.blue)
ctx.gg.draw_rect(f32(center + x * scale), f32(center + y * scale), 1, 1, gx.red)
}
}
fn update() {
2020-01-16 02:07:59 +03:00
for {
gg.post_empty_event()
time.sleep_ms(16) // 60 fps
2020-01-16 02:07:59 +03:00
}
}