diff options
Diffstat (limited to 'examples/hello.c')
| -rw-r--r-- | examples/hello.c | 54 |
1 files changed, 21 insertions, 33 deletions
diff --git a/examples/hello.c b/examples/hello.c index 31ab18b..9e324a6 100644 --- a/examples/hello.c +++ b/examples/hello.c @@ -1,38 +1,26 @@ -/* A complete CPU-rendered window. Build instructions: docs/getting-started.md. */ -#include <yasl/app.h> -#include <stdio.h> +/* A CPU-rendered window. Build instructions: docs/getting-started.md. */ +#include <yasl/yasl.h> +#include <yasl/cpu.h> -static bool failed; -static void draw(void *ud, struct yasl_surface *surface, struct yasl_frame *frame) +int main(void) { - (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); + yasl_surface *surface = yasl_surface_create("Hello YASL", 640, 480, YASL_SURFACE_CPU); + yasl_event event; + while (surface && yasl_wait_event(&event, -1) >= 0 && + event.type != YASL_EVENT_CLOSE && event.type != YASL_EVENT_QUIT) { + if (event.type != YASL_EVENT_REFRESH) continue; + int w, h; + uint32_t *pixels = yasl_cpu_pixels(surface, &w, &h); + for (int y = 0; pixels && y < h; y++) { + 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)); + pixels[(size_t)y * w + x] = 0xff000000 | r << 16 | g << 8 | (255 - r); + } } + yasl_cpu_present(surface); } - - 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; + yasl_shutdown(); + return surface ? 0 : 1; } |
