Initial project import
This commit is contained in:
commit
23f2d29e32
11957 files changed
+1754577
No files matched your search
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2019 Lars Pontoppidan
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,12 @@
|
||||
// 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 particle
|
||||
|
||||
// * Color
|
||||
pub struct Color {
|
||||
mut:
|
||||
r u8
|
||||
g u8
|
||||
b u8
|
||||
a u8
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
// 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 particle
|
||||
|
||||
import math.vec
|
||||
import sokol.sgl
|
||||
|
||||
const default_life_time = 1000
|
||||
const default_v_color = Color{93, 136, 193, 255}
|
||||
|
||||
// * Module public
|
||||
pub fn new(location vec.Vec2[f64]) &Particle {
|
||||
p := &Particle{
|
||||
location: location
|
||||
velocity: vec.Vec2[f64]{0, 0}
|
||||
acceleration: vec.Vec2[f64]{0, 0}
|
||||
color: default_v_color
|
||||
life_time: default_life_time
|
||||
life_time_init: default_life_time
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
fn remap(v f64, min f64, max f64, new_min f64, new_max f64) f64 {
|
||||
return (((v - min) * (new_max - new_min)) / (max - min)) + new_min
|
||||
}
|
||||
|
||||
// Particle
|
||||
pub struct Particle {
|
||||
pub mut:
|
||||
location vec.Vec2[f64]
|
||||
velocity vec.Vec2[f64]
|
||||
acceleration vec.Vec2[f64]
|
||||
color Color
|
||||
life_time f64
|
||||
life_time_init f64
|
||||
}
|
||||
|
||||
pub fn (mut p Particle) update(dt f64) {
|
||||
mut acc := p.acceleration
|
||||
acc.multiply_scalar(dt)
|
||||
p.velocity = p.velocity.add(acc)
|
||||
p.location = p.location.add(p.velocity)
|
||||
lt := p.life_time - (1000 * dt)
|
||||
if lt > 0 {
|
||||
p.life_time = lt
|
||||
p.color.r = p.color.r - 1 // u8(remap(p.life_time,0.0,p.life_time_init,0,p.color.r))
|
||||
p.color.g = p.color.g - 1 // u8(remap(p.life_time,0.0,p.life_time_init,0,p.color.g))
|
||||
p.color.b = p.color.b - 1 // u8(remap(p.life_time,0.0,p.life_time_init,0,p.color.b))
|
||||
p.color.a = u8(int(remap(p.life_time, 0.0, p.life_time_init, 0, 255))) - 10
|
||||
} else {
|
||||
p.life_time = 0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (p Particle) is_dead() bool {
|
||||
return p.life_time <= 0.0
|
||||
}
|
||||
|
||||
pub fn (p Particle) draw() {
|
||||
l := p.location
|
||||
sgl.c4b(p.color.r, p.color.g, p.color.b, p.color.a)
|
||||
lx := f32(l.x)
|
||||
ly := f32(l.y)
|
||||
sgl.v2f(lx, ly)
|
||||
sgl.v2f(lx + 2, ly)
|
||||
sgl.v2f(lx + 2, ly + 2)
|
||||
sgl.v2f(lx, ly + 2)
|
||||
}
|
||||
|
||||
pub fn (mut p Particle) reset() {
|
||||
p.location.zero()
|
||||
p.acceleration.zero()
|
||||
p.velocity.zero()
|
||||
// p.color = Color{93, 136, 193, 255}
|
||||
p.color = default_v_color
|
||||
p.life_time = default_life_time
|
||||
p.life_time_init = p.life_time
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
// 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 particle
|
||||
|
||||
import math.vec
|
||||
import rand
|
||||
import sokol.sgl
|
||||
|
||||
pub struct SystemConfig {
|
||||
pub:
|
||||
pool int
|
||||
}
|
||||
|
||||
pub struct System {
|
||||
pub:
|
||||
width int
|
||||
height int
|
||||
mut:
|
||||
pool []&Particle
|
||||
bin []&Particle
|
||||
}
|
||||
|
||||
pub fn (mut s System) init(sc SystemConfig) {
|
||||
unsafe { s.pool.flags.set(.noslices | .noshrink) }
|
||||
unsafe { s.bin.flags.set(.noslices | .noshrink) }
|
||||
for i := 0; i < sc.pool; i++ {
|
||||
p := new(vec.Vec2[f64]{f32(s.width) * 0.5, f32(s.height) * 0.5})
|
||||
s.bin << p
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (mut s System) update(dt f64) {
|
||||
mut p := &Particle(unsafe { nil })
|
||||
mut moved := 0
|
||||
for i := 0; i < s.pool.len; i++ {
|
||||
p = s.pool[i]
|
||||
p.update(dt)
|
||||
if p.is_dead() {
|
||||
s.bin << p
|
||||
s.pool.delete(i)
|
||||
moved++
|
||||
}
|
||||
}
|
||||
if moved != 0 {
|
||||
$if trace_moves_spool_to_sbin ? {
|
||||
eprintln('${moved:4} particles s.pool -> s.bin')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (s System) draw() {
|
||||
sgl.begin_quads()
|
||||
for p in s.pool {
|
||||
p.draw()
|
||||
}
|
||||
sgl.end()
|
||||
}
|
||||
|
||||
pub fn (mut s System) reset() {
|
||||
for i in 0 .. s.pool.len {
|
||||
mut p := s.pool[i]
|
||||
p.reset()
|
||||
p.life_time = 0
|
||||
}
|
||||
for i in 0 .. s.bin.len {
|
||||
mut p := s.pool[i]
|
||||
p.reset()
|
||||
p.life_time = 0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (mut s System) explode(x f32, y f32) {
|
||||
mut reserve := 500
|
||||
center := vec.Vec2[f64]{x, y}
|
||||
mut p := &Particle(unsafe { nil })
|
||||
mut moved := 0
|
||||
for i := 0; i < s.bin.len && reserve > 0; i++ {
|
||||
p = s.bin[i]
|
||||
p.reset()
|
||||
p.location.from(center)
|
||||
p.acceleration = vec.Vec2[f64]{rand.f32_in_range(-0.5, 0.5) or { -0.5 }, rand.f32_in_range(-0.5, 0.5) or {
|
||||
-0.5
|
||||
}}
|
||||
p.velocity = vec.Vec2[f64]{rand.f32_in_range(-0.5, 0.5) or { -0.5 }, rand.f32_in_range(-0.5, 0.5) or {
|
||||
-0.5
|
||||
}}
|
||||
p.life_time = rand.f64_in_range(500, 2000) or { 500 }
|
||||
s.pool << p
|
||||
s.bin.delete(i)
|
||||
moved++
|
||||
reserve--
|
||||
}
|
||||
if moved != 0 {
|
||||
$if trace_moves_sbin_to_spool ? {
|
||||
eprintln('${moved:4} particles s.bin -> s.pool')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (mut s System) free() {
|
||||
for p in s.pool {
|
||||
if unsafe { p == 0 } {
|
||||
print(ptr_str(p) + ' ouch')
|
||||
continue
|
||||
}
|
||||
unsafe { free(p) }
|
||||
}
|
||||
s.pool.clear()
|
||||
for p in s.bin {
|
||||
if unsafe { p == 0 } {
|
||||
print(ptr_str(p) + ' ouch')
|
||||
continue
|
||||
}
|
||||
unsafe {
|
||||
// println('Freeing from bin')
|
||||
free(p)
|
||||
}
|
||||
}
|
||||
s.bin.clear()
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
// 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.sapp
|
||||
import sokol.gfx
|
||||
import sokol.sgl
|
||||
import particle
|
||||
|
||||
fn main() {
|
||||
mut app := &App{
|
||||
width: 800
|
||||
height: 400
|
||||
pass_action: gfx.create_clear_pass_action(0.1, 0.1, 0.1, 1.0)
|
||||
}
|
||||
app.init()
|
||||
app.run()
|
||||
}
|
||||
|
||||
struct App {
|
||||
pass_action gfx.PassAction
|
||||
mut:
|
||||
width int
|
||||
height int
|
||||
frame i64
|
||||
last i64
|
||||
ps particle.System
|
||||
alpha_pip sgl.Pipeline
|
||||
}
|
||||
|
||||
fn (mut a App) init() {
|
||||
a.frame = 0
|
||||
a.last = time.ticks()
|
||||
a.ps = particle.System{
|
||||
width: a.width
|
||||
height: a.height
|
||||
}
|
||||
a.ps.init(particle.SystemConfig{
|
||||
pool: 20000
|
||||
})
|
||||
}
|
||||
|
||||
fn (mut a App) cleanup() {
|
||||
unsafe {
|
||||
a.ps.free()
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut a App) run() {
|
||||
title := 'V Particle Example'
|
||||
desc := sapp.Desc{
|
||||
width: a.width
|
||||
height: a.height
|
||||
user_data: a
|
||||
init_userdata_cb: init
|
||||
frame_userdata_cb: frame
|
||||
event_userdata_cb: event
|
||||
window_title: &char(title.str)
|
||||
cleanup_userdata_cb: cleanup
|
||||
}
|
||||
sapp.run(&desc)
|
||||
}
|
||||
|
||||
fn (a App) draw() {
|
||||
sgl.load_pipeline(a.alpha_pip)
|
||||
a.ps.draw()
|
||||
}
|
||||
|
||||
fn init(mut app App) {
|
||||
desc := sapp.create_desc()
|
||||
gfx.setup(&desc)
|
||||
sgl_desc := sgl.Desc{
|
||||
max_vertices: 50 * 65536
|
||||
}
|
||||
sgl.setup(&sgl_desc)
|
||||
mut pipdesc := gfx.PipelineDesc{}
|
||||
unsafe { vmemset(&pipdesc, 0, int(sizeof(pipdesc))) }
|
||||
|
||||
color_state := gfx.ColorTargetState{
|
||||
blend: gfx.BlendState{
|
||||
enabled: true
|
||||
src_factor_rgb: .src_alpha
|
||||
dst_factor_rgb: .one_minus_src_alpha
|
||||
}
|
||||
}
|
||||
pipdesc.colors[0] = color_state
|
||||
|
||||
app.alpha_pip = sgl.make_pipeline(&pipdesc)
|
||||
}
|
||||
|
||||
fn cleanup(mut app App) {
|
||||
app.cleanup()
|
||||
gfx.shutdown()
|
||||
}
|
||||
|
||||
fn frame(mut app App) {
|
||||
app.width = sapp.width()
|
||||
app.height = sapp.height()
|
||||
t := time.ticks()
|
||||
dt := f64(t - app.last) / 1000.0
|
||||
app.ps.update(dt)
|
||||
draw(app)
|
||||
pass := sapp.create_default_pass(app.pass_action)
|
||||
gfx.begin_pass(&pass)
|
||||
sgl.default_pipeline()
|
||||
sgl.draw()
|
||||
gfx.end_pass()
|
||||
gfx.commit()
|
||||
app.frame++
|
||||
app.last = t
|
||||
}
|
||||
|
||||
fn event(ev &sapp.Event, mut app App) {
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
Reference in new issue
Block a user