vfmt: reformat examples/sokol/particles *.v files

pull/5622/head
Delyan Angelov 2020-07-02 15:31:42 +03:00
parent 05e11f6336
commit 6ec86fa344
5 changed files with 74 additions and 115 deletions

View File

@ -48,7 +48,7 @@ fn (mut a App) cleanup() {
fn (a App) run() { fn (a App) run() {
title := 'V Particle Example' title := 'V Particle Example'
desc := C.sapp_desc { desc := C.sapp_desc{
width: a.width width: a.width
height: a.height height: a.height
user_data: &a user_data: &a
@ -67,7 +67,7 @@ fn (a App) draw() {
} }
fn init(user_data voidptr) { fn init(user_data voidptr) {
desc := C.sg_desc { desc := C.sg_desc{
mtl_device: sapp.metal_get_device() mtl_device: sapp.metal_get_device()
mtl_renderpass_descriptor_cb: sapp.metal_get_renderpass_descriptor mtl_renderpass_descriptor_cb: sapp.metal_get_renderpass_descriptor
mtl_drawable_cb: sapp.metal_get_drawable mtl_drawable_cb: sapp.metal_get_drawable

View File

@ -1,11 +1,8 @@
// Copyright(C) 2019 Lars Pontoppidan. All rights reserved. // 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 // Use of this source code is governed by an MIT license file distributed with this software package
module particle module particle
/* // * Color
* Color
*/
pub struct Color { pub struct Color {
mut: mut:
r byte r byte

View File

@ -1,6 +1,5 @@
// Copyright(C) 2019 Lars Pontoppidan. All rights reserved. // 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 // Use of this source code is governed by an MIT license file distributed with this software package
module particle module particle
import vec2 import vec2
@ -8,21 +7,16 @@ import sokol.sgl
const ( const (
default_life_time = 1000 default_life_time = 1000
default_v_color = Color{93, 136, 193, 255 } default_v_color = Color{93, 136, 193, 255}
) )
/* // * Module public
* Module public
*/
pub fn new(location vec2.Vec2) &Particle { pub fn new(location vec2.Vec2) &Particle {
p := &Particle{
p := &Particle { location: location
location: location, velocity: vec2.Vec2{0, 0}
velocity: vec2.Vec2{0,0} acceleration: vec2.Vec2{0, 0}
acceleration: vec2.Vec2{0,0}
color: default_v_color color: default_v_color
life_time: default_life_time life_time: default_life_time
life_time_init: default_life_time life_time_init: default_life_time
} }
@ -33,20 +27,15 @@ fn remap(v, min, max, new_min, new_max f64) f64 {
return (((v - min) * (new_max - new_min)) / (max - min)) + new_min return (((v - min) * (new_max - new_min)) / (max - min)) + new_min
} }
/* // * Particle
* Particle
*/
pub struct Particle { pub struct Particle {
mut: mut:
location vec2.Vec2 location vec2.Vec2
velocity vec2.Vec2 velocity vec2.Vec2
acceleration vec2.Vec2 acceleration vec2.Vec2
color Color
color Color life_time f64
life_time_init f64
life_time f64
life_time_init f64
} }
pub fn (mut p Particle) update(dt f64) { pub fn (mut p Particle) update(dt f64) {
@ -54,16 +43,13 @@ pub fn (mut p Particle) update(dt f64) {
acc.multiply_f64(dt) acc.multiply_f64(dt)
p.velocity = p.velocity.add(acc) p.velocity = p.velocity.add(acc)
p.location = p.location.add(p.velocity) p.location = p.location.add(p.velocity)
lt := p.life_time - (1000 * dt) lt := p.life_time - (1000 * dt)
if lt > 0 { if lt > 0 {
p.life_time = lt p.life_time = lt
p.color.r = p.color.r - 1 // byte(remap(p.life_time,0.0,p.life_time_init,0,p.color.r))
p.color.r = p.color.r - 1 //byte(remap(p.life_time,0.0,p.life_time_init,0,p.color.r)) p.color.g = p.color.g - 1 // byte(remap(p.life_time,0.0,p.life_time_init,0,p.color.g))
p.color.g = p.color.g - 1 //byte(remap(p.life_time,0.0,p.life_time_init,0,p.color.g)) p.color.b = p.color.b - 1 // byte(remap(p.life_time,0.0,p.life_time_init,0,p.color.b))
p.color.b = p.color.b - 1 //byte(remap(p.life_time,0.0,p.life_time_init,0,p.color.b)) // p.color.a = byte(remap(p.life_time,0.0,p.life_time_init,0,255))
//p.color.a = byte(remap(p.life_time,0.0,p.life_time_init,0,255))
} else { } else {
p.life_time = 0 p.life_time = 0
} }
@ -75,9 +61,7 @@ pub fn (p Particle) is_dead() bool {
pub fn (p Particle) draw() { pub fn (p Particle) draw() {
l := p.location l := p.location
sgl.c4b(p.color.r, p.color.g, p.color.b, p.color.a) sgl.c4b(p.color.r, p.color.g, p.color.b, p.color.a)
lx := f32(l.x) lx := f32(l.x)
ly := f32(l.y) ly := f32(l.y)
sgl.v2f(lx, ly) sgl.v2f(lx, ly)
@ -90,10 +74,8 @@ pub fn (mut p Particle) reset() {
p.location.zero() p.location.zero()
p.acceleration.zero() p.acceleration.zero()
p.velocity.zero() p.velocity.zero()
// p.color = Color{93, 136, 193, 255}
//p.color = Color{93, 136, 193, 255}
p.color = default_v_color p.color = default_v_color
p.life_time = default_life_time p.life_time = default_life_time
p.life_time_init = p.life_time p.life_time_init = p.life_time
} }

View File

@ -1,38 +1,35 @@
// Copyright(C) 2019 Lars Pontoppidan. All rights reserved. // 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 // Use of this source code is governed by an MIT license file distributed with this software package
module particle module particle
import vec2 import vec2
import rand import rand
import sokol.sgl import sokol.sgl
pub struct SystemConfig { pub struct SystemConfig {
pool int pool int
} }
pub struct System { pub struct System {
width int width int
height int height int
mut: mut:
pool []&Particle pool []&Particle
bin []&Particle bin []&Particle
} }
pub fn (mut s System) init(sc SystemConfig) { pub fn (mut s System) init(sc SystemConfig) {
for i := 0; i < sc.pool; i++ { for i := 0; i < sc.pool; i++ {
p := particle.new( vec2.Vec2{f32(s.width)*0.5, f32(s.height)*0.5} ) p := new(vec2.Vec2{f32(s.width) * 0.5, f32(s.height) * 0.5})
s.pool << p s.pool << p
} }
} }
pub fn (mut s System) update(dt f64) { pub fn (mut s System) update(dt f64) {
mut p := &Particle(0) mut p := &Particle(0)
for i := 0; i < s.pool.len; i++ { for i := 0; i < s.pool.len; i++ {
p = s.pool[i] p = s.pool[i]
p.update(dt) p.update(dt)
if p.is_dead() { if p.is_dead() {
s.bin << p s.bin << p
s.pool.delete(i) s.pool.delete(i)
@ -59,26 +56,20 @@ pub fn (mut s System) reset() {
} }
} }
pub fn (mut s System) explode(x f32, y f32) { pub fn (mut s System) explode(x, y f32) {
mut reserve := 500 mut reserve := 500
center := vec2.Vec2{x, y}
center := vec2.Vec2{x,y}
mut p := &Particle(0) mut p := &Particle(0)
for i := 0; i < s.bin.len && reserve > 0; i++ { for i := 0; i < s.bin.len && reserve > 0; i++ {
p = s.bin[i] p = s.bin[i]
p.reset() p.reset()
p.location.from(center) p.location.from(center)
p.acceleration = vec2.Vec2{rand.f32_in_range(-0.5,0.5),rand.f32_in_range(-0.5,0.5)} p.acceleration = vec2.Vec2{rand.f32_in_range(-0.5, 0.5), rand.f32_in_range(-0.5,
p.velocity = vec2.Vec2{rand.f32_in_range(-0.5,0.5),rand.f32_in_range(-0.5,0.5)} 0.5)}
p.life_time = rand.f64_in_range(500,2000) p.velocity = vec2.Vec2{rand.f32_in_range(-0.5, 0.5), rand.f32_in_range(-0.5, 0.5)}
p.life_time = rand.f64_in_range(500, 2000)
s.pool << p s.pool << p
s.bin.delete(i) s.bin.delete(i)
reserve-- reserve--
} }
} }
@ -86,24 +77,23 @@ pub fn (mut s System) explode(x f32, y f32) {
pub fn (mut s System) free() { pub fn (mut s System) free() {
for p in s.pool { for p in s.pool {
if p == 0 { if p == 0 {
print(ptr_str(p)+' ouch') print(ptr_str(p) + ' ouch')
continue continue
} }
unsafe{ unsafe {
free(p) free(p)
} }
} }
s.pool.clear() s.pool.clear()
for p in s.bin { for p in s.bin {
if p == 0 { if p == 0 {
print(ptr_str(p)+' ouch') print(ptr_str(p) + ' ouch')
continue continue
} }
unsafe {
unsafe{ // println('Freeing from bin')
//println('Freeing from bin')
free(p) free(p)
} }
} }
s.bin.clear() s.bin.clear()
} }

View File

@ -1,99 +1,89 @@
// Copyright(C) 2019 Lars Pontoppidan. All rights reserved. // 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 // Use of this source code is governed by an MIT license file distributed with this software package
module vec2 module vec2
pub struct Vec2 { pub struct Vec2 {
pub mut: pub mut:
x f64 x f64
y f64 y f64
} }
pub fn (mut v Vec2) zero() { pub fn (mut v Vec2) zero() {
v.x = 0.0 v.x = 0.0
v.y = 0.0 v.y = 0.0
} }
pub fn (mut v Vec2) from(src Vec2) { pub fn (mut v Vec2) from(src Vec2) {
v.x = src.x v.x = src.x
v.y = src.y v.y = src.y
} }
/* // * Addition
* Addition
*/
// + operator overload. Adds two vectors // + operator overload. Adds two vectors
pub fn (v1 Vec2) + (v2 Vec2) Vec2 { pub fn (v1 Vec2) +(v2 Vec2) Vec2 {
return Vec2{v1.x + v2.x, v1.y + v2.y} return Vec2{v1.x + v2.x, v1.y + v2.y}
} }
pub fn (v Vec2) add(vector Vec2) Vec2 { pub fn (v Vec2) add(vector Vec2) Vec2 {
return Vec2{ v.x + vector.x, v.y + vector.y } return Vec2{v.x + vector.x, v.y + vector.y}
} }
pub fn (v Vec2) add_f64(scalar f64) Vec2 { pub fn (v Vec2) add_f64(scalar f64) Vec2 {
return Vec2{ v.x + scalar, v.y + scalar } return Vec2{v.x + scalar, v.y + scalar}
} }
pub fn (mut v Vec2) plus(vector Vec2) { pub fn (mut v Vec2) plus(vector Vec2) {
v.x += vector.x v.x += vector.x
v.y += vector.y v.y += vector.y
} }
pub fn (mut v Vec2) plus_f64(scalar f64) { pub fn (mut v Vec2) plus_f64(scalar f64) {
v.x += scalar v.x += scalar
v.y += scalar v.y += scalar
} }
// * Subtraction
/* pub fn (v1 Vec2) -(v2 Vec2) Vec2 {
* Subtraction return Vec2{v1.x - v2.x, v1.y - v2.y}
*/
pub fn (v1 Vec2) - (v2 Vec2) Vec2 {
return Vec2{v1.x - v2.x, v1.y - v2.y}
} }
pub fn (v Vec2) sub(vector Vec2) Vec2 { pub fn (v Vec2) sub(vector Vec2) Vec2 {
return Vec2{ v.x - vector.x, v.y - vector.y } return Vec2{v.x - vector.x, v.y - vector.y}
} }
pub fn (v Vec2) sub_f64(scalar f64) Vec2 { pub fn (v Vec2) sub_f64(scalar f64) Vec2 {
return Vec2{ v.x - scalar, v.y - scalar } return Vec2{v.x - scalar, v.y - scalar}
} }
pub fn (mut v Vec2) subtract(vector Vec2) { pub fn (mut v Vec2) subtract(vector Vec2) {
v.x -= vector.x v.x -= vector.x
v.y -= vector.y v.y -= vector.y
} }
pub fn (mut v Vec2) subtract_f64(scalar f64) { pub fn (mut v Vec2) subtract_f64(scalar f64) {
v.x -= scalar v.x -= scalar
v.y -= scalar v.y -= scalar
} }
/* // * Multiplication
* Multiplication pub fn (v1 Vec2) *(v2 Vec2) Vec2 {
*/ return Vec2{v1.x * v2.x, v1.y * v2.y}
pub fn (v1 Vec2) * (v2 Vec2) Vec2 {
return Vec2{v1.x * v2.x, v1.y * v2.y}
} }
pub fn (v Vec2) mul(vector Vec2) Vec2 { pub fn (v Vec2) mul(vector Vec2) Vec2 {
return Vec2{ v.x * vector.x, v.y * vector.y } return Vec2{v.x * vector.x, v.y * vector.y}
} }
pub fn (v Vec2) mul_f64(scalar f64) Vec2 { pub fn (v Vec2) mul_f64(scalar f64) Vec2 {
return Vec2{ v.x * scalar, v.y * scalar } return Vec2{v.x * scalar, v.y * scalar}
} }
pub fn (mut v Vec2) multiply(vector Vec2) { pub fn (mut v Vec2) multiply(vector Vec2) {
v.x *= vector.x v.x *= vector.x
v.y *= vector.y v.y *= vector.y
} }
pub fn (mut v Vec2) multiply_f64(scalar f64) { pub fn (mut v Vec2) multiply_f64(scalar f64) {
v.x *= scalar v.x *= scalar
v.y *= scalar v.y *= scalar
} }