gg: add examples/gg/stars.v

pull/7866/head
Delyan Angelov 2021-01-04 12:19:05 +02:00
parent 21d5db43af
commit 3a357d9718
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 166 additions and 1 deletions

134
examples/gg/stars.v 100644
View File

@ -0,0 +1,134 @@
module main
import os
import gg
import gx
import rand
import sokol.sgl
const (
win_width = 800
win_height = 600
max_stars = 5000
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
image gg.Image
stars []Star
v_letters []VLetter
}
fn main() {
mut app := &App{
gg: 0
stars: []Star{len: max_stars}
v_letters: []VLetter{len: max_v_letters}
}
app.gg = gg.new_context(
bg_color: gx.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)
app.stars[i].y = rand.f32_in_range(-200.0, 200.0)
app.stars[i].z = rand.f32_in_range(-200.0, -100.0)
app.stars[i].r = rand.f32_in_range(0.1, 1.0)
app.stars[i].g = rand.f32_in_range(0.1, 1.0)
app.stars[i].b = rand.f32_in_range(0.1, 1.0)
}
for i in 0 .. max_v_letters {
app.v_letters[i].x = rand.f32_in_range(-20.0, 20.0)
app.v_letters[i].y = rand.f32_in_range(-20.0, 20.0)
app.v_letters[i].z = rand.f32_in_range(-5.0, -1.0)
app.v_letters[i].w = rand.f32_in_range(5, 20)
app.v_letters[i].h = app.v_letters[i].w
app.v_letters[i].angle = rand.f32_in_range(0, 6.283184)
app.v_letters[i].dangle = rand.f32_in_range(-0.05, 0.05)
app.v_letters[i].dz = rand.f32_in_range(-0.1, -0.01)
}
app.gg.run()
}
fn init_images(mut app App) {
app.image = app.gg.create_image(os.resource_abs_path('logo.png'))
}
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)
app.stars[i].y = rand.f32_in_range(-200.0, 200.0)
app.stars[i].z = rand.f32_in_range(-200.0, -100.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)
app.v_letters[i].y += rand.f32_in_range(-0.05, 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)
app.v_letters[i].h = 10.0
app.v_letters[i].w = 10.0
}
}
}

View File

@ -174,7 +174,7 @@ pub fn (ctx &Context) draw_image(x f32, y f32, width f32, height f32, img_ &Imag
// TODO remove copy pasta, merge the functions
pub fn (ctx &Context) draw_image_flipped(x f32, y f32, width f32, height f32, img_ &Image) {
if img_.id >= ctx.image_cache.len {
eprintln('gg: draw_image() bad img id $img_.id (img cache len = $ctx.image_cache.len)')
eprintln('gg: draw_image_flipped() bad img id $img_.id (img cache len = $ctx.image_cache.len)')
return
}
img := ctx.image_cache[img_.id] // fetch the image from cache
@ -207,3 +207,34 @@ pub fn (ctx &Context) draw_image_by_id(x f32, y f32, width f32, height f32, id i
img := ctx.image_cache[id]
ctx.draw_image(x, y, width, height, img)
}
pub fn (ctx &Context) draw_image_3d(x f32, y f32, z f32, width f32, height f32, img_ &Image) {
if img_.id >= ctx.image_cache.len {
eprintln('gg: draw_image_3d() bad img id $img_.id (img cache len = $ctx.image_cache.len)')
return
}
img := ctx.image_cache[img_.id] // fetch the image from cache
if !img.simg_ok {
return
}
u0 := f32(0.0)
v0 := f32(0.0)
u1 := f32(1.0)
v1 := f32(1.0)
x0 := f32(x) * ctx.scale
y0 := f32(y) * ctx.scale
x1 := f32(x + width) * ctx.scale
y1 := f32(y + height) * ctx.scale
//
sgl.load_pipeline(ctx.timage_pip)
sgl.enable_texture()
sgl.texture(img.simg)
sgl.begin_quads()
sgl.c4b(255, 255, 255, 255)
sgl.v3f_t2f(x0, y0, z, u0, v0)
sgl.v3f_t2f(x1, y0, z, u1, v0)
sgl.v3f_t2f(x1, y1, z, u1, v1)
sgl.v3f_t2f(x0, y1, z, u0, v1)
sgl.end()
sgl.disable_texture()
}