diff options
author | Vito Caputo <vcaputo@gnugeneration.com> | 2017-02-08 20:03:38 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-08 20:03:38 -0800 |
commit | 8563edd058bb5e564b5e6c2bafde6636b3fce6ee (patch) | |
tree | d3232ac628eee1517e258d9b512b0f9a838e9f02 | |
parent | 9d032314e9db794dc88889bc24bf50bbafc3ec8d (diff) | |
parent | b35ba71b6de341f7b87beb3bc078600f3d191612 (diff) |
Consolidate fb_fragment_t interactions
sparkler and stars both cleared fragments and drew individual pixels into fragments, add that functionality to fb.h and cleanup sparkler and stars accordingly.
-rw-r--r-- | src/fb.h | 41 | ||||
-rw-r--r-- | src/modules/sparkler/draw.h | 32 | ||||
-rw-r--r-- | src/modules/sparkler/helpers.h | 36 | ||||
-rw-r--r-- | src/modules/sparkler/rocket.c | 10 | ||||
-rw-r--r-- | src/modules/sparkler/simple.c | 10 | ||||
-rw-r--r-- | src/modules/sparkler/spark.c | 10 | ||||
-rw-r--r-- | src/modules/sparkler/sparkler.c | 2 | ||||
-rw-r--r-- | src/modules/sparkler/xplode.c | 11 | ||||
-rw-r--r-- | src/modules/stars/draw.h | 16 | ||||
-rw-r--r-- | src/modules/stars/stars.c | 4 |
10 files changed, 104 insertions, 68 deletions
@@ -2,6 +2,7 @@ #define _FB_H #include <stdint.h> +#include <string.h> #include <sys/types.h> #include <xf86drmMode.h> /* for drmModeModeInfoPtr */ @@ -34,4 +35,44 @@ void fb_get_put_pages_count(fb_t *fb, unsigned *count); fb_t * fb_new(int drm_fd, uint32_t crtc_id, uint32_t *connectors, int n_connectors, drmModeModeInfoPtr mode, int n_pages); void fb_fragment_divide(fb_fragment_t *fragment, unsigned n_fragments, fb_fragment_t fragments[]); + +/* checks if a coordinate is contained within a fragment */ +static inline int fb_fragment_contains(fb_fragment_t *fragment, int x, int y) +{ + if (x < fragment->x || x >= fragment->x + fragment->width || + y < fragment->y || y >= fragment->y + fragment->height) + return 0; + + return 1; +} + + +/* puts a pixel into the fragment, no bounds checking is performed. */ +static inline void fb_fragment_put_pixel_unchecked(fb_fragment_t *fragment, int x, int y, uint32_t pixel) +{ + uint32_t *pixels = fragment->buf; + + /* FIXME this assumes stride is aligned to 4 */ + pixels[(y * (fragment->width + (fragment->stride >> 2))) + x] = pixel; +} + + +/* puts a pixel into the fragment, bounds checking is performed with a draw performed return status */ +static inline int fb_fragment_put_pixel_checked(fb_fragment_t *fragment, int x, int y, uint32_t pixel) +{ + if (!fb_fragment_contains(fragment, x, y)) + return 0; + + fb_fragment_put_pixel_unchecked(fragment, x, y, pixel); + + return 1; +} + + +/* zero a fragment */ +static inline void fb_fragment_zero(fb_fragment_t *fragment) +{ + memset(fragment->buf, 0, ((fragment->width << 2) + fragment->stride) * fragment->height); +} + #endif diff --git a/src/modules/sparkler/draw.h b/src/modules/sparkler/draw.h deleted file mode 100644 index 5010374..0000000 --- a/src/modules/sparkler/draw.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef _DRAW_H -#define _DRAW_H - -#include <stdint.h> - -#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 int 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/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 <stdint.h> + +#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 6b9dc5e..ae628d3 100644 --- a/src/modules/sparkler/rocket.c +++ b/src/modules/sparkler/rocket.c @@ -1,6 +1,7 @@ #include <stdlib.h> -#include "draw.h" +#include "fb.h" +#include "helpers.h" #include "particle.h" #include "particles.h" @@ -122,10 +123,11 @@ 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 (!should_draw_expire_if_oob(particles, p, x, y, f, &ctxt->longevity)) /* kill off parts that wander off screen */ - ctxt->longevity = 0; - } + return; + + fb_fragment_put_pixel_unchecked(f, x, y, 0xff0000); } diff --git a/src/modules/sparkler/simple.c b/src/modules/sparkler/simple.c index e453e46..5524282 100644 --- a/src/modules/sparkler/simple.c +++ b/src/modules/sparkler/simple.c @@ -1,6 +1,7 @@ #include <stdlib.h> -#include "draw.h" +#include "fb.h" +#include "helpers.h" #include "particle.h" #include "particles.h" @@ -97,10 +98,11 @@ 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 (!should_draw_expire_if_oob(particles, p, x, y, f, &ctxt->longevity)) /* immediately kill off stars that wander off screen */ - ctxt->longevity = 0; - } + return; + + 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 ea68ac2..aa449c6 100644 --- a/src/modules/sparkler/spark.c +++ b/src/modules/sparkler/spark.c @@ -1,6 +1,7 @@ #include <stdlib.h> -#include "draw.h" +#include "fb.h" +#include "helpers.h" #include "particle.h" #include "particles.h" @@ -47,10 +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 (!draw_pixel(f, x, y, makergb(0xff, 0xa0, 0x20, ((float)ctxt->longevity / ctxt->lifetime)))) { + if (!should_draw_expire_if_oob(particles, p, x, y, f, &ctxt->longevity)) /* offscreen */ - ctxt->longevity = 0; - } + return; + + fb_fragment_put_pixel_unchecked(f, x, y, makergb(0xff, 0xa0, 0x20, ((float)ctxt->longevity / ctxt->lifetime))); } 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); diff --git a/src/modules/sparkler/xplode.c b/src/modules/sparkler/xplode.c index 24a436e..3e3beb4 100644 --- a/src/modules/sparkler/xplode.c +++ b/src/modules/sparkler/xplode.c @@ -1,6 +1,7 @@ #include <stdlib.h> -#include "draw.h" +#include "fb.h" +#include "helpers.h" #include "particle.h" #include "particles.h" @@ -60,16 +61,16 @@ 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 (!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); } else { color = makergb(0xff, 0xff, 0x00, ((float)ctxt->longevity / ctxt->lifetime)); } - if (!draw_pixel(f, x, y, color)) { - /* offscreen */ - ctxt->longevity = 0; - } + fb_fragment_put_pixel_unchecked(f, x, y, color); } diff --git a/src/modules/stars/draw.h b/src/modules/stars/draw.h index 5010374..0b68c00 100644 --- a/src/modules/stars/draw.h +++ b/src/modules/stars/draw.h @@ -3,8 +3,6 @@ #include <stdint.h> -#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) { @@ -15,18 +13,4 @@ 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) -{ - 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/stars/stars.c b/src/modules/stars/stars.c index e009714..2ff9b99 100644 --- a/src/modules/stars/stars.c +++ b/src/modules/stars/stars.c @@ -37,13 +37,13 @@ static void stars(fb_fragment_t *fragment) } // draw space (or blank the frame, if you prefer) - memset(fragment->buf, 0, ((fragment->width << 2) + fragment->stride) * fragment->height); + fb_fragment_zero(fragment); // draw stars for (;;) { int ret = process_point( u, &rp ); if (ret==0) break; - if (ret==1) draw_pixel(fragment, rp.x+(width/2), rp.y+(height/2), + if (ret==1) fb_fragment_put_pixel_unchecked(fragment, rp.x+(width/2), rp.y+(height/2), makergb(0xFF, 0xFF, 0xFF, (float)rp.opacity/OPACITY_MAX) ); } |