From 15c47e36ac516e739183c5337f3c6a4cbb6ad1c5 Mon Sep 17 00:00:00 2001 From: whaffman Date: Tue, 6 May 2025 14:45:46 +0200 Subject: [PATCH] some norm and background --- inc/types.h | 7 +- src/collision.c | 7 +- src/game.c | 16 ++--- src/main.c | 6 +- src/player.c | 26 ++++--- src/render/DDAscratch.c | 148 ++++++++++++++++++++-------------------- src/screen.c | 42 +++++++++--- test.cub | 17 ++--- 8 files changed, 144 insertions(+), 125 deletions(-) diff --git a/inc/types.h b/inc/types.h index 29b85b8..9d942dd 100644 --- a/inc/types.h +++ b/inc/types.h @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* 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_image_t *img; mlx_image_t *minimap; - unsigned int width; + mlx_image_t *background; + unsigned int width; unsigned int height; } t_screen; @@ -91,7 +92,7 @@ typedef struct s_render { float perp_dist; t_side side; - float wall_x; + float wall_x; } t_render; typedef struct s_game diff --git a/src/collision.c b/src/collision.c index 322c4df..6275b6b 100644 --- a/src/collision.c +++ b/src/collision.c @@ -6,18 +6,17 @@ /* By: whaffman +#+ */ /* +#+ */ /* 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" - int collision_horizontal(t_map *map, t_player *player, float xa) { 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); } @@ -25,6 +24,6 @@ int collision_vertical(t_map *map, t_player *player, float ya) { 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); } diff --git a/src/game.c b/src/game.c index 3128a65..56ed656 100644 --- a/src/game.c +++ b/src/game.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* 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); } -void free_game(t_game **game) +void free_game(t_game **game) { if (game && *game) { @@ -40,10 +40,10 @@ void free_game(t_game **game) void game_loop(void *param) { - t_game *game; - float delta_time; - static int framecount = 0; - static int fps = 0; + t_game *game; + float delta_time; + static int framecount = 0; + static int fps = 0; framecount++; game = (t_game *)param; @@ -51,7 +51,7 @@ void game_loop(void *param) fps += (int)(1.f / delta_time); if (framecount % 20 == 0) { - fprintf(stderr, "FPS: %d\n", fps/20); + fprintf(stderr, "FPS: %d\n", fps / 20); fps = 0; } render_clear(game->screen); @@ -59,7 +59,7 @@ void game_loop(void *param) cast_rays(game); render_map(game->screen, game->map); render_entities(game); - keyboard_update(game); // Goes last + keyboard_update(game); } void game_free(t_game *game) diff --git a/src/main.c b/src/main.c index 3fbb717..592b56a 100644 --- a/src/main.c +++ b/src/main.c @@ -6,11 +6,11 @@ /* 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) { @@ -30,4 +30,4 @@ int main(int argc, char **argv) mlx_loop(game->screen->mlx); game_terminate(game); return (EXIT_SUCCESS); -} \ No newline at end of file +} diff --git a/src/player.c b/src/player.c index 8e262c6..e14ccab 100644 --- a/src/player.c +++ b/src/player.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* 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); } - static void move(t_map *map, t_player *player, int dir, float delta) { 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; 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; - if ( ya != 0 && collision_vertical(map, player, ya)) + if (ya != 0 && collision_vertical(map, player, ya)) player->pos.y += ya; } + static void strave(t_map *map, t_player *player, int dir, float delta) { 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; 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; - if ( ya != 0 && collision_vertical(map, player, ya)) + if (ya != 0 && collision_vertical(map, player, ya)) player->pos.y += ya; } 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->camera = rot(player->camera, rot_speed); } @@ -88,9 +83,12 @@ void player_render(t_screen *screen, t_player *player) { 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 ; render_circle(screen, mul(player->pos, TILE_SIZE), 4, 0x111111ff); - direction = add(mul(player->pos,TILE_SIZE), mul(player->dir, TILE_SIZE)); - render_line(screen, mul(player->pos,TILE_SIZE), direction, 0xa83232ff); + direction = add(mul(player->pos, TILE_SIZE), mul(player->dir, TILE_SIZE)); + render_line(screen, mul(player->pos, TILE_SIZE), direction, 0xa83232ff); } diff --git a/src/render/DDAscratch.c b/src/render/DDAscratch.c index 8e329ed..fe973d2 100644 --- a/src/render/DDAscratch.c +++ b/src/render/DDAscratch.c @@ -6,7 +6,7 @@ /* 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 -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 - + (ray_dir.x != 0) * fabs(1 / ray_dir.x); - delta_dist.y = (ray_dir.y == 0) * 1e30 - + (ray_dir.y != 0) * fabs(1 / ray_dir.y); + delta_dist.x = (ray_dir.x == 0) * 1e30 + + (ray_dir.x != 0) * fabs(1 / ray_dir.x); + delta_dist.y = (ray_dir.y == 0) * 1e30 + + (ray_dir.y != 0) * fabs(1 / ray_dir.y); 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.y = 2 * (ray_dir.y >= 0) - 1; 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 int raydir_x_pos = (ray_dir.x >= 0); - const int raydir_y_pos = (ray_dir.y >= 0); - t_vec2 side_dist; + 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_y_pos = (ray_dir.y >= 0); + t_vec2 side_dist; - side_dist.x = ((1 - raydir_x_pos) * frac_pos.x - + raydir_x_pos * (1 - frac_pos.x)) * delta_dist.x; - side_dist.y = ((1 - raydir_y_pos) * frac_pos.y - + raydir_y_pos * (1 - frac_pos.y)) * delta_dist.y; + side_dist.x = ((1 - raydir_x_pos) * frac_pos.x + + raydir_x_pos * (1 - frac_pos.x)) * delta_dist.x; + side_dist.y = ((1 - raydir_y_pos) * frac_pos.y + + raydir_y_pos * (1 - frac_pos.y)) * delta_dist.y; return (side_dist); } - -#include -int DDA_main(t_vec2 ray_dir, t_vec2_int map_pos, t_vec2 *side_dist, t_map *map) +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 step = get_step(ray_dir); - int side; - int hit; + const t_vec2 delta_dist = get_delta_dist(ray_dir); + const t_vec2 step = get_step(ray_dir); + int side; + int hit; 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; map_pos.x += step.x * (1 - 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 || map_pos.y < 0 || map_pos.y >= map->height) 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); } -// Returns the distance to the wall hit if the wall is hit in y direction the result is -// negative, if the wall is hit in x direction the result is positive -float DDA(t_vec2 ray_dir, t_vec2 pos, t_map *map) +// Returns the distance to the wall hit if the wall is hit in y +// direction the result is negative, if the wall is hit +// 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); - t_vec2 side_dist; - int side; + const t_vec2 delta_dist = get_delta_dist(ray_dir); + t_vec2 side_dist; + int side; side_dist = get_side_dist(ray_dir, pos, delta_dist); - side = DDA_main(ray_dir, - (t_vec2_int){(int)pos.x, (int)pos.y}, - &side_dist, - map); - return ((1 - side) * (side_dist.x - delta_dist.x) - - side * (side_dist.y - delta_dist.y)); + side = dda_main(ray_dir, + (t_vec2_int){(int)pos.x, (int)pos.y}, + &side_dist, + map); + return ((1 - side) * (side_dist.x - delta_dist.x) + - 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) return (SIDE_EAST); @@ -109,64 +107,66 @@ 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 pos; - t_render render; - float perp_dist; + t_vec2 ray_dir; + t_vec2 pos; + t_render render; + float perp_dist; - ray_dir = add(game->player->dir, - mul(game->player->camera, - (2.0f * x / (float)game->screen->width - 1))); + ray_dir = add(game->player->dir, + mul(game->player->camera, + (2.0f * x / (float)game->screen->width - 1))); 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.side = get_side(ray_dir, 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); + render.wall_x = (perp_dist > 0) * (pos.x + ray_dir.x * perp_dist) + + (perp_dist <= 0) * (pos.y + ray_dir.y * perp_dist); render.wall_x -= floor(render.wall_x); return (render); } - -unsigned int get_color(t_render render) +unsigned int get_color(t_render render) { - float dist; - //int alpha; - const unsigned int color[4] = { + float dist; + const unsigned int color[4] = { 0x488B49, 0x4AAD52, 0x6EB257, 0xC5E063 }; - dist = (render.perp_dist == 0) * 1e30 - + (render.perp_dist > 1) * render.perp_dist - + (render.perp_dist <= 1) * 1; - //alpha = (int)(1.0 / dist * 255); + dist = (render.perp_dist == 0) * 1e30 + + (render.perp_dist > 1) * render.perp_dist + + (render.perp_dist <= 1) * 1; return (color[render.side] << 8 |(int)(1.0 / dist * 255)); } + void draw_line(t_game *game, t_render render, int x) { - int y; - int color; - int lineHeight; - int drawStart; - int drawEnd; + int y; + int color; + int lineHeight; + int drawStart; + int drawEnd; y = 0; lineHeight = (int)(game->screen->height / render.perp_dist); drawStart = -lineHeight / 2 + game->screen->height / 2; - if(drawStart < 0) drawStart = 0; + if (drawStart < 0) + drawStart = 0; 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) { 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) - 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 color = get_color(render); 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; - t_render render; + int x; + t_render render; x = 0; while (x < game->screen->width) { render = cast_ray(game, x); draw_line(game, render, x); - //ETC - x++; } -} \ No newline at end of file +} diff --git a/src/screen.c b/src/screen.c index 7974801..ab04d56 100644 --- a/src/screen.c +++ b/src/screen.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* 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) { t_screen *screen; - mlx_image_t *img; - mlx_image_t *minimap; mlx_t *mlx; screen = malloc(sizeof(t_screen)); @@ -27,20 +25,38 @@ int screen_create(t_game **game) mlx = mlx_init(WIDTH, HEIGHT, TITLE, false); if (!mlx) return (FAILURE); - //TODO: figure out why errno = 11 after this call screen->mlx = mlx; - img = mlx_new_image(screen->mlx, WIDTH, HEIGHT); - if (!img) + screen->img = mlx_new_image(screen->mlx, WIDTH, HEIGHT); + if (!screen->img) return (FAILURE); - minimap = mlx_new_image(screen->mlx, WIDTH , HEIGHT ); - if (!minimap) + screen->minimap = mlx_new_image(screen->mlx, WIDTH, HEIGHT); + if (!screen->minimap) + return (FAILURE); + screen->background = mlx_new_image(screen->mlx, WIDTH, HEIGHT); + if (!screen->background) return (FAILURE); - screen->img = img; - screen->minimap = minimap; (*game)->screen = screen; 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 m_width; @@ -48,6 +64,12 @@ int screen_display(t_screen *screen) m_width = 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) { printf(RED"Failed to display buffer image\n"RESET); diff --git a/test.cub b/test.cub index 9b8b34e..bbd05dd 100644 --- a/test.cub +++ b/test.cub @@ -22,12 +22,13 @@ C 100,100,200 1000001110001000000000000001111100000001000000001 100W000000001111111111100000000000111111000000001 1000000W00001 1000011111111 1000001001 -1000011111001 100001 1111111001 -100001 1001 1000011111111 1001 -111111 1001 1000000000001 1001 - 1001 1111111111111 1001 - 1001 1001 - 1001 1001 - 1001111111111111111111111111111111111001 +1000011111001 111111100001 1111111111110001 +100001 1001 1000000000011111111000010111 10001 +111111 1001 1000001000000000001000000101 100001 + 1001 1000001111111111111000010001 1000001 + 1001 1000000000000000000001110001 100001 + 1001 1000000000000000000011 11111 100001 + 10011111111100111111111111111111001110001 1000000000000000000000000000000000000001 - 1111111111111111111111111111111111111111 \ No newline at end of file + 11111100111111101100111100011101011111111 + 1111 1111111 11111 11111 \ No newline at end of file