summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorSchark <jordan@schark.online>2023-05-11 21:17:16 -0700
committerSchark <jordan@schark.online>2023-05-11 21:17:16 -0700
commit1d21cae59d8f5670eefee331c91b5a3250f40fb2 (patch)
treec0a96b7df08898fa28fe0ec2875c13c7c04e6289 /src/main.c
parent70d9a26e5e80927f1c3f4b178b17037a7f6311e9 (diff)
downloadgamedev-1d21cae59d8f5670eefee331c91b5a3250f40fb2.tar.gz
gamedev-1d21cae59d8f5670eefee331c91b5a3250f40fb2.zip
Added basic fog effect
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c44
1 files changed, 32 insertions, 12 deletions
diff --git a/src/main.c b/src/main.c
index 6e0b7d0..51fb50a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
+ {1, 0, 0, 0, 0, 0, 0, 0, 0, 0,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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);