2020-07-01 14:29:58 +03:00
|
|
|
// Copyright(C) 2019 Lars Pontoppidan. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license file distributed with this software package
|
|
|
|
module main
|
|
|
|
|
|
|
|
import time
|
|
|
|
import sokol
|
|
|
|
import sokol.sapp
|
|
|
|
import sokol.gfx
|
|
|
|
import sokol.sgl
|
|
|
|
import particle
|
|
|
|
|
|
|
|
const (
|
|
|
|
used_import = sokol.used_import
|
|
|
|
)
|
|
|
|
|
|
|
|
fn main() {
|
2020-07-24 12:34:19 +03:00
|
|
|
mut app := &App{
|
2020-07-01 14:29:58 +03:00
|
|
|
width: 800
|
|
|
|
height: 400
|
|
|
|
pass_action: gfx.create_clear_pass(0.1, 0.1, 0.1, 1.0)
|
|
|
|
}
|
|
|
|
app.init()
|
|
|
|
app.run()
|
|
|
|
}
|
|
|
|
|
|
|
|
struct App {
|
2022-01-02 21:36:01 +03:00
|
|
|
pass_action gfx.PassAction
|
2020-07-01 14:29:58 +03:00
|
|
|
mut:
|
2021-04-20 17:16:35 +03:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
frame i64
|
|
|
|
last i64
|
|
|
|
ps particle.System
|
2022-01-03 16:05:24 +03:00
|
|
|
alpha_pip sgl.Pipeline
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut a App) init() {
|
|
|
|
a.frame = 0
|
|
|
|
a.last = time.ticks()
|
2020-09-08 01:38:24 +03:00
|
|
|
a.ps = particle.System{
|
|
|
|
width: a.width
|
|
|
|
height: a.height
|
|
|
|
}
|
2020-07-01 14:29:58 +03:00
|
|
|
a.ps.init(particle.SystemConfig{
|
|
|
|
pool: 20000
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut a App) cleanup() {
|
2022-08-20 08:03:07 +03:00
|
|
|
unsafe {
|
|
|
|
a.ps.free()
|
|
|
|
}
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
2020-08-19 12:00:15 +03:00
|
|
|
fn (mut a App) run() {
|
2020-07-01 14:29:58 +03:00
|
|
|
title := 'V Particle Example'
|
2021-12-26 14:02:51 +03:00
|
|
|
desc := sapp.Desc{
|
2020-07-01 14:29:58 +03:00
|
|
|
width: a.width
|
|
|
|
height: a.height
|
2020-08-19 12:00:15 +03:00
|
|
|
user_data: a
|
2020-07-01 14:29:58 +03:00
|
|
|
init_userdata_cb: init
|
|
|
|
frame_userdata_cb: frame
|
|
|
|
event_userdata_cb: event
|
|
|
|
window_title: title.str
|
|
|
|
html5_canvas_name: title.str
|
|
|
|
cleanup_userdata_cb: cleanup
|
|
|
|
}
|
|
|
|
sapp.run(&desc)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (a App) draw() {
|
2020-09-28 07:14:15 +03:00
|
|
|
sgl.load_pipeline(a.alpha_pip)
|
2020-07-01 14:29:58 +03:00
|
|
|
a.ps.draw()
|
|
|
|
}
|
|
|
|
|
2023-01-06 09:28:01 +03:00
|
|
|
fn init(mut app App) {
|
2020-08-23 08:47:50 +03:00
|
|
|
desc := sapp.create_desc()
|
2020-07-01 14:29:58 +03:00
|
|
|
gfx.setup(&desc)
|
2022-01-03 16:05:24 +03:00
|
|
|
sgl_desc := sgl.Desc{
|
2020-07-01 14:29:58 +03:00
|
|
|
max_vertices: 50 * 65536
|
|
|
|
}
|
|
|
|
sgl.setup(&sgl_desc)
|
2022-01-02 21:36:01 +03:00
|
|
|
mut pipdesc := gfx.PipelineDesc{}
|
2022-01-09 17:08:11 +03:00
|
|
|
unsafe { vmemset(&pipdesc, 0, int(sizeof(pipdesc))) }
|
2021-04-07 21:39:23 +03:00
|
|
|
|
2022-01-02 21:36:01 +03:00
|
|
|
color_state := gfx.ColorState{
|
|
|
|
blend: gfx.BlendState{
|
2021-04-07 21:39:23 +03:00
|
|
|
enabled: true
|
2022-01-02 21:36:01 +03:00
|
|
|
src_factor_rgb: .src_alpha
|
|
|
|
dst_factor_rgb: .one_minus_src_alpha
|
2021-04-07 21:39:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pipdesc.colors[0] = color_state
|
|
|
|
|
2020-09-28 07:14:15 +03:00
|
|
|
app.alpha_pip = sgl.make_pipeline(&pipdesc)
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
2023-01-06 09:28:01 +03:00
|
|
|
fn cleanup(mut app App) {
|
2020-07-01 14:29:58 +03:00
|
|
|
app.cleanup()
|
|
|
|
gfx.shutdown()
|
|
|
|
}
|
|
|
|
|
2023-01-06 09:28:01 +03:00
|
|
|
fn frame(mut app App) {
|
2020-07-01 14:29:58 +03:00
|
|
|
app.width = sapp.width()
|
|
|
|
app.height = sapp.height()
|
|
|
|
t := time.ticks()
|
|
|
|
dt := f64(t - app.last) / 1000.0
|
|
|
|
app.ps.update(dt)
|
|
|
|
draw(app)
|
|
|
|
gfx.begin_default_pass(&app.pass_action, app.width, app.height)
|
2020-09-28 07:14:15 +03:00
|
|
|
sgl.default_pipeline()
|
2020-07-01 14:29:58 +03:00
|
|
|
sgl.draw()
|
|
|
|
gfx.end_pass()
|
|
|
|
gfx.commit()
|
|
|
|
app.frame++
|
|
|
|
app.last = t
|
|
|
|
}
|
|
|
|
|
2021-12-26 14:02:51 +03:00
|
|
|
fn event(ev &sapp.Event, mut app App) {
|
2020-07-01 14:29:58 +03:00
|
|
|
if ev.@type == .mouse_move {
|
|
|
|
app.ps.explode(ev.mouse_x, ev.mouse_y)
|
|
|
|
}
|
|
|
|
if ev.@type == .mouse_up || ev.@type == .mouse_down {
|
|
|
|
if ev.mouse_button == .left {
|
|
|
|
is_pressed := ev.@type == .mouse_down
|
|
|
|
if is_pressed {
|
|
|
|
app.ps.explode(ev.mouse_x, ev.mouse_y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ev.@type == .key_up || ev.@type == .key_down {
|
|
|
|
if ev.key_code == .r {
|
|
|
|
is_pressed := ev.@type == .key_down
|
|
|
|
if is_pressed {
|
|
|
|
app.ps.reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-19 12:00:15 +03:00
|
|
|
if ev.@type == .touches_began || ev.@type == .touches_moved {
|
|
|
|
if ev.num_touches > 0 {
|
|
|
|
touch_point := ev.touches[0]
|
|
|
|
app.ps.explode(touch_point.pos_x, touch_point.pos_y)
|
|
|
|
}
|
|
|
|
}
|
2020-07-01 14:29:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn draw(a &App) {
|
|
|
|
sgl.defaults()
|
|
|
|
sgl.matrix_mode_projection()
|
|
|
|
sgl.ortho(0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
|
|
|
|
sgl.push_matrix()
|
|
|
|
a.draw()
|
|
|
|
sgl.pop_matrix()
|
|
|
|
}
|