summaryrefslogtreecommitdiff
path: root/examples/hello.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/hello.c')
-rw-r--r--examples/hello.c104
1 files changed, 28 insertions, 76 deletions
diff --git a/examples/hello.c b/examples/hello.c
index 0c474c2..4153394 100644
--- a/examples/hello.c
+++ b/examples/hello.c
@@ -1,104 +1,56 @@
/* A complete CPU-rendered window. Build instructions: docs/getting-started.md. */
-#include <yasl/yasl.h>
+#include <yasl/app.h>
#include <stdio.h>
-#include <string.h>
struct app {
- bool running;
bool failed;
- bool smoke_test;
};
-static void quit(void *ud)
+static int init(void *ud, struct yasl_display *display)
{
- ((struct app *)ud)->running = false;
-}
-
-static void lost(void *ud)
-{
- struct app *app = ud;
- app->failed = true;
- app->running = false;
-}
-
-static void close_window(struct yasl_surface *surface, void *ud)
-{
- (void)surface;
- quit(ud);
+ 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 void configure(struct yasl_surface *surface, struct yasl_geometry geometry, void *ud)
+static int event(void *ud, const struct yasl_event *event)
{
- (void)geometry;
(void)ud;
- yasl_surface_schedule_redraw(surface);
+ return event->type == YASL_EVENT_WINDOW_CLOSE_REQUEST ? YASL_APP_QUIT : YASL_APP_CONTINUE;
}
-static void draw(struct yasl_surface *surface, struct yasl_frame *frame, void *ud)
+static void draw(void *ud, struct yasl_surface *surface, struct yasl_frame_context *frame)
{
struct app *app = ud;
- size_t count = (size_t)frame->target.size.x * frame->target.size.y;
+ size_t count = (size_t)frame->size.x * frame->size.y;
for (size_t i = 0; i < count; i++)
- frame->target.pixels[i] = 0xff306090; /* Opaque premultiplied ARGB. */
+ frame->cpu.pixels[i] = 0xff306090; /* Opaque premultiplied ARGB. */
- struct yasl_damage_list damage = {.n = -1};
- if (!yasl_surface_present(surface, &frame->target, &damage)) {
+ if (!yasl_frame_present_cpu(frame, NULL, 0)) {
fprintf(stderr, "Could not present the draw target\n");
app->failed = true;
- app->running = false;
}
- if (app->smoke_test) app->running = false;
+ if (app->failed) yasl_surface_destroy(surface);
}
-int main(int argc, char **argv)
+int main(void)
{
- struct app app = {.running = true};
- if (argc == 2 && strcmp(argv[1], "--smoke-test") == 0)
- app.smoke_test = true;
- else if (argc != 1) {
- fprintf(stderr, "Usage: %s [--smoke-test]\n", argv[0]);
- return 1;
- }
-
- struct yasl_display_cbs display_callbacks = {.quit = quit, .lost = lost};
- struct yasl_display_spec display_spec = {
+ struct app app = {0};
+ struct yasl_app_desc desc = {
.app_id = "org.example.hello",
- .cbs = &display_callbacks,
- .ud = &app,
- };
- struct yasl_display *display = yasl_display_open(NULL, &display_spec);
- if (!display) {
- perror("yasl_display_open");
- return 1;
- }
- struct yasl_window_cbs callbacks = {
- .draw = draw, .configure = configure, .close = close_window,
- };
- struct yasl_window_spec spec = {
- .title = "Hello YASL", .size = {640, 480},
- .cbs = &callbacks, .ud = &app,
+ .userdata = &app,
+ .init = init,
+ .event = event,
+ .frame = draw,
};
- struct yasl_surface *surface = yasl_surface_create_toplevel(display, &spec);
- if (!surface) {
- perror("yasl_surface_create_toplevel");
- yasl_display_close(display);
- return 1;
- }
- yasl_surface_schedule_redraw(surface);
-
- while (app.running) {
- if (yasl_display_dispatch(display, -1) < 0) {
- perror("yasl_display_dispatch");
- app.failed = true;
- break;
- }
- /* Drain even unused input so queued events do not cause a busy loop. */
- struct yasl_input_event event;
- while (yasl_surface_input_poll(surface, &event)) {}
- if (app.running) yasl_display_flush(display);
- }
- yasl_surface_destroy(surface);
- yasl_display_close(display);
- return app.failed ? 1 : 0;
+ int result = yasl_run(&desc);
+ if (result) fprintf(stderr, "Could not run the application\n");
+ return result || app.failed ? 1 : 0;
}