summaryrefslogtreecommitdiff
path: root/examples/ui.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/ui.c')
-rwxr-xr-xexamples/ui.c385
1 files changed, 192 insertions, 193 deletions
diff --git a/examples/ui.c b/examples/ui.c
index 4730a9d..dc54cc0 100755
--- a/examples/ui.c
+++ b/examples/ui.c
@@ -27,164 +27,192 @@ exit $? # */
#include "../graphics.c"
-i32 main(i32 argc, c8 **argv) {
- (void) argc;
- (void) argv;
-
- platform = (Platform) {
- .title = "UI",
- .frame_width = 960,
- .frame_height = 720,
- };
-
- p_init();
-
- b8 button_0_down = 0;
- b8 button_1_down = 0;
- b8 button_1_checked = 0;
-
- i64 text_len = 0;
- static c32 text[512] = {0};
-
- i64 cursor = 0;
- i64 selection = 0;
-
- while (!platform.done) {
- p_wait_events();
-
- for (i32 j = 0; j < platform.frame_height; ++j)
- for (i32 i = 0; i < platform.frame_width; ++i)
- platform.pixels[j * platform.frame_width + i] = 0x302000;
-
- if (platform.cursor_x >= 40 && platform.cursor_x < 100 && platform.cursor_y >= 40 && platform.cursor_y < 100) {
- button_0_down = platform.key_down[BUTTON_LEFT];
- if (button_0_down)
- fill_rectangle(OP_SET, 0xffffff, 40, 40, 60, 60);
- else
- fill_rectangle(OP_SET, 0x00ff00, 40, 40, 60, 60);
- } else {
- button_0_down = 0;
- fill_rectangle(OP_SET, 0x208020, 40, 40, 60, 60);
- }
-
- if (platform.cursor_x >= 40 && platform.cursor_x < 100 && platform.cursor_y >= 120 && platform.cursor_y < 180) {
- button_1_down = platform.key_down[BUTTON_LEFT];
- if (platform.key_pressed[BUTTON_LEFT])
- button_1_checked = !button_1_checked;
- if (button_1_down)
- fill_rectangle(OP_SET, 0xffffff, 40, 120, 60, 60);
- else if (button_1_checked)
- fill_rectangle(OP_SET, 0xff8080, 40, 120, 60, 60);
- else
- fill_rectangle(OP_SET, 0x80ff80, 40, 120, 60, 60);
- } else {
- button_1_down = 0;
- if (button_1_checked)
- fill_rectangle(OP_SET, 0xff0000, 40, 120, 60, 60);
- else
- fill_rectangle(OP_SET, 0x00ff00, 40, 120, 60, 60);
- }
-
- i64 w = platform.frame_width / 2;
- i64 h = platform.frame_height / 2;
- i64 x0 = w / 2;
- i64 y0 = h / 2;
-
- i64 color = 0xff7f7f;
-
- if (platform.cursor_x >= x0 && platform.cursor_x < x0 + w &&
- platform.cursor_y >= y0 && platform.cursor_y < y0 + h)
- color = 0xffffff;
-
- for (i64 i = 0; i < platform.input_size; ++i)
- if (platform.input[i].ctrl)
- switch (platform.input[i].key) {
- case 'v': {
- if (selection != 0) {
- i64 i0 = selection < 0 ? cursor + selection : cursor;
- i64 i1 = selection < 0 ? cursor : cursor + selection;
- for (i64 i = 0; i1 + i < text_len; ++i)
- text[i0 + i] = text[i1 + i];
- selection = 0;
- cursor = i0;
- text_len -= i1 - i0;
- }
+b8 button_0_down = 0;
+b8 button_1_down = 0;
+b8 button_1_checked = 0;
+
+i64 text_len = 0;
+static c32 text[512] = {0};
+
+i64 cursor = 0;
+i64 selection = 0;
+
+void update_and_render_frame(void) {
+ p_wait_events();
+
+ for (i32 j = 0; j < platform.frame_height; ++j)
+ for (i32 i = 0; i < platform.frame_width; ++i)
+ platform.pixels[j * platform.frame_width + i] = 0x302000;
+
+ if (platform.cursor_x >= 40 && platform.cursor_x < 100 && platform.cursor_y >= 40 && platform.cursor_y < 100) {
+ button_0_down = platform.key_down[BUTTON_LEFT];
+ if (button_0_down)
+ fill_rectangle(OP_SET, 0xffffff, 40, 40, 60, 60);
+ else
+ fill_rectangle(OP_SET, 0x00ff00, 40, 40, 60, 60);
+ } else {
+ button_0_down = 0;
+ fill_rectangle(OP_SET, 0x208020, 40, 40, 60, 60);
+ }
- for (i64 n = 0; n < platform.clipboard_size;) {
- c32 c = utf8_read(platform.clipboard_size - n, platform.clipboard + n);
+ if (platform.cursor_x >= 40 && platform.cursor_x < 100 && platform.cursor_y >= 120 && platform.cursor_y < 180) {
+ button_1_down = platform.key_down[BUTTON_LEFT];
+ if (platform.key_pressed[BUTTON_LEFT])
+ button_1_checked = !button_1_checked;
+ if (button_1_down)
+ fill_rectangle(OP_SET, 0xffffff, 40, 120, 60, 60);
+ else if (button_1_checked)
+ fill_rectangle(OP_SET, 0xff8080, 40, 120, 60, 60);
+ else
+ fill_rectangle(OP_SET, 0x80ff80, 40, 120, 60, 60);
+ } else {
+ button_1_down = 0;
+ if (button_1_checked)
+ fill_rectangle(OP_SET, 0xff0000, 40, 120, 60, 60);
+ else
+ fill_rectangle(OP_SET, 0x00ff00, 40, 120, 60, 60);
+ }
- if (text_len < (i64) (sizeof text / sizeof *text)) {
- for (i64 j = text_len; j > cursor; --j)
- text[j] = text[j - 1];
- text[cursor++] = c;
- ++text_len;
- }
+ i64 w = platform.frame_width / 2;
+ i64 h = platform.frame_height / 2;
+ i64 x0 = w / 2;
+ i64 y0 = h / 2;
- n += utf8_size(c);
- }
- } break;
+ i64 color = 0xff7f7f;
- case 'x': {
- i64 len = 0;
- static c8 buf[1024];
+ if (platform.cursor_x >= x0 && platform.cursor_x < x0 + w &&
+ platform.cursor_y >= y0 && platform.cursor_y < y0 + h)
+ color = 0xffffff;
+ for (i64 i = 0; i < platform.input_size; ++i)
+ if (platform.input[i].ctrl)
+ switch (platform.input[i].key) {
+ case 'v': {
+ if (selection != 0) {
i64 i0 = selection < 0 ? cursor + selection : cursor;
i64 i1 = selection < 0 ? cursor : cursor + selection;
- for (i64 i = 0; len + 4 <= (i64) sizeof buf && i < i1 - i0; ++i)
- len += utf8_write(text[i0 + i], buf + len);
-
- if (len > 0)
- p_clipboard_write(len, buf);
-
for (i64 i = 0; i1 + i < text_len; ++i)
text[i0 + i] = text[i1 + i];
selection = 0;
cursor = i0;
text_len -= i1 - i0;
- } break;
+ }
- case 'c': {
- i64 len = 0;
- static c8 buf[1024];
+ for (i64 n = 0; n < platform.clipboard_size;) {
+ c32 c = utf8_read(platform.clipboard_size - n, platform.clipboard + n);
- i64 i0 = selection < 0 ? cursor + selection : cursor;
- i64 i1 = selection < 0 ? cursor : cursor + selection;
- for (i64 i = 0; len + 4 <= (i64) sizeof buf && i < i1 - i0; ++i)
- len += utf8_write(text[i0 + i], buf + len);
-
- if (len > 0)
- p_clipboard_write(len, buf);
- } break;
-
- default:;
- }
- else
- switch (platform.input[i].key) {
- case KEY_LEFT:
- if (platform.key_down[MOD_SHIFT]) {
- if (cursor > 0)
- ++selection;
- } else if (selection != 0) {
- cursor = selection < 0 ? cursor + selection + 1 : cursor + 1;
- selection = 0;
+ if (text_len < (i64) (sizeof text / sizeof *text)) {
+ for (i64 j = text_len; j > cursor; --j)
+ text[j] = text[j - 1];
+ text[cursor++] = c;
+ ++text_len;
}
+
+ n += utf8_size(c);
+ }
+ } break;
+
+ case 'x': {
+ i64 len = 0;
+ static c8 buf[1024];
+
+ i64 i0 = selection < 0 ? cursor + selection : cursor;
+ i64 i1 = selection < 0 ? cursor : cursor + selection;
+ for (i64 i = 0; len + 4 <= (i64) sizeof buf && i < i1 - i0; ++i)
+ len += utf8_write(text[i0 + i], buf + len);
+
+ if (len > 0)
+ p_clipboard_write(len, buf);
+
+ for (i64 i = 0; i1 + i < text_len; ++i)
+ text[i0 + i] = text[i1 + i];
+ selection = 0;
+ cursor = i0;
+ text_len -= i1 - i0;
+ } break;
+
+ case 'c': {
+ i64 len = 0;
+ static c8 buf[1024];
+
+ i64 i0 = selection < 0 ? cursor + selection : cursor;
+ i64 i1 = selection < 0 ? cursor : cursor + selection;
+ for (i64 i = 0; len + 4 <= (i64) sizeof buf && i < i1 - i0; ++i)
+ len += utf8_write(text[i0 + i], buf + len);
+
+ if (len > 0)
+ p_clipboard_write(len, buf);
+ } break;
+
+ default:;
+ }
+ else
+ switch (platform.input[i].key) {
+ case KEY_LEFT:
+ if (platform.key_down[MOD_SHIFT]) {
if (cursor > 0)
- --cursor;
- break;
-
- case KEY_RIGHT:
- if (platform.key_down[MOD_SHIFT]) {
- if (cursor < text_len)
- --selection;
- } else if (selection != 0) {
- cursor = selection < 0 ? cursor - 1 : cursor + selection - 1;
- selection = 0;
- }
+ ++selection;
+ } else if (selection != 0) {
+ cursor = selection < 0 ? cursor + selection + 1 : cursor + 1;
+ selection = 0;
+ }
+ if (cursor > 0)
+ --cursor;
+ break;
+
+ case KEY_RIGHT:
+ if (platform.key_down[MOD_SHIFT]) {
if (cursor < text_len)
- ++cursor;
- break;
+ --selection;
+ } else if (selection != 0) {
+ cursor = selection < 0 ? cursor - 1 : cursor + selection - 1;
+ selection = 0;
+ }
+ if (cursor < text_len)
+ ++cursor;
+ break;
- case '\b':
+ case '\b':
+ if (selection != 0) {
+ i64 i0 = selection < 0 ? cursor + selection : cursor;
+ i64 i1 = selection < 0 ? cursor : cursor + selection;
+ for (i64 i = 0; i1 + i < text_len; ++i)
+ text[i0 + i] = text[i1 + i];
+ selection = 0;
+ cursor = i0;
+ text_len -= i1 - i0;
+ } else if (cursor > 0 && text_len > 0) {
+ for (i64 i = cursor; i < text_len; ++i)
+ text[i - 1] = text[i];
+ --cursor;
+ --text_len;
+ }
+ break;
+
+ case KEY_DELETE:
+ if (selection != 0) {
+ i64 i0 = selection < 0 ? cursor + selection : cursor;
+ i64 i1 = selection < 0 ? cursor : cursor + selection;
+ for (i64 i = 0; i1 + i < text_len; ++i)
+ text[i0 + i] = text[i1 + i];
+ selection = 0;
+ cursor = i0;
+ text_len -= i1 - i0;
+ } else if (cursor < text_len) {
+ for (i64 i = cursor + 1; i < text_len; ++i)
+ text[i - 1] = text[i];
+ --text_len;
+ }
+ break;
+
+ case '\n':
+ case '\r':
+ case '\t':
+ platform.input[i].c = platform.input[i].key;
+ // fallthrough
+
+ default:
+ if (platform.input[i].c) {
if (selection != 0) {
i64 i0 = selection < 0 ? cursor + selection : cursor;
i64 i1 = selection < 0 ? cursor : cursor + selection;
@@ -193,64 +221,35 @@ i32 main(i32 argc, c8 **argv) {
selection = 0;
cursor = i0;
text_len -= i1 - i0;
- } else if (cursor > 0 && text_len > 0) {
- for (i64 i = cursor; i < text_len; ++i)
- text[i - 1] = text[i];
- --cursor;
- --text_len;
}
- break;
- case KEY_DELETE:
- if (selection != 0) {
- i64 i0 = selection < 0 ? cursor + selection : cursor;
- i64 i1 = selection < 0 ? cursor : cursor + selection;
- for (i64 i = 0; i1 + i < text_len; ++i)
- text[i0 + i] = text[i1 + i];
- selection = 0;
- cursor = i0;
- text_len -= i1 - i0;
- } else if (cursor < text_len) {
- for (i64 i = cursor + 1; i < text_len; ++i)
- text[i - 1] = text[i];
- --text_len;
+ if (text_len < (i64) (sizeof text / sizeof *text)) {
+ for (i64 i = text_len; i > cursor; --i)
+ text[i] = text[i - 1];
+ text[cursor++] = platform.input[i].c;
+ ++text_len;
}
- break;
-
- case '\n':
- case '\r':
- case '\t':
- platform.input[i].c = platform.input[i].key;
- // fallthrough
-
- default:
- if (platform.input[i].c) {
- if (selection != 0) {
- i64 i0 = selection < 0 ? cursor + selection : cursor;
- i64 i1 = selection < 0 ? cursor : cursor + selection;
- for (i64 i = 0; i1 + i < text_len; ++i)
- text[i0 + i] = text[i1 + i];
- selection = 0;
- cursor = i0;
- text_len -= i1 - i0;
- }
-
- if (text_len < (i64) (sizeof text / sizeof *text)) {
- for (i64 i = text_len; i > cursor; --i)
- text[i] = text[i - 1];
- text[cursor++] = platform.input[i].c;
- ++text_len;
- }
- }
- }
+ }
+ }
- draw_text_area(0, x0 + 8, y0 - 8, w, h, 10., 10., text_len, text);
- draw_text_area(color, x0, y0, w, h, 10., 10., text_len, text);
- draw_selection_cursor(0xffffff, x0, y0, w, h, 10., 10., cursor, selection, text_len, text);
+ draw_text_area(0, x0 + 8, y0 - 8, w, h, 10., 10., text_len, text);
+ draw_text_area(color, x0, y0, w, h, 10., 10., text_len, text);
+ draw_selection_cursor(0xffffff, x0, y0, w, h, 10., 10., cursor, selection, text_len, text);
- p_render_frame();
- }
+ p_render_frame();
+}
+
+i32 main(i32 argc, c8 **argv) {
+ (void) argc;
+ (void) argv;
+
+ platform = (Platform) {
+ .title = "UI",
+ .frame_width = 960,
+ .frame_height = 720,
+ };
+
+ p_event_loop();
- p_cleanup();
return 0;
}