added alpha blending

This commit is contained in:
lukas 2020-12-26 14:30:28 +01:00
parent b6231669cc
commit 2b88f95aaf

View file

@ -15,7 +15,7 @@ unsigned int px_pixelcount = 0;
unsigned int px_clientcount = 0; unsigned int px_clientcount = 0;
struct RGBLedMatrix *matrix; struct RGBLedMatrix *matrix;
struct LedCanvas *offscreen_canvas; struct LedCanvas *offscreen_canvas;
uint32_t colorarray[192][128] = {0}; //bad bad style hardcoded size...
// User sessions // User sessions
typedef struct PxSession { typedef struct PxSession {
@ -70,6 +70,22 @@ void px_on_close(NetClient *client, int error) {
px_clientcount--; px_clientcount--;
} }
static inline uint32_t blend(uint32_t fg, uint32_t bg)
{
uint32_t result;
uint32_t fg_red = (fg & 0xFF000000) >> 24;
uint32_t fg_green = (fg & 0x00FF0000) >> 16;
uint32_t fg_blue = (fg & 0x0000FF00) >> 8;
uint32_t fg_alpha = (fg & 0x000000FF) >> 0;
uint32_t bg_red = (bg & 0xFF000000) >> 24;
uint32_t bg_green = (bg & 0x00FF0000) >> 16;
uint32_t bg_blue = (bg & 0x0000FF00) >> 8;
uint32_t bg_alpha = 256 - fg_alpha;
result = (((fg_red * fg_alpha + bg_red * bg_alpha) >> 8) << 24) + (((fg_green * fg_alpha + bg_green * bg_alpha) >> 8) << 16) + (((fg_blue * fg_alpha + bg_blue * bg_alpha) >> 8) << 8) + 0xff;
return result;
}
void px_on_read(NetClient *client, char *line) { void px_on_read(NetClient *client, char *line) {
if (fast_str_startswith("PX ", line)) { if (fast_str_startswith("PX ", line)) {
const char * ptr = line + 3; const char * ptr = line + 3;
@ -100,6 +116,7 @@ void px_on_read(NetClient *client, char *line) {
if (*endptr == '\0') { if (*endptr == '\0') {
uint32_t c; uint32_t c;
//canvaspixel_get_px(x, y, &c); //canvaspixel_get_px(x, y, &c);
c = colorarray[x][y];
char str[64]; char str[64];
sprintf(str, "PX %u %u %06X", x, y, (c >> 8)); sprintf(str, "PX %u %u %06X", x, y, (c >> 8));
net_send(client, str); net_send(client, str);
@ -132,7 +149,13 @@ void px_on_read(NetClient *client, char *line) {
px_pixelcount++; px_pixelcount++;
//canvaspixel_set_px(x, y, c); //canvaspixel_set_px(x, y, c);
led_canvas_set_pixel(offscreen_canvas, x, y, c >>24&255 , c>>16&255, c>>8&255);
if (x < px_width && y < px_height){
c = blend(c, colorarray[x][y]);
colorarray[x][y] = c;
led_canvas_set_pixel(offscreen_canvas, x, y, c >>24&255 , c>>16&255, c>>8&255);
}
//offscreen_canvas = led_matrix_swap_on_vsync(matrix, offscreen_canvas); //offscreen_canvas = led_matrix_swap_on_vsync(matrix, offscreen_canvas);
} else if (fast_str_startswith("SIZE", line)) { } else if (fast_str_startswith("SIZE", line)) {
@ -193,6 +216,7 @@ void *myThreadFun(/**struct RGBLedMatrix *matrix, struct LedCanvas *offscreen_ca
} }
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
canvaspixel_setcb_key(&px_on_key); canvaspixel_setcb_key(&px_on_key);
@ -202,6 +226,7 @@ int main(int argc, char **argv) {
//int width, height; //int width, height;
//int x, y, i; //int x, y, i;
memset(&options, 0, sizeof(options)); memset(&options, 0, sizeof(options));
options.rows = 32; options.rows = 32;
options.cols = 64; options.cols = 64;