summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2025-01-12 12:13:56 +0100
committerMitya Selivanov <automainint@guattari.tech>2025-01-12 12:13:56 +0100
commit29d736aecc5db0deb67053931e5b1cf03a9defc3 (patch)
tree50caf18887aeda07b24bee5d4143130f8546aff7 /examples
parent464575c72a8a20b5e565f91cebfeb470edc8542e (diff)
downloadreduced_system_layer-29d736aecc5db0deb67053931e5b1cf03a9defc3.zip
f32 colors; g_ prefix
Diffstat (limited to 'examples')
-rw-r--r--[-rwxr-xr-x]examples/echo.c27
-rw-r--r--[-rwxr-xr-x]examples/game_of_life.c29
-rw-r--r--[-rwxr-xr-x]examples/graph.c81
-rw-r--r--[-rwxr-xr-x]examples/julia_set.c61
-rw-r--r--[-rwxr-xr-x]examples/labyrinth.c29
-rw-r--r--[-rwxr-xr-x]examples/particles.c66
-rw-r--r--examples/proto.c5
-rw-r--r--[-rwxr-xr-x]examples/sinewave.c45
-rw-r--r--[-rwxr-xr-x]examples/ui.c99
9 files changed, 115 insertions, 327 deletions
diff --git a/examples/echo.c b/examples/echo.c
index 2444747..10f3cd5 100755..100644
--- a/examples/echo.c
+++ b/examples/echo.c
@@ -1,30 +1,3 @@
-#if 0 /*
-#/ ================================================================
-#/
-#/ echo.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
-
#ifdef __wasm__
#error Not implemented!
#endif
diff --git a/examples/game_of_life.c b/examples/game_of_life.c
index 540251d..fdd3d54 100755..100644
--- a/examples/game_of_life.c
+++ b/examples/game_of_life.c
@@ -1,30 +1,3 @@
-#if 0 /*
-#/ ================================================================
-#/
-#/ game_of_life.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 "../reduced_system_layer.c"
void update_and_render_frame(void) {
@@ -37,7 +10,7 @@ i32 main(i32 argc, c8 **argv) {
(void) argc;
(void) argv;
- platform = (Platform) {
+ g_platform = (Platform) {
.title = "Conway's Game of Life",
.frame_width = 960,
.frame_height = 720,
diff --git a/examples/graph.c b/examples/graph.c
index 6ad5fdc..f03cc93 100755..100644
--- a/examples/graph.c
+++ b/examples/graph.c
@@ -1,30 +1,3 @@
-#if 0 /*
-#/ ================================================================
-#/
-#/ graph.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"
enum {
@@ -80,12 +53,12 @@ void draw_node(i64 node_index) {
Node n = world.nodes[node_index];
- u32 color = 0; // black color
+ vec3_f32 color = {0}; // black color
if (n.highlight)
- color = 0xff00ff; // pink color
+ color = (vec3_f32) { 1.f, .0f, 1.f }; // pink color
if (n.hover)
- color = 0x007f00; // green color
+ color = (vec3_f32) { 0.f, .5f, 0.f }; // green color
fill_ellipse(
OP_SET, // set pixels
@@ -105,12 +78,12 @@ void draw_edge(i64 edge_index) {
Node n0 = world.nodes[e.src];
Node n1 = world.nodes[e.dst];
- u32 color = 0x7f7f7f; // grey color
+ vec3_f32 color = { .5f, .5f, .5f }; // grey color
if (e.highlight)
- color = 0xff00ff; // pink color
+ color = (vec3_f32) { 1.f, .0f, 1.f }; // pink color
if (e.hover)
- color = 0x007f00; // green color
+ color = (vec3_f32) { 0.f, .5f, 0.f }; // green color
fill_line(
OP_SET, // set pixels
@@ -146,8 +119,8 @@ void update_node(i64 node_index) {
n.y - n.radius,
n.radius * 2,
n.radius * 2,
- platform.cursor_x,
- platform.cursor_y
+ g_platform.cursor_x,
+ g_platform.cursor_y
);
}
@@ -165,8 +138,8 @@ void update_edge(i64 edge_index) {
n1.x,
n1.y,
e.width,
- platform.cursor_x,
- platform.cursor_y
+ g_platform.cursor_x,
+ g_platform.cursor_y
);
}
@@ -421,7 +394,7 @@ void update_and_render_frame(void) {
update_edge(i);
}
- if (platform.key_pressed[KEY_DELETE]) {
+ if (g_platform.key_pressed[KEY_DELETE]) {
for (i64 i = 0; i < MAX_NUM_EDGES; ++i)
if (world.edges[i].enabled && world.edges[i].hover)
remove_edge(i);
@@ -433,7 +406,7 @@ void update_and_render_frame(void) {
path_changed = 1;
}
- if (platform.key_pressed['1']) {
+ if (g_platform.key_pressed['1']) {
for (i64 i = 0; i < MAX_NUM_NODES; ++i)
if (world.nodes[i].enabled && world.nodes[i].hover) {
path_src = i;
@@ -442,7 +415,7 @@ void update_and_render_frame(void) {
}
}
- if (platform.key_pressed['2'])
+ if (g_platform.key_pressed['2'])
for (i64 i = 0; i < MAX_NUM_NODES; ++i)
if (world.nodes[i].enabled && world.nodes[i].hover) {
path_dst = i;
@@ -455,13 +428,13 @@ void update_and_render_frame(void) {
path_changed = 0;
}
- if (platform.key_pressed[BUTTON_LEFT]) {
+ if (g_platform.key_pressed[BUTTON_LEFT]) {
for (i64 i = 0; i < MAX_NUM_NODES; ++i)
if (world.nodes[i].enabled && world.nodes[i].hover) {
drag_on = 1;
drag_node = i;
- drag_x0 = platform.cursor_x;
- drag_y0 = platform.cursor_y;
+ drag_x0 = g_platform.cursor_x;
+ drag_y0 = g_platform.cursor_y;
for (i64 j = 0; j < MAX_NUM_NODES; ++j) {
world.nodes[j].drag_x = world.nodes[j].x;
world.nodes[j].drag_y = world.nodes[j].y;
@@ -470,10 +443,10 @@ void update_and_render_frame(void) {
}
if (!drag_on)
- add_node(platform.cursor_x, platform.cursor_y);
+ add_node(g_platform.cursor_x, g_platform.cursor_y);
}
- if (platform.key_pressed[BUTTON_RIGHT])
+ if (g_platform.key_pressed[BUTTON_RIGHT])
for (i64 i = 0; i < MAX_NUM_NODES; ++i)
if (world.nodes[i].enabled && world.nodes[i].hover) {
adding_edge = 1;
@@ -489,7 +462,7 @@ void update_and_render_frame(void) {
break;
}
- if (adding_edge && !platform.key_down[BUTTON_RIGHT]) {
+ if (adding_edge && !g_platform.key_down[BUTTON_RIGHT]) {
adding_edge = 0;
add_edge(adding_src, adding_dst);
path_changed = 1;
@@ -497,8 +470,8 @@ void update_and_render_frame(void) {
if (drag_on) {
if (check_node_index(drag_node)) {
- f64 dx = platform.cursor_x - drag_x0;
- f64 dy = platform.cursor_y - drag_y0;
+ f64 dx = g_platform.cursor_x - drag_x0;
+ f64 dy = g_platform.cursor_y - drag_y0;
world.nodes[drag_node].x = world.nodes[drag_node].drag_x + dx;
world.nodes[drag_node].y = world.nodes[drag_node].drag_y + dy;
@@ -525,26 +498,26 @@ void update_and_render_frame(void) {
path_changed = 1;
}
- if (!platform.key_down[BUTTON_LEFT])
+ if (!g_platform.key_down[BUTTON_LEFT])
drag_on = 0;
}
// Render
- fill_rectangle(OP_SET, 0xa0b0c0, 0, 0, platform.frame_width, platform.frame_height);
+ fill_rectangle(OP_SET, (vec3_f32) { .7f, .8f, .9f }, 0, 0, g_platform.frame_width, g_platform.frame_height);
if (adding_edge) {
f64 x0 = world.nodes[adding_src].x;
f64 y0 = world.nodes[adding_src].y;
- f64 x1 = platform.cursor_x;
- f64 y1 = platform.cursor_y;
+ f64 x1 = g_platform.cursor_x;
+ f64 y1 = g_platform.cursor_y;
if (adding_src != adding_dst) {
x1 = world.nodes[adding_dst].x;
y1 = world.nodes[adding_dst].y;
}
- fill_line(OP_SET, 0x7f007f, x0, y0, x1, y1, 30);
+ fill_line(OP_SET, (vec3_f32) { .5f, 0.f, .5f }, x0, y0, x1, y1, 30);
}
draw_graph();
@@ -556,7 +529,7 @@ i32 main(i32 argc, c8 **argv) {
(void) argc;
(void) argv;
- platform = (Platform) {
+ g_platform = (Platform) {
.title = "Graph",
.frame_width = 960,
.frame_height = 720,
diff --git a/examples/julia_set.c b/examples/julia_set.c
index c25b028..26e7a7a 100755..100644
--- a/examples/julia_set.c
+++ b/examples/julia_set.c
@@ -1,30 +1,3 @@
-#if 0 /*
-#/ ================================================================
-#/
-#/ julia_set.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 \
- -g -O3 \
- -fsanitize=undefined,address,leak \
- -lX11 -lm -lasound \
- -o $BIN $SRC && \
- ./$BIN $@ && rm $BIN
-exit $? # */
-#endif
-
#include "../reduced_system_layer.c"
i64 p = 4;
@@ -43,43 +16,43 @@ void update_and_render_frame(void) {
i64 time_elapsed = p_time() - time_0;
time_0 += time_elapsed;
- b8 left = platform.key_down[KEY_LEFT];
- b8 right = platform.key_down[KEY_RIGHT];
- b8 up = platform.key_down[KEY_UP];
- b8 down = platform.key_down[KEY_DOWN];
+ b8 left = g_platform.key_down[KEY_LEFT];
+ b8 right = g_platform.key_down[KEY_RIGHT];
+ b8 up = g_platform.key_down[KEY_UP];
+ b8 down = g_platform.key_down[KEY_DOWN];
if (!left && !right && !up && !down && num_events == 0) {
p_sleep_for(1);
return;
}
- if (platform.key_pressed['\n'])
+ if (g_platform.key_pressed['\n'])
p = (p == 1 ? 4 : 1);
- if (platform.key_pressed[KEY_ESCAPE]) {
+ if (g_platform.key_pressed[KEY_ESCAPE]) {
view_x = 0.;
view_y = 0.;
view_s = 1.;
}
- f64 d = (platform.key_down[MOD_CTRL] ? .00005 : .001) * view_s * time_elapsed;
+ f64 d = (g_platform.key_down[MOD_CTRL] ? .00005 : .001) * view_s * time_elapsed;
if (left) { cx -= d; cy -= d; }
if (right) { cx += d; cy += d; }
if (up) { cx += d; cy -= d; }
if (down) { cx -= d; cy += d; }
- if (platform.key_down[BUTTON_LEFT]) {
- view_x += platform.cursor_dx * view_s;
- view_y += platform.cursor_dy * view_s;
+ if (g_platform.key_down[BUTTON_LEFT]) {
+ view_x += g_platform.cursor_dx * view_s;
+ view_y += g_platform.cursor_dy * view_s;
}
- view_s += .1 * platform.wheel_dy * view_s;
+ view_s += .1 * g_platform.wheel_dy * view_s;
- for (i32 j = 0; j + p <= platform.frame_height; j += p)
- for (i32 i = 0; i + p <= platform.frame_width; i += p) {
- f64 x = .003 * ((i - platform.frame_width * .5) * view_s - view_x);
- f64 y = .003 * ((j - platform.frame_height * .5) * view_s - view_y);
+ for (i32 j = 0; j + p <= g_platform.frame_height; j += p)
+ for (i32 i = 0; i + p <= g_platform.frame_width; i += p) {
+ f64 x = .003 * ((i - g_platform.frame_width * .5) * view_s - view_x);
+ f64 y = .003 * ((j - g_platform.frame_height * .5) * view_s - view_y);
i64 n = 0;
@@ -98,7 +71,7 @@ void update_and_render_frame(void) {
for (i32 jj = 0; jj < p; ++jj)
for (i32 ii = 0; ii < p; ++ii)
- platform.pixels[(j + jj) * platform.frame_width + (i + ii)] = c;
+ g_platform.pixels[(j + jj) * g_platform.frame_width + (i + ii)] = rgb_f32_from_u32(c);
}
p_render_frame();
@@ -108,7 +81,7 @@ i32 main(i32 argc, c8 **argv) {
(void) argc;
(void) argv;
- platform = (Platform) {
+ g_platform = (Platform) {
.title = "Julia Set",
.frame_width = 960,
.frame_height = 720,
diff --git a/examples/labyrinth.c b/examples/labyrinth.c
index 28ca0da..54bed19 100755..100644
--- a/examples/labyrinth.c
+++ b/examples/labyrinth.c
@@ -1,30 +1,3 @@
-#if 0 /*
-#/ ================================================================
-#/
-#/ labyrinth.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 "../reduced_system_layer.c"
void update_and_render_frame(void) {
@@ -37,7 +10,7 @@ i32 main(i32 argc, c8 **argv) {
(void) argc;
(void) argv;
- platform = (Platform) {
+ g_platform = (Platform) {
.title = "Labyrinth",
.frame_width = 960,
.frame_height = 720,
diff --git a/examples/particles.c b/examples/particles.c
index bf90123..37a77a5 100755..100644
--- a/examples/particles.c
+++ b/examples/particles.c
@@ -1,30 +1,3 @@
-#if 0 /*
-#/ ================================================================
-#/
-#/ particles.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"
typedef struct {
@@ -40,9 +13,9 @@ typedef struct {
Entity entities[10 * 1024 * 1024];
} World;
-World world = {0};
-u32 background;
-i64 time_0;
+World world = {0};
+vec3_f32 background = { .0f, .0f, .08f };
+i64 time_0;
void update_and_render_frame(void) {
p_handle_events();
@@ -50,21 +23,21 @@ void update_and_render_frame(void) {
i64 time_elapsed = p_time() - time_0;
time_0 += time_elapsed;
- if (platform.key_pressed[BUTTON_LEFT]) {
+ if (g_platform.key_pressed[BUTTON_LEFT]) {
i64 n = world.num_entities++;
world.entities[n] = (Entity) {
- .x = (f64) platform.cursor_x - (f64) platform.frame_width / 2,
- .y = -((f64) platform.cursor_y - (f64) platform.frame_height / 2),
+ .x = (f64) g_platform.cursor_x - (f64) g_platform.frame_width / 2,
+ .y = -((f64) g_platform.cursor_y - (f64) g_platform.frame_height / 2),
};
}
if (time_elapsed > 0) {
- if (platform.key_down[BUTTON_RIGHT]) {
+ if (g_platform.key_down[BUTTON_RIGHT]) {
for (i64 n = 0; n < time_elapsed; ++n)
world.entities[world.num_entities + n] = (Entity) {
- .x = (f64) platform.cursor_x - (f64) platform.frame_width / 2,
- .y = -((f64) platform.cursor_y - (f64) platform.frame_height / 2),
+ .x = (f64) g_platform.cursor_x - (f64) g_platform.frame_width / 2,
+ .y = -((f64) g_platform.cursor_y - (f64) g_platform.frame_height / 2),
};
world.num_entities += time_elapsed;
@@ -111,23 +84,23 @@ void update_and_render_frame(void) {
world.time += time_elapsed;
}
- 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] = background;
+ 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] = background;
for (i64 n = 0; n < world.num_entities; ++n) {
Entity *e = &world.entities[n];
- i32 x = platform.frame_width / 2 + (i32) floor(e->x + .5);
- i32 y = platform.frame_height / 2 - (i32) floor(e->y + .5);
+ i32 x = g_platform.frame_width / 2 + (i32) floor(e->x + .5);
+ i32 y = g_platform.frame_height / 2 - (i32) floor(e->y + .5);
for (i32 j = y - 10; j <= y + 10; ++j) {
- if (j < 0 || j >= platform.frame_height) continue;
+ if (j < 0 || j >= g_platform.frame_height) continue;
for (i32 i = x - 10; i <= x + 10; ++i) {
- if (i < 0 || i >= platform.frame_width) continue;
+ if (i < 0 || i >= g_platform.frame_width) continue;
if ((i - x) * (i - x) + (j - y) * (j - y) > 100) continue;
f64 v = (e->vx * e->vx + e->vy * e->vy) * 8.;
- platform.pixels[j * platform.frame_width + i] = u32_from_rgb(-.2 + v * 2., .1 + v * .7, 1. - v);
+ g_platform.pixels[j * g_platform.frame_width + i] = (vec3_f32) { -.2 + v * 2., .1 + v * .7, 1. - v };
}
}
}
@@ -139,14 +112,13 @@ i32 main(i32 argc, c8 **argv) {
(void) argc;
(void) argv;
- platform = (Platform) {
+ g_platform = (Platform) {
.title = "Gravity",
.frame_width = 960,
.frame_height = 720,
};
- background = u32_from_rgb(.0f, .0f, .08f);
- time_0 = p_time();
+ time_0 = p_time();
srand(p_time());
diff --git a/examples/proto.c b/examples/proto.c
new file mode 100644
index 0000000..dbb64a8
--- /dev/null
+++ b/examples/proto.c
@@ -0,0 +1,5 @@
+int main(int argc, char **argv) {
+ (void) argc;
+ (void) argv;
+ return 0;
+}
diff --git a/examples/sinewave.c b/examples/sinewave.c
index a1f19f8..3a127ae 100755..100644
--- a/examples/sinewave.c
+++ b/examples/sinewave.c
@@ -1,30 +1,3 @@
-#if 0 /*
-#/ ================================================================
-#/
-#/ sinewave.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"
i64 time_0 = 0;
@@ -33,26 +6,26 @@ i64 audio_samples = 0;
f32 frames[AUDIO_SAMPLE_RATE * AUDIO_NUM_CHANNELS] = {0};
b8 ui_button(f64 x, f64 y, f64 width, f64 height) {
- b8 has_cursor = platform.cursor_x >= x && platform.cursor_x < x + width &&
- platform.cursor_y >= y && platform.cursor_y < y + height;
+ b8 has_cursor = g_platform.cursor_x >= x && g_platform.cursor_x < x + width &&
+ g_platform.cursor_y >= y && g_platform.cursor_y < y + height;
- b8 is_pressed = has_cursor && platform.key_down[BUTTON_LEFT];
+ b8 is_pressed = has_cursor && g_platform.key_down[BUTTON_LEFT];
if (is_pressed)
- fill_rectangle(OP_SET, 0xffffff, x, y, width, height);
+ fill_rectangle(OP_SET, (vec3_f32) { 1.f, 1.f, 1.f }, x, y, width, height);
else if (has_cursor)
- fill_rectangle(OP_SET, 0xa0a000, x, y, width, height);
+ fill_rectangle(OP_SET, (vec3_f32) { .8f, .8f, 0.f }, x, y, width, height);
else
- fill_rectangle(OP_SET, 0x808030, x, y, width, height);
+ fill_rectangle(OP_SET, (vec3_f32) { .8f, .8f, .2f }, x, y, width, height);
- return has_cursor && platform.key_pressed[BUTTON_LEFT];
+ return has_cursor && g_platform.key_pressed[BUTTON_LEFT];
}
void update_and_render_frame(void) {
i32 num_events = p_handle_events();
if (num_events > 0) {
- fill_rectangle(OP_SET, 0x202020, 0, 0, platform.frame_width, platform.frame_height);
+ fill_rectangle(OP_SET, (vec3_f32) { .1f, .1f, .1f }, 0, 0, g_platform.frame_width, g_platform.frame_height);
if (ui_button(100, 100, 200, 200))
p_queue_sound(0, AUDIO_SAMPLE_RATE, frames);
@@ -70,7 +43,7 @@ i32 main(i32 argc, c8 **argv) {
(void) argc;
(void) argv;
- platform = (Platform) {
+ g_platform = (Platform) {
.title = "Sine Wave",
.graceful_exit = 1,
};
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,