2019-07-07 21:46:21 +02:00
|
|
|
// Build this example with
|
2019-07-15 17:32:00 +02:00
|
|
|
// v -live bounce.v
|
2019-07-07 21:46:21 +02:00
|
|
|
module main
|
|
|
|
|
|
|
|
import gx
|
|
|
|
import gl
|
|
|
|
import gg
|
|
|
|
import glfw
|
|
|
|
import time
|
|
|
|
|
|
|
|
struct Game {
|
2019-11-06 04:57:04 +01:00
|
|
|
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
|
2019-09-04 14:10:18 +02:00
|
|
|
main_wnd &glfw.Window
|
2019-07-07 21:46:21 +02:00
|
|
|
draw_fn voidptr
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2019-10-12 05:03:15 +02:00
|
|
|
glfw.init_glfw()
|
2019-07-07 21:46:21 +02:00
|
|
|
width := 600
|
|
|
|
height := 300
|
|
|
|
mut game := &Game{
|
2019-11-06 04:57:04 +01:00
|
|
|
gg: 0
|
|
|
|
dx: 2
|
|
|
|
dy: 2
|
|
|
|
height: height
|
|
|
|
width: width
|
2019-12-09 16:53:17 +01:00
|
|
|
main_wnd: 0
|
|
|
|
draw_fn: 0
|
2019-07-07 21:46:21 +02:00
|
|
|
}
|
2019-11-06 04:57:04 +01:00
|
|
|
window := glfw.create_window(glfw.WinCfg {
|
|
|
|
width: width
|
|
|
|
height: height
|
|
|
|
borderless: false
|
2019-07-07 21:46:21 +02:00
|
|
|
title: 'Hot code reloading demo'
|
2019-11-06 04:57:04 +01:00
|
|
|
ptr: game
|
|
|
|
always_on_top: true
|
|
|
|
})
|
2019-07-07 21:46:21 +02:00
|
|
|
//window.onkeydown(key_down)
|
2019-11-06 04:57:04 +01:00
|
|
|
game.main_wnd = window
|
2019-07-07 21:46:21 +02:00
|
|
|
window.make_context_current()
|
2019-11-06 04:57:04 +01:00
|
|
|
gg.init_gg()
|
2019-07-17 02:55:18 +02:00
|
|
|
game.gg = gg.new_context(gg.Cfg {
|
2019-07-07 21:46:21 +02:00
|
|
|
width: width
|
|
|
|
height: height
|
|
|
|
font_size: 20
|
|
|
|
use_ortho: true
|
2019-12-09 16:53:17 +01:00
|
|
|
window_user_ptr: 0
|
2019-07-07 21:46:21 +02:00
|
|
|
})
|
2019-07-17 02:55:18 +02:00
|
|
|
println('Starting the game loop...')
|
2019-07-07 21:46:21 +02:00
|
|
|
go game.run()
|
|
|
|
for {
|
2020-01-14 18:58:46 +01:00
|
|
|
if window.should_close() {
|
|
|
|
break
|
|
|
|
}
|
2019-07-07 21:46:21 +02:00
|
|
|
gl.clear()
|
|
|
|
gl.clear_color(255, 255, 255, 255)
|
|
|
|
game.draw()
|
|
|
|
window.swap_buffers()
|
|
|
|
glfw.wait_events()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2019-10-20 09:19:37 +02:00
|
|
|
width = 50
|
2019-12-09 16:53:17 +01:00
|
|
|
red = gx.rgb(255,0,0)
|
|
|
|
green = gx.rgb(0,255,0)
|
|
|
|
blue = gx.rgb(0,0,255)
|
2019-07-07 21:46:21 +02:00
|
|
|
)
|
|
|
|
|
2019-12-09 16:53:17 +01:00
|
|
|
// Try uncommenting or changing the lines inside the live functions.
|
|
|
|
// Guess what will happen:
|
2019-07-15 11:38:12 +02:00
|
|
|
[live]
|
2019-07-17 02:55:18 +02:00
|
|
|
fn (game &Game) draw() {
|
2019-12-09 16:53:17 +01:00
|
|
|
game.gg.draw_rect(game.x, game.y, width, width, blue)
|
|
|
|
// game.gg.draw_rect(game.x, game.y, width, width, gx.rgb(128,10,255))
|
|
|
|
}
|
|
|
|
|
|
|
|
[live]
|
|
|
|
fn (game mut Game) update_model() {
|
|
|
|
// game.x = 0 game.y = 0 game.dx = 1 game.dy = 1
|
|
|
|
// game.dx = 3 game.dy = 3
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-17 02:55:18 +02:00
|
|
|
fn (game mut Game) run() {
|
2019-07-07 21:46:21 +02:00
|
|
|
for {
|
2019-12-09 16:53:17 +01:00
|
|
|
game.update_model()
|
|
|
|
glfw.post_empty_event() // Refresh
|
2019-07-15 18:33:11 +02:00
|
|
|
time.sleep_ms(17)
|
2019-07-07 21:46:21 +02:00
|
|
|
}
|
|
|
|
}
|