summaryrefslogtreecommitdiff
path: root/examples/ui.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/ui.c')
-rw-r--r--[-rwxr-xr-x]examples/ui.c99
1 files changed, 36 insertions, 63 deletions
diff --git a/examples/ui.c b/examples/ui.c
index 79bdd70..684b428 100755..100644
--- a/examples/ui.c
+++ b/examples/ui.c
@@ -1,30 +1,3 @@
-#if 0 /*
-#/ ================================================================
-#/
-#/ ui.c
-#/
-#/ ================================================================
-#/
-#/ Self-compilation shell script
-#/
-SRC=${0##*./}
-BIN=${SRC%.*}
-gcc \
- -Wall -Wextra -Werror -pedantic \
- -Wno-old-style-declaration \
- -Wno-missing-braces \
- -Wno-unused-variable \
- -Wno-unused-but-set-variable \
- -Wno-unused-parameter \
- -Wno-overlength-strings \
- -O3 \
- -fsanitize=undefined,address,leak \
- -lX11 -lm -lasound \
- -o $BIN $SRC && \
- ./$BIN $@ && rm $BIN
-exit $? # */
-#endif
-
#include "../graphics.c"
b8 button_0_down = 0;
@@ -40,53 +13,53 @@ 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;
+ for (i32 j = 0; j < g_platform.frame_height; ++j)
+ for (i32 i = 0; i < g_platform.frame_width; ++i)
+ g_platform.pixels[j * g_platform.frame_width + i] = (vec3_f32) { .2f, .1f, 0.f };
- 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 (g_platform.cursor_x >= 40 && g_platform.cursor_x < 100 && g_platform.cursor_y >= 40 && g_platform.cursor_y < 100) {
+ button_0_down = g_platform.key_down[BUTTON_LEFT];
if (button_0_down)
- fill_rectangle(OP_SET, 0xffffff, 40, 40, 60, 60);
+ fill_rectangle(OP_SET, (vec3_f32) { 1.f, 1.f, 1.f }, 40, 40, 60, 60);
else
- fill_rectangle(OP_SET, 0x00ff00, 40, 40, 60, 60);
+ fill_rectangle(OP_SET, (vec3_f32) { 0.f, 1.f, 0.f }, 40, 40, 60, 60);
} else {
button_0_down = 0;
- fill_rectangle(OP_SET, 0x208020, 40, 40, 60, 60);
+ fill_rectangle(OP_SET, (vec3_f32) { .1f, .5f, .1f }, 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])
+ if (g_platform.cursor_x >= 40 && g_platform.cursor_x < 100 && g_platform.cursor_y >= 120 && g_platform.cursor_y < 180) {
+ button_1_down = g_platform.key_down[BUTTON_LEFT];
+ if (g_platform.key_pressed[BUTTON_LEFT])
button_1_checked = !button_1_checked;
if (button_1_down)
- fill_rectangle(OP_SET, 0xffffff, 40, 120, 60, 60);
+ fill_rectangle(OP_SET, (vec3_f32) { 1.f, 1.f, 1.f }, 40, 120, 60, 60);
else if (button_1_checked)
- fill_rectangle(OP_SET, 0xff8080, 40, 120, 60, 60);
+ fill_rectangle(OP_SET, (vec3_f32) { 1.f, .5f, .5f }, 40, 120, 60, 60);
else
- fill_rectangle(OP_SET, 0x80ff80, 40, 120, 60, 60);
+ fill_rectangle(OP_SET, (vec3_f32) { .5f, 1.f, .5f }, 40, 120, 60, 60);
} else {
button_1_down = 0;
if (button_1_checked)
- fill_rectangle(OP_SET, 0xff0000, 40, 120, 60, 60);
+ fill_rectangle(OP_SET, (vec3_f32) { 1.f, 0.f, 0.f }, 40, 120, 60, 60);
else
- fill_rectangle(OP_SET, 0x00ff00, 40, 120, 60, 60);
+ fill_rectangle(OP_SET, (vec3_f32) { 0.f, 1.f, 0.f }, 40, 120, 60, 60);
}
- i64 w = platform.frame_width / 2;
- i64 h = platform.frame_height / 2;
+ i64 w = g_platform.frame_width / 2;
+ i64 h = g_platform.frame_height / 2;
i64 x0 = w / 2;
i64 y0 = h / 2;
- i64 color = 0xff7f7f;
+ vec3_f32 color = { 1.f, .5f, .5f };
- if (platform.cursor_x >= x0 && platform.cursor_x < x0 + w &&
- platform.cursor_y >= y0 && platform.cursor_y < y0 + h)
- color = 0xffffff;
+ if (g_platform.cursor_x >= x0 && g_platform.cursor_x < x0 + w &&
+ g_platform.cursor_y >= y0 && g_platform.cursor_y < y0 + h)
+ color = (vec3_f32) { 1.f, 1.f, 1.f };
- for (i64 i = 0; i < platform.input_size; ++i)
- if (platform.input[i].ctrl)
- switch (platform.input[i].key) {
+ for (i64 i = 0; i < g_platform.input_size; ++i)
+ if (g_platform.input[i].ctrl)
+ switch (g_platform.input[i].key) {
case KEY_V: {
if (selection != 0) {
i64 i0 = selection < 0 ? cursor + selection : cursor;
@@ -98,8 +71,8 @@ void update_and_render_frame(void) {
text_len -= i1 - i0;
}
- for (i64 n = 0; n < platform.clipboard_size;) {
- c32 c = utf8_read(platform.clipboard_size - n, platform.clipboard + n);
+ for (i64 n = 0; n < g_platform.clipboard_size;) {
+ c32 c = utf8_read(g_platform.clipboard_size - n, g_platform.clipboard + n);
if (text_len < (i64) (sizeof text / sizeof *text)) {
for (i64 j = text_len; j > cursor; --j)
@@ -147,9 +120,9 @@ void update_and_render_frame(void) {
default:;
}
else
- switch (platform.input[i].key) {
+ switch (g_platform.input[i].key) {
case KEY_LEFT:
- if (platform.key_down[MOD_SHIFT]) {
+ if (g_platform.key_down[MOD_SHIFT]) {
if (cursor > 0)
++selection;
} else if (selection != 0) {
@@ -161,7 +134,7 @@ void update_and_render_frame(void) {
break;
case KEY_RIGHT:
- if (platform.key_down[MOD_SHIFT]) {
+ if (g_platform.key_down[MOD_SHIFT]) {
if (cursor < text_len)
--selection;
} else if (selection != 0) {
@@ -207,11 +180,11 @@ void update_and_render_frame(void) {
case KEY_ENTER:
case KEY_TAB:
- platform.input[i].c = platform.input[i].key;
+ g_platform.input[i].c = g_platform.input[i].key;
// fallthrough
default:
- if (platform.input[i].c) {
+ if (g_platform.input[i].c) {
if (selection != 0) {
i64 i0 = selection < 0 ? cursor + selection : cursor;
i64 i1 = selection < 0 ? cursor : cursor + selection;
@@ -225,15 +198,15 @@ void update_and_render_frame(void) {
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[cursor++] = g_platform.input[i].c;
++text_len;
}
}
}
- draw_text_area(0, x0 + 8, y0 - 8, w, h, 10., 10., text_len, text);
+ draw_text_area((vec3_f32) {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_selection_cursor((vec3_f32) { 1.f, 1.f, 1.f }, x0, y0, w, h, 10., 10., cursor, selection, text_len, text);
p_render_frame();
}
@@ -242,7 +215,7 @@ i32 main(i32 argc, c8 **argv) {
(void) argc;
(void) argv;
- platform = (Platform) {
+ g_platform = (Platform) {
.title = "UI",
.frame_width = 960,
.frame_height = 720,