mirror of
https://codeberg.org/ACME-Corporation/cub3d.git
synced 2025-12-06 09:58:09 +01:00
Compare commits
12 commits
a18c115ef1
...
c796d44b56
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c796d44b56 | ||
|
|
cab1036359 | ||
|
|
c8da6964d0 | ||
|
|
10576f5751 | ||
|
|
2e0fd93831 | ||
|
|
9a113374a6 | ||
|
|
258372bf09 | ||
|
|
7a1e8c18df | ||
|
|
aed03e79c3 | ||
|
|
134a6e14f0 | ||
|
|
d3ba05a36d | ||
|
|
07dc80ba6a |
17 changed files with 1825 additions and 3598 deletions
2
Makefile
2
Makefile
|
|
@ -28,6 +28,8 @@ SOURCEFILES = \
|
||||||
src/raycast/ray.c \
|
src/raycast/ray.c \
|
||||||
src/raycast/walls.c \
|
src/raycast/walls.c \
|
||||||
src/renderer/render.c \
|
src/renderer/render.c \
|
||||||
|
src/sprites/sort_sprites.c \
|
||||||
|
src/sprites/sprite_caster.c \
|
||||||
|
|
||||||
OBJECTS = $(SOURCEFILES:.c=.o)
|
OBJECTS = $(SOURCEFILES:.c=.o)
|
||||||
NAME = cub3d
|
NAME = cub3d
|
||||||
|
|
|
||||||
82
ressources/box.xpm
Normal file
82
ressources/box.xpm
Normal file
|
|
@ -0,0 +1,82 @@
|
||||||
|
/* XPM */
|
||||||
|
static char * box_xpm[] = {
|
||||||
|
"64 64 15 1",
|
||||||
|
" c #FF00DC",
|
||||||
|
". c #914500",
|
||||||
|
"+ c #B2A600",
|
||||||
|
"@ c #B9B400",
|
||||||
|
"# c #A78B00",
|
||||||
|
"$ c #B5AD00",
|
||||||
|
"% c #A38100",
|
||||||
|
"& c #9A6800",
|
||||||
|
"* c #965800",
|
||||||
|
"= c #A58700",
|
||||||
|
"- c #CAD500",
|
||||||
|
"; c #BEBE00",
|
||||||
|
"> c #C7D000",
|
||||||
|
", c #C1C500",
|
||||||
|
"' c #6A3300",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
"................++@@#$..........................................",
|
||||||
|
"..............%%&&..#$............@.............................",
|
||||||
|
"..............**.....##.........................................",
|
||||||
|
"....................&+.............@$...........................",
|
||||||
|
"....................+&..............$@..........................",
|
||||||
|
"................................+&...@..........................",
|
||||||
|
"..................=-...........##....;&.........................",
|
||||||
|
"..................>,............$$##+#..........................",
|
||||||
|
"..................................##&...........................",
|
||||||
|
"''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''",
|
||||||
|
"''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''",
|
||||||
|
"''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''",
|
||||||
|
"''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''",
|
||||||
|
"''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''",
|
||||||
|
"''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''",
|
||||||
|
"''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''",
|
||||||
|
"''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''",
|
||||||
|
"''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''",
|
||||||
|
"''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''",
|
||||||
|
"''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''",
|
||||||
|
"''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''",
|
||||||
|
"''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''",
|
||||||
|
"''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''"};
|
||||||
4980
ressources/test.xpm
4980
ressources/test.xpm
File diff suppressed because it is too large
Load diff
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/07/17 14:54:36 by kcolin #+# #+# */
|
/* Created: 2025/07/17 14:54:36 by kcolin #+# #+# */
|
||||||
/* Updated: 2025/08/05 15:22:09 by kcolin ### ########.fr */
|
/* Updated: 2025/08/07 11:38:18 by tchampio ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -22,8 +22,11 @@
|
||||||
# define RESSOURCE_DIR "ressources"
|
# define RESSOURCE_DIR "ressources"
|
||||||
# define MOVEMENT_SPEED 0.000005
|
# define MOVEMENT_SPEED 0.000005
|
||||||
# define ROTATION_SPEED 0.000002
|
# define ROTATION_SPEED 0.000002
|
||||||
# define PLANE_VALUE 0.66
|
# define PLANE_VALUE 0.6
|
||||||
# define TEXTURE_SIZE 64
|
# define TEXTURE_SIZE 64
|
||||||
|
# define SPRITE_TRANPARENCY_COLOR 0xff00dc
|
||||||
|
// 4 static ones, 3 perks, the box and 25 zombies at max
|
||||||
|
# define MAX_SPRITES 30 // FIXME: Change to 30
|
||||||
# ifdef BONUS
|
# ifdef BONUS
|
||||||
# define COMPILED_TEXT "Compiled with bonuses"
|
# define COMPILED_TEXT "Compiled with bonuses"
|
||||||
# else
|
# else
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/07/17 14:59:37 by kcolin #+# #+# */
|
/* Created: 2025/07/17 14:59:37 by kcolin #+# #+# */
|
||||||
/* Updated: 2025/08/05 13:09:22 by kcolin ### ########.fr */
|
/* Updated: 2025/08/07 11:28:49 by tchampio ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -15,10 +15,12 @@
|
||||||
|
|
||||||
# include "map/mapdata.h"
|
# include "map/mapdata.h"
|
||||||
# include "draw/img_data.h"
|
# include "draw/img_data.h"
|
||||||
|
# include "sprites/sprite.h"
|
||||||
# include "player/player.h"
|
# include "player/player.h"
|
||||||
# include "utils/keypresses.h"
|
# include "utils/keypresses.h"
|
||||||
# include "consts.h"
|
# include "consts.h"
|
||||||
|
|
||||||
|
// the 4 static sprites are some of the perks and the mystery box
|
||||||
typedef struct s_cub3d_data
|
typedef struct s_cub3d_data
|
||||||
{
|
{
|
||||||
void *mlx;
|
void *mlx;
|
||||||
|
|
@ -34,6 +36,10 @@ typedef struct s_cub3d_data
|
||||||
int *screen_matrix;
|
int *screen_matrix;
|
||||||
int delta;
|
int delta;
|
||||||
int last_tick;
|
int last_tick;
|
||||||
|
t_sprite **sprite_list;
|
||||||
|
double zbuffer[WIDTH];
|
||||||
|
int sprite_order[MAX_SPRITES];
|
||||||
|
double sprite_distances[MAX_SPRITES];
|
||||||
} t_cub3d_data;
|
} t_cub3d_data;
|
||||||
|
|
||||||
#endif // CUB3D_DATA_H
|
#endif // CUB3D_DATA_H
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,21 @@
|
||||||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/07/17 14:28:56 by kcolin #+# #+# */
|
/* Created: 2025/07/17 14:28:56 by kcolin #+# #+# */
|
||||||
/* Updated: 2025/08/06 11:43:12 by kcolin ### ########.fr */
|
/* Updated: 2025/08/06 14:02:08 by tchampio ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "img_data.h"
|
#include "img_data.h"
|
||||||
#include "../consts.h"
|
#include "../consts.h"
|
||||||
|
|
||||||
|
int my_mlx_pixel_get(t_img_data *img, int x, int y)
|
||||||
|
{
|
||||||
|
char *dst;
|
||||||
|
|
||||||
|
dst = img->addr + (y * img->line_length + x * (img->bits_per_pixel / 8));
|
||||||
|
return (*(int *)dst);
|
||||||
|
}
|
||||||
|
|
||||||
void my_mlx_pixel_put(t_img_data *data, int x, int y, int color)
|
void my_mlx_pixel_put(t_img_data *data, int x, int y, int color)
|
||||||
{
|
{
|
||||||
char *dst;
|
char *dst;
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/07/17 14:30:04 by kcolin #+# #+# */
|
/* Created: 2025/07/17 14:30:04 by kcolin #+# #+# */
|
||||||
/* Updated: 2025/07/17 15:57:47 by kcolin ### ########.fr */
|
/* Updated: 2025/08/06 12:08:04 by tchampio ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -16,5 +16,6 @@
|
||||||
# include "img_data.h"
|
# include "img_data.h"
|
||||||
|
|
||||||
void my_mlx_pixel_put(t_img_data *data, int x, int y, int color);
|
void my_mlx_pixel_put(t_img_data *data, int x, int y, int color);
|
||||||
|
int my_mlx_pixel_get(t_img_data *img, int x, int y);
|
||||||
|
|
||||||
#endif // DRAWUTILS_H
|
#endif // DRAWUTILS_H
|
||||||
|
|
|
||||||
16
src/main.c
16
src/main.c
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */
|
/* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */
|
||||||
/* Updated: 2025/07/31 15:01:00 by kcolin ### ########.fr */
|
/* Updated: 2025/08/07 12:27:47 by tchampio ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -19,6 +19,9 @@
|
||||||
#include "raycast/raycaster.h"
|
#include "raycast/raycaster.h"
|
||||||
#include "renderer/render.h"
|
#include "renderer/render.h"
|
||||||
#include "raycast/ray.h"
|
#include "raycast/ray.h"
|
||||||
|
#include "sprites/sprite_caster.h"
|
||||||
|
#include "utils/inits.h"
|
||||||
|
#include "sprites/sprite.h"
|
||||||
#include "utils/hooks.h"
|
#include "utils/hooks.h"
|
||||||
#include "utils/inits.h"
|
#include "utils/inits.h"
|
||||||
#include <bits/types/struct_timeval.h>
|
#include <bits/types/struct_timeval.h>
|
||||||
|
|
@ -39,6 +42,7 @@ int game_loop(t_cub3d_data *data)
|
||||||
reset_matrix(data);
|
reset_matrix(data);
|
||||||
raycaster(data, &ray);
|
raycaster(data, &ray);
|
||||||
move_player(data);
|
move_player(data);
|
||||||
|
sprite_caster(data);
|
||||||
matrix_to_image(data);
|
matrix_to_image(data);
|
||||||
draw_map(data->map, &data->player, data->img_data);
|
draw_map(data->map, &data->player, data->img_data);
|
||||||
mlx_put_image_to_window(data->mlx, data->mlx_win,
|
mlx_put_image_to_window(data->mlx, data->mlx_win,
|
||||||
|
|
@ -56,6 +60,16 @@ int main(int argc, char **argv)
|
||||||
if (argc < 2)
|
if (argc < 2)
|
||||||
return (ft_printf("Error: Missing cub3d file\n"), 1);
|
return (ft_printf("Error: Missing cub3d file\n"), 1);
|
||||||
init_cub3d_data(&data, argv);
|
init_cub3d_data(&data, argv);
|
||||||
|
// placing a sprite next to player to ease debugging
|
||||||
|
data.sprite_list = ft_calloc(sizeof(t_sprite *), 3);
|
||||||
|
data.sprite_list[0] = ft_calloc(sizeof(t_sprite), 1);
|
||||||
|
data.sprite_list[1] = ft_calloc(sizeof(t_sprite), 1);
|
||||||
|
data.sprite_list[0]->x = data.map->startx + 1;
|
||||||
|
data.sprite_list[0]->y = data.map->starty;
|
||||||
|
data.sprite_list[0]->image = load_single_texture(&data, "ressources/box.xpm");
|
||||||
|
data.sprite_list[1]->x = data.map->startx;
|
||||||
|
data.sprite_list[1]->y = data.map->starty - 2;
|
||||||
|
data.sprite_list[1]->image = load_single_texture(&data, "ressources/test.xpm");
|
||||||
mlx_hook(data.mlx_win, KeyPress, KeyPressMask, keypress_handler, &data);
|
mlx_hook(data.mlx_win, KeyPress, KeyPressMask, keypress_handler, &data);
|
||||||
mlx_hook(data.mlx_win, KeyRelease, KeyReleaseMask,
|
mlx_hook(data.mlx_win, KeyRelease, KeyReleaseMask,
|
||||||
keyrelease_handler, &data);
|
keyrelease_handler, &data);
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/07/31 11:55:41 by kcolin #+# #+# */
|
/* Created: 2025/07/31 11:55:41 by kcolin #+# #+# */
|
||||||
/* Updated: 2025/08/06 11:33:27 by kcolin ### ########.fr */
|
/* Updated: 2025/08/06 14:02:40 by tchampio ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -109,6 +109,7 @@ void raycaster(t_cub3d_data *data, t_ray *ray)
|
||||||
calculate_wall_dist(ray, data->map);
|
calculate_wall_dist(ray, data->map);
|
||||||
calculate_wall_height(ray, &data->player);
|
calculate_wall_height(ray, &data->player);
|
||||||
render_walls(data, ray, x);
|
render_walls(data, ray, x);
|
||||||
|
data->zbuffer[x] = ray->wall_dist;
|
||||||
x++;
|
x++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/07/31 13:17:39 by kcolin #+# #+# */
|
/* Created: 2025/07/31 13:17:39 by kcolin #+# #+# */
|
||||||
/* Updated: 2025/08/05 14:59:24 by kcolin ### ########.fr */
|
/* Updated: 2025/08/06 14:15:08 by tchampio ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -14,6 +14,7 @@
|
||||||
#include "../consts.h"
|
#include "../consts.h"
|
||||||
#include "ray.h"
|
#include "ray.h"
|
||||||
#include "../renderer/render.h"
|
#include "../renderer/render.h"
|
||||||
|
#include "../draw/drawutils.h"
|
||||||
|
|
||||||
t_cardinal_dir get_cardinal(t_ray *ray)
|
t_cardinal_dir get_cardinal(t_ray *ray)
|
||||||
{
|
{
|
||||||
|
|
@ -33,14 +34,6 @@ t_cardinal_dir get_cardinal(t_ray *ray)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int my_mlx_pixel_get(t_img_data *img, int x, int y)
|
|
||||||
{
|
|
||||||
char *dst;
|
|
||||||
|
|
||||||
dst = img->addr + (y * img->line_length + x * (img->bits_per_pixel / 8));
|
|
||||||
return (*(int *)dst);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int get_color(t_cub3d_data *data, t_ray *ray, int tex_y)
|
static int get_color(t_cub3d_data *data, t_ray *ray, int tex_y)
|
||||||
{
|
{
|
||||||
t_cardinal_dir dir;
|
t_cardinal_dir dir;
|
||||||
|
|
|
||||||
78
src/sprites/sort_sprites.c
Normal file
78
src/sprites/sort_sprites.c
Normal file
|
|
@ -0,0 +1,78 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* sort_sprites.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/08/06 14:17:58 by tchampio #+# #+# */
|
||||||
|
/* Updated: 2025/08/07 13:44:33 by tchampio ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../cub3d_data.h"
|
||||||
|
|
||||||
|
static void swap(int i, int j, int *spriteorder, double *spritedist)
|
||||||
|
{
|
||||||
|
double tmp_dist;
|
||||||
|
int tmp_order;
|
||||||
|
|
||||||
|
tmp_order = spriteorder[i];
|
||||||
|
tmp_dist = spritedist[i];
|
||||||
|
spriteorder[i] = spriteorder[j];
|
||||||
|
spritedist[i] = spritedist[j];
|
||||||
|
spriteorder[j] = tmp_order;
|
||||||
|
spritedist[j] = tmp_dist;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void bubble_sort(double *dist, int *order, int count)
|
||||||
|
{
|
||||||
|
int j;
|
||||||
|
int k;
|
||||||
|
|
||||||
|
j = 0;
|
||||||
|
while (j < count)
|
||||||
|
{
|
||||||
|
k = 0;
|
||||||
|
while (k < count - j - 1)
|
||||||
|
{
|
||||||
|
if (dist[k] < dist[k + 1])
|
||||||
|
swap(k, k + 1, order, dist);
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// the sorting part is a readaptation of the std::sort function from c++
|
||||||
|
// more info on here:
|
||||||
|
// https://stackoverflow.com/questions/23816797/how-does-stdsort-work-for
|
||||||
|
// -list-of-pairs (url coupé en deux pour la norme >:( )
|
||||||
|
void sort_sprites(int *spriteorder, double *spritedist, t_cub3d_data *data)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int order[MAX_SPRITES];
|
||||||
|
double dist[MAX_SPRITES];
|
||||||
|
int spritescount;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
spritescount = 0;
|
||||||
|
while (data->sprite_list[i] && i < MAX_SPRITES)
|
||||||
|
{
|
||||||
|
order[i] = i;
|
||||||
|
dist[i] = ((data->player.x - data->sprite_list[i]->x)
|
||||||
|
* (data->player.x - data->sprite_list[i]->x)
|
||||||
|
+ (data->player.y - data->sprite_list[i]->y)
|
||||||
|
* (data->player.y - data->sprite_list[i]->y));
|
||||||
|
spritescount++;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
bubble_sort(dist, order, spritescount);
|
||||||
|
i = 0;
|
||||||
|
while (i < spritescount)
|
||||||
|
{
|
||||||
|
spritedist[i] = dist[i];
|
||||||
|
spriteorder[i] = order[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
20
src/sprites/sort_sprites.h
Normal file
20
src/sprites/sort_sprites.h
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* sort_sprites.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/08/06 14:18:47 by tchampio #+# #+# */
|
||||||
|
/* Updated: 2025/08/06 14:19:23 by tchampio ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef SORT_SPRITES_H
|
||||||
|
# define SORT_SPRITES_H
|
||||||
|
|
||||||
|
# include "../cub3d_data.h"
|
||||||
|
|
||||||
|
void sort_sprites(int *spriteorder, double *spritedist, t_cub3d_data *data);
|
||||||
|
|
||||||
|
#endif // SORT_SPRITES_H
|
||||||
58
src/sprites/sprite.h
Normal file
58
src/sprites/sprite.h
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* sprite.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/08/05 12:59:44 by tchampio #+# #+# */
|
||||||
|
/* Updated: 2025/08/06 14:41:17 by tchampio ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef SPRITE_H
|
||||||
|
# define SPRITE_H
|
||||||
|
|
||||||
|
# include "../draw/img_data.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* x - real position for the sprite
|
||||||
|
* y - real position for the sprite
|
||||||
|
* image - texture
|
||||||
|
* img_width - width of texture
|
||||||
|
* img_height - height of texture
|
||||||
|
* sprite_pos_x - Position of the sprite relative to the player
|
||||||
|
* sprite_pos_y - Position of the sprite relative to the player
|
||||||
|
* inv_det - Inversion of the matrix (playerpos and spritepos)
|
||||||
|
* transform_x - Result of the inversion
|
||||||
|
* transform_y - Result of the inversion
|
||||||
|
* sprite_screen_x - Position of the sprite on screen (horizontal)
|
||||||
|
* sprite_height - height of the sprite on screen
|
||||||
|
* sprite_draw_start_y - for the while loop when rendering (col)
|
||||||
|
* sprite_draw_end_y - for the while loop when rendering (col)
|
||||||
|
* sprite_width - width of the sprite on screen
|
||||||
|
* sprite_draw_start_x - for the while loop when rendering (row)
|
||||||
|
* sprite_draw_end_x - for the while loop when rendering (row)
|
||||||
|
*/
|
||||||
|
typedef struct s_sprite
|
||||||
|
{
|
||||||
|
double x;
|
||||||
|
double y;
|
||||||
|
t_img_data *image;
|
||||||
|
int img_width;
|
||||||
|
int img_height;
|
||||||
|
double sprite_pos_x;
|
||||||
|
double sprite_pos_y;
|
||||||
|
double inv_det;
|
||||||
|
double transform_x;
|
||||||
|
double transform_y;
|
||||||
|
int sprite_screen_x;
|
||||||
|
int sprite_height;
|
||||||
|
int sprite_draw_start_y;
|
||||||
|
int sprite_draw_end_y;
|
||||||
|
int sprite_width;
|
||||||
|
int sprite_draw_start_x;
|
||||||
|
int sprite_draw_end_x;
|
||||||
|
} t_sprite;
|
||||||
|
|
||||||
|
#endif // SPRITE_H
|
||||||
117
src/sprites/sprite_caster.c
Normal file
117
src/sprites/sprite_caster.c
Normal file
|
|
@ -0,0 +1,117 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* sprite_caster.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/08/05 15:51:01 by tchampio #+# #+# */
|
||||||
|
/* Updated: 2025/08/07 13:28:07 by tchampio ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../cub3d_data.h"
|
||||||
|
#include "../draw/drawutils.h"
|
||||||
|
#include "../renderer/render.h"
|
||||||
|
#include "sort_sprites.h"
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static void calculate_pos_and_transform(t_cub3d_data *data, t_sprite *sprite,
|
||||||
|
int i)
|
||||||
|
{
|
||||||
|
sprite->sprite_pos_x = data->sprite_list[data->sprite_order[i]]->x
|
||||||
|
- data->player.x;
|
||||||
|
sprite->sprite_pos_y = data->sprite_list[data->sprite_order[i]]->y
|
||||||
|
- data->player.y;
|
||||||
|
sprite->inv_det = 1.0 / (data->player.plane_x * data->player.dir_y
|
||||||
|
- data->player.dir_x * data->player.plane_y);
|
||||||
|
sprite->transform_x = sprite->inv_det * (data->player.dir_y
|
||||||
|
* sprite->sprite_pos_x - data->player.dir_x * sprite->sprite_pos_y);
|
||||||
|
sprite->transform_y = sprite->inv_det * (-data->player.plane_y
|
||||||
|
* sprite->sprite_pos_x
|
||||||
|
+ data->player.plane_x * sprite->sprite_pos_y);
|
||||||
|
sprite->sprite_screen_x = (int)((WIDTH / 2)
|
||||||
|
* (1 + sprite->transform_x / sprite->transform_y));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void sprite_calculate_pos_and_dist(t_cub3d_data *data, int i)
|
||||||
|
{
|
||||||
|
t_sprite *sprite;
|
||||||
|
|
||||||
|
sprite = data->sprite_list[i];
|
||||||
|
calculate_pos_and_transform(data, sprite, i);
|
||||||
|
sprite->sprite_height = (int)fabs((HEIGHT / sprite->transform_y));
|
||||||
|
sprite->sprite_draw_start_y = -sprite->sprite_height / 2 + HEIGHT / 2;
|
||||||
|
if (sprite->sprite_draw_start_y < 0)
|
||||||
|
sprite->sprite_draw_start_y = 0;
|
||||||
|
sprite->sprite_draw_end_y = sprite->sprite_height / 2 + HEIGHT / 2;
|
||||||
|
if (sprite->sprite_draw_end_y >= HEIGHT)
|
||||||
|
sprite->sprite_draw_end_y = HEIGHT - 1;
|
||||||
|
sprite->sprite_width = (int)fabs((HEIGHT / sprite->transform_y));
|
||||||
|
sprite->sprite_draw_start_x = -sprite->sprite_width / 2
|
||||||
|
+ sprite->sprite_screen_x;
|
||||||
|
if (sprite->sprite_draw_start_x < 0)
|
||||||
|
sprite->sprite_draw_start_x = 0;
|
||||||
|
sprite->sprite_draw_end_x = sprite->sprite_width / 2
|
||||||
|
+ sprite->sprite_screen_x;
|
||||||
|
if (sprite->sprite_draw_end_x >= WIDTH)
|
||||||
|
sprite->sprite_draw_end_x = WIDTH - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void render_sprite_col(t_cub3d_data *data, int i, int tex_x, int stripe)
|
||||||
|
{
|
||||||
|
int j;
|
||||||
|
int color;
|
||||||
|
int tex_y;
|
||||||
|
int d;
|
||||||
|
|
||||||
|
j = data->sprite_list[i]->sprite_draw_start_y;
|
||||||
|
while (j < data->sprite_list[i]->sprite_draw_end_y)
|
||||||
|
{
|
||||||
|
d = (j) * 256 - HEIGHT * 128
|
||||||
|
+ data->sprite_list[i]->sprite_height * 128;
|
||||||
|
tex_y = ((d * SIZE) / data->sprite_list[i]->sprite_height) / 256;
|
||||||
|
color = my_mlx_pixel_get(
|
||||||
|
data->sprite_list[data->sprite_order[i]]->image,
|
||||||
|
tex_x, tex_y);
|
||||||
|
if (color != SPRITE_TRANPARENCY_COLOR)
|
||||||
|
matrix_set(data, stripe, j, color);
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void render_sprites(t_cub3d_data *data, int i)
|
||||||
|
{
|
||||||
|
int stripe;
|
||||||
|
int tex_x;
|
||||||
|
|
||||||
|
stripe = data->sprite_list[i]->sprite_draw_start_x;
|
||||||
|
while (stripe < data->sprite_list[i]->sprite_draw_end_x)
|
||||||
|
{
|
||||||
|
tex_x = (int)(256 * (stripe
|
||||||
|
- (-data->sprite_list[i]->sprite_width / 2
|
||||||
|
+ data->sprite_list[i]->sprite_screen_x))
|
||||||
|
* SIZE / data->sprite_list[i]->sprite_width) / 256;
|
||||||
|
if (data->sprite_list[i]->transform_y > 0 && stripe > 0
|
||||||
|
&& stripe < WIDTH
|
||||||
|
&& data->sprite_list[i]->transform_y < data->zbuffer[stripe])
|
||||||
|
render_sprite_col(data, i, tex_x, stripe);
|
||||||
|
stripe++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sprite_caster(t_cub3d_data *data)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
sort_sprites(data->sprite_order, data->sprite_distances, data);
|
||||||
|
i = 0;
|
||||||
|
while (data->sprite_list[i] && i < MAX_SPRITES)
|
||||||
|
{
|
||||||
|
sprite_calculate_pos_and_dist(data, i);
|
||||||
|
render_sprites(data, i);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
20
src/sprites/sprite_caster.h
Normal file
20
src/sprites/sprite_caster.h
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* sprite_caster.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/08/06 12:13:32 by tchampio #+# #+# */
|
||||||
|
/* Updated: 2025/08/06 12:14:23 by tchampio ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef SPRITE_CASTER_H
|
||||||
|
# define SPRITE_CASTER_H
|
||||||
|
|
||||||
|
# include "../cub3d_data.h"
|
||||||
|
|
||||||
|
void sprite_caster(t_cub3d_data *data);
|
||||||
|
|
||||||
|
#endif // SPRITE_CASTER_H
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/07/31 13:43:05 by kcolin #+# #+# */
|
/* Created: 2025/07/31 13:43:05 by kcolin #+# #+# */
|
||||||
/* Updated: 2025/08/05 13:19:40 by kcolin ### ########.fr */
|
/* Updated: 2025/08/05 15:45:01 by tchampio ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -72,4 +72,5 @@ void init_cub3d_data(t_cub3d_data *data, char **argv)
|
||||||
init_player(&data->player, data->map);
|
init_player(&data->player, data->map);
|
||||||
data->screen_matrix = ft_calloc(sizeof(int), WIDTH * HEIGHT);
|
data->screen_matrix = ft_calloc(sizeof(int), WIDTH * HEIGHT);
|
||||||
load_textures(data);
|
load_textures(data);
|
||||||
|
ft_memset(data->sprite_distances, -1, MAX_SPRITES);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
|
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/07/31 13:28:47 by tchampio #+# #+# */
|
/* Created: 2025/07/31 13:28:47 by tchampio #+# #+# */
|
||||||
/* Updated: 2025/07/31 13:29:35 by tchampio ### ########.fr */
|
/* Updated: 2025/08/06 14:14:11 by tchampio ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -15,6 +15,7 @@
|
||||||
|
|
||||||
# include "../cub3d_data.h"
|
# include "../cub3d_data.h"
|
||||||
|
|
||||||
void init_cub3d_data(t_cub3d_data *data, char **argv);
|
void init_cub3d_data(t_cub3d_data *data, char **argv);
|
||||||
|
t_img_data *load_single_texture(t_cub3d_data *data, char *path);
|
||||||
|
|
||||||
#endif // INITS_H
|
#endif // INITS_H
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue