Skip to content

Commit

Permalink
Merge pull request #23 from seb3s/add-alpha-channel-to-sprites
Browse files Browse the repository at this point in the history
add alpha channel to sprites
  • Loading branch information
crertel authored May 5, 2024
2 parents a9bd325 + f33922c commit c68fc00
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ For example, for apps running on a Mac/PC/Linux, it is usually set to `host`, wh

If you are building for Nerves, it will use `cairo-fb`


Previous versions of `scenic_driver_local` would use `bcm` (Broadcom Manager) for any of `rpi`, `rpi0`, `rip2`, `rpi3`, and `rpi3a` and `drm` for `bbb` and `rpi4`.

You can explicitly use these by setting `SCENIC_LOCAL_TARGET=bcm` or `SCENIC_LOCAL_TARGET=drm`, **but these options are being deprecated**.
Please try the default of `SCENIC_LOCAL_TARGET=cairo-fb` as this should work universally on any Nerves target.

Expand Down
3 changes: 2 additions & 1 deletion c_src/device/cairo/cairo_script_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ static void draw_sprite(scenic_cairo_ctx_t* p_ctx,

cairo_rectangle(p_ctx->cr, sprite.dx, sprite.dy, sprite.dw, sprite.dh);
cairo_scale(p_ctx->cr, sprite.dw / sprite.sw, sprite.dh / sprite.sh);
cairo_fill(p_ctx->cr);
cairo_clip(p_ctx->cr);
cairo_paint_with_alpha(p_ctx->cr, sprite.alpha);

cairo_restore(p_ctx->cr);
}
Expand Down
14 changes: 7 additions & 7 deletions c_src/device/nvg/nvg_script_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ void script_ops_draw_text(void* v_ctx,
// see: https://github.com/memononen/nanovg/issues/348
static void draw_image(NVGcontext* p_ctx,
sid_t id,
const sprite_t s)
const sprite_t sprite)
{
float ax, ay;
NVGpaint img_pattern;
Expand All @@ -236,20 +236,20 @@ static void draw_image(NVGcontext* p_ctx,
int iw,ih;
nvgImageSize(p_ctx, p_image->image_id, &iw, &ih);

// Aspect ration of pixel in x an y dimensions. This allows us to scale
// Aspect ratio of pixel in x and y dimensions. This allows us to scale
// the sprite to fill the whole rectangle.
ax = s.dw / s.sw;
ay = s.dh / s.sh;
ax = sprite.dw / sprite.sw;
ay = sprite.dh / sprite.sh;

// create the temporary pattern
img_pattern = nvgImagePattern(p_ctx,
s.dx - s.sx*ax, s.dy - s.sy*ay,
sprite.dx - sprite.sx*ax, sprite.dy - sprite.sy*ay,
(float)iw*ax, (float)ih*ay,
0, p_image->image_id, 1.0);
0, p_image->image_id, sprite.alpha);

// draw the image into a rect
nvgBeginPath(p_ctx);
nvgRect(p_ctx, s.dx, s.dy, s.dw, s.dh);
nvgRect(p_ctx, sprite.dx, sprite.dy, sprite.dw, sprite.dh);
nvgFillPaint(p_ctx, img_pattern);
nvgFill(p_ctx);

Expand Down
5 changes: 3 additions & 2 deletions c_src/scenic/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,11 @@ void render_script(void* v_ctx, sid_t id)
.dx = get_float(p, i + 16),
.dy = get_float(p, i + 20),
.dw = get_float(p, i + 24),
.dh = get_float(p, i + 28)
.dh = get_float(p, i + 28),
.alpha = get_float( p, i + 32 )
};

i += 32;
i += 36;
}
script_ops_draw_sprites(v_ctx, id, count, sprites);
free(sprites);
Expand Down
6 changes: 4 additions & 2 deletions c_src/scenic/script_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,12 @@ void log_script_ops_draw_sprites(const char* prefix, const char* func, log_level
for (int i = 0; i < count; i++) {
log_message(level, "%s %s: index: %d %{"
"s: {{%.1f,%.1f},{%.1f,%.1f}}, "
"d: {{%.1f,%.1f},{%.1f,%.1f}}"
"d: {{%.1f,%.1f},{%.1f,%.1f}}, "
"alpha: %.1f}"
"}", prefix, func, i,
sprites[i].sx, sprites[i].sy, sprites[i].sw, sprites[i].sh,
sprites[i].dx, sprites[i].dy, sprites[i].dw, sprites[i].dh);
sprites[i].dx, sprites[i].dy, sprites[i].dw, sprites[i].dh,
sprites[i].alpha);
}
}

Expand Down
1 change: 1 addition & 0 deletions c_src/scenic/script_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ typedef struct {
float dy;
float dw;
float dh;
float alpha;
} sprite_t;

typedef struct {
Expand Down

0 comments on commit c68fc00

Please sign in to comment.