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

examples: rename hot_code_reloading to hot_reload

This commit is contained in:
Alexander Medvednikov
2019-08-20 00:08:45 +03:00
parent 01586d6d67
commit 707ddba143
6 changed files with 4 additions and 3 deletions

3
examples/hot_reload/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
bounce
graph
message

View File

@ -0,0 +1,89 @@
// Build this example with
// v -live bounce.v
module main
import gx
import gl
import gg
import glfw
import time
struct Game {
mut:
gg *gg.GG
x int
y int
dy int
dx int
height int
width int
main_wnd *glfw.Window
draw_fn voidptr
}
fn main() {
glfw.init()
width := 600
height := 300
mut game := &Game{
gg: 0
dx: 2
dy: 2
height: height
width: width
}
mut window := glfw.create_window(glfw.WinCfg {
width: width
height: height
borderless: false
title: 'Hot code reloading demo'
ptr: game
always_on_top: true
})
//window.onkeydown(key_down)
game.main_wnd = window
window.make_context_current()
gg.init()
game.gg = gg.new_context(gg.Cfg {
width: width
height: height
font_size: 20
use_ortho: true
})
println('Starting the game loop...')
go game.run()
for {
gl.clear()
gl.clear_color(255, 255, 255, 255)
game.draw()
window.swap_buffers()
glfw.wait_events()
}
}
const (
W = 50
)
[live]
fn (game &Game) draw() {
game.gg.draw_rect(game.x, game.y, W, W, gx.rgb(255, 0, 0))
}
fn (game mut Game) run() {
for {
game.x += game.dx
game.y += game.dy
if game.y >= game.height - W || game.y <= 0 {
game.dy = - game.dy
}
if game.x >= game.width - W || game.x <= 0 {
game.dx = - game.dx
}
// Refresh
time.sleep_ms(17)
glfw.post_empty_event()
}
}

View File

@ -0,0 +1,60 @@
module main
import gx
import gl
import gg
import time
import glfw
import math
const (
Size = 700
Scale = 50.0
)
struct Context {
gg *gg.GG
}
fn main() {
glfw.init()
ctx:= &Context{
gg: gg.new_context(gg.Cfg {
width: Size
height: Size
use_ortho: true
create_window: true
window_title: 'Graph builder'
window_user_ptr: ctx
always_on_top: true
})
}
go update() // update the scene in the background in case the window isn't focused
for {
gg.clear(gx.White)
ctx.draw()
ctx.gg.render()
}
}
[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)
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)
}
}
fn update() {
for {
gg.post_empty_event()
time.sleep_ms(300)
}
}

View File

@ -0,0 +1,22 @@
// Build this example with
// v -live message.v
module main
import time
import os
[live]
fn print_message() {
println('Hello! Modify this message while the program is running.')
}
fn main() {
for {
print_message()
time.sleep_ms(500)
}
}