Skip to content

Commit fa4e61c

Browse files
committed
examples: add examples/hot_reload/tunnel.v
1 parent 793487f commit fa4e61c

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed

examples/hot_reload/tunnel.v

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
// Try commenting or changing the drawing functions, that are at the bottom of the file,
2+
// and that are inside functions marked with @[live] ...
3+
import gg
4+
import math
5+
6+
fn main() {
7+
mut state := init_state()
8+
gg.start(
9+
window_title: 'Tunnel'
10+
bg_color: gg.Color{255, 255, 255, 255}
11+
width: 1024
12+
height: 768
13+
frame_fn: unsafe { state.draw }
14+
event_fn: fn (event &gg.Event, ctx voidptr) {
15+
if event.typ == .char && event.char_code == `f` {
16+
gg.toggle_fullscreen()
17+
return
18+
}
19+
}
20+
)
21+
}
22+
23+
struct BigRect {
24+
mut:
25+
id int
26+
t f32
27+
x f32
28+
y f32
29+
w f32
30+
bwidth f32
31+
c gg.Color
32+
}
33+
34+
struct State {
35+
mut:
36+
t f32
37+
real_t f32
38+
tparams gg.DrawTextParams
39+
rects []BigRect
40+
}
41+
42+
const colors = []gg.Color{len: 1024, init: gg.Color{u8(100 +
43+
100 * math.sin(f32(index) / 50) * math.cos(f32(index) / 50)), u8(100 +
44+
100 * math.cos(f32(index) / 50)), 100 + u8(index % 50), 255}}
45+
46+
fn init_state() State {
47+
mut state := State{
48+
real_t: 1300
49+
tparams: gg.DrawTextParams{
50+
x: 40
51+
size: 20
52+
color: gg.Color{255, 255, 255, 255}
53+
}
54+
rects: [
55+
BigRect{
56+
x: 350
57+
y: 250
58+
w: 30
59+
bwidth: 10
60+
c: gg.Color{0, 0, 255, 255}
61+
},
62+
]
63+
}
64+
for i in 1 .. 50 {
65+
state.rects << BigRect{
66+
id: i
67+
}
68+
}
69+
return state
70+
}
71+
72+
fn (mut state State) draw(ctx &gg.Context) {
73+
update(mut state)
74+
ctx.begin()
75+
mut r0 := &state.rects[0]
76+
draw_big_rect(ctx, r0)
77+
for i in 1 .. state.rects.len {
78+
draw_big_rect(ctx, state.rects[i])
79+
}
80+
ctx.draw_text2(gg.DrawTextParams{
81+
...state.tparams
82+
y: 0
83+
text: 'real_t: ${state.real_t:7.3f}, r: ${state.t:7.3f}, r0.x: ${r0.x:7.3f}, r0.y: ${r0.y:7.3f}'
84+
})
85+
draw_center_point(ctx, r0, state.t)
86+
ctx.show_fps()
87+
ctx.end()
88+
}
89+
90+
@[live]
91+
fn update(mut state State) {
92+
state.real_t += 0.1
93+
state.t += 0.5 * math.sinf(state.real_t / 7000)
94+
mut r0 := &state.rects[0]
95+
r0.t = state.t
96+
r0.x = 550 + 450 * math.sinf(state.t / 10) * math.sinf(state.t / 50)
97+
r0.y = 350 + 350 * math.cosf(state.t / 20) * math.cosf(-state.t / 50)
98+
r0.x += 5 * math.sinf(state.t)
99+
r0.y += 5 * math.cosf(state.t)
100+
r0.c = colors[int((50000 + state.t) / 200 * (1 + math.sin(state.t / 5))) % colors.len]
101+
for i := state.rects.len - 1; i > 0; i-- {
102+
state.rects[i] = state.rects[i - 1]
103+
state.rects[i].w *= 1.11 + f32(i) / 1000
104+
state.rects[i].bwidth *= 1.09
105+
}
106+
}
107+
108+
@[live]
109+
fn draw_center_point(ctx &gg.Context, br BigRect, t f32) {
110+
b := u8(128 + 128 * math.sin(t / 12))
111+
c := gg.Color{255 - b, b, b, 55}
112+
ctx.draw_circle_filled(br.x, br.y, 8, c)
113+
}
114+
115+
@[live]
116+
fn draw_big_rect(ctx &gg.Context, br BigRect) {
117+
radius := 20
118+
x := br.x
119+
y := br.y
120+
w := br.w
121+
c := br.c
122+
bwidth := br.bwidth
123+
base := gg.DrawRectParams{
124+
radius: radius
125+
is_rounded: true
126+
color: c
127+
}
128+
rleft := gg.DrawRectParams{
129+
...base
130+
x: x - (w / 2)
131+
y: y - (w / 2)
132+
w: bwidth
133+
h: w
134+
}
135+
rright := gg.DrawRectParams{
136+
...base
137+
x: x + (w / 2) - bwidth
138+
y: y - w + (w / 2)
139+
w: bwidth
140+
h: w
141+
}
142+
rtop := gg.DrawRectParams{
143+
...base
144+
x: x - (w / 2)
145+
y: y - (w / 2)
146+
w: w
147+
h: bwidth
148+
}
149+
rbottom := gg.DrawRectParams{
150+
...base
151+
x: x + (w / 2) - w
152+
y: y + (w / 2) - bwidth
153+
w: w
154+
h: bwidth
155+
}
156+
border := gg.DrawRectParams{
157+
x: x - (w / 2) + bwidth + 1
158+
y: y - (w / 2) + bwidth + 2
159+
w: w - 2 * bwidth - 2
160+
h: w - 2 * bwidth - 1
161+
color: gg.Color{255, 255, 255, 155}
162+
style: .stroke
163+
}
164+
ctx.draw_rect(rtop)
165+
ctx.draw_rect(rleft)
166+
ctx.draw_rect(rright)
167+
ctx.draw_rect(border)
168+
ctx.draw_rect(rbottom)
169+
}

0 commit comments

Comments
 (0)