diff options
Diffstat (limited to 'examples/simple_window.c')
| -rw-r--r-- | examples/simple_window.c | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/examples/simple_window.c b/examples/simple_window.c new file mode 100644 index 0000000..41a5375 --- /dev/null +++ b/examples/simple_window.c @@ -0,0 +1,97 @@ +#include "yasl/yasl.h" +#include <stdio.h> +#include <stdlib.h> + +static bool running = true; + +static void on_draw(struct yasl_surface *s, struct yasl_frame *frame, void *ud) +{ + (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; + } + } + } + struct yasl_damage_list dmg = { .n = -1 }; + yasl_surface_present(s, &frame->target, &dmg); +} + +static void on_close(struct yasl_surface *s, void *ud) +{ + (void)s; + (void)ud; + printf("Window close requested\n"); + running = false; +} + +static void on_quit(void *ud) +{ + (void)ud; + running = false; +} + +int main(void) +{ + struct yasl_loop *loop = yasl_loop_create(); + if (!loop) { + fprintf(stderr, "Failed to create event loop\n"); + return 1; + } + + 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); + 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", + .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, + .close = on_close, + }; + yasl_surface_set_callbacks(s, &surf_cbs, NULL); + yasl_surface_schedule_redraw(s); + + 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; + } + } + yasl_display_flush(d); + } + + yasl_surface_destroy(s); + yasl_display_close(d); + yasl_loop_destroy(loop); + return 0; +} |
