summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorBerke Güzel <wenekar1@gmail.com>2026-10-14 13:13:52 +0300
committerBerke Güzel <wenekar1@gmail.com>2026-10-14 13:13:52 +0300
commit1ab3e5a123109704789c2768e11a4f9af515821d (patch)
tree31ee03e58093a58100c6eb58681d5d29648a5ddd /examples
parentd86fa6215d40caa7852dbbd6a8cf4aa8e89dd879 (diff)
Redefine public and rendering APIs with explicit contracts
Diffstat (limited to 'examples')
-rw-r--r--examples/game.c12
-rw-r--r--examples/hello.c56
-rw-r--r--examples/notepad.c14
-rw-r--r--examples/simple_window.c10
-rw-r--r--examples/tablet.c18
5 files changed, 70 insertions, 40 deletions
diff --git a/examples/game.c b/examples/game.c
index 0be4461..850b39b 100644
--- a/examples/game.c
+++ b/examples/game.c
@@ -12,10 +12,10 @@ typedef void (*viewport_fn)(int, int, int, int);
int main(int argc, char **argv)
{
bool smoke = argc == 2 && !strcmp(argv[1], "--smoke-test");
- yasl_surface *s = yasl_gl_surface_create("YASL OpenGL", 800, 600, 0, NULL, NULL);
- if (!s || !yasl_gl_make_current(s)) {
+ yasl_surface *s = yasl_gl_surface_create(&(struct yasl_surface_options){.title="YASL OpenGL", .width=800, .height=600, .type=YASL_SURFACE_NATIVE, .flags=0}, NULL);
+ if (!s || (yasl_gl_make_current(s) < 0)) {
const char *error;
- yasl_get_error(&error);
+ error = strerror(errno);
fprintf(stderr, "%s\n", error);
yasl_shutdown();
return 1;
@@ -27,7 +27,7 @@ int main(int argc, char **argv)
yasl_shutdown();
return 1;
}
- yasl_gl_swap_interval(s, 1);
+ yasl_gl_set_swap_interval(s, 1);
int result = 0, frames = 0;
bool online = true;
for (bool running = true; running;) {
@@ -40,7 +40,7 @@ int main(int argc, char **argv)
if (e.type == YASL_EVENT_DISPLAY_LOST) online = false;
if (e.type == YASL_EVENT_DISPLAY_RESTORED) {
/* The context is new: bind it again and restore GL state here. */
- online = yasl_gl_make_current(s) && yasl_gl_swap_interval(s, 1);
+ online = (yasl_gl_make_current(s) == 0) && (yasl_gl_set_swap_interval(s, 1) == 0);
}
if (n > 0 || !online || !running) continue;
int w, h;
@@ -49,7 +49,7 @@ int main(int argc, char **argv)
double t = yasl_time_ns() / 1e9;
clear_color((float)(0.3 + 0.2 * sin(t)), 0.2f, 0.4f, 1);
clear(0x00004000u);
- if (!yasl_gl_swap_buffers(s)) { result = 1; break; }
+ if ((yasl_gl_present(s) < 0)) { result = 1; break; }
if (smoke && ++frames == 3) break;
}
yasl_gl_make_current(NULL);
diff --git a/examples/hello.c b/examples/hello.c
index 9e324a6..054051d 100644
--- a/examples/hello.c
+++ b/examples/hello.c
@@ -1,26 +1,50 @@
-/* A CPU-rendered window. Build instructions: docs/getting-started.md. */
+/* A CPU-rendered window. */
#include <yasl/yasl.h>
#include <yasl/cpu.h>
int main(void)
{
- 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);
+ struct yasl_surface_options options = {
+ .title = "Hello YASL",
+ .width = 640,
+ .height = 480,
+ .type = YASL_SURFACE_CPU,
+ };
+ yasl_surface *surface = yasl_surface_create(&options);
+ if (!surface)
+ return 1;
+
+ int result = 0;
+ for (;;) {
+ yasl_event event;
+ if (yasl_wait_event(&event, -1) < 0) {
+ result = 1;
+ break;
+ }
+ if (event.type == YASL_EVENT_CLOSE || event.type == YASL_EVENT_QUIT)
+ break;
+ if (event.type != YASL_EVENT_REFRESH)
+ continue;
+
+ struct yasl_cpu_buffer buffer;
+ if (yasl_cpu_acquire(surface, &buffer) < 0) {
+ result = 1;
+ break;
+ }
+ for (int y = 0; y < buffer.height; y++) {
+ uint32_t *row = (uint32_t *)((unsigned char *)buffer.pixels + y * buffer.stride_bytes);
+ for (int x = 0; x < buffer.width; x++) {
+ /* Opaque premultiplied ARGB: red grows rightward, green downward. */
+ uint32_t r = (uint32_t)(x * 255 / (buffer.width > 1 ? buffer.width - 1 : 1));
+ uint32_t g = (uint32_t)(y * 255 / (buffer.height > 1 ? buffer.height - 1 : 1));
+ row[x] = 0xff000000 | r << 16 | g << 8 | (255 - r);
}
}
- yasl_cpu_present(surface);
+ if (yasl_cpu_present(surface, &buffer) < 0) {
+ result = 1;
+ break;
+ }
}
yasl_shutdown();
- return surface ? 0 : 1;
+ return result;
}
diff --git a/examples/notepad.c b/examples/notepad.c
index 9134653..ff12038 100644
--- a/examples/notepad.c
+++ b/examples/notepad.c
@@ -576,7 +576,7 @@ static void editor_copy(struct editor *ed)
snprintf(buf, (size_t)MAX_LINES * MAX_LINE_LEN, "%s\n", ed->lines[ed->cursor_row]);
}
- if (!yasl_clipboard_set(YASL_CLIPBOARD, buf))
+ if ((yasl_clipboard_set(YASL_CLIPBOARD, buf) < 0))
editor_set_status(ed, "Clipboard copy failed");
free(buf);
editor_set_status(ed, "Copied to clipboard");
@@ -988,7 +988,7 @@ static void prompt_execute(struct editor *ed)
static bool editor_init(struct editor *ed)
{
- ed->surface = yasl_surface_create("Untitled - YASL Notepad", 960, 640, YASL_SURFACE_CPU);
+ ed->surface = yasl_surface_create(&(struct yasl_surface_options){.title="Untitled - YASL Notepad", .width=960, .height=640, .type=YASL_SURFACE_CPU, .flags=0});
if (!ed->surface) return false;
ed->surface_scale = yasl_surface_get_scale(ed->surface);
yasl_surface_set_text_input(ed->surface, true, YASL_TEXT_NORMAL);
@@ -1483,8 +1483,10 @@ static void on_event(struct editor *ed, const yasl_event *ev)
static void editor_render(struct editor *ed)
{
- int w, h;
- uint32_t *pix = yasl_cpu_pixels(ed->surface, &w, &h);
+ struct yasl_cpu_buffer buffer;
+ int acquired = yasl_cpu_acquire(ed->surface, &buffer);
+ int w = buffer.width, h = buffer.height;
+ uint32_t *pix = acquired == 0 ? buffer.pixels : NULL;
if (!pix) return;
double display_scale = yasl_surface_get_scale(ed->surface);
@@ -1624,7 +1626,7 @@ static void editor_render(struct editor *ed)
ed->is_modified ? 0xfffab387 : status_accent, scale);
}
- if (!yasl_cpu_present(ed->surface)) {
+ if ((yasl_cpu_present(ed->surface, &buffer) < 0)) {
fprintf(stderr, "Could not present CPU image\n");
ed->failed = true;
ed->running = false;
@@ -1697,7 +1699,7 @@ int main(int argc, char **argv)
}
}
- if (!yasl_set_app_id("org.yasl.notepad") || !editor_init(&ed)) {
+ if ((yasl_init(&(struct yasl_options){.app_id="org.yasl.notepad"}) < 0) || !editor_init(&ed)) {
fprintf(stderr, "Could not initialize YASL Notepad\n");
editor_shutdown(&ed);
yasl_shutdown();
diff --git a/examples/simple_window.c b/examples/simple_window.c
index cb12db1..4b4b552 100644
--- a/examples/simple_window.c
+++ b/examples/simple_window.c
@@ -6,7 +6,7 @@
int main(int argc, char **argv)
{
bool smoke = argc == 2 && !strcmp(argv[1], "--smoke-test");
- yasl_surface *s = yasl_surface_create("YASL input", 640, 480, YASL_SURFACE_CPU);
+ yasl_surface *s = yasl_surface_create(&(struct yasl_surface_options){.title="YASL input", .width=640, .height=480, .type=YASL_SURFACE_CPU, .flags=0});
if (!s) return 1;
double px = -100, py = -100, pressure = 0;
bool dirty = false;
@@ -29,8 +29,10 @@ int main(int argc, char **argv)
}
if (e.type == YASL_EVENT_REFRESH) dirty = true;
if (dirty) {
- int w, h;
- uint32_t *pixels = yasl_cpu_pixels(s, &w, &h);
+ struct yasl_cpu_buffer buffer;
+ int acquired = yasl_cpu_acquire(s, &buffer);
+ int w = buffer.width, h = buffer.height;
+ uint32_t *pixels = acquired == 0 ? buffer.pixels : NULL;
if (!pixels) break;
double scale = yasl_surface_get_scale(s), radius = (8 + pressure * 48) * scale;
for (int y = 0; y < h; y++) {
@@ -39,7 +41,7 @@ int main(int argc, char **argv)
pixels[(size_t)y * w + x] = dx * dx + dy * dy < radius * radius ? 0xffffc060u : 0xff203040u;
}
}
- if (!yasl_cpu_present(s)) break;
+ if ((yasl_cpu_present(s, &buffer) < 0)) break;
dirty = false;
frames++;
}
diff --git a/examples/tablet.c b/examples/tablet.c
index 176a82b..ce796a4 100644
--- a/examples/tablet.c
+++ b/examples/tablet.c
@@ -24,10 +24,11 @@ static void stamp(uint32_t *pixels, int w, int h, double x, double y, double rad
}
/* Paints paper over the area a resize added, or everything when clear is set. */
-static uint32_t *canvas(yasl_surface *s, int *w, int *h, int *painted_w, int *painted_h, bool clear)
+static uint32_t *canvas(yasl_surface *s, int *w, int *h, int *painted_w, int *painted_h, bool clear, struct yasl_cpu_buffer *buffer)
{
- uint32_t *pixels = yasl_cpu_pixels(s, w, h);
- if (!pixels) return NULL;
+ if (yasl_cpu_acquire(s, buffer) < 0) return NULL;
+ *w = buffer->width; *h = buffer->height;
+ uint32_t *pixels = buffer->pixels;
if (clear) *painted_w = *painted_h = 0;
for (int y = 0; y < *h; y++)
for (int x = y < *painted_h ? *painted_w : 0; x < *w; x++) pixels[(size_t)y * *w + x] = PAPER;
@@ -43,11 +44,11 @@ int main(int argc, char **argv)
if (!strcmp(argv[i], "--smoke-test")) smoke = true;
else if (!strcmp(argv[i], "--backend") && i + 1 < argc) {
const char *b = argv[++i];
- if (!yasl_set_backend(!strcmp(b, "wayland") ? YASL_BACKEND_WAYLAND :
- !strcmp(b, "x11") ? YASL_BACKEND_X11 : YASL_BACKEND_COCOA)) return 1;
+ if (!(yasl_init(&(struct yasl_options){.backend=!strcmp(b, "wayland") ? YASL_BACKEND_WAYLAND :
+ !strcmp(b, "x11") ? YASL_BACKEND_X11 : YASL_BACKEND_COCOA}) == 0)) return 1;
} else return 1;
}
- yasl_surface *s = yasl_surface_create("YASL pen canvas — C clears, Escape exits", 800, 600, YASL_SURFACE_CPU);
+ yasl_surface *s = yasl_surface_create(&(struct yasl_surface_options){.title="YASL pen canvas — C clears, Escape exits", .width=800, .height=600, .type=YASL_SURFACE_CPU, .flags=0});
if (!s) return 1;
int painted_w = 0, painted_h = 0, frames = 0, result = 1;
bool contact = false;
@@ -87,7 +88,8 @@ int main(int argc, char **argv)
if (draw || present) {
int w, h;
int old_w = painted_w, old_h = painted_h;
- uint32_t *pixels = canvas(s, &w, &h, &painted_w, &painted_h, clear);
+ struct yasl_cpu_buffer buffer;
+ uint32_t *pixels = canvas(s, &w, &h, &painted_w, &painted_h, clear, &buffer);
if (!pixels) break;
if (w != old_w || h != old_h) contact = false;
if (draw) {
@@ -99,7 +101,7 @@ int main(int argc, char **argv)
for (int i = 1; i <= steps; i++) stamp(pixels, w, h, x - dx + dx * i / steps, y - dy + dy * i / steps, radius, erase);
last_x = x; last_y = y;
}
- if (!yasl_cpu_present(s)) break;
+ if ((yasl_cpu_present(s, &buffer) < 0)) break;
frames++;
}
if (e.type == YASL_EVENT_PEN || e.type == YASL_EVENT_POINTER) contact = down;