Merge branch 'master' of git.a2s.su:iiiypuk/snipplets.dev

This commit is contained in:
Alexander Popov 2023-07-05 21:52:38 +03:00
commit 43736dbe0f
Signed by: iiiypuk
GPG Key ID: E47FE0AB36CD5ED6
9 changed files with 215 additions and 0 deletions

View File

@ -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

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

BIN
~/.assets/v-logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -0,0 +1,4 @@
* draw rect
* draw text
* draw image
* key event

42
~/V/example_window/draw.v Normal file
View File

@ -0,0 +1,42 @@
import gg
import gx
fn (game &Game) draw() {
game.gg.begin()
// draw filled texture
game.draw_bg_texture()
// 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 (game &Game) draw_bg_texture() {
game.gg.draw_image(game.bg_texture.pos_x - game.bg_texture.image.width, game.bg_texture.pos_y,
game.bg_texture.image.width, game.bg_texture.image.height, game.bg_texture.image)
for x in 0 .. gg.window_size().width / game.bg_texture.image.width + 3 {
game.gg.draw_image(x * game.bg_texture.image.width + game.bg_texture.pos_x, game.bg_texture.pos_y - game.bg_texture.image.height,
game.bg_texture.image.width, game.bg_texture.image.height, game.bg_texture.image)
game.gg.draw_image(game.bg_texture.pos_x - game.bg_texture.image.width, game.bg_texture.pos_y - game.bg_texture.image.height,
game.bg_texture.image.width, game.bg_texture.image.height, game.bg_texture.image)
for y in 0 .. gg.window_size().height / game.bg_texture.image.height + 3 {
game.gg.draw_image(x * game.bg_texture.image.width + game.bg_texture.pos_x,
y * game.bg_texture.image.height + game.bg_texture.pos_y, game.bg_texture.image.width,
game.bg_texture.image.height, game.bg_texture.image)
game.gg.draw_image(game.bg_texture.pos_x - game.bg_texture.image.width,
y * game.bg_texture.image.height + game.bg_texture.pos_y, game.bg_texture.image.width,
game.bg_texture.image.height, game.bg_texture.image)
}
}
}

112
~/V/example_window/game.v Normal file
View File

@ -0,0 +1,112 @@
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 {}
}
}

37
~/V/example_window/text.v Normal file
View File

@ -0,0 +1,37 @@
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))
}

View File

@ -0,0 +1,15 @@
// import gg
fn (mut game Game) update() {
if game.bg_texture.pos_x >= game.bg_texture.image.width {
game.bg_texture.pos_x = 0
} else {
game.bg_texture.pos_x += .5
}
if game.bg_texture.pos_y >= game.bg_texture.image.height {
game.bg_texture.pos_y = 0
} else {
game.bg_texture.pos_y += .5
}
}