first draw

This commit is contained in:
Willem Haffmans 2025-05-04 13:45:48 +02:00
parent 5a5d2cff5b
commit 540e9acf63
7 changed files with 80 additions and 27 deletions

View File

@ -3,10 +3,10 @@
# :::::::: # # :::::::: #
# Makefile :+: :+: # # Makefile :+: :+: #
# +:+ # # +:+ #
# By: qmennen <qmennen@student.codam.nl> +#+ # # By: whaffman <whaffman@student.codam.nl> +#+ #
# +#+ # # +#+ #
# Created: 2024/10/15 11:48:46 by whaffman #+# #+# # # Created: 2024/10/15 11:48:46 by whaffman #+# #+# #
# Updated: 2025/04/19 14:50:25 by whaffman ######## odam.nl # # Updated: 2025/05/04 13:30:55 by whaffman ######## odam.nl #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -27,7 +27,7 @@ MLX42_PATH = $(LIB_PATH)/MLX42
MLX42_INC_PATH = $(MLX42_PATH)/include/MLX42 MLX42_INC_PATH = $(MLX42_PATH)/include/MLX42
MLX42 = $(MLX42_PATH)/build/libmlx42.a MLX42 = $(MLX42_PATH)/build/libmlx42.a
CC = cc CC = clang
RM = rm -rf RM = rm -rf
INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH) -I./$(MLX42_INC_PATH) INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH) -I./$(MLX42_INC_PATH)

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */ /* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */
/* Updated: 2025/05/02 16:59:28 by whaffman ######## odam.nl */ /* Updated: 2025/05/04 13:35:12 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,7 +31,7 @@
# define WHITE "\033[0;37m" # define WHITE "\033[0;37m"
# define NUM_KEYS 256 # define NUM_KEYS 256
# define TILE_SIZE 16 # define TILE_SIZE 1
# include "MLX42.h" # include "MLX42.h"
# include "allowed.h" # include "allowed.h"

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* :::::::: */
/* render.h :+: :+: :+: */ /* render.h :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+ */
/* Created: 2025/04/15 16:28:16 by qmennen #+# #+# */ /* Created: 2025/04/15 16:28:16 by qmennen #+# #+# */
/* Updated: 2025/04/17 20:08:08 by qmennen ### ########.fr */ /* Updated: 2025/05/04 13:21:04 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,5 +21,7 @@ void render_circle(t_screen *screen, t_vec2 point, int radius, unsigned int colo
void render_clear(t_screen *screen); void render_clear(t_screen *screen);
void render_entities(t_game *game); void render_entities(t_game *game);
void render_map(t_screen *screen, t_map *map); void render_map(t_screen *screen, t_map *map);
void cast_rays(t_game *game);
#endif #endif

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/02 18:06:18 by whaffman ######## odam.nl */ /* Updated: 2025/05/04 13:17:15 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -60,8 +60,8 @@ typedef struct s_map
char *SO_texture; char *SO_texture;
char *WE_texture; char *WE_texture;
char *EA_texture; char *EA_texture;
int floor_color; unsigned int floor_color;
int ceiling_color; unsigned int ceiling_color;
} t_map; } t_map;
typedef struct s_keyboard typedef struct s_keyboard

View File

@ -3,10 +3,10 @@
/* :::::::: */ /* :::::::: */
/* game.c :+: :+: */ /* game.c :+: :+: */
/* +:+ */ /* +:+ */
/* By: qmennen <qmennen@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/04/23 11:57:06 by whaffman ######## odam.nl */ /* Updated: 2025/05/04 13:21:37 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -47,8 +47,9 @@ void game_loop(void *param)
delta_time = game->screen->mlx->delta_time; delta_time = game->screen->mlx->delta_time;
render_clear(game->screen); render_clear(game->screen);
player_update(game, delta_time); player_update(game, delta_time);
render_entities(game); // render_entities(game);
render_map(game->screen, game->map); // render_map(game->screen, game->map);
cast_rays(game);
keyboard_update(game); // Goes last keyboard_update(game); // Goes last
} }

View File

@ -3,10 +3,10 @@
/* :::::::: */ /* :::::::: */
/* player.c :+: :+: */ /* player.c :+: :+: */
/* +:+ */ /* +:+ */
/* By: qmennen <qmennen@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/04/25 13:54:11 by whaffman ######## odam.nl */ /* Updated: 2025/05/04 13:39:49 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,13 +19,13 @@ int player_create(t_game **game)
player = malloc(sizeof(t_player)); player = malloc(sizeof(t_player));
if (!player) if (!player)
return (FAILURE); return (FAILURE);
player->pos.x = 2 * TILE_SIZE; player->pos.x = 0;
player->pos.y = 2 * TILE_SIZE; player->pos.y = 0;
player->dir.x = 1; player->dir.x = 1;
player->dir.y = 0; player->dir.y = 0;
player->camera.x = 0; player->camera.x = 0;
player->camera.y = 0.66f; player->camera.y = 0.66f;
player->speed = 80.f; player->speed = 1.f;
player->fov = 90.f; player->fov = 90.f;
(*game)->player = player; (*game)->player = player;
return (SUCCESS); return (SUCCESS);

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/02 20:29:40 by whaffman ######## odam.nl */ /* Updated: 2025/05/04 13:38:50 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -48,6 +48,8 @@ t_vec2 get_side_dist(t_vec2 ray_dir, t_vec2 pos, t_vec2 delta_dist)
return (side_dist); return (side_dist);
} }
#include <stdio.h>
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 delta_dist = get_delta_dist(ray_dir);
@ -63,6 +65,10 @@ 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
|| map_pos.y < 0 || map_pos.y >= map->height)
printf("Out of bounds: %d %d\n", map_pos.x, map_pos.y);
hit = (map->grid[map_pos.y][map_pos.x] == TILE_WALL); hit = (map->grid[map_pos.y][map_pos.x] == TILE_WALL);
} }
return (side); return (side);
@ -123,15 +129,59 @@ t_render cast_ray(t_game *game, int x)
return (render); return (render);
} }
unsigned int get_color(t_render render)
{
float dist;
const unsigned int color[4] = {
0x488B49,
0x4AAD52,
0x6EB257,
0xC5E063
};
dist = (render.perp_dist == 0) * 1e30
+ (render.perp_dist != 0) * render.perp_dist;
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;
y = 0;
lineHeight = (int)(game->screen->height / render.perp_dist);
drawStart = -lineHeight / 2 + game->screen->height / 2;
if(drawStart < 0) drawStart = 0;
drawEnd = lineHeight / 2 + game->screen->height / 2;
if(drawEnd >= game->screen->height) drawEnd = game->screen->height - 1;
while (y < game->screen->height)
{
if (y < drawStart)
color = game->map->ceiling_color << 8 | 0xFF;
else if (y > drawEnd)
color = game->map->floor_color << 8 | 0xFF;
else
color = get_color(render);
mlx_put_pixel(game->screen->img, x, y, color);
y++;
}
}
void cast_rays(t_game *game) void cast_rays(t_game *game)
{ {
int x; int x;
t_render render[WIDTH]; t_render render;
x = 0; x = 0;
while (x < game->screen->width) while (x < game->screen->width)
{ {
render[x] = cast_ray(game, x); render = cast_ray(game, x);
(void)render;
draw_line(game, render, x);
//ETC //ETC
x++; x++;