aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/examples/gg/stars.v
blob: 815cfeb341ac149f75fc6fe8c6a6c86ffc482503 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
		}
	}
}