summaryrefslogtreecommitdiff
path: root/examples/tablet.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/tablet.c')
-rw-r--r--examples/tablet.c37
1 files changed, 17 insertions, 20 deletions
diff --git a/examples/tablet.c b/examples/tablet.c
index ce796a4..8c62fc0 100644
--- a/examples/tablet.c
+++ b/examples/tablet.c
@@ -1,6 +1,5 @@
/* Pen canvas: pressure, eraser and coherent strokes drawn straight into the CPU surface. */
#include <yasl/yasl.h>
-#include <yasl/cpu.h>
#include <string.h>
#include <math.h>
@@ -24,11 +23,10 @@ static void stamp(uint32_t *pixels, int w, int h, double x, double y, double rad
}
/* Paints paper over the area a resize added, or everything when clear is set. */
-static uint32_t *canvas(yasl_surface *s, int *w, int *h, int *painted_w, int *painted_h, bool clear, struct yasl_cpu_buffer *buffer)
+static uint32_t *canvas(yasl_surface *s, int *w, int *h, int *painted_w, int *painted_h, bool clear)
{
- if (yasl_cpu_acquire(s, buffer) < 0) return NULL;
- *w = buffer->width; *h = buffer->height;
- uint32_t *pixels = buffer->pixels;
+ uint32_t *pixels = yasl_cpu_pixels(s, w, h);
+ if (!pixels) return NULL;
if (clear) *painted_w = *painted_h = 0;
for (int y = 0; y < *h; y++)
for (int x = y < *painted_h ? *painted_w : 0; x < *w; x++) pixels[(size_t)y * *w + x] = PAPER;
@@ -44,11 +42,11 @@ int main(int argc, char **argv)
if (!strcmp(argv[i], "--smoke-test")) smoke = true;
else if (!strcmp(argv[i], "--backend") && i + 1 < argc) {
const char *b = argv[++i];
- if (!(yasl_init(&(struct yasl_options){.backend=!strcmp(b, "wayland") ? YASL_BACKEND_WAYLAND :
- !strcmp(b, "x11") ? YASL_BACKEND_X11 : YASL_BACKEND_COCOA}) == 0)) return 1;
+ if (yasl_init(!strcmp(b, "wayland") ? YASL_BACKEND_WAYLAND :
+ !strcmp(b, "x11") ? YASL_BACKEND_X11 : YASL_BACKEND_COCOA, NULL) < 0) return 1;
} else return 1;
}
- yasl_surface *s = yasl_surface_create(&(struct yasl_surface_options){.title="YASL pen canvas — C clears, Escape exits", .width=800, .height=600, .type=YASL_SURFACE_CPU, .flags=0});
+ yasl_surface *s = yasl_surface_create(&(struct yasl_surface_options){.title="YASL pen canvas — C clears, Escape exits", .width=800, .height=600});
if (!s) return 1;
int painted_w = 0, painted_h = 0, frames = 0, result = 1;
bool contact = false;
@@ -58,26 +56,26 @@ int main(int argc, char **argv)
int n = yasl_wait_event(&e, smoke ? 20 : -1);
if (n < 0) break;
if (smoke && frames && n == 0) { result = 0; break; }
- bool escape = e.type == YASL_EVENT_KEY && e.key.action == YASL_KEY_PRESS && e.key.key == YASL_KEY_ESCAPE;
+ bool escape = e.type == YASL_EVENT_KEY && e.action == YASL_KEY_PRESS && e.key.key == YASL_KEY_ESCAPE;
if (e.type == YASL_EVENT_CLOSE || e.type == YASL_EVENT_QUIT || escape) { result = 0; break; }
bool clear = false, draw = false, erase = false, down = false, present = e.type == YASL_EVENT_REFRESH;
double x = 0, y = 0, p = 0.5;
switch (e.type) {
case YASL_EVENT_KEY:
- if (e.key.action == YASL_KEY_PRESS && e.key.key == YASL_KEY_C) clear = present = true;
+ if (e.action == YASL_KEY_PRESS && e.key.key == YASL_KEY_C) clear = present = true;
break;
case YASL_EVENT_PEN:
- x = e.pen.x; y = e.pen.y;
- p = (e.pen.axes_valid & YASL_PEN_PRESSURE) ? e.pen.pressure : 0.5;
+ x = e.x; y = e.y;
+ p = (e.pen.axes & YASL_PEN_PRESSURE) ? e.pen.pressure : 0.5;
erase = e.pen.tool == YASL_PEN_ERASER;
down = e.pen.in_contact;
- draw = down && (e.pen.action == YASL_PEN_DOWN || e.pen.action == YASL_PEN_MOVE);
- if (e.pen.action == YASL_PEN_LEAVE || e.pen.action == YASL_PEN_CANCEL) contact = false;
+ draw = down && (e.action == YASL_PEN_DOWN || e.action == YASL_PEN_MOVE);
+ if (e.action == YASL_PEN_LEAVE || e.action == YASL_PEN_CANCEL) contact = false;
break;
case YASL_EVENT_POINTER:
- x = e.pointer.x; y = e.pointer.y;
+ x = e.x; y = e.y;
down = (e.pointer.buttons & YASL_BUTTON_BIT(YASL_BUTTON_PRIMARY)) != 0;
- draw = down && (e.pointer.action == YASL_POINTER_DOWN || e.pointer.action == YASL_POINTER_MOVE);
+ draw = down && (e.action == YASL_POINTER_DOWN || e.action == YASL_POINTER_MOVE);
break;
case YASL_EVENT_INPUT_RESET:
contact = false;
@@ -88,12 +86,11 @@ int main(int argc, char **argv)
if (draw || present) {
int w, h;
int old_w = painted_w, old_h = painted_h;
- struct yasl_cpu_buffer buffer;
- uint32_t *pixels = canvas(s, &w, &h, &painted_w, &painted_h, clear, &buffer);
+ uint32_t *pixels = canvas(s, &w, &h, &painted_w, &painted_h, clear);
if (!pixels) break;
if (w != old_w || h != old_h) contact = false;
if (draw) {
- double scale = yasl_surface_get_scale(s);
+ double scale = yasl_surface_get_info(s).scale;
x *= scale; y *= scale;
double radius = (1.5 + 12 * p) * scale, dx = contact ? x - last_x : 0, dy = contact ? y - last_y : 0;
int steps = (int)ceil(hypot(dx, dy) / (radius * 0.4));
@@ -101,7 +98,7 @@ int main(int argc, char **argv)
for (int i = 1; i <= steps; i++) stamp(pixels, w, h, x - dx + dx * i / steps, y - dy + dy * i / steps, radius, erase);
last_x = x; last_y = y;
}
- if ((yasl_cpu_present(s, &buffer) < 0)) break;
+ if (yasl_cpu_present(s) < 0) break;
frames++;
}
if (e.type == YASL_EVENT_PEN || e.type == YASL_EVENT_POINTER) contact = down;