From ee2adedfd19dbfd1db0ddc914ef01150fcb498cd Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Wed, 8 Apr 2020 22:58:14 +0000 Subject: [PATCH] examples: draw: Draw a larget dot --- examples/draw.c | 58 +++++++++++++++++++++++++++++++++++--------- examples/meson.build | 1 + 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/examples/draw.c b/examples/draw.c index fa866ae..19e2817 100644 --- a/examples/draw.c +++ b/examples/draw.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Andri Yngvason + * Copyright (c) 2019 - 2020 Andri Yngvason * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -18,21 +18,64 @@ #include #include +#include #include +#include #include #include #include #include +#include #include #define MAX_COORD 128 +struct coord { + int x, y; +}; + struct draw { struct nvnc_fb* fb; - struct { uint16_t x, y; } coord[MAX_COORD]; + struct coord coord[MAX_COORD]; int index; }; +int coord_distance_between(struct coord a, struct coord b) +{ + float x = abs(a.x - b.x); + float y = abs(a.y - b.y); + return round(sqrt(x * x + y * y)); +} + +void draw_dot(struct nvnc* nvnc, struct nvnc_fb* fb, struct coord coord, + int radius, uint32_t colour) +{ + uint32_t* image = nvnc_fb_get_addr(fb); + int width = nvnc_fb_get_width(fb); + int height = nvnc_fb_get_height(fb); + + struct coord start, stop; + + start.x = MAX(0, coord.x - radius); + start.y = MAX(0, coord.y - radius); + stop.x = MIN(width, coord.x + radius); + stop.y = MIN(height, coord.y + radius); + + struct pixman_region16 region; + pixman_region_init_rect(®ion, start.x, start.y, + stop.x - start.x, stop.y - start.y); + nvnc_damage_region(nvnc, ®ion); + pixman_region_fini(®ion); + + /* The brute force method. ;) */ + for (int y = start.y; y < stop.y; ++y) + for (int x = start.x; x < stop.x; ++x) { + struct coord point = { .x = x, .y = y }; + if (coord_distance_between(point, coord) <= radius) + image[x + y * width] = colour; + } +} + void on_pointer_event(struct nvnc_client* client, uint16_t x, uint16_t y, enum nvnc_button_mask buttons) { @@ -67,15 +110,8 @@ void on_render(struct nvnc* server, struct nvnc_fb* fb) struct draw* draw = nvnc_get_userdata(server); assert(draw); - uint32_t* image = nvnc_fb_get_addr(draw->fb); - int width = nvnc_fb_get_width(draw->fb); - - for (int i = 0; i < draw->index; ++i) { - uint16_t x = draw->coord[i].x; - uint16_t y = draw->coord[i].y; - - image[x + y * width] = 0; - } + for (int i = 0; i < draw->index; ++i) + draw_dot(server, fb, draw->coord[i], 16, 0); draw->index = 0; } diff --git a/examples/meson.build b/examples/meson.build index ddab518..4f6cc22 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -9,6 +9,7 @@ executable( neatvnc_dep, pixman, aml, + libm, ] )