snipplets.dev/~/V/example_window/game.v

113 lines
2.0 KiB
V

import os
import gg
import gx
import sokol.sapp
const (
win_width = 512
win_height = 512
)
struct Game {
mut:
gg &gg.Context = unsafe { nil }
player Player
bg_texture BgTexture
}
struct BgTexture {
mut:
image gg.Image
pos_x f32
pos_y f32
}
struct Player {
mut:
pos_x int
pos_y int
lives int
image gg.Image
}
fn frame(mut game Game) {
game.update()
game.draw()
}
fn main() {
print('Loading... ')
mut game := &Game{}
mut font_path := os.resource_abs_path(os.join_path('..', '..', '.assets', 'monogram-extended.ttf'))
game.gg = gg.new_context(
bg_color: gx.hex(0x2A2A3AFF)
width: win_width
height: win_height
create_window: true
enable_dragndrop: false
window_title: 'Runner'
font_path: font_path
user_data: game
frame_fn: frame
event_fn: on_event
)
game.player = &Player{
pos_x: 10
pos_y: 10
lives: 3
}
game.bg_texture = &BgTexture{
pos_x: 0
pos_y: 0
}
game.bg_texture.image = game.gg.create_image(os.resource_abs_path(os.join_path('..',
'..', '.assets', 'fondo para itchio.png'))) or { panic(err) }
game.player.image = game.gg.create_image(os.resource_abs_path(os.join_path('..', '..',
'.assets', 'v-logo.png'))) or { panic(err) }
println('OK!\n')
println('High DPI: ${gg.high_dpi()}')
println('Window size: ${gg.window_size().width}x${gg.window_size().height}')
println('Screen size: ${gg.screen_size().width}x${gg.screen_size().height}')
println('Fullscreen: ${gg.is_fullscreen()}\n')
game.gg.run()
println('\nBye!')
}
fn (mut game Game) key_down(key gg.KeyCode) {
match key {
// vfmt off
.escape { game.gg.quit() }
// vfmt on
.f11 {
sapp.toggle_fullscreen()
println('Set fullscreen: ${gg.is_fullscreen()}')
}
// vfmt off
.left { game.player.pos_x -= 2 }
.right { game.player.pos_x += 2 }
.up { game.player.pos_y -= 2 }
.down { game.player.pos_y += 2 }
// vfmt on
else {}
}
}
fn on_event(e &gg.Event, mut game Game) {
// println('code=$e.char_code')
// println('code=$e.key_code')
match e.typ {
.key_down {
game.key_down(e.key_code)
}
else {}
}
}