examples: add support for transparency / opacity / alpha in particle example (#6488)
parent
06cade6c31
commit
0e2f267805
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue