update V game

This commit is contained in:
2024-05-11 21:12:26 +03:00
parent 18710fa443
commit a8e26be242
14 changed files with 136 additions and 16 deletions

1
projects/V/Game/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
Game

82
projects/V/Game/README.md Normal file
View File

@ -0,0 +1,82 @@
* draw rect
* draw text
* draw image
* key event
## Build
### Linux
`...`
### Android
**Подготовка Android окружения**.
Необходимо выполнить следующие шаги:
1. Установка модуля `vab` для **V**
2. Установка **Android NDK**
3. Установка **android-tools**
4. Получение идентификатора устройства для деплоя
**Установка модуля `vab` для V**
```sh
v install vab
# Installed `vab` in ~/.vmodules/vab .
vab install "platforms;android-33" # Silent output... wait!
```
**Получение идентификатора устройства**
```sh
adb devices -l
```
https://developer.android.com/ndk
https://dl.google.com/android/repository/android-ndk-r26d-linux.zip
https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip
Сборка `debug` пакета.
```sh
ANDROID_NDK_ROOT="/home/user/.android/android-ndk-r26d"
VAB_FLAGS="-v 3 --name 'V App' --api 33 --build-tools 29.0.0" ~/.vmodules/vab/vab .
~/.vmodules/vab/vab --device ca4fcde6 run /home/user/Develop/snipplets.dev/projects/V/Game/v_app.apk
```
Сборка `release` пакета:
```sh
# Генерация sign.keystore
keytool -genkey -v -keystore sign.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000
```
```sh
KEYSTORE_PASSWORD="Mix9BEAH" \
KEYSTORE_ALIAS_PASSWORD="Mix9BEAH" \
VAB_FLAGS="-v 3 --name 'V App' --api 33 --build-tools 29.0.0" \
~/.vmodules/vab/vab -prod --name "V App" --package-id "me.a2s.example" --icon ./assets/vlang.png --version-code 1 \
--keystore ./sign.keystore --keystore-alias "alias_name" .
```
### HTML (WASM)
Guide: https://github.com/vlang/v/tree/master/examples/2048#compiling-to-wasm
## OTHER
```sh
VCROSS_COMPILER_NAME=/home/user/.android/android-ndk-r26d/toolchains/llvm/prebuilt/linux-x86_64/bin/clang-17 /home/user/.local/bin/vlang/v -os android .
VCROSS_COMPILER_NAME=/home/user/.android/android-ndk-r26d/toolchains/llvm/prebuilt/linux-x86_64/bin/clang-17 /home/user/.local/bin/vlang/v -os android -nocache -cc clang .
ANDROID_SDK_ROOT="/home/user/.android/sdk/cmdline-tools/33" ANDROID_NDK_ROOT="/home/user/.android/android-ndk-r26d" ANDROID_PACKAGE_NAME="test app" ANDROID_PACKAGE_ID="me.a2s.example" VAB_FLAGS="-v 3 --name $ANDROID_PACKAGE_NAME --package-id $ANDROID_PACKAGE_ID --api 33 --build-tools 26.0.2" /home/user/.vmodules/vab/vab .
VCROSS_COMPILER_NAME=/home/user/.android/android-ndk-r26d/toolchains/llvm/prebuilt/linux-x86_64/bin/clang-17 /home/user/.local/bin/vlang/v -os android -nocache -cc clang -DSOKOL_GLES3 -dump-modules "/tmp/vab/v/v.modules" -dump-c-flags "/tmp/vab/v/v.cflags" .
VCROSS_COMPILER_NAME=/home/user/.android/android-ndk-r26d/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android33-clang /home/user/.local/bin/vlang/v -os android android.v
```

BIN
projects/V/Game/android Executable file

Binary file not shown.

View File

@ -0,0 +1,3 @@
fn main() {
print('Loading... ')
}

View File

@ -0,0 +1 @@
../../../../assets/monogram-extended.ttf

View File

@ -0,0 +1 @@
../../../../assets/tiled_bg.png

View File

@ -0,0 +1 @@
../../../../assets/languages/vlang.png

30
projects/V/Game/build.sh Executable file
View File

@ -0,0 +1,30 @@
#!/bin/bash
# Обновление PATH
PATH="~/.local/bin/vlang:~/.vmodules/vab:$PATH"
# Настройки Android
export ANDROID_SDK_ROOT="/home/user/.android/sdk/cmdline-tools/33"
export ANDROID_NDK_ROOT="/home/user/.android/android-ndk-r26d"
ANDROID_DEVICE_ID="ca4fcde6"
ANDROID_PACKAGE_NAME="test app"
ANDROID_PACKAGE_ID="me.a2s.example"
VAB_FLAGS_DEBUG="-v 3 --name $ANDROID_PACKAGE_NAME --package-id $ANDROID_PACKAGE_ID --api 33 --build-tools 29.0.0"
VAB_FLAGS_RELEASE="-v 3 --name $ANDROID_PACKAGE_NAME --package-id $ANDROID_PACKAGE_ID --api 33 --build-tools 29.0.0 --icon ./assets/vlang.png --version-code 1 --keystore ./sign.keystore --keystore-alias \"alias_name\""
debug() {
VAB_FLAGS=$VAB_FLAGS_DEBUG vab $(pwd)
# vab --device $ANDROID_DEVICE_ID run ./v_app.apk
}
release() {
echo "WIP"
}
test() {
VAB_FLAGS="-v 3 --name $ANDROID_PACKAGE_NAME --package-id $ANDROID_PACKAGE_ID --api 33 --build-tools 26.0.2" vab android.v
}
test

42
projects/V/Game/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(game.player.pos_x, game.player.pos_y, 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)
}
}
}

110
projects/V/Game/game.v Normal file
View File

@ -0,0 +1,110 @@
import os
import gg
import gx
import sokol.sapp
const win_width = 768
const win_height = 768
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: 50
pos_y: 50
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',
'tiled_bg.png'))) or { panic(err) }
game.player.image = game.gg.create_image(os.resource_abs_path(os.join_path('assets',
'vlang.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
projects/V/Game/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))
}

15
projects/V/Game/update.v Normal file
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
}
}

7
projects/V/Game/v.mod Normal file
View File

@ -0,0 +1,7 @@
Module {
name: 'game',
description: '...',
version: '0.0.0',
repo_url: '...',
dependencies: []
}