2021-01-27 00:21:02 +03:00
|
|
|
import os
|
2021-01-27 00:13:11 +03:00
|
|
|
import objects
|
|
|
|
import gg
|
|
|
|
import gx
|
|
|
|
import rand
|
|
|
|
|
|
|
|
struct App {
|
|
|
|
mut:
|
2021-01-27 00:40:30 +03:00
|
|
|
gg &gg.Context = 0
|
|
|
|
ui &objects.UIParams = 0
|
2021-01-27 00:13:11 +03:00
|
|
|
rockets []objects.Rocket
|
|
|
|
frames [][]objects.Rocket
|
2021-01-27 00:40:30 +03:00
|
|
|
// i thought about using a fixed fifo queue for the frames but the array
|
2021-01-27 00:13:11 +03:00
|
|
|
// seemed to work fine, if you'd like a challenge try implementing it with the queue :)
|
2021-03-27 18:28:09 +03:00
|
|
|
draw_flag bool = true
|
2021-01-27 00:13:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn on_frame(mut app App) {
|
2021-03-27 18:28:09 +03:00
|
|
|
if !app.draw_flag {
|
|
|
|
return
|
|
|
|
}
|
2021-01-27 00:13:11 +03:00
|
|
|
app.gg.begin()
|
|
|
|
|
|
|
|
// drawing previous frames
|
|
|
|
for mut frame in app.frames {
|
|
|
|
for mut rocket in frame {
|
|
|
|
if !rocket.exploded {
|
|
|
|
rocket.color.a = byte(f32_max(rocket.color.a - 8, 0))
|
|
|
|
rocket.draw(mut app.gg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// chance of firing new rocket
|
|
|
|
if rand.intn(30) == 0 {
|
|
|
|
app.rockets << objects.new_rocket()
|
|
|
|
}
|
|
|
|
// simulating rockets
|
|
|
|
app.rockets = app.rockets.filter(!it.dead)
|
|
|
|
|
|
|
|
for mut rocket in app.rockets {
|
|
|
|
rocket.tick(mut app.gg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// adding frame
|
|
|
|
mut frame := app.rockets.clone()
|
|
|
|
|
|
|
|
for mut rocket in frame {
|
|
|
|
rocket.particles = []
|
|
|
|
}
|
|
|
|
|
|
|
|
app.frames << frame
|
|
|
|
|
|
|
|
// trimming out frames
|
|
|
|
if app.frames.len > 30 {
|
|
|
|
app.frames.delete(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
app.gg.end()
|
|
|
|
}
|
|
|
|
|
2021-02-17 08:50:09 +03:00
|
|
|
fn on_event(e &gg.Event, mut app App) {
|
2021-01-27 00:40:30 +03:00
|
|
|
match e.typ {
|
2021-04-20 17:16:35 +03:00
|
|
|
.resized, .resumed {
|
|
|
|
app.resize()
|
|
|
|
}
|
|
|
|
.iconified {
|
|
|
|
app.draw_flag = false
|
|
|
|
}
|
|
|
|
.restored {
|
2021-03-27 18:28:09 +03:00
|
|
|
app.draw_flag = true
|
|
|
|
app.resize()
|
2021-04-20 17:16:35 +03:00
|
|
|
}
|
2021-03-27 18:28:09 +03:00
|
|
|
else {
|
2021-04-20 17:16:35 +03:00
|
|
|
// println("Type ${e.typ}")
|
2021-03-27 18:28:09 +03:00
|
|
|
}
|
2021-01-27 00:40:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut app App) resize() {
|
2021-03-27 18:28:09 +03:00
|
|
|
size := gg.window_size()
|
|
|
|
// avoid calls when minimized
|
|
|
|
if size.width < 2 && size.height < 2 {
|
|
|
|
return
|
|
|
|
}
|
2021-02-17 08:50:09 +03:00
|
|
|
mut s := gg.dpi_scale()
|
2021-04-20 17:16:35 +03:00
|
|
|
|
2021-01-27 00:40:30 +03:00
|
|
|
if s == 0.0 {
|
|
|
|
s = 1.0
|
2021-04-20 17:16:35 +03:00
|
|
|
}
|
2021-01-27 00:40:30 +03:00
|
|
|
app.ui.dpi_scale = s
|
2021-02-17 08:50:09 +03:00
|
|
|
app.ui.width = size.width
|
|
|
|
app.ui.height = size.height
|
2021-01-27 00:40:30 +03:00
|
|
|
}
|
|
|
|
|
2021-04-20 17:16:35 +03:00
|
|
|
// is needed for easier diagnostics on windows
|
|
|
|
[console]
|
2021-01-27 00:13:11 +03:00
|
|
|
fn main() {
|
2021-01-27 00:21:02 +03:00
|
|
|
mut font_path := os.resource_abs_path(os.join_path('../assets/fonts/', 'RobotoMono-Regular.ttf'))
|
|
|
|
$if android {
|
|
|
|
font_path = 'fonts/RobotoMono-Regular.ttf'
|
|
|
|
}
|
|
|
|
|
2021-01-27 00:13:11 +03:00
|
|
|
mut app := &App{}
|
2021-01-27 00:40:30 +03:00
|
|
|
app.ui = objects.get_params()
|
2021-01-27 00:13:11 +03:00
|
|
|
|
|
|
|
app.gg = gg.new_context(
|
2021-01-27 00:40:30 +03:00
|
|
|
width: app.ui.width
|
|
|
|
height: app.ui.height
|
2021-01-27 00:13:11 +03:00
|
|
|
window_title: 'Fireworks!'
|
|
|
|
bg_color: gx.black
|
|
|
|
use_ortho: true
|
|
|
|
user_data: app
|
|
|
|
frame_fn: on_frame
|
2021-01-27 00:40:30 +03:00
|
|
|
event_fn: on_event
|
2021-01-27 00:21:02 +03:00
|
|
|
font_path: font_path
|
2021-01-27 00:13:11 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
app.gg.run()
|
|
|
|
}
|