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 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