summaryrefslogtreecommitdiff
path: root/examples/hello.c
blob: 31ab18b8cd5e983ba5fbc2cb4b61a3df475d5e7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* A complete CPU-rendered window. Build instructions: docs/getting-started.md. */
#include <yasl/app.h>
#include <stdio.h>

static bool failed;
static void draw(void *ud, struct yasl_surface *surface, struct yasl_frame *frame)
{
    (void)ud;
    int w = frame->target.size.x, h = frame->target.size.y;
    for (int y = 0; y < h; y++) {
        uint32_t *row = frame->target.pixels + (size_t)y * (frame->target.stride_bytes / 4);
        for (int x = 0; x < w; x++) {
            /* Red grows rightward, green downward, blue fades; opaque premultiplied ARGB. */
            uint32_t r = (uint32_t)(x * 255 / (w > 1 ? w - 1 : 1));
            uint32_t g = (uint32_t)(y * 255 / (h > 1 ? h - 1 : 1));
            row[x] = 0xff000000 | r << 16 | g << 8 | (255 - r);
        }
    }

    if (!yasl_frame_present(frame, NULL, 0)) {
        fprintf(stderr, "Could not present the draw target\n");
        failed = true;
    }
    if (failed) yasl_app_exit(yasl_surface_display(surface), YASL_APP_ERROR);
}

int main(void)
{
    struct yasl_window_spec window = {.title = "Hello YASL", .size_logical = {640, 480}};
    struct yasl_app_desc desc = {
        .app_id = "org.example.hello",
        .window = &window,
        .frame = draw,
    };
    int result = yasl_run(&desc);
    if (result) fprintf(stderr, "Could not run the application\n");
    return result || failed ? 1 : 0;
}