summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2024-11-02 04:08:54 +0100
committerMitya Selivanov <automainint@guattari.tech>2024-11-02 04:08:54 +0100
commitaaa61a3eeb5b1ade783b5a7ec31a6a7fd6b3393e (patch)
treea187f9e503c3c5a8573a04b05a75c34cf6d8e716
parent48c5d9b83afd1cd56e73449aebaf461cb48360d9 (diff)
downloadreduced_system_layer-aaa61a3eeb5b1ade783b5a7ec31a6a7fd6b3393e.zip
Update examples
-rwxr-xr-xexamples/echo.c12
-rwxr-xr-xexamples/game_of_life.c15
-rwxr-xr-xexamples/graph.c197
-rwxr-xr-xexamples/gravity.c169
-rwxr-xr-xexamples/julia_set.c146
-rwxr-xr-xexamples/labyrinth.c15
-rwxr-xr-xexamples/landscape.c100
-rwxr-xr-xreduced_system_layer.c1
8 files changed, 317 insertions, 338 deletions
diff --git a/examples/echo.c b/examples/echo.c
index 7790c6a..1a04fbb 100755
--- a/examples/echo.c
+++ b/examples/echo.c
@@ -25,6 +25,10 @@ gcc \
exit $? # */
#endif
+#ifdef __wasm__
+#error Not implemented!
+#endif
+
#include "../reduced_system_layer.c"
enum {
@@ -34,11 +38,15 @@ enum {
PORT = 42069,
};
+void update_and_render_frame(void) {
+ // Do nothing.
+}
+
i32 main(i32 argc, c8 **argv) {
if (argc < 2) {
if (argc == 1)
printf("Usade: %s recv|send\n", argv[0]);
- return -1;
+ return 0;
}
i32 mode = 0;
@@ -50,7 +58,7 @@ i32 main(i32 argc, c8 **argv) {
if (mode == 0) {
printf("Usage: %s recv|send\n", argv[0]);
- return -1;
+ return 0;
}
static c8 buf[256] = "";
diff --git a/examples/game_of_life.c b/examples/game_of_life.c
index 3f33d90..7fee606 100755
--- a/examples/game_of_life.c
+++ b/examples/game_of_life.c
@@ -27,6 +27,12 @@ exit $? # */
#include "../reduced_system_layer.c"
+void update_and_render_frame(void) {
+ p_handle_events();
+ p_render_frame();
+ p_sleep_for(0);
+}
+
i32 main(i32 argc, c8 **argv) {
(void) argc;
(void) argv;
@@ -37,14 +43,7 @@ i32 main(i32 argc, c8 **argv) {
.frame_height = 720,
};
- p_init();
-
- while (!platform.done) {
- p_handle_events();
- p_render_frame();
- p_sleep_for(0);
- }
+ p_event_loop();
- p_cleanup();
return 0;
}
diff --git a/examples/graph.c b/examples/graph.c
index 037635f..36c110f 100755
--- a/examples/graph.c
+++ b/examples/graph.c
@@ -361,135 +361,134 @@ void highlight_path(i64 src, i64 dst) {
}
}
-i32 main(i32 argc, c8 **argv) {
- (void) argc;
- (void) argv;
+b8 adding_edge = 0;
+i64 adding_src = 0;
+i64 adding_dst = 0;
- platform = (Platform) {
- .title = "Graph",
- .frame_width = 960,
- .frame_height = 720,
- };
+b8 path_changed = 0;
+i64 path_src = -1;
+i64 path_dst = -1;
- p_init();
+void update_and_render_frame(void) {
+ p_wait_events();
- add_node(100, 100);
- add_node(300, 100);
- add_node(120, 300);
-
- add_edge(0, 1);
- add_edge(0, 2);
- add_edge(1, 2);
+ // Input events
- b8 adding_edge = 0;
- i64 adding_src = 0;
- i64 adding_dst = 0;
+ b8 hover_node = 0;
- b8 path_changed = 0;
- i64 path_src = -1;
- i64 path_dst = -1;
+ for (i64 i = 0; i < MAX_NUM_NODES; ++i)
+ if (world.nodes[i].enabled) {
+ update_node(i);
+ if (world.nodes[i].hover)
+ hover_node = 1;
+ }
- while (!platform.done) {
- p_wait_events();
+ for (i64 i = 0; i < MAX_NUM_EDGES; ++i)
+ if (world.edges[i].enabled) {
+ if (hover_node)
+ world.edges[i].hover = 0;
+ else
+ update_edge(i);
+ }
- // Input events
+ if (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);
- b8 hover_node = 0;
+ for (i64 i = 0; i < MAX_NUM_NODES; ++i)
+ if (world.nodes[i].enabled && world.nodes[i].hover)
+ remove_node(i);
+ }
+ if (platform.key_pressed['1']) {
for (i64 i = 0; i < MAX_NUM_NODES; ++i)
- if (world.nodes[i].enabled) {
- update_node(i);
- if (world.nodes[i].hover)
- hover_node = 1;
+ if (world.nodes[i].enabled && world.nodes[i].hover) {
+ path_src = i;
+ path_changed = 1;
+ break;
}
+ }
- for (i64 i = 0; i < MAX_NUM_EDGES; ++i)
- if (world.edges[i].enabled) {
- if (hover_node)
- world.edges[i].hover = 0;
- else
- update_edge(i);
+ if (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;
+ path_changed = 1;
+ break;
}
- if (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);
+ if (path_changed) {
+ highlight_path(path_src, path_dst);
+ path_changed = 0;
+ }
- for (i64 i = 0; i < MAX_NUM_NODES; ++i)
- if (world.nodes[i].enabled && world.nodes[i].hover)
- remove_node(i);
- }
+ if (platform.key_pressed[BUTTON_LEFT])
+ add_node(platform.cursor_x, platform.cursor_y);
- if (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;
- path_changed = 1;
- break;
- }
- }
+ if (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;
+ adding_src = i;
+ adding_dst = i;
+ break;
+ }
- if (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;
- path_changed = 1;
- break;
- }
+ if (adding_edge)
+ for (i64 i = 0; i < MAX_NUM_NODES; ++i)
+ if (world.nodes[i].enabled && world.nodes[i].hover) {
+ adding_dst = i;
+ break;
+ }
- if (path_changed) {
- highlight_path(path_src, path_dst);
- path_changed = 0;
- }
+ if (adding_edge && !platform.key_down[BUTTON_RIGHT]) {
+ adding_edge = 0;
+ add_edge(adding_src, adding_dst);
+ }
- if (platform.key_pressed[BUTTON_LEFT])
- add_node(platform.cursor_x, platform.cursor_y);
+ // Render
- if (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;
- adding_src = i;
- adding_dst = i;
- break;
- }
+ fill_rectangle(OP_SET, 0xffffff, 0, 0, platform.frame_width, platform.frame_height);
- if (adding_edge)
- for (i64 i = 0; i < MAX_NUM_NODES; ++i)
- if (world.nodes[i].enabled && world.nodes[i].hover) {
- adding_dst = i;
- break;
- }
+ 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;
- if (adding_edge && !platform.key_down[BUTTON_RIGHT]) {
- adding_edge = 0;
- add_edge(adding_src, adding_dst);
+ if (adding_src != adding_dst) {
+ x1 = world.nodes[adding_dst].x;
+ y1 = world.nodes[adding_dst].y;
}
- // Render
+ fill_line(OP_SET, 0x7f007f, x0, y0, x1, y1, 30);
+ }
- fill_rectangle(OP_SET, 0xffffff, 0, 0, platform.frame_width, platform.frame_height);
+ draw_graph();
- 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;
+ p_render_frame();
+}
- if (adding_src != adding_dst) {
- x1 = world.nodes[adding_dst].x;
- y1 = world.nodes[adding_dst].y;
- }
+i32 main(i32 argc, c8 **argv) {
+ (void) argc;
+ (void) argv;
- fill_line(OP_SET, 0x7f007f, x0, y0, x1, y1, 30);
- }
+ platform = (Platform) {
+ .title = "Graph",
+ .frame_width = 960,
+ .frame_height = 720,
+ };
+
+ add_node(100, 100);
+ add_node(300, 100);
+ add_node(120, 300);
- draw_graph();
+ add_edge(0, 1);
+ add_edge(0, 2);
+ add_edge(1, 2);
- p_render_frame();
- }
+ p_event_loop();
- p_cleanup();
return 0;
}
diff --git a/examples/gravity.c b/examples/gravity.c
index 246aab1..d21a4f5 100755
--- a/examples/gravity.c
+++ b/examples/gravity.c
@@ -40,116 +40,117 @@ typedef struct {
Entity entities[10 * 1024 * 1024];
} World;
-i32 main(i32 argc, c8 **argv) {
- (void) argc;
- (void) argv;
+World world = {0};
+u32 background;
+i64 time_0;
- u32 background = u32_from_rgb(.0f, .0f, .08f);
+void update_and_render_frame(void) {
+ p_handle_events();
- platform = (Platform) {
- .title = "Gravity",
- .frame_width = 960,
- .frame_height = 720,
- };
+ i64 time_elapsed = p_time() - time_0;
+ time_0 += time_elapsed;
- p_init();
+ if (platform.key_pressed[BUTTON_LEFT]) {
+ i64 n = world.num_entities++;
- srand(time(0));
+ world.entities[n] = (Entity) {
+ .x = (f64) platform.cursor_x - (f64) platform.frame_width / 2,
+ .y = -((f64) platform.cursor_y - (f64) platform.frame_height / 2),
+ };
+ }
- static World world = {0};
- i64 time_0 = p_time();
+ if (time_elapsed > 0) {
+ if (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),
+ };
- while (!platform.done) {
- p_handle_events();
+ world.num_entities += time_elapsed;
+ }
- i64 time_elapsed = p_time() - time_0;
- time_0 += time_elapsed;
+ for (i64 n = 0; n < world.num_entities; ++n) {
+ Entity *e = &world.entities[n];
- if (platform.key_pressed[BUTTON_LEFT]) {
- i64 n = world.num_entities++;
+ for (i64 k = 0; k < n; ++k) {
+ Entity *u = &world.entities[k];
- world.entities[n] = (Entity) {
- .x = (f64) platform.cursor_x - (f64) platform.frame_width / 2,
- .y = -((f64) platform.cursor_y - (f64) platform.frame_height / 2),
- };
- }
+ f64 dx = e->x - u->x;
+ f64 dy = e->y - u->y;
+ f64 d = dx * dx + dy * dy;
+ if (d < 1000.) continue;
- if (time_elapsed > 0) {
- if (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),
- };
+ f64 r = sqrt(d);
+ dx /= r;
+ dy /= r;
- world.num_entities += time_elapsed;
- }
+ f64 a = (1. / d) * time_elapsed;
- for (i64 n = 0; n < world.num_entities; ++n) {
- Entity *e = &world.entities[n];
+ if (d < 5000.)
+ a = -a;
- for (i64 k = 0; k < n; ++k) {
- Entity *u = &world.entities[k];
+ e->vx -= dx * a;
+ e->vy -= dy * a;
+ u->vx += dx * a;
+ u->vy += dy * a;
+ }
+ }
- f64 dx = e->x - u->x;
- f64 dy = e->y - u->y;
- f64 d = dx * dx + dy * dy;
- if (d < 1000.) continue;
+ for (i64 n = 0; n < world.num_entities; ++n) {
+ Entity *e = &world.entities[n];
- f64 r = sqrt(d);
- dx /= r;
- dy /= r;
+ e->x += e->vx * time_elapsed;
+ e->y += e->vy * time_elapsed;
- f64 a = (1. / d) * time_elapsed;
+ f64 z = (M_PI * 2.0) * (rand() % 10000) * .0001;
+ e->vx += .0001 * cos(z);
+ e->vy += .0001 * sin(z);
+ }
- if (d < 5000.)
- a = -a;
-
- e->vx -= dx * a;
- e->vy -= dy * a;
- u->vx += dx * a;
- u->vy += dy * a;
- }
- }
+ world.time += time_elapsed;
+ }
- for (i64 n = 0; n < world.num_entities; ++n) {
- Entity *e = &world.entities[n];
+ 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;
- e->x += e->vx * time_elapsed;
- e->y += e->vy * time_elapsed;
+ for (i64 n = 0; n < world.num_entities; ++n) {
+ Entity *e = &world.entities[n];
- f64 z = (M_PI * 2.0) * (rand() % 10000) * .0001;
- e->vx += .0001 * cos(z);
- e->vy += .0001 * sin(z);
- }
+ i32 x = platform.frame_width / 2 + (i32) floor(e->x + .5);
+ i32 y = platform.frame_height / 2 - (i32) floor(e->y + .5);
- world.time += time_elapsed;
+ for (i32 j = y - 10; j <= y + 10; ++j) {
+ if (j < 0 || j >= platform.frame_height) continue;
+ for (i32 i = x - 10; i <= x + 10; ++i) {
+ if (i < 0 || i >= 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);
+ }
}
+ }
- 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;
+ p_render_frame();
+}
- for (i64 n = 0; n < world.num_entities; ++n) {
- Entity *e = &world.entities[n];
+i32 main(i32 argc, c8 **argv) {
+ (void) argc;
+ (void) argv;
- i32 x = platform.frame_width / 2 + (i32) floor(e->x + .5);
- i32 y = 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;
- for (i32 i = x - 10; i <= x + 10; ++i) {
- if (i < 0 || i >= 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);
- }
- }
- }
+ platform = (Platform) {
+ .title = "Gravity",
+ .frame_width = 960,
+ .frame_height = 720,
+ };
- p_render_frame();
- }
+ background = u32_from_rgb(.0f, .0f, .08f);
+ time_0 = p_time();
+
+ srand(p_time());
+
+ p_event_loop();
- p_cleanup();
return 0;
}
diff --git a/examples/julia_set.c b/examples/julia_set.c
index 747c339..0d6c5be 100755
--- a/examples/julia_set.c
+++ b/examples/julia_set.c
@@ -27,98 +27,96 @@ exit $? # */
#include "../reduced_system_layer.c"
-i32 main(i32 argc, c8 **argv) {
- (void) argc;
- (void) argv;
-
- platform = (Platform) {
- .title = "Julia Set",
- .frame_width = 960,
- .frame_height = 720,
- };
-
- p_init();
-
- i64 p = 4;
+i64 p = 4;
+f64 view_x = 0.;
+f64 view_y = 0.;
+f64 view_s = 1.;
+f64 cx = -.771;
+f64 cy = .1005;
+f64 radius = 2.;
+i64 limit = 1024;
+i64 time_0;
+
+void update_and_render_frame(void) {
+ i32 num_events = p_handle_events();
+
+ 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];
+
+ if (!left && !right && !up && !down && num_events == 0) {
+ p_sleep_for(1);
+ return;
+ }
- f64 view_x = 0.;
- f64 view_y = 0.;
- f64 view_s = 1.;
+ if (platform.key_pressed['\n'])
+ p = (p == 1 ? 4 : 1);
- f64 cx = -.771;
- f64 cy = .1005;
- f64 radius = 2.;
- i64 limit = 1024;
+ if (platform.key_pressed[KEY_ESCAPE]) {
+ view_x = 0.;
+ view_y = 0.;
+ view_s = 1.;
+ }
- i64 time = p_time();
+ f64 d = (platform.key_down[MOD_CTRL] ? .00005 : .001) * view_s * time_elapsed;
- while (!platform.done) {
- i32 num_events = p_handle_events();
+ if (left) { cx -= d; cy -= d; }
+ if (right) { cx += d; cy += d; }
+ if (up) { cx += d; cy -= d; }
+ if (down) { cx -= d; cy += d; }
- i64 time_elapsed = p_time() - time;
- time += time_elapsed;
+ if (platform.key_down[BUTTON_LEFT]) {
+ view_x += platform.cursor_dx * view_s;
+ view_y += platform.cursor_dy * view_s;
+ }
- 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];
+ view_s += .1 * platform.wheel_dy * view_s;
- if (!left && !right && !up && !down && num_events == 0) {
- p_sleep_for(1);
- continue;
- }
+ 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);
- if (platform.key_pressed['\n'])
- p = (p == 1 ? 4 : 1);
+ i64 n = 0;
- if (platform.key_pressed[KEY_ESCAPE]) {
- view_x = 0.;
- view_y = 0.;
- view_s = 1.;
- }
+ for (; x * x + y * y < radius * radius && n < limit; ++n) {
+ f64 z = x * x - y * y;
+ y = 2. * x * y + cy;
+ x = z + cx;
+ }
- f64 d = (platform.key_down[MOD_CTRL] ? .00005 : .001) * view_s * time_elapsed;
+ u32 c;
- 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 (n == limit)
+ c = 0;
+ else
+ c = 0xffffff - n * 8 - n * 256 * 4;
- if (platform.key_down[BUTTON_LEFT]) {
- view_x += platform.cursor_dx * view_s;
- view_y += platform.cursor_dy * view_s;
+ for (i32 jj = 0; jj < p; ++jj)
+ for (i32 ii = 0; ii < p; ++ii)
+ platform.pixels[(j + jj) * platform.frame_width + (i + ii)] = c;
}
- view_s += .1 * 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);
-
- i64 n = 0;
-
- for (; x * x + y * y < radius * radius && n < limit; ++n) {
- f64 z = x * x - y * y;
- y = 2. * x * y + cy;
- x = z + cx;
- }
+ p_render_frame();
+}
- u32 c;
+i32 main(i32 argc, c8 **argv) {
+ (void) argc;
+ (void) argv;
- if (n == limit)
- c = 0;
- else
- c = 0xffffff - n * 8 - n * 256 * 4;
+ platform = (Platform) {
+ .title = "Julia Set",
+ .frame_width = 960,
+ .frame_height = 720,
+ };
- for (i32 jj = 0; jj < p; ++jj)
- for (i32 ii = 0; ii < p; ++ii)
- platform.pixels[(j + jj) * platform.frame_width + (i + ii)] = c;
- }
+ time_0 = p_time();
- p_render_frame();
- }
+ p_event_loop();
- p_cleanup();
return 0;
}
diff --git a/examples/labyrinth.c b/examples/labyrinth.c
index 2c34054..ef0e96d 100755
--- a/examples/labyrinth.c
+++ b/examples/labyrinth.c
@@ -27,6 +27,12 @@ exit $? # */
#include "../reduced_system_layer.c"
+void update_and_render_frame(void) {
+ p_handle_events();
+ p_render_frame();
+ p_sleep_for(0);
+}
+
i32 main(i32 argc, c8 **argv) {
(void) argc;
(void) argv;
@@ -37,14 +43,7 @@ i32 main(i32 argc, c8 **argv) {
.frame_height = 720,
};
- p_init();
-
- while (!platform.done) {
- p_handle_events();
- p_render_frame();
- p_sleep_for(0);
- }
+ p_event_loop();
- p_cleanup();
return 0;
}
diff --git a/examples/landscape.c b/examples/landscape.c
index 4690875..94b868f 100755
--- a/examples/landscape.c
+++ b/examples/landscape.c
@@ -25,7 +25,7 @@ gcc \
exit $? # */
#endif
-#include "../reduced_system_layer.c"
+#include "../graphics.c"
#define EPS 1e-8
@@ -63,6 +63,8 @@ typedef struct {
} Screen_Area;
typedef struct {
+ u32 background;
+ i64 system_time;
i64 time;
i32 map_num_x;
i32 map_num_y;
@@ -209,87 +211,59 @@ Vec3 screen_to_world(i32 x, i32 y) {
return add(world.eye_position, r);
}
-i64 time_milliseconds() {
- struct timespec t;
- timespec_get(&t, TIME_UTC);
+void update_and_render_frame(void) {
+ p_handle_events();
- return (i64) t.tv_sec * 1000ll + (i64) t.tv_nsec / 1000000ll;
-}
+ i64 time_elapsed = p_time() - world.system_time;
+ world.system_time += time_elapsed;
+
+ resolve_axes();
+
+ if (time_elapsed > 0) {
+ if (platform.key_down['w'])
+ world.tilt += .001 * time_elapsed;
+ if (platform.key_down['s'])
+ world.tilt -= .001 * time_elapsed;
+
+ f64 k = world.eye_position.z;
+ if (world.zoom > EPS)
+ k /= world.zoom;
+
+ if (platform.wheel_dy != 0)
+ world.eye_position.z -= (f64) platform.wheel_dy;
+
+ if (platform.key_down[BUTTON_RIGHT]) {
+ world.eye_position.x -= platform.cursor_dx * k;
+ world.eye_position.y += platform.cursor_dy * k;
+ }
-u32 u32_from_rgb(f32 red, f32 green, f32 blue) {
- i32 r = (i32) floor(red * 255.f);
- i32 g = (i32) floor(green * 255.f);
- i32 b = (i32) floor(blue * 255.f);
+ world.time += time_elapsed;
+ }
- if (r < 0) r = 0;
- if (r > 255) r = 255;
- if (g < 0) g = 0;
- if (g > 255) g = 255;
- if (b < 0) b = 0;
- if (b > 255) b = 255;
+ 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] = world.background;
- return (r << 16) | (g << 8) | b;
+ p_render_frame();
}
i32 main(i32 argc, c8 **argv) {
(void) argc;
(void) argv;
- u32 background = u32_from_rgb(.03f, .08f, .01f);
-
platform = (Platform) {
.title = "Landscape",
.frame_width = 960,
.frame_height = 720,
};
- p_init();
-
+ world.background = u32_from_rgb(.03f, .08f, .01f);
+ world.system_time = p_time();
world.screen_distance = 1.;
world.zoom = 1e+3;
+ world.eye_position = (Vec3) { .z = 10., };
- world.eye_position = (Vec3) {
- .z = 10.,
- };
-
- i64 time_0 = time_milliseconds();
-
- while (!platform.done) {
- p_handle_events();
-
- i64 time_elapsed = time_milliseconds() - time_0;
- time_0 += time_elapsed;
-
- resolve_axes();
-
- if (time_elapsed > 0) {
- if (platform.key_down['w'])
- world.tilt += .001 * time_elapsed;
- if (platform.key_down['s'])
- world.tilt -= .001 * time_elapsed;
-
- f64 k = world.eye_position.z;
- if (world.zoom > EPS)
- k /= world.zoom;
-
- if (platform.wheel_dy != 0)
- world.eye_position.z -= (f64) platform.wheel_dy;
-
- if (platform.key_down[BUTTON_RIGHT]) {
- world.eye_position.x -= platform.cursor_dx * k;
- world.eye_position.y += platform.cursor_dy * k;
- }
-
- 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;
-
- p_render_frame();
- }
+ p_event_loop();
- p_cleanup();
return 0;
}
diff --git a/reduced_system_layer.c b/reduced_system_layer.c
index b7b0d5a..211cf1b 100755
--- a/reduced_system_layer.c
+++ b/reduced_system_layer.c
@@ -112,6 +112,7 @@ typedef double f64;
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
+#include <stdlib.h>
#include <math.h>
#endif