From 7f512eaf72fbc5e04c36533497957b88c9f4b06f Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 20 Jul 2019 17:38:00 +0200 Subject: [PATCH] examples/hot_code_reloading: graph builder improvements --- examples/hot_code_reloading/graph.v | 38 ++++++++++++----------------- vlib/gg/gg.v | 9 +++++-- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/examples/hot_code_reloading/graph.v b/examples/hot_code_reloading/graph.v index 18d6ebf6cd..c8a67d96b6 100644 --- a/examples/hot_code_reloading/graph.v +++ b/examples/hot_code_reloading/graph.v @@ -4,11 +4,11 @@ import gx import gg import time import glfw +import math const ( - WIDTH = 1000 - HEIGHT = 1000 - SCALE = 50 + Size = 1000 + Scale = 50.0 ) struct Context { @@ -19,12 +19,13 @@ fn main() { glfw.init() ctx:= &Context{ gg: gg.new_context(gg.Cfg { - width: WIDTH - height: HEIGHT + width: Size + height: Size use_ortho: true create_window: true - window_title: 'graph builder' + window_title: 'Graph builder' window_user_ptr: ctx + always_on_top: true }) } for { @@ -35,22 +36,13 @@ fn main() { } [live] -fn (ctx & Context) draw() { - // x axis - ctx.gg.draw_line(0, HEIGHT / 2, WIDTH, HEIGHT / 2) - // y axis - ctx.gg.draw_line(WIDTH / 2, 0, WIDTH / 2, HEIGHT) - mut prev_x := f64(0) - mut prev_y := f64(0) - center := f64(WIDTH / 2) - for x := f64(- 10); x <= f64(10); x += 0.01 { - //y := (x * x - 2) * f64(SCALE) - y := (1.0 / x) * f64(SCALE) - //ctx.gg.draw_line(int(center + prev_x), int(center+prev_y), - //int(center + x*f64(10)), int(center+y)) - ctx.gg.draw_rect(int(center) + int(x * f64(SCALE)), int(center - y), 2, 1, gx.Black) - // gx.draw_rect_f(center + (x * f64(SCALE)), center - y, 1, 1, gx.BLACK) - prev_x = x - prev_y = y +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) + for x := -10.0; x <= 10.0; x += 0.002 { + y := (x - 1) * (x - 1) + 1 + ctx.gg.draw_rect(center + x * Scale, + center - y * Scale, 1, 1, gx.Black) } } diff --git a/vlib/gg/gg.v b/vlib/gg/gg.v index 9d1f8158fa..9f0af64235 100644 --- a/vlib/gg/gg.v +++ b/vlib/gg/gg.v @@ -56,6 +56,7 @@ struct Cfg { create_window bool window_user_ptr voidptr window_title string + always_on_top bool } struct GG { @@ -85,6 +86,7 @@ pub fn new_context(cfg Cfg) *GG { width: cfg.width height: cfg.height ptr: cfg.window_user_ptr + always_on_top: cfg.always_on_top }) window.make_context_current() init() @@ -401,7 +403,7 @@ pub fn create_image(file string) u32 { return texture } -pub fn (ctx &GG) draw_line_c(x, y, x2, y2 int, color gx.Color) { +pub fn (ctx &GG) draw_line_c(x, y, x2, y2 f32, color gx.Color) { C.glDeleteBuffers(1, &ctx.VAO) C.glDeleteBuffers(1, &ctx.VBO) ctx.shader.use() @@ -415,7 +417,7 @@ pub fn (ctx &GG) draw_line_c(x, y, x2, y2 int, color gx.Color) { gl.draw_arrays(GL_LINES, 0, 2) } -pub fn (c &GG) draw_line(x, y, x2, y2 int) { +pub fn (c &GG) draw_line(x, y, x2, y2 f32) { c.draw_line_c(x, y, x2, y2, gx.Gray) } @@ -423,6 +425,9 @@ pub fn (c &GG) draw_vertical(x, y, height int) { c.draw_line(x, y, x, y + height) } + +//ctx.gg.draw_line(center + prev_x, center+prev_y, center + x*10.0, center+y) + // fn (ctx &GG) draw_image(x, y, w, h f32, img stbi.Image) { pub fn (ctx &GG) draw_image(x, y, w, h f32, tex_id u32) { // println('DRAW IMAGE $x $y $w $h $tex_id')