From 0e2f26780555ded85ca22694b4a245bddf8ecbdd Mon Sep 17 00:00:00 2001 From: Larpon Date: Mon, 28 Sep 2020 06:14:15 +0200 Subject: [PATCH] examples: add support for transparency / opacity / alpha in particle example (#6488) --- examples/sokol/particles/modules/particle/particle.v | 4 ++-- examples/sokol/particles/modules/particle/system.v | 8 +++----- examples/sokol/particles/particles.v | 10 ++++++++++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/examples/sokol/particles/modules/particle/particle.v b/examples/sokol/particles/modules/particle/particle.v index 63dc6fcc0c..03489e89b3 100644 --- a/examples/sokol/particles/modules/particle/particle.v +++ b/examples/sokol/particles/modules/particle/particle.v @@ -27,7 +27,7 @@ fn remap(v, min, max, new_min, new_max f64) f64 { return (((v - min) * (new_max - new_min)) / (max - min)) + new_min } -// * Particle +// Particle pub struct Particle { mut: location vec2.Vec2 @@ -49,7 +49,7 @@ pub fn (mut p Particle) update(dt f64) { 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.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(int(remap(p.life_time, 0.0, p.life_time_init, 0, 255))) - 10 } else { p.life_time = 0 } diff --git a/examples/sokol/particles/modules/particle/system.v b/examples/sokol/particles/modules/particle/system.v index ec2e7fed96..003e33677b 100644 --- a/examples/sokol/particles/modules/particle/system.v +++ b/examples/sokol/particles/modules/particle/system.v @@ -46,12 +46,12 @@ pub fn (s System) draw() { } pub fn (mut s System) reset() { - for i in 0..s.pool.len { + 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 { + for i in 0 .. s.bin.len { mut p := s.pool[i] p.reset() p.life_time = 0 @@ -82,9 +82,7 @@ pub fn (mut s System) free() { print(ptr_str(p) + ' ouch') continue } - unsafe { - free(p) - } + unsafe {free(p)} } s.pool.clear() for p in s.bin { diff --git a/examples/sokol/particles/particles.v b/examples/sokol/particles/particles.v index 28e8bc8607..bc6805f6aa 100644 --- a/examples/sokol/particles/particles.v +++ b/examples/sokol/particles/particles.v @@ -31,6 +31,7 @@ mut: frame i64 last i64 ps particle.System + alpha_pip C.sgl_pipeline } fn (mut a App) init() { @@ -66,16 +67,24 @@ fn (mut a App) run() { } fn (a App) draw() { + sgl.load_pipeline(a.alpha_pip) a.ps.draw() } fn init(user_data voidptr) { + mut app := &App(user_data) desc := sapp.create_desc() gfx.setup(&desc) sgl_desc := C.sgl_desc_t{ max_vertices: 50 * 65536 } sgl.setup(&sgl_desc) + mut pipdesc := C.sg_pipeline_desc{} + unsafe {C.memset(&pipdesc, 0, sizeof(pipdesc))} + pipdesc.blend.enabled = true + pipdesc.blend.src_factor_rgb = C.SG_BLENDFACTOR_SRC_ALPHA + pipdesc.blend.dst_factor_rgb = C.SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA + app.alpha_pip = sgl.make_pipeline(&pipdesc) } fn cleanup(user_data voidptr) { @@ -93,6 +102,7 @@ fn frame(user_data voidptr) { app.ps.update(dt) draw(app) gfx.begin_default_pass(&app.pass_action, app.width, app.height) + sgl.default_pipeline() sgl.draw() gfx.end_pass() gfx.commit()