diff options
author | Schark <jordan@schark.online> | 2023-05-11 21:17:16 -0700 |
---|---|---|
committer | Schark <jordan@schark.online> | 2023-05-11 21:17:16 -0700 |
commit | 1d21cae59d8f5670eefee331c91b5a3250f40fb2 (patch) | |
tree | c0a96b7df08898fa28fe0ec2875c13c7c04e6289 | |
parent | 70d9a26e5e80927f1c3f4b178b17037a7f6311e9 (diff) | |
download | gamedev-1d21cae59d8f5670eefee331c91b5a3250f40fb2.tar.gz gamedev-1d21cae59d8f5670eefee331c91b5a3250f40fb2.zip |
Added basic fog effect
-rw-r--r-- | readme | 2 | ||||
-rw-r--r-- | src/main.c | 44 |
2 files changed, 32 insertions, 14 deletions
@@ -6,8 +6,6 @@ Current Todo: - Create bounds so player cannot enter undefined areas (outer walls, inner structures) - Experiment with warp-wrapping, just for fun :) - Move FPS counter from window title to rendered in-game (low priority) - - "Render distance" to support larger maps not losing performance - - And for aesthetics, of course - Eventually clean code (move appropriate code to different files, headers) - Level editor @@ -3,19 +3,29 @@ #include <math.h> // TODO: move to implicit map definition with level editor in future -#define MAP_HEIGHT 10 -#define MAP_WIDTH 10 +#define MAP_HEIGHT 20 +#define MAP_WIDTH 20 int world_map[MAP_HEIGHT][MAP_WIDTH] = { - {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, - {1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, - {1, 0, 0, 0, 0, 0, 2, 2, 0, 1}, - {1, 0, 0, 0, 0, 0, 2, 2, 0, 1}, - {1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, - {1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, - {1, 0, 3, 3, 0, 0, 4, 4, 0, 1}, - {1, 0, 3, 3, 0, 0, 4, 4, 0, 1}, - {1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, - {1, 1, 1, 1, 1, 1, 1, 1, 1, 1} + {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + {1, 0, 0, 0,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, 4, 4, 4, 0, 1}, + {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, + {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, }; // TODO: move player code to dedicated file after complication demands it @@ -98,6 +108,11 @@ void raycast(Player *player) { double perp_wall_dist; if (side == 0 || side == 2) { perp_wall_dist = (map_x - player->pos_x + (1 - step_x) / 2) / ray_dir_x; } else { perp_wall_dist = (map_y - player->pos_y + (1 - step_y) / 2) / ray_dir_y; } + + // fog effect + float fog_distance = 20.0f; + float scale_factor = 1.0f - perp_wall_dist / fog_distance; + if (scale_factor < 0.0f) { scale_factor = 0.0f; } // calculate height of line on screen int line_height = (int)(screen_height / perp_wall_dist); @@ -130,6 +145,11 @@ void raycast(Player *player) { color.b /= 4; } + // scale brightness in accordance with distance (for fog) + color.r *= scale_factor; + color.g *= scale_factor; + color.b *= scale_factor; + // TODO: recommended to put this in its own function or something at some point, // as it deals with library calls outside of this function's scope glBegin(GL_LINES); |