summaryrefslogtreecommitdiff
path: root/examples/hello.c
blob: 4153394f3ee1c6affbadf3cda4871408b0e421dc (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* A complete CPU-rendered window. Build instructions: docs/getting-started.md. */
#include <yasl/app.h>

#include <stdio.h>

struct app {
    bool failed;
};

static int init(void *ud, struct yasl_display *display)
{
    struct yasl_window_spec spec = {.title = "Hello YASL", .size = {640, 480}};
    struct yasl_surface *surface = yasl_surface_create_toplevel(display, &spec);
    if (!surface) {
        perror("yasl_surface_create_toplevel");
        ((struct app *)ud)->failed = true;
        return YASL_APP_QUIT;
    }
    yasl_surface_request_frame(surface);
    return YASL_APP_CONTINUE;
}

static int event(void *ud, const struct yasl_event *event)
{
    (void)ud;
    return event->type == YASL_EVENT_WINDOW_CLOSE_REQUEST ? YASL_APP_QUIT : YASL_APP_CONTINUE;
}

static void draw(void *ud, struct yasl_surface *surface, struct yasl_frame_context *frame)
{
    struct app *app = ud;
    size_t count = (size_t)frame->size.x * frame->size.y;
    for (size_t i = 0; i < count; i++)
        frame->cpu.pixels[i] = 0xff306090; /* Opaque premultiplied ARGB. */

    if (!yasl_frame_present_cpu(frame, NULL, 0)) {
        fprintf(stderr, "Could not present the draw target\n");
        app->failed = true;
    }
    if (app->failed) yasl_surface_destroy(surface);
}

int main(void)
{
    struct app app = {0};
    struct yasl_app_desc desc = {
        .app_id = "org.example.hello",
        .userdata = &app,
        .init = init,
        .event = event,
        .frame = draw,
    };
    int result = yasl_run(&desc);
    if (result) fprintf(stderr, "Could not run the application\n");
    return result || app.failed ? 1 : 0;
}