summaryrefslogtreecommitdiff
path: root/examples/landscape.c
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 /examples/landscape.c
parent48c5d9b83afd1cd56e73449aebaf461cb48360d9 (diff)
downloadreduced_system_layer-aaa61a3eeb5b1ade783b5a7ec31a6a7fd6b3393e.zip
Update examples
Diffstat (limited to 'examples/landscape.c')
-rwxr-xr-xexamples/landscape.c100
1 files changed, 37 insertions, 63 deletions
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;
}