examples: fix compilation of the graph.v example
parent
93294d4a97
commit
2b545f6b90
|
@ -7,34 +7,38 @@ import math
|
||||||
|
|
||||||
const (
|
const (
|
||||||
size = 700
|
size = 700
|
||||||
scale = 50.0
|
scale = 50.0
|
||||||
pi = math.pi
|
pi = math.pi
|
||||||
)
|
)
|
||||||
|
|
||||||
struct Context {
|
struct Context {
|
||||||
|
mut:
|
||||||
gg &gg.Context
|
gg &gg.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
gconfig := gg.Cfg {
|
mut context := &Context{
|
||||||
width: size
|
gg: 0
|
||||||
height: size
|
|
||||||
use_ortho: true
|
|
||||||
create_window: true
|
|
||||||
window_title: 'Graph builder'
|
|
||||||
always_on_top: true
|
|
||||||
}
|
|
||||||
ctx := &Context{ gg: gg.new_context(gconfig) }
|
|
||||||
ctx.gg.window.set_user_ptr( ctx )
|
|
||||||
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()
|
|
||||||
ctx.gg.render()
|
|
||||||
}
|
}
|
||||||
|
context.gg = gg.new_context({
|
||||||
|
width: size
|
||||||
|
height: size
|
||||||
|
font_size: 20
|
||||||
|
use_ortho: true
|
||||||
|
user_data: context
|
||||||
|
window_title: 'Graph builder'
|
||||||
|
create_window: true
|
||||||
|
frame_fn: frame
|
||||||
|
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]
|
[live]
|
||||||
|
@ -42,28 +46,21 @@ fn (ctx &Context) draw() {
|
||||||
center := f32(size / 2)
|
center := f32(size / 2)
|
||||||
ctx.gg.draw_line(0, center, size, center, gx.gray) // x axis
|
ctx.gg.draw_line(0, center, size, center, gx.gray) // x axis
|
||||||
ctx.gg.draw_line(center, 0, center, size, gx.gray) // y axis
|
ctx.gg.draw_line(center, 0, center, size, gx.gray) // y axis
|
||||||
atime := f64( time.ticks() / 10 )
|
atime := f64(time.ticks() / 10)
|
||||||
stime := math.sin( 2.0 * pi * f64( time.ticks() % 6000 ) / 6000 )
|
stime := math.sin(2.0 * pi * f64(time.ticks() % 6000) / 6000)
|
||||||
mut y := 0.0
|
mut y := 0.0
|
||||||
y = 1.0
|
y = 1.0
|
||||||
for x := -10.0; x <= 10.0; x += 0.02 {
|
for x := -10.0; x <= 10.0; x += 0.02 {
|
||||||
//y = x*x + 2
|
// y = x*x + 2
|
||||||
y = x*x + stime*stime
|
y = x * x + stime * stime
|
||||||
//y = stime
|
// y = stime
|
||||||
//y = stime * x
|
// y = stime * x
|
||||||
y = stime*1.0*math.sin(x + stime+atime/50) * x
|
y = stime * 1.0 * math.sin(x + stime + atime / 50) * x
|
||||||
//y = (stime * x) * x + stime
|
// y = (stime * x) * x + stime
|
||||||
//y = (x + 3) * (x + 3) / stime + stime*2.5
|
// y = (x + 3) * (x + 3) / stime + stime*2.5
|
||||||
//y = math.sqrt(30.0 - x * x) * stime
|
// y = math.sqrt(30.0 - x * x) * stime
|
||||||
//y -= (stime-0.5) + 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.blue)
|
||||||
ctx.gg.draw_rect(f32(center + x * scale), f32(center + y * scale), 1, 1, gx.red)
|
ctx.gg.draw_rect(f32(center + x * scale), f32(center + y * scale), 1, 1, gx.red)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update() {
|
|
||||||
for {
|
|
||||||
gg.post_empty_event()
|
|
||||||
time.sleep_ms(16) // 60 fps
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue