summaryrefslogtreecommitdiff
path: root/examples/simple_window.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/simple_window.c')
-rw-r--r--examples/simple_window.c117
1 files changed, 94 insertions, 23 deletions
diff --git a/examples/simple_window.c b/examples/simple_window.c
index 41a5375..a8a2861 100644
--- a/examples/simple_window.c
+++ b/examples/simple_window.c
@@ -1,8 +1,13 @@
#include "yasl/yasl.h"
#include <stdio.h>
#include <stdlib.h>
+#include <stdbool.h>
static bool running = true;
+static int mouse_x = -1;
+static int mouse_y = -1;
+static bool mouse_down = false;
+static int color_theme = 0;
static void on_draw(struct yasl_surface *s, struct yasl_frame *frame, void *ud)
{
@@ -10,25 +15,67 @@ static void on_draw(struct yasl_surface *s, struct yasl_frame *frame, void *ud)
int w = frame->target.size.x;
int h = frame->target.size.y;
uint32_t *pixels = frame->target.pixels;
- if (pixels) {
- for (int y = 0; y < h; y++) {
- for (int x = 0; x < w; x++) {
- uint8_t r = (uint8_t)((x * 255) / (w > 0 ? w : 1));
- uint8_t g = (uint8_t)((y * 255) / (h > 0 ? h : 1));
- uint8_t b = 128;
- pixels[y * w + x] = (255u << 24) | (r << 16) | (g << 8) | b;
+ if (!pixels || w <= 0 || h <= 0)
+ return;
+
+ for (int y = 0; y < h; y++) {
+ for (int x = 0; x < w; x++) {
+ uint8_t r, g, b;
+ uint8_t u = (uint8_t)((x * 255) / (w > 0 ? w : 1));
+ uint8_t v = (uint8_t)((y * 255) / (h > 0 ? h : 1));
+
+ switch (color_theme % 3) {
+ case 0:
+ r = u;
+ g = v;
+ b = 160;
+ break;
+ case 1:
+ r = 40;
+ g = u;
+ b = v;
+ break;
+ default:
+ r = v;
+ g = 60;
+ b = u;
+ break;
}
+
+ /* Draw interactive pointer indicator */
+ if (mouse_x >= 0 && mouse_y >= 0) {
+ int dx = x - mouse_x;
+ int dy = y - mouse_y;
+ int dist_sq = dx * dx + dy * dy;
+ int radius = mouse_down ? 36 : 20;
+ if (dist_sq < radius * radius) {
+ r = (uint8_t)(255 - r / 2);
+ g = (uint8_t)(255 - g / 2);
+ b = (uint8_t)(255 - b / 2);
+ }
+ }
+
+ pixels[y * w + x] = (255u << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
}
}
+
struct yasl_damage_list dmg = { .n = -1 };
yasl_surface_present(s, &frame->target, &dmg);
}
+static void on_configure(struct yasl_surface *s, struct yasl_geometry geo, void *ud)
+{
+ (void)ud;
+ printf("[YASL] Resized to %dx%d (scale: %.2fx)\n",
+ geo.size.x, geo.size.y, (double)geo.scale_120 / YASL_SCALE_UNIT);
+ yasl_surface_schedule_redraw(s);
+}
+
static void on_close(struct yasl_surface *s, void *ud)
{
(void)s;
(void)ud;
- printf("Window close requested\n");
+ printf("[YASL] Window close requested\n");
running = false;
}
@@ -40,58 +87,82 @@ static void on_quit(void *ud)
int main(void)
{
- struct yasl_loop *loop = yasl_loop_create();
- if (!loop) {
- fprintf(stderr, "Failed to create event loop\n");
- return 1;
- }
-
+ /* YASL manages its event loop internally when NULL is passed */
struct yasl_display_cbs disp_cbs = { .quit = on_quit };
struct yasl_display_spec disp_spec = {
.app_id = "org.yasl.example",
.cbs = &disp_cbs,
};
- struct yasl_display *d = yasl_display_open(loop, &disp_spec);
+ struct yasl_display *d = yasl_display_open(NULL, &disp_spec);
if (!d) {
fprintf(stderr, "Failed to open display\n");
- yasl_loop_destroy(loop);
return 1;
}
struct yasl_surface_spec surf_spec = {
- .title = "YASL Simple Window",
+ .title = "YASL Interactive Window",
.size = { .x = 640, .y = 480 },
};
struct yasl_surface *s = yasl_surface_create_toplevel(d, &surf_spec);
if (!s) {
fprintf(stderr, "Failed to create surface\n");
yasl_display_close(d);
- yasl_loop_destroy(loop);
return 1;
}
struct yasl_surface_cbs surf_cbs = {
.draw = on_draw,
+ .configure = on_configure,
.close = on_close,
};
yasl_surface_set_callbacks(s, &surf_cbs, NULL);
yasl_surface_schedule_redraw(s);
+ printf("=== YASL Demo Running ===\n");
+ printf(" - Move or Click mouse inside window to interact\n");
+ printf(" - Press Space to cycle background themes\n");
+ printf(" - Press Escape or close window to exit\n\n");
+
while (running) {
yasl_display_dispatch(d);
+
struct yasl_input_event ev;
while (yasl_surface_input_poll(s, &ev)) {
- if (ev.kind == YASL_INPUT_KEY && ev.key.action == YASL_KEY_PRESS) {
- printf("Key pressed: keysym=%u\n", ev.key.key);
- if (ev.key.key == 0xff1b) /* Escape */
- running = false;
+ if (ev.kind == YASL_INPUT_KEY) {
+ if (ev.key.action == YASL_KEY_PRESS) {
+ if (ev.key.key == 0xff1b) { /* Escape */
+ running = false;
+ } else if (ev.key.key == ' ' || ev.key.key == 0xff0d) { /* Space or Return */
+ color_theme++;
+ yasl_surface_schedule_redraw(s);
+ }
+ }
+ } else if (ev.kind == YASL_INPUT_MOUSE) {
+ if (ev.mouse.kind == YASL_PTR_MOTION) {
+ mouse_x = ev.mouse.pos.x;
+ mouse_y = ev.mouse.pos.y;
+ yasl_surface_schedule_redraw(s);
+ } else if (ev.mouse.kind == YASL_PTR_PRESS) {
+ mouse_down = true;
+ mouse_x = ev.mouse.pos.x;
+ mouse_y = ev.mouse.pos.y;
+ yasl_surface_schedule_redraw(s);
+ } else if (ev.mouse.kind == YASL_PTR_RELEASE) {
+ mouse_down = false;
+ yasl_surface_schedule_redraw(s);
+ } else if (ev.mouse.kind == YASL_PTR_LEAVE) {
+ mouse_x = -1;
+ mouse_y = -1;
+ mouse_down = false;
+ yasl_surface_schedule_redraw(s);
+ }
}
}
+
yasl_display_flush(d);
}
yasl_surface_destroy(s);
yasl_display_close(d);
- yasl_loop_destroy(loop);
return 0;
}