summaryrefslogtreecommitdiff
path: root/examples/hello.c
diff options
context:
space:
mode:
authorBerke Güzel <wenekar1@gmail.com>2026-10-13 23:41:31 +0300
committerBerke Güzel <wenekar1@gmail.com>2026-10-13 23:41:31 +0300
commit76073fe5fad8799e5850805f2f090af2cb62c741 (patch)
tree9658019babe4760a3634bdab4da0731a10e42e68 /examples/hello.c
parentdd1775a9927922c14f8679ccbc4d3ca8d5917e20 (diff)
Simplify the public event and CPU API
Applications now need far less ceremony: hello.c is a 20-line loop that handles REFRESH, CLOSE and QUIT and draws into the surface's own pixels. - Fold GEOMETRY, STATE and FOCUS into one WINDOW event carrying size, scale and state; unchanged values send nothing. - Guarantee REFRESH once a window has a size and after every resize, never at size zero. - Merge DROP_HOVER into DROP with over/leave/data actions and give drag and drop actions enums. - Keep event strings valid until the next wait and drop yasl_event_release; empty or failed waits report NONE. - Replace copy-in presentation with a lent pixel buffer: yasl_cpu_pixels and yasl_cpu_present. - Remove yasl_poll_event, yasl_cpu_read_pixels, unused event fields and comments that restated names. - Wayland: resize floating windows in request_size instead of reporting it unsupported, and stop marking new GPU surfaces as Vulkan so yasl_vulkan_create_surface no longer fails with NATIVE_WINDOW_IN_USE. Also includes the in-progress public API refactor that was uncommitted in the tree (removal of the app/loop layer, new surface, GL and Vulkan entry points). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'examples/hello.c')
-rw-r--r--examples/hello.c54
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;
}