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

90 lines
1.3 KiB
V
Raw Normal View History

2019-07-07 22:46:21 +03:00
// Build this example with
// v -live bounce.v
2019-07-07 22:46:21 +03:00
module main
import gx
import gl
import gg
import glfw
import time
struct Game {
mut:
2019-09-04 15:10:18 +03:00
gg &gg.GG
2019-07-07 22:46:21 +03:00
x int
y int
dy int
dx int
height int
width int
2019-09-04 15:10:18 +03:00
main_wnd &glfw.Window
2019-07-07 22:46:21 +03:00
draw_fn voidptr
}
fn main() {
glfw.init()
width := 600
height := 300
mut game := &Game{
2019-07-17 03:55:18 +03:00
gg: 0
dx: 2
dy: 2
2019-07-07 22:46:21 +03:00
height: height
width: width
}
mut window := glfw.create_window(glfw.WinCfg {
2019-07-15 19:33:11 +03:00
width: width
height: height
borderless: false
2019-07-07 22:46:21 +03:00
title: 'Hot code reloading demo'
ptr: game
2019-07-15 19:33:11 +03:00
always_on_top: true
2019-07-07 22:46:21 +03:00
})
//window.onkeydown(key_down)
game.main_wnd = window
window.make_context_current()
gg.init()
2019-07-17 03:55:18 +03:00
game.gg = gg.new_context(gg.Cfg {
2019-07-07 22:46:21 +03:00
width: width
height: height
font_size: 20
use_ortho: true
})
2019-07-17 03:55:18 +03:00
println('Starting the game loop...')
2019-07-07 22:46:21 +03:00
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]
2019-07-17 03:55:18 +03:00
fn (game &Game) draw() {
game.gg.draw_rect(game.x, game.y, W, W, gx.rgb(255, 0, 0))
2019-07-07 22:46:21 +03:00
}
2019-07-17 03:55:18 +03:00
fn (game mut Game) run() {
2019-07-07 22:46:21 +03:00
for {
2019-07-17 03:55:18 +03:00
game.x += game.dx
game.y += game.dy
if game.y >= game.height - W || game.y <= 0 {
game.dy = - game.dy
2019-07-07 22:46:21 +03:00
}
2019-07-17 03:55:18 +03:00
if game.x >= game.width - W || game.x <= 0 {
game.dx = - game.dx
2019-07-07 22:46:21 +03:00
}
// Refresh
2019-07-15 19:33:11 +03:00
time.sleep_ms(17)
2019-07-07 22:46:21 +03:00
glfw.post_empty_event()
}
}