v/examples/hot_reload/bounce.v

88 lines
1.5 KiB
V
Raw Normal View History

2019-07-07 21:46:21 +02:00
// Build this example with
// v -live bounce.v
2019-07-07 21:46:21 +02:00
module main
import gx
import gg
import time
struct Game {
mut:
2019-09-04 14:10:18 +02:00
gg &gg.GG
2019-07-07 21:46:21 +02:00
x int
y int
dy int
dx int
height int
width int
draw_fn voidptr
}
const (
window_width = 400
window_height = 300
width = 50
)
2019-07-07 21:46:21 +02:00
fn main() {
mut game := &Game{
gg: 0
dx: 2
dy: 2
height: window_height
width: window_width
draw_fn: 0
2019-07-07 21:46:21 +02:00
}
game.gg = gg.new_context({
width: window_width
height: window_height
2019-07-07 21:46:21 +02:00
font_size: 20
use_ortho: true
user_data: game
window_title: 'Hot code reloading demo'
create_window: true
frame_fn: frame
bg_color: gx.white
2019-07-07 21:46:21 +02:00
})
// window.onkeydown(key_down)
2019-07-17 02:55:18 +02:00
println('Starting the game loop...')
2019-07-07 21:46:21 +02:00
go game.run()
game.gg.run()
2019-07-07 21:46:21 +02:00
}
// Try uncommenting or changing the lines inside the live functions.
// Guess what will happen:
[live]
fn frame (mut game Game) {
game.gg.begin()
game.gg.draw_rect(game.x, game.y, width, width, gx.blue)
game.gg.draw_rect(window_width - width - game.x + 10, 200 - game.y + width, width, width, gx.rgb(228, 10, 55))
game.gg.draw_rect(game.x - 25, 250 - game.y, width, width, gx.rgb(28, 240, 55))
game.gg.end()
}
[live]
fn (mut game Game) update_model() {
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
}
2019-07-07 21:46:21 +02:00
}
fn (mut game Game) run() {
2019-07-07 21:46:21 +02:00
for {
game.update_model()
//glfw.post_empty_event() // Refresh
time.sleep_ms(17) // 60fps
2019-07-07 21:46:21 +02:00
}
}