example windows on V
This commit is contained in:
parent
13b5d6dbdd
commit
c22309081a
@ -30,6 +30,11 @@ indent_size = 2
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
|
||||
# V
|
||||
[*.v]
|
||||
indent_style = tab
|
||||
indent_size = 4
|
||||
|
||||
# JavaScript
|
||||
[*.js]
|
||||
indent_style = space
|
||||
|
BIN
~/.assets/v-logo.png
Normal file
BIN
~/.assets/v-logo.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.2 KiB |
4
~/V/example_window/README.md
Normal file
4
~/V/example_window/README.md
Normal file
@ -0,0 +1,4 @@
|
||||
* draw rect
|
||||
* draw text
|
||||
* draw image
|
||||
* key event
|
117
~/V/example_window/game.v
Normal file
117
~/V/example_window/game.v
Normal file
@ -0,0 +1,117 @@
|
||||
module main
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
struct Player {
|
||||
mut:
|
||||
pos_x int
|
||||
pos_y int
|
||||
lives int
|
||||
image gg.Image
|
||||
}
|
||||
|
||||
fn frame(mut game Game) {
|
||||
game.gg.begin()
|
||||
|
||||
game.gg.draw_rect_filled(game.player.pos_x, game.player.pos_y, 50, 50, gx.hex(0x5C7A56FF))
|
||||
|
||||
// draw ui
|
||||
game.draw_text(gg.window_size().width - 10, 0, 'Lives: ${game.player.lives}', 36,
|
||||
gx.white, 'right', 'top', true)
|
||||
|
||||
// draw image
|
||||
game.gg.draw_image(0, game.gg.window_size().height - game.player.image.height, game.player.image.width,
|
||||
game.player.image.height, game.player.image)
|
||||
|
||||
game.gg.end()
|
||||
}
|
||||
|
||||
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.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 {
|
||||
.escape {
|
||||
game.gg.quit()
|
||||
}
|
||||
.f11 {
|
||||
sapp.toggle_fullscreen()
|
||||
println('Set fullscreen: ${gg.is_fullscreen()}')
|
||||
}
|
||||
.left {
|
||||
game.player.pos_x -= 2
|
||||
}
|
||||
.right {
|
||||
game.player.pos_x += 2
|
||||
}
|
||||
.up {
|
||||
game.player.pos_y -= 2
|
||||
}
|
||||
.down {
|
||||
game.player.pos_y += 2
|
||||
}
|
||||
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 {}
|
||||
}
|
||||
}
|
39
~/V/example_window/text.v
Normal file
39
~/V/example_window/text.v
Normal file
@ -0,0 +1,39 @@
|
||||
module main
|
||||
|
||||
import gx
|
||||
|
||||
fn (game &Game) text_format(color gx.Color, size int, h_align gx.HorizontalAlign, v_align gx.VerticalAlign) gx.TextCfg {
|
||||
return gx.TextCfg{
|
||||
color: color
|
||||
align: h_align
|
||||
vertical_align: v_align
|
||||
size: size
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (game &Game) draw_text(x int, y int, text string, size int, color gx.Color, ha string, va string, shadow bool) {
|
||||
mut v_align := gx.VerticalAlign.baseline
|
||||
mut h_align := gx.HorizontalAlign.left
|
||||
|
||||
match va {
|
||||
'top' { v_align = gx.VerticalAlign.top }
|
||||
'middle' { v_align = gx.VerticalAlign.middle }
|
||||
'bottom' { v_align = gx.VerticalAlign.bottom }
|
||||
'baseline' { v_align = gx.VerticalAlign.baseline }
|
||||
else { v_align = gx.VerticalAlign.top }
|
||||
}
|
||||
|
||||
match ha {
|
||||
'left' { h_align = gx.HorizontalAlign.left }
|
||||
'center' { h_align = gx.HorizontalAlign.center }
|
||||
'right' { h_align = gx.HorizontalAlign.right }
|
||||
else { h_align = gx.HorizontalAlign.left }
|
||||
}
|
||||
|
||||
if shadow {
|
||||
game.gg.draw_text(x + 2, y + 2, text, game.text_format(gx.black, size, h_align,
|
||||
v_align))
|
||||
}
|
||||
|
||||
game.gg.draw_text(x, y, text, game.text_format(color, size, h_align, v_align))
|
||||
}
|
Loading…
Reference in New Issue
Block a user