v/examples/gg/stars.v
Eliyaan (Nopana) bbb61ab368
Some checks failed
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
native backend CI / native-backend-ubuntu (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
native backend CI / native-backend-windows (push) Waiting to run
Shy and PV CI / v-compiles-puzzle-vibes (push) Waiting to run
Sanitized CI / sanitize-undefined-clang (push) Waiting to run
Sanitized CI / sanitize-undefined-gcc (push) Waiting to run
Sanitized CI / tests-sanitize-address-clang (push) Waiting to run
Sanitized CI / sanitize-address-msvc (push) Waiting to run
Sanitized CI / sanitize-address-gcc (push) Waiting to run
Sanitized CI / sanitize-memory-clang (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-macos (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
wasm backend CI / wasm-backend (ubuntu-22.04) (push) Waiting to run
wasm backend CI / wasm-backend (windows-2022) (push) Waiting to run
Workflow Lint / lint-yml-workflows (push) Has been cancelled
gg, gx: deprecate gx and replace all occurences with gg (which now contains all the functionality of gx) (#24966)
2025-08-14 19:53:56 +03:00

131 lines
3.2 KiB
V

module main
import os.asset
import gg
import rand
import sokol.sgl
const win_width = 800
const win_height = 600
const max_stars = 5000
const max_v_letters = 5
struct Star {
mut:
x f32
y f32
z f32
r f32
g f32
b f32
}
struct VLetter {
mut:
x f32
y f32
z f32
w f32
h f32
angle f32
dz f32
dangle f32
}
struct App {
mut:
gg &gg.Context = unsafe { nil }
image gg.Image
stars []Star
v_letters []VLetter
}
fn main() {
mut app := &App{
stars: []Star{len: max_stars}
v_letters: []VLetter{len: max_v_letters}
}
app.gg = gg.new_context(
bg_color: gg.black
width: win_width
height: win_height
create_window: true
window_title: 'Star Vield'
frame_fn: frame
init_fn: init_images
user_data: app
)
for i in 0 .. max_stars {
app.stars[i].x = rand.f32_in_range(-200.0, 200.0) or { -200.0 }
app.stars[i].y = rand.f32_in_range(-200.0, 200.0) or { -200.0 }
app.stars[i].z = rand.f32_in_range(-200.0, -100.0) or { -200.0 }
app.stars[i].r = rand.f32_in_range(0.1, 1.0) or { 0.1 }
app.stars[i].g = rand.f32_in_range(0.1, 1.0) or { 0.1 }
app.stars[i].b = rand.f32_in_range(0.1, 1.0) or { 0.1 }
}
for i in 0 .. max_v_letters {
app.v_letters[i].x = rand.f32_in_range(-20.0, 20.0) or { -20.0 }
app.v_letters[i].y = rand.f32_in_range(-20.0, 20.0) or { -20.0 }
app.v_letters[i].z = rand.f32_in_range(-5.0, -1.0) or { -5.0 }
app.v_letters[i].w = rand.f32_in_range(5, 20) or { 5 }
app.v_letters[i].h = app.v_letters[i].w
app.v_letters[i].angle = rand.f32_in_range(0, 6.283184) or { 0 }
app.v_letters[i].dangle = rand.f32_in_range(-0.05, 0.05) or { -0.05 }
app.v_letters[i].dz = rand.f32_in_range(-0.1, -0.01) or { -0.1 }
}
app.gg.run()
}
fn init_images(mut app App) {
logo_path := asset.get_path('../assets', 'logo.png')
app.image = app.gg.create_image(logo_path) or { panic(err) }
}
fn frame(mut app App) {
app.gg.begin()
app.draw()
app.gg.end()
}
// fn C.glPointSize(size f32)
fn (mut app App) draw() {
sgl.defaults()
sgl.perspective(sgl.rad(90), 1.0, 1.0, 100.0)
// C.glPointSize(3.0)
sgl.begin_points()
for i in 0 .. app.stars.len {
s := app.stars[i]
sgl.v3f_c3f(s.x, s.y, s.z, s.r, s.g, s.b)
app.stars[i].z += 0.3
if app.stars[i].z > -1.0 {
app.stars[i].x = rand.f32_in_range(-200.0, 200.0) or { -200.0 }
app.stars[i].y = rand.f32_in_range(-200.0, 200.0) or { -200.0 }
app.stars[i].z = rand.f32_in_range(-200.0, -100.0) or { -200.0 }
}
}
sgl.end()
// ////
for i in 0 .. app.v_letters.len {
v := app.v_letters[i]
sgl.defaults()
sgl.perspective(sgl.rad(90), 1.0, 1.0, 100.0)
sgl.rotate(v.angle, 0, 0, 1)
app.gg.draw_image_3d(v.x, v.y, v.z, v.w, v.h, app.image)
//
app.v_letters[i].z += app.v_letters[i].dz
app.v_letters[i].angle += app.v_letters[i].dangle
if app.v_letters[i].z > -60.0 {
app.v_letters[i].x += rand.f32_in_range(-0.05, 0.05) or { -0.05 }
app.v_letters[i].y += rand.f32_in_range(-0.05, 0.05) or { -0.05 }
}
if app.v_letters[i].z < -95.0 {
app.v_letters[i].h *= 0.8
app.v_letters[i].w *= 0.8
}
if app.v_letters[i].z < -100.0 {
app.v_letters[i].z = rand.f32_in_range(-5.0, -1.0) or { -5.0 }
app.v_letters[i].h = 10.0
app.v_letters[i].w = 10.0
}
}
}