2019-07-07 22:46:21 +03:00
|
|
|
// Build this example with
|
2019-07-15 18:32:00 +03:00
|
|
|
// 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:
|
|
|
|
vg *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{
|
|
|
|
vg: 0
|
2019-07-16 03:23:09 +03:00
|
|
|
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()
|
|
|
|
game.vg = gg.new_context(gg.Cfg {
|
|
|
|
width: width
|
|
|
|
height: height
|
|
|
|
font_size: 20
|
|
|
|
use_ortho: true
|
|
|
|
})
|
|
|
|
println('Starting 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
|
|
|
|
)
|
|
|
|
|
2019-07-15 12:38:12 +03:00
|
|
|
[live]
|
2019-07-07 22:46:21 +03:00
|
|
|
fn (ctx &Game) draw() {
|
2019-07-15 19:33:11 +03:00
|
|
|
ctx.vg.draw_rect(ctx.x, ctx.y, W, W, gx.rgb(255, 0, 0))
|
2019-07-07 22:46:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (ctx mut Game) run() {
|
|
|
|
for {
|
|
|
|
ctx.x += ctx.dx
|
|
|
|
ctx.y += ctx.dy
|
|
|
|
if ctx.y >= ctx.height - W || ctx.y <= 0 {
|
|
|
|
ctx.dy = - ctx.dy
|
|
|
|
}
|
|
|
|
if ctx.x >= ctx.width - W || ctx.x <= 0 {
|
|
|
|
ctx.dx = - ctx.dx
|
|
|
|
}
|
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|