From 91e5956932d17de17c0c75be20c834cf7a6c6ed9 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Fri, 3 Feb 2017 13:06:26 -0800 Subject: rototiller: extricate draw_pixel() bounds checking If a z-buffer is added these checks will need to be done independent from and prior to drawing. Also it's silly to makergb() pixels which can't be drawn. --- src/modules/sparkler/draw.h | 8 +------- src/modules/sparkler/rocket.c | 7 ++++++- src/modules/sparkler/simple.c | 7 ++++++- src/modules/sparkler/spark.c | 8 +++++++- src/modules/sparkler/xplode.c | 12 ++++++++---- 5 files changed, 28 insertions(+), 14 deletions(-) (limited to 'src/modules/sparkler') diff --git a/src/modules/sparkler/draw.h b/src/modules/sparkler/draw.h index 5010374..b988319 100644 --- a/src/modules/sparkler/draw.h +++ b/src/modules/sparkler/draw.h @@ -15,18 +15,12 @@ static inline uint32_t makergb(uint32_t r, uint32_t g, uint32_t b, float intensi return (((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff)); } -static inline int draw_pixel(fb_fragment_t *f, int x, int y, uint32_t pixel) +static inline void draw_pixel(fb_fragment_t *f, int x, int y, uint32_t pixel) { uint32_t *pixels = f->buf; - if (y < 0 || y >= f->height || x < 0 || x >= f->width) { - return 0; - } - /* FIXME this assumes stride is aligned to 4 */ pixels[(y * (f->width + (f->stride >> 2))) + x] = pixel; - - return 1; } #endif diff --git a/src/modules/sparkler/rocket.c b/src/modules/sparkler/rocket.c index 6b9dc5e..2088cc7 100644 --- a/src/modules/sparkler/rocket.c +++ b/src/modules/sparkler/rocket.c @@ -1,6 +1,7 @@ #include #include "draw.h" +#include "fb.h" #include "particle.h" #include "particles.h" @@ -122,10 +123,14 @@ static void rocket_draw(particles_t *particles, particle_t *p, int x, int y, fb_ { rocket_ctxt_t *ctxt = p->ctxt; - if (!draw_pixel(f, x, y, 0xff0000)) { + if (!fb_fragment_contains(f, x, y)) { /* kill off parts that wander off screen */ ctxt->longevity = 0; + + return; } + + draw_pixel(f, x, y, 0xff0000); } diff --git a/src/modules/sparkler/simple.c b/src/modules/sparkler/simple.c index e453e46..74edc24 100644 --- a/src/modules/sparkler/simple.c +++ b/src/modules/sparkler/simple.c @@ -1,6 +1,7 @@ #include #include "draw.h" +#include "fb.h" #include "particle.h" #include "particles.h" @@ -97,10 +98,14 @@ static void simple_draw(particles_t *particles, particle_t *p, int x, int y, fb_ { simple_ctxt_t *ctxt = p->ctxt; - if (!draw_pixel(f, x, y, makergb(0xff, 0xff, 0xff, ((float)ctxt->longevity / ctxt->lifetime)))) { + if (!fb_fragment_contains(f, x, y)) { /* immediately kill off stars that wander off screen */ ctxt->longevity = 0; + + return; } + + draw_pixel(f, x, y, makergb(0xff, 0xff, 0xff, ((float)ctxt->longevity / ctxt->lifetime))); } diff --git a/src/modules/sparkler/spark.c b/src/modules/sparkler/spark.c index ea68ac2..786be78 100644 --- a/src/modules/sparkler/spark.c +++ b/src/modules/sparkler/spark.c @@ -1,6 +1,7 @@ #include #include "draw.h" +#include "fb.h" #include "particle.h" #include "particles.h" @@ -47,10 +48,15 @@ static void spark_draw(particles_t *particles, particle_t *p, int x, int y, fb_f { spark_ctxt_t *ctxt = p->ctxt; - if (!draw_pixel(f, x, y, makergb(0xff, 0xa0, 0x20, ((float)ctxt->longevity / ctxt->lifetime)))) { + if (!fb_fragment_contains(f, x, y)) { /* offscreen */ ctxt->longevity = 0; + + return; } + + draw_pixel(f, x, y, makergb(0xff, 0xa0, 0x20, ((float)ctxt->longevity / ctxt->lifetime))); + } diff --git a/src/modules/sparkler/xplode.c b/src/modules/sparkler/xplode.c index 24a436e..5f99f7b 100644 --- a/src/modules/sparkler/xplode.c +++ b/src/modules/sparkler/xplode.c @@ -1,6 +1,7 @@ #include #include "draw.h" +#include "fb.h" #include "particle.h" #include "particles.h" @@ -60,16 +61,19 @@ static void xplode_draw(particles_t *particles, particle_t *p, int x, int y, fb_ xplode_ctxt_t *ctxt = p->ctxt; uint32_t color; + if (!fb_fragment_contains(f, x, y)) { + /* offscreen */ + ctxt->longevity = 0; + return; + } + if (ctxt->longevity == ctxt->lifetime) { color = makergb(0xff, 0xff, 0xa0, 1.0); } else { color = makergb(0xff, 0xff, 0x00, ((float)ctxt->longevity / ctxt->lifetime)); } - if (!draw_pixel(f, x, y, color)) { - /* offscreen */ - ctxt->longevity = 0; - } + draw_pixel(f, x, y, color); } -- cgit v1.2.1 From 90fde2df2d8c59b0b885e6e02b5e89dbe966a5e9 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Fri, 3 Feb 2017 15:43:51 -0800 Subject: sparkler: use fb.h put_pixel helpers discards draw_pixel(), introduces helpers.h and a convenience function for bounds checking and oob extermination. Move makergb to helpers.h, draw.h gets removed in a later commit. --- src/modules/sparkler/draw.h | 9 --------- src/modules/sparkler/helpers.h | 36 ++++++++++++++++++++++++++++++++++++ src/modules/sparkler/rocket.c | 9 +++------ src/modules/sparkler/simple.c | 9 +++------ src/modules/sparkler/spark.c | 10 +++------- src/modules/sparkler/xplode.c | 9 +++------ 6 files changed, 48 insertions(+), 34 deletions(-) create mode 100644 src/modules/sparkler/helpers.h (limited to 'src/modules/sparkler') diff --git a/src/modules/sparkler/draw.h b/src/modules/sparkler/draw.h index b988319..10701e0 100644 --- a/src/modules/sparkler/draw.h +++ b/src/modules/sparkler/draw.h @@ -5,15 +5,6 @@ #include "fb.h" -/* helper for scaling rgb colors and packing them into an pixel */ -static inline uint32_t makergb(uint32_t r, uint32_t g, uint32_t b, float intensity) -{ - r = (((float)intensity) * r); - g = (((float)intensity) * g); - b = (((float)intensity) * b); - - return (((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff)); -} static inline void draw_pixel(fb_fragment_t *f, int x, int y, uint32_t pixel) { diff --git a/src/modules/sparkler/helpers.h b/src/modules/sparkler/helpers.h new file mode 100644 index 0000000..e263d36 --- /dev/null +++ b/src/modules/sparkler/helpers.h @@ -0,0 +1,36 @@ +#ifndef _HELPERS_H +#define _HELPERS_H + +#include + +#include "fb.h" +#include "particle.h" +#include "particles.h" + + +/* helper for scaling rgb colors and packing them into an pixel */ +static inline uint32_t makergb(uint32_t r, uint32_t g, uint32_t b, float intensity) +{ + r = (((float)intensity) * r); + g = (((float)intensity) * g); + b = (((float)intensity) * b); + + return (((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff)); +} + + +/* return if the particle should be drawn, and set *longevity to 0 if out of bounds */ +static inline int should_draw_expire_if_oob(particles_t *particles, particle_t *p, int x, int y, fb_fragment_t *f, int *longevity) +{ + if (!fb_fragment_contains(f, x, y)) { + /* offscreen */ + if (longevity) + *longevity = 0; + + return 0; + } + + return 1; +} + +#endif diff --git a/src/modules/sparkler/rocket.c b/src/modules/sparkler/rocket.c index 2088cc7..ae628d3 100644 --- a/src/modules/sparkler/rocket.c +++ b/src/modules/sparkler/rocket.c @@ -1,7 +1,7 @@ #include -#include "draw.h" #include "fb.h" +#include "helpers.h" #include "particle.h" #include "particles.h" @@ -123,14 +123,11 @@ static void rocket_draw(particles_t *particles, particle_t *p, int x, int y, fb_ { rocket_ctxt_t *ctxt = p->ctxt; - if (!fb_fragment_contains(f, x, y)) { + if (!should_draw_expire_if_oob(particles, p, x, y, f, &ctxt->longevity)) /* kill off parts that wander off screen */ - ctxt->longevity = 0; - return; - } - draw_pixel(f, x, y, 0xff0000); + fb_fragment_put_pixel_unchecked(f, x, y, 0xff0000); } diff --git a/src/modules/sparkler/simple.c b/src/modules/sparkler/simple.c index 74edc24..5524282 100644 --- a/src/modules/sparkler/simple.c +++ b/src/modules/sparkler/simple.c @@ -1,7 +1,7 @@ #include -#include "draw.h" #include "fb.h" +#include "helpers.h" #include "particle.h" #include "particles.h" @@ -98,14 +98,11 @@ static void simple_draw(particles_t *particles, particle_t *p, int x, int y, fb_ { simple_ctxt_t *ctxt = p->ctxt; - if (!fb_fragment_contains(f, x, y)) { + if (!should_draw_expire_if_oob(particles, p, x, y, f, &ctxt->longevity)) /* immediately kill off stars that wander off screen */ - ctxt->longevity = 0; - return; - } - draw_pixel(f, x, y, makergb(0xff, 0xff, 0xff, ((float)ctxt->longevity / ctxt->lifetime))); + fb_fragment_put_pixel_unchecked(f, x, y, makergb(0xff, 0xff, 0xff, ((float)ctxt->longevity / ctxt->lifetime))); } diff --git a/src/modules/sparkler/spark.c b/src/modules/sparkler/spark.c index 786be78..aa449c6 100644 --- a/src/modules/sparkler/spark.c +++ b/src/modules/sparkler/spark.c @@ -1,7 +1,7 @@ #include -#include "draw.h" #include "fb.h" +#include "helpers.h" #include "particle.h" #include "particles.h" @@ -48,15 +48,11 @@ static void spark_draw(particles_t *particles, particle_t *p, int x, int y, fb_f { spark_ctxt_t *ctxt = p->ctxt; - if (!fb_fragment_contains(f, x, y)) { + if (!should_draw_expire_if_oob(particles, p, x, y, f, &ctxt->longevity)) /* offscreen */ - ctxt->longevity = 0; - return; - } - - draw_pixel(f, x, y, makergb(0xff, 0xa0, 0x20, ((float)ctxt->longevity / ctxt->lifetime))); + fb_fragment_put_pixel_unchecked(f, x, y, makergb(0xff, 0xa0, 0x20, ((float)ctxt->longevity / ctxt->lifetime))); } diff --git a/src/modules/sparkler/xplode.c b/src/modules/sparkler/xplode.c index 5f99f7b..3e3beb4 100644 --- a/src/modules/sparkler/xplode.c +++ b/src/modules/sparkler/xplode.c @@ -1,7 +1,7 @@ #include -#include "draw.h" #include "fb.h" +#include "helpers.h" #include "particle.h" #include "particles.h" @@ -61,11 +61,8 @@ static void xplode_draw(particles_t *particles, particle_t *p, int x, int y, fb_ xplode_ctxt_t *ctxt = p->ctxt; uint32_t color; - if (!fb_fragment_contains(f, x, y)) { - /* offscreen */ - ctxt->longevity = 0; + if (!should_draw_expire_if_oob(particles, p, x, y, f, &ctxt->longevity)) return; - } if (ctxt->longevity == ctxt->lifetime) { color = makergb(0xff, 0xff, 0xa0, 1.0); @@ -73,7 +70,7 @@ static void xplode_draw(particles_t *particles, particle_t *p, int x, int y, fb_ color = makergb(0xff, 0xff, 0x00, ((float)ctxt->longevity / ctxt->lifetime)); } - draw_pixel(f, x, y, color); + fb_fragment_put_pixel_unchecked(f, x, y, color); } -- cgit v1.2.1 From f21492a1e8314e27ef307bc4b2629468d40e6c72 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Fri, 3 Feb 2017 15:46:08 -0800 Subject: sparkler: drop draw.h --- src/modules/sparkler/draw.h | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 src/modules/sparkler/draw.h (limited to 'src/modules/sparkler') diff --git a/src/modules/sparkler/draw.h b/src/modules/sparkler/draw.h deleted file mode 100644 index 10701e0..0000000 --- a/src/modules/sparkler/draw.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef _DRAW_H -#define _DRAW_H - -#include - -#include "fb.h" - - -static inline void draw_pixel(fb_fragment_t *f, int x, int y, uint32_t pixel) -{ - uint32_t *pixels = f->buf; - - /* FIXME this assumes stride is aligned to 4 */ - pixels[(y * (f->width + (f->stride >> 2))) + x] = pixel; -} - -#endif -- cgit v1.2.1 From b35ba71b6de341f7b87beb3bc078600f3d191612 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Fri, 3 Feb 2017 16:14:04 -0800 Subject: *: use fb_fragment_zero() instead of memset() --- src/modules/sparkler/sparkler.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/modules/sparkler') diff --git a/src/modules/sparkler/sparkler.c b/src/modules/sparkler/sparkler.c index 0bb0fcf..e2ff76e 100644 --- a/src/modules/sparkler/sparkler.c +++ b/src/modules/sparkler/sparkler.c @@ -35,7 +35,7 @@ static void sparkler(fb_fragment_t *fragment) initialized = 1; } - memset(buf, 0, ((fragment->width << 2) + fragment->stride) * fragment->height); + fb_fragment_zero(fragment); particles_age(particles); particles_draw(particles, fragment); -- cgit v1.2.1