some norm and background
This commit is contained in:
parent
fba7312770
commit
15c47e36ac
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* 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,6 +75,7 @@ typedef struct s_screen
|
||||
mlx_t *mlx;
|
||||
mlx_image_t *img;
|
||||
mlx_image_t *minimap;
|
||||
mlx_image_t *background;
|
||||
unsigned int width;
|
||||
unsigned int height;
|
||||
} t_screen;
|
||||
|
||||
@ -6,13 +6,12 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* 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;
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
14
src/player.c
14
src/player.c
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* 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;
|
||||
@ -44,6 +43,7 @@ static void move(t_map *map, t_player *player, int dir, float delta)
|
||||
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;
|
||||
@ -59,11 +59,6 @@ static void strave(t_map *map, t_player *player, int dir, float delta)
|
||||
|
||||
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,7 +83,10 @@ 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));
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -48,9 +48,7 @@ t_vec2 get_side_dist(t_vec2 ray_dir, t_vec2 pos, t_vec2 delta_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 step = get_step(ray_dir);
|
||||
@ -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,16 +71,17 @@ 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;
|
||||
|
||||
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},
|
||||
&side_dist,
|
||||
map);
|
||||
@ -120,7 +118,7 @@ t_render cast_ray(t_game *game, int x)
|
||||
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)
|
||||
@ -129,11 +127,9 @@ t_render cast_ray(t_game *game, int x)
|
||||
return (render);
|
||||
}
|
||||
|
||||
|
||||
unsigned int get_color(t_render render)
|
||||
{
|
||||
float dist;
|
||||
//int alpha;
|
||||
const unsigned int color[4] = {
|
||||
0x488B49,
|
||||
0x4AAD52,
|
||||
@ -144,9 +140,9 @@ unsigned int get_color(t_render render)
|
||||
dist = (render.perp_dist == 0) * 1e30
|
||||
+ (render.perp_dist > 1) * render.perp_dist
|
||||
+ (render.perp_dist <= 1) * 1;
|
||||
//alpha = (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)
|
||||
{
|
||||
int y;
|
||||
@ -158,15 +154,19 @@ void draw_line(t_game *game, t_render render, int x)
|
||||
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);
|
||||
@ -184,8 +184,6 @@ void cast_rays(t_game *game)
|
||||
{
|
||||
render = cast_ray(game, x);
|
||||
draw_line(game, render, x);
|
||||
//ETC
|
||||
|
||||
x++;
|
||||
}
|
||||
}
|
||||
42
src/screen.c
42
src/screen.c
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* 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);
|
||||
|
||||
17
test.cub
17
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
|
||||
11111100111111101100111100011101011111111
|
||||
1111 1111111 11111 11111
|
||||
Loading…
Reference in New Issue
Block a user