some norm and background

This commit is contained in:
whaffman 2025-05-06 14:45:46 +02:00
parent fba7312770
commit 15c47e36ac
8 changed files with 144 additions and 125 deletions

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */ /* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */
/* Updated: 2025/05/04 16:53:48 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 13:58:16 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -75,7 +75,8 @@ typedef struct s_screen
mlx_t *mlx; mlx_t *mlx;
mlx_image_t *img; mlx_image_t *img;
mlx_image_t *minimap; mlx_image_t *minimap;
unsigned int width; mlx_image_t *background;
unsigned int width;
unsigned int height; unsigned int height;
} t_screen; } t_screen;

View File

@ -6,18 +6,17 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/04/22 14:40:59 by qmennen #+# #+# */ /* Created: 2025/04/22 14:40:59 by qmennen #+# #+# */
/* Updated: 2025/05/04 14:51:40 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 14:18:07 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "collision.h" #include "collision.h"
int collision_horizontal(t_map *map, t_player *player, float xa) int collision_horizontal(t_map *map, t_player *player, float xa)
{ {
t_tile tile; t_tile tile;
tile = get_tile(map, (int) ((player->pos.x + xa)), (int) ((player->pos.y))); tile = get_tile(map, (int)((player->pos.x + xa)), (int)((player->pos.y)));
return (tile != TILE_WALL && tile != TILE_VOID); return (tile != TILE_WALL && tile != TILE_VOID);
} }
@ -25,6 +24,6 @@ int collision_vertical(t_map *map, t_player *player, float ya)
{ {
t_tile tile; t_tile tile;
tile = get_tile(map, (int) ((player->pos.x)), (int) ((player->pos.y + ya))); tile = get_tile(map, (int)((player->pos.x)), (int)((player->pos.y + ya)));
return (tile != TILE_WALL && tile != TILE_VOID); return (tile != TILE_WALL && tile != TILE_VOID);
} }

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */ /* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */
/* Updated: 2025/05/04 16:44:45 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 14:18:50 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,7 +24,7 @@ int game_create(t_game **game)
return (SUCCESS); return (SUCCESS);
} }
void free_game(t_game **game) void free_game(t_game **game)
{ {
if (game && *game) if (game && *game)
{ {
@ -40,10 +40,10 @@ void free_game(t_game **game)
void game_loop(void *param) void game_loop(void *param)
{ {
t_game *game; t_game *game;
float delta_time; float delta_time;
static int framecount = 0; static int framecount = 0;
static int fps = 0; static int fps = 0;
framecount++; framecount++;
game = (t_game *)param; game = (t_game *)param;
@ -51,7 +51,7 @@ void game_loop(void *param)
fps += (int)(1.f / delta_time); fps += (int)(1.f / delta_time);
if (framecount % 20 == 0) if (framecount % 20 == 0)
{ {
fprintf(stderr, "FPS: %d\n", fps/20); fprintf(stderr, "FPS: %d\n", fps / 20);
fps = 0; fps = 0;
} }
render_clear(game->screen); render_clear(game->screen);
@ -59,7 +59,7 @@ void game_loop(void *param)
cast_rays(game); cast_rays(game);
render_map(game->screen, game->map); render_map(game->screen, game->map);
render_entities(game); render_entities(game);
keyboard_update(game); // Goes last keyboard_update(game);
} }
void game_free(t_game *game) void game_free(t_game *game)

View File

@ -6,11 +6,11 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/04/15 16:01:29 by qmennen #+# #+# */ /* Created: 2025/04/15 16:01:29 by qmennen #+# #+# */
/* Updated: 2025/04/24 11:39:05 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 14:17:33 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
# include "cub3d.h" #include "cub3d.h"
int main(int argc, char **argv) int main(int argc, char **argv)
{ {

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/04/15 18:53:19 by qmennen #+# #+# */ /* Created: 2025/04/15 18:53:19 by qmennen #+# #+# */
/* Updated: 2025/05/04 16:58:40 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 14:20:37 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,7 +31,6 @@ int player_create(t_game **game)
return (SUCCESS); return (SUCCESS);
} }
static void move(t_map *map, t_player *player, int dir, float delta) static void move(t_map *map, t_player *player, int dir, float delta)
{ {
float xa; float xa;
@ -39,11 +38,12 @@ static void move(t_map *map, t_player *player, int dir, float delta)
xa = dir * player->dir.x * player->speed * delta; xa = dir * player->dir.x * player->speed * delta;
ya = dir * player->dir.y * player->speed * delta; ya = dir * player->dir.y * player->speed * delta;
if ( xa != 0 && collision_horizontal(map, player, xa)) if (xa != 0 && collision_horizontal(map, player, xa))
player->pos.x += xa; player->pos.x += xa;
if ( ya != 0 && collision_vertical(map, player, ya)) if (ya != 0 && collision_vertical(map, player, ya))
player->pos.y += ya; player->pos.y += ya;
} }
static void strave(t_map *map, t_player *player, int dir, float delta) static void strave(t_map *map, t_player *player, int dir, float delta)
{ {
float xa; float xa;
@ -51,19 +51,14 @@ static void strave(t_map *map, t_player *player, int dir, float delta)
xa = dir * perp(player->dir).x * player->speed * delta; xa = dir * perp(player->dir).x * player->speed * delta;
ya = dir * perp(player->dir).y * player->speed * delta; ya = dir * perp(player->dir).y * player->speed * delta;
if ( xa != 0 && collision_horizontal(map, player, xa)) if (xa != 0 && collision_horizontal(map, player, xa))
player->pos.x += xa; player->pos.x += xa;
if ( ya != 0 && collision_vertical(map, player, ya)) if (ya != 0 && collision_vertical(map, player, ya))
player->pos.y += ya; player->pos.y += ya;
} }
static void rotate(t_player *player, float rot_speed) static void rotate(t_player *player, float rot_speed)
{ {
// double old_x;
// // old_x = player->dir.x;
// // player->dir.x = player->dir.x * cos(rot_speed) - player->dir.y * sin(rot_speed);
// // player->dir.y = old_x * sin(rot_speed) + player->dir.y * cos(rot_speed);
player->dir = rot(player->dir, rot_speed); player->dir = rot(player->dir, rot_speed);
player->camera = rot(player->camera, rot_speed); player->camera = rot(player->camera, rot_speed);
} }
@ -88,9 +83,12 @@ void player_render(t_screen *screen, t_player *player)
{ {
t_vec2 direction; t_vec2 direction;
if (player->pos.x < 0 || player->pos.x >= screen->width || player->pos.y < 0 || player->pos.y >= screen->height) if (player->pos.x < 0
|| player->pos.x >= screen->width
|| player->pos.y < 0
|| player->pos.y >= screen->height)
return ; return ;
render_circle(screen, mul(player->pos, TILE_SIZE), 4, 0x111111ff); render_circle(screen, mul(player->pos, TILE_SIZE), 4, 0x111111ff);
direction = add(mul(player->pos,TILE_SIZE), mul(player->dir, TILE_SIZE)); direction = add(mul(player->pos, TILE_SIZE), mul(player->dir, TILE_SIZE));
render_line(screen, mul(player->pos,TILE_SIZE), direction, 0xa83232ff); render_line(screen, mul(player->pos, TILE_SIZE), direction, 0xa83232ff);
} }

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/05/02 11:58:09 by whaffman #+# #+# */ /* Created: 2025/05/02 11:58:09 by whaffman #+# #+# */
/* Updated: 2025/05/04 17:01:12 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 14:25:00 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,48 +14,46 @@
#include "vec_math.h" #include "vec_math.h"
#include <math.h> #include <math.h>
t_vec2 get_delta_dist(t_vec2 ray_dir) t_vec2 get_delta_dist(t_vec2 ray_dir)
{ {
t_vec2 delta_dist; t_vec2 delta_dist;
delta_dist.x = (ray_dir.x == 0) * 1e30 delta_dist.x = (ray_dir.x == 0) * 1e30
+ (ray_dir.x != 0) * fabs(1 / ray_dir.x); + (ray_dir.x != 0) * fabs(1 / ray_dir.x);
delta_dist.y = (ray_dir.y == 0) * 1e30 delta_dist.y = (ray_dir.y == 0) * 1e30
+ (ray_dir.y != 0) * fabs(1 / ray_dir.y); + (ray_dir.y != 0) * fabs(1 / ray_dir.y);
return (delta_dist); return (delta_dist);
} }
t_vec2 get_step(t_vec2 ray_dir) t_vec2 get_step(t_vec2 ray_dir)
{ {
t_vec2 step; t_vec2 step;
step.x = 2 * (ray_dir.x >= 0) - 1; step.x = 2 * (ray_dir.x >= 0) - 1;
step.y = 2 * (ray_dir.y >= 0) - 1; step.y = 2 * (ray_dir.y >= 0) - 1;
return (step); return (step);
} }
t_vec2 get_side_dist(t_vec2 ray_dir, t_vec2 pos, t_vec2 delta_dist) t_vec2 get_side_dist(t_vec2 ray_dir, t_vec2 pos, t_vec2 delta_dist)
{ {
const t_vec2 frac_pos = (t_vec2){pos.x - (int)pos.x, pos.y - (int)pos.y}; const t_vec2 frac_pos = (t_vec2){pos.x - (int)pos.x, pos.y - (int)pos.y};
const int raydir_x_pos = (ray_dir.x >= 0); const int raydir_x_pos = (ray_dir.x >= 0);
const int raydir_y_pos = (ray_dir.y >= 0); const int raydir_y_pos = (ray_dir.y >= 0);
t_vec2 side_dist; t_vec2 side_dist;
side_dist.x = ((1 - raydir_x_pos) * frac_pos.x side_dist.x = ((1 - raydir_x_pos) * frac_pos.x
+ raydir_x_pos * (1 - frac_pos.x)) * delta_dist.x; + raydir_x_pos * (1 - frac_pos.x)) * delta_dist.x;
side_dist.y = ((1 - raydir_y_pos) * frac_pos.y side_dist.y = ((1 - raydir_y_pos) * frac_pos.y
+ raydir_y_pos * (1 - frac_pos.y)) * delta_dist.y; + raydir_y_pos * (1 - frac_pos.y)) * delta_dist.y;
return (side_dist); return (side_dist);
} }
int dda_main(t_vec2 ray_dir, t_vec2_int map_pos, t_vec2 *side_dist, t_map *map)
#include <stdio.h>
int DDA_main(t_vec2 ray_dir, t_vec2_int map_pos, t_vec2 *side_dist, t_map *map)
{ {
const t_vec2 delta_dist = get_delta_dist(ray_dir); const t_vec2 delta_dist = get_delta_dist(ray_dir);
const t_vec2 step = get_step(ray_dir); const t_vec2 step = get_step(ray_dir);
int side; int side;
int hit; int hit;
hit = 0; hit = 0;
while (hit == 0) while (hit == 0)
@ -65,7 +63,6 @@ int DDA_main(t_vec2 ray_dir, t_vec2_int map_pos, t_vec2 *side_dist, t_map *map)
side_dist->y += delta_dist.y * side; side_dist->y += delta_dist.y * side;
map_pos.x += step.x * (1 - side); map_pos.x += step.x * (1 - side);
map_pos.y += step.y * side; map_pos.y += step.y * side;
// printf("map_pos: %d %d\n", map_pos.x, map_pos.y);
if (map_pos.x < 0 || map_pos.x >= map->width if (map_pos.x < 0 || map_pos.x >= map->width
|| map_pos.y < 0 || map_pos.y >= map->height) || map_pos.y < 0 || map_pos.y >= map->height)
printf("Out of bounds: %d %d\n", map_pos.x, map_pos.y); printf("Out of bounds: %d %d\n", map_pos.x, map_pos.y);
@ -74,26 +71,27 @@ int DDA_main(t_vec2 ray_dir, t_vec2_int map_pos, t_vec2 *side_dist, t_map *map)
return (side); return (side);
} }
// Returns the distance to the wall hit if the wall is hit in y direction the result is // Returns the distance to the wall hit if the wall is hit in y
// negative, if the wall is hit in x direction the result is positive // direction the result is negative, if the wall is hit
float DDA(t_vec2 ray_dir, t_vec2 pos, t_map *map) // in x direction the result is positive
float dda(t_vec2 ray_dir, t_vec2 pos, t_map *map)
{ {
const t_vec2 delta_dist = get_delta_dist(ray_dir); const t_vec2 delta_dist = get_delta_dist(ray_dir);
t_vec2 side_dist; t_vec2 side_dist;
int side; int side;
side_dist = get_side_dist(ray_dir, pos, delta_dist); side_dist = get_side_dist(ray_dir, pos, delta_dist);
side = DDA_main(ray_dir, side = dda_main(ray_dir,
(t_vec2_int){(int)pos.x, (int)pos.y}, (t_vec2_int){(int)pos.x, (int)pos.y},
&side_dist, &side_dist,
map); map);
return ((1 - side) * (side_dist.x - delta_dist.x) return ((1 - side) * (side_dist.x - delta_dist.x)
- side * (side_dist.y - delta_dist.y)); - side * (side_dist.y - delta_dist.y));
} }
t_side get_side(t_vec2 ray_dir, float perp_dist) t_side get_side(t_vec2 ray_dir, float perp_dist)
{ {
if(perp_dist > 0) if (perp_dist > 0)
{ {
if (ray_dir.x > 0) if (ray_dir.x > 0)
return (SIDE_EAST); return (SIDE_EAST);
@ -109,32 +107,30 @@ t_side get_side(t_vec2 ray_dir, float perp_dist)
} }
} }
t_render cast_ray(t_game *game, int x) t_render cast_ray(t_game *game, int x)
{ {
t_vec2 ray_dir; t_vec2 ray_dir;
t_vec2 pos; t_vec2 pos;
t_render render; t_render render;
float perp_dist; float perp_dist;
ray_dir = add(game->player->dir, ray_dir = add(game->player->dir,
mul(game->player->camera, mul(game->player->camera,
(2.0f * x / (float)game->screen->width - 1))); (2.0f * x / (float)game->screen->width - 1)));
pos = game->player->pos; pos = game->player->pos;
perp_dist = DDA(ray_dir, pos, game->map); perp_dist = dda(ray_dir, pos, game->map);
render.perp_dist = fabs(perp_dist); render.perp_dist = fabs(perp_dist);
render.side = get_side(ray_dir, perp_dist); render.side = get_side(ray_dir, perp_dist);
render.wall_x = (perp_dist > 0) * (pos.x + ray_dir.x * perp_dist) render.wall_x = (perp_dist > 0) * (pos.x + ray_dir.x * perp_dist)
+ (perp_dist <= 0) * (pos.y + ray_dir.y * perp_dist); + (perp_dist <= 0) * (pos.y + ray_dir.y * perp_dist);
render.wall_x -= floor(render.wall_x); render.wall_x -= floor(render.wall_x);
return (render); return (render);
} }
unsigned int get_color(t_render render)
unsigned int get_color(t_render render)
{ {
float dist; float dist;
//int alpha; const unsigned int color[4] = {
const unsigned int color[4] = {
0x488B49, 0x488B49,
0x4AAD52, 0x4AAD52,
0x6EB257, 0x6EB257,
@ -142,31 +138,35 @@ unsigned int get_color(t_render render)
}; };
dist = (render.perp_dist == 0) * 1e30 dist = (render.perp_dist == 0) * 1e30
+ (render.perp_dist > 1) * render.perp_dist + (render.perp_dist > 1) * render.perp_dist
+ (render.perp_dist <= 1) * 1; + (render.perp_dist <= 1) * 1;
//alpha = (int)(1.0 / dist * 255);
return (color[render.side] << 8 |(int)(1.0 / dist * 255)); return (color[render.side] << 8 |(int)(1.0 / dist * 255));
} }
void draw_line(t_game *game, t_render render, int x) void draw_line(t_game *game, t_render render, int x)
{ {
int y; int y;
int color; int color;
int lineHeight; int lineHeight;
int drawStart; int drawStart;
int drawEnd; int drawEnd;
y = 0; y = 0;
lineHeight = (int)(game->screen->height / render.perp_dist); lineHeight = (int)(game->screen->height / render.perp_dist);
drawStart = -lineHeight / 2 + game->screen->height / 2; drawStart = -lineHeight / 2 + game->screen->height / 2;
if(drawStart < 0) drawStart = 0; if (drawStart < 0)
drawStart = 0;
drawEnd = lineHeight / 2 + game->screen->height / 2; drawEnd = lineHeight / 2 + game->screen->height / 2;
if(drawEnd >= game->screen->height) drawEnd = game->screen->height - 1; if (drawEnd >= game->screen->height)
drawEnd = game->screen->height - 1;
while (y < game->screen->height) while (y < game->screen->height)
{ {
if (y < drawStart) if (y < drawStart)
color = game->map->ceiling_color << 8 | (int)fabs(2 * y * 0xFF /(float) game->screen->height - 0xFF); color = game->map->ceiling_color << 8
| (int)fabs(2 * y * 0xFF / (float)game->screen->height - 0xFF);
else if (y > drawEnd) else if (y > drawEnd)
color = game->map->floor_color << 8 | (int)fabs(2 * y * 0xFF /(float) game->screen->height - 0xFF); color = game->map->floor_color << 8
| (int)fabs(2 * y * 0xFF / (float)game->screen->height - 0xFF);
else else
color = get_color(render); color = get_color(render);
mlx_put_pixel(game->screen->img, x, y, color); mlx_put_pixel(game->screen->img, x, y, color);
@ -174,18 +174,16 @@ void draw_line(t_game *game, t_render render, int x)
} }
} }
void cast_rays(t_game *game) void cast_rays(t_game *game)
{ {
int x; int x;
t_render render; t_render render;
x = 0; x = 0;
while (x < game->screen->width) while (x < game->screen->width)
{ {
render = cast_ray(game, x); render = cast_ray(game, x);
draw_line(game, render, x); draw_line(game, render, x);
//ETC
x++; x++;
} }
} }

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/04/15 15:30:27 by qmennen #+# #+# */ /* Created: 2025/04/15 15:30:27 by qmennen #+# #+# */
/* Updated: 2025/05/04 16:57:06 by whaffman ######## odam.nl */ /* Updated: 2025/05/06 14:23:03 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,8 +15,6 @@
int screen_create(t_game **game) int screen_create(t_game **game)
{ {
t_screen *screen; t_screen *screen;
mlx_image_t *img;
mlx_image_t *minimap;
mlx_t *mlx; mlx_t *mlx;
screen = malloc(sizeof(t_screen)); screen = malloc(sizeof(t_screen));
@ -27,20 +25,38 @@ int screen_create(t_game **game)
mlx = mlx_init(WIDTH, HEIGHT, TITLE, false); mlx = mlx_init(WIDTH, HEIGHT, TITLE, false);
if (!mlx) if (!mlx)
return (FAILURE); return (FAILURE);
//TODO: figure out why errno = 11 after this call
screen->mlx = mlx; screen->mlx = mlx;
img = mlx_new_image(screen->mlx, WIDTH, HEIGHT); screen->img = mlx_new_image(screen->mlx, WIDTH, HEIGHT);
if (!img) if (!screen->img)
return (FAILURE); return (FAILURE);
minimap = mlx_new_image(screen->mlx, WIDTH , HEIGHT ); screen->minimap = mlx_new_image(screen->mlx, WIDTH, HEIGHT);
if (!minimap) if (!screen->minimap)
return (FAILURE);
screen->background = mlx_new_image(screen->mlx, WIDTH, HEIGHT);
if (!screen->background)
return (FAILURE); return (FAILURE);
screen->img = img;
screen->minimap = minimap;
(*game)->screen = screen; (*game)->screen = screen;
return (SUCCESS); return (SUCCESS);
} }
void fill_background(t_screen *screen, int color)
{
int i;
int j;
i = 0;
while (i < screen->width)
{
j = 0;
while (j < screen->height)
{
mlx_put_pixel(screen->background, i, j, color);
j++;
}
i++;
}
}
int screen_display(t_screen *screen) int screen_display(t_screen *screen)
{ {
int m_width; int m_width;
@ -48,6 +64,12 @@ int screen_display(t_screen *screen)
m_width = 0; m_width = 0;
m_height = 0; m_height = 0;
fill_background(screen, 0x000000FF);
if (mlx_image_to_window(screen->mlx, screen->background, 0, 0) < 0)
{
printf(RED"Failed to display buffer image\n"RESET);
return (FAILURE);
}
if (mlx_image_to_window(screen->mlx, screen->img, 0, 0) < 0) if (mlx_image_to_window(screen->mlx, screen->img, 0, 0) < 0)
{ {
printf(RED"Failed to display buffer image\n"RESET); printf(RED"Failed to display buffer image\n"RESET);

View File

@ -22,12 +22,13 @@ C 100,100,200
1000001110001000000000000001111100000001000000001 1000001110001000000000000001111100000001000000001
100W000000001111111111100000000000111111000000001 100W000000001111111111100000000000111111000000001
1000000W00001 1000011111111 1000001001 1000000W00001 1000011111111 1000001001
1000011111001 100001 1111111001 1000011111001 111111100001 1111111111110001
100001 1001 1000011111111 1001 100001 1001 1000000000011111111000010111 10001
111111 1001 1000000000001 1001 111111 1001 1000001000000000001000000101 100001
1001 1111111111111 1001 1001 1000001111111111111000010001 1000001
1001 1001 1001 1000000000000000000001110001 100001
1001 1001 1001 1000000000000000000011 11111 100001
1001111111111111111111111111111111111001 10011111111100111111111111111111001110001
1000000000000000000000000000000000000001 1000000000000000000000000000000000000001
1111111111111111111111111111111111111111 11111100111111101100111100011101011111111
1111 1111111 11111 11111