aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile3
-rw-r--r--include/cub3d.h43
m---------libft0
-rw-r--r--maze.cub33
-rw-r--r--minimalist.cub16
-rw-r--r--room.cub24
-rw-r--r--src/capture.c100
-rw-r--r--src/event.c5
-rw-r--r--src/helper.c32
-rw-r--r--src/main.c84
-rw-r--r--src/parse/parse_check.c29
-rw-r--r--src/parse/parse_textures.c2
-rw-r--r--src/render.c51
-rw-r--r--src/render_sprite.c84
-rw-r--r--src/render_state.c6
-rw-r--r--src/state.c42
-rw-r--r--src/texture.c6
-rw-r--r--src/vector.c9
-rw-r--r--textures/cheese.xpm516
19 files changed, 932 insertions, 153 deletions
diff --git a/Makefile b/Makefile
index 8df44eb..56893ed 100644
--- a/Makefile
+++ b/Makefile
@@ -27,7 +27,8 @@ SRCFILES = main.c \
helper.c \
error.c \
capture.c \
- texture.c
+ texture.c \
+ render_sprite.c
SRC = $(addprefix $(SRCDIR)/,$(SRCFILES))
$(info $(SRC))
OBJ = $(SRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
diff --git a/include/cub3d.h b/include/cub3d.h
index 12c6fe1..36d59c5 100644
--- a/include/cub3d.h
+++ b/include/cub3d.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/15 06:40:37 by cacharle #+# #+# */
-/* Updated: 2020/02/02 09:36:22 by cacharle ### ########.fr */
+/* Updated: 2020/02/03 01:43:17 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
@@ -38,8 +38,6 @@
# define TRUE 1
# define FALSE 0
-# define SQUARE(x) ((x) * (x))
-
typedef int t_bool;
typedef struct
@@ -95,6 +93,12 @@ typedef struct
int endian;
} t_image;
+typedef struct
+{
+ t_vector pos;
+ double dist;
+} t_sprite;
+
typedef struct s_state
{
t_bool running;
@@ -109,10 +113,11 @@ typedef struct s_state
t_color ceilling_color;
t_color floor_color;
t_image window;
- t_image sprite_window;
- t_image *surface;
char *textures_path[TEXTURES_NUM];
t_image textures[TEXTURES_NUM];
+ double *z_buffer;
+ int sprites_num;
+ t_sprite *sprites;
} t_state;
typedef enum
@@ -173,13 +178,13 @@ typedef struct
t_byte4 color_used;
t_byte4 color_important;
} info_header;
- struct
- {
- t_byte1 blue;
- t_byte1 green;
- t_byte1 red;
- t_byte1 reserved;
- } color_table;
+ // struct
+ // {
+ // t_byte1 blue;
+ // t_byte1 green;
+ // t_byte1 red;
+ // t_byte1 reserved;
+ // } color_table;
} t_bmp_header;
/*
@@ -243,8 +248,8 @@ void load_texture(void *mlx_ptr, t_image *image, char *path);
*/
int render_update(void *param);
-void render_update_window(t_state *state, t_cell target);
-void render_column(t_state *state, int x, t_cell target);
+void render_update_window(t_state *state);
+void render_column(t_state *state, int x);
void render_window_column(t_state *state, t_render_state *rstate);
void render_texture(t_state *state, t_render_state *rstate, int *i);
@@ -253,6 +258,7 @@ void render_texture(t_state *state, t_render_state *rstate, int *i);
*/
t_vector vector_add(t_vector a, t_vector b);
+t_vector vector_sub(t_vector a, t_vector b);
t_vector vector_scale(t_vector v, double scalar);
t_vector vector_rotate(t_vector v, double angle);
double vector_norm(t_vector v);
@@ -278,7 +284,7 @@ t_bool helper_is_player_cell(t_cell cell);
void helper_free_splited(char **splited);
void helper_rotate_player(t_state *state, double rotation);
void helper_init_dir_plane(t_state *state, int y, int x);
-// int get_tex_x()
+t_bool state_init_sprites(t_state *state);
/*
** render_state.c
@@ -306,4 +312,11 @@ int capture(t_state *state);
t_bool bmp_write(t_image *image, t_bmp_header *header);
void bmp_fill_header(t_image *image, t_bmp_header *header);
+/*
+** render_sprite.c
+*/
+
+void render_update_sprite(t_state *state);
+
+
#endif
diff --git a/libft b/libft
-Subproject d22609e03717283e85a23587203af1b8b7d2fde
+Subproject f4e1232957b1270da70f57fcad4cd6371947e44
diff --git a/maze.cub b/maze.cub
new file mode 100644
index 0000000..c8a9113
--- /dev/null
+++ b/maze.cub
@@ -0,0 +1,33 @@
+R 1500 1200
+NO ./textures/wood.xpm
+SO ./textures/eagle.xpm
+WE ./textures/redbrick.xpm
+EA ./textures/eagle.xpm
+
+S ./textures/barrel.xpm
+F 10,10,10
+C 40,40,40
+
+1111111111111111111111111111111111111
+1010000000100000001000001000000000101
+1010111110101110111110111011101110101
+1000000010001010000010000010101000101
+1111101011111011111011101010101111101
+1000101000000010000010101010100000101
+1010111111101010111110111010111110101
+1010000000101010001000001000001000101
+1011111110111011101011101111101011101
+1000001000101000101000100000101000001
+1011101110101110101111101010101111111
+1000100010101000100010001010100010001
+1111111010101011111010111010111011101
+1000001010000010001010001010001000001
+1011101011111011101011101110111111101
+1010001000001000001000101010000000101
+1010111110101110111110101011111110101
+1010000010101010100000100000001010001
+1011111010101011101111101111101011111
+1010001010100000100000101000100000001
+1010111011101110111110111010111110101
+1N10000000001000001000000010000000101
+1111111111111111111111111111111111111
diff --git a/minimalist.cub b/minimalist.cub
index aefdc70..25953f8 100644
--- a/minimalist.cub
+++ b/minimalist.cub
@@ -4,21 +4,21 @@ SO ./textures/eagle.xpm
WE ./textures/redbrick.xpm
EA ./textures/eagle.xpm
-S ./textures/barrel.xpm
+S ./textures/cheese.xpm
F 10,10,10
C 40,40,40
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
-1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
-1 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 2 0 0 0 0 0 1 0 0 0 1
-1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1
+1 0 0 0 0 0 2 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
+1 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 2 0 0 0 0 0 1 0 0 2 1
+1 2 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1
1 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1
-1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 1 0 1 1 1 1 0 0 0 1
+1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 1 0 1 1 1 1 0 2 0 1
1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 1
1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 0 1 0 0 1 0 0 0 1
-1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 1
-1 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 2 0 1
-1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1 0 0 0 1
+1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 0 0 0 0 0 0 1 2 0 0 1
+1 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 1
+1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1 0 2 0 1
1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 0 0 1
1 1 1 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 N 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
diff --git a/room.cub b/room.cub
new file mode 100644
index 0000000..78297c2
--- /dev/null
+++ b/room.cub
@@ -0,0 +1,24 @@
+R 1500 1200
+NO ./textures/wood.xpm
+SO ./textures/eagle.xpm
+WE ./textures/redbrick.xpm
+EA ./textures/mossy.xpm
+
+S ./textures/barrel.xpm
+F 10,10,10
+C 40,40,40
+
+1 1 1 1 1 1 1 1 1 1 1 1
+1 0 0 0 0 0 0 0 0 0 0 1
+1 0 0 0 0 0 0 0 0 0 0 1
+1 0 0 0 0 0 0 0 0 0 0 1
+1 0 0 0 0 0 0 0 0 0 0 1
+1 0 0 0 0 0 0 0 0 0 0 1
+1 0 0 0 0 0 S 0 0 0 0 1
+1 0 0 0 0 0 0 0 0 0 0 1
+1 0 0 0 0 0 0 0 0 0 0 1
+1 0 0 0 0 0 0 0 0 0 0 1
+1 0 0 0 0 0 0 0 0 0 0 1
+1 0 0 0 0 0 0 0 0 0 0 1
+1 0 0 0 0 0 0 0 0 0 0 1
+1 1 1 1 1 1 1 1 1 1 1 1
diff --git a/src/capture.c b/src/capture.c
index a052709..72c1f81 100644
--- a/src/capture.c
+++ b/src/capture.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/11 13:15:11 by cacharle #+# #+# */
-/* Updated: 2020/02/02 08:33:14 by cacharle ### ########.fr */
+/* Updated: 2020/02/03 02:25:43 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,14 +14,14 @@
#define BM_FILE_TYPE 19778
#define DATA_OFFSET 1078
-#define BITMAP_INFO_HEADER_SIZE 40
+#define BITMAP_INFO_HEADER_SIZE 0x424d
#define CAPTURE_FILENAME "capture.bmp"
int capture(t_state *state)
{
t_bmp_header header;
- render_update_window(state, CELL_WALL);
+ render_update_window(state);
bmp_fill_header(&state->window, &header);
if (!bmp_write(&state->window, &header))
{
@@ -31,15 +31,81 @@ int capture(t_state *state)
return (0);
}
+unsigned char* createBitmapFileHeader(int height, int width, int paddingSize){
+ int fileSize = 14 + 40 + (3*width+paddingSize) * height;
+
+ static unsigned char fileHeader[] = {
+ 0,0, /// signature
+ 0,0,0,0, /// image file size in bytes
+ 0,0,0,0, /// reserved
+ 0,0,0,0, /// start of pixel array
+ };
+
+ fileHeader[ 0] = (unsigned char)('B');
+ fileHeader[ 1] = (unsigned char)('M');
+ fileHeader[ 2] = (unsigned char)(fileSize );
+ fileHeader[ 3] = (unsigned char)(fileSize>> 8);
+ fileHeader[ 4] = (unsigned char)(fileSize>>16);
+ fileHeader[ 5] = (unsigned char)(fileSize>>24);
+ fileHeader[10] = (unsigned char)(14 + 40);
+
+ return fileHeader;
+}
+
+unsigned char* createBitmapInfoHeader(int height, int width){
+ static unsigned char infoHeader[] = {
+ 0,0,0,0, /// header size
+ 0,0,0,0, /// image width
+ 0,0,0,0, /// image height
+ 0,0, /// number of color planes
+ 0,0, /// bits per pixel
+ 0,0,0,0, /// compression
+ 0,0,0,0, /// image size
+ 0,0,0,0, /// horizontal resolution
+ 0,0,0,0, /// vertical resolution
+ 0,0,0,0, /// colors in color table
+ 0,0,0,0, /// important color count
+ };
+
+ infoHeader[ 0] = (unsigned char)(40);
+ infoHeader[ 4] = (unsigned char)(width );
+ infoHeader[ 5] = (unsigned char)(width>> 8);
+ infoHeader[ 6] = (unsigned char)(width>>16);
+ infoHeader[ 7] = (unsigned char)(width>>24);
+ infoHeader[ 8] = (unsigned char)(height );
+ infoHeader[ 9] = (unsigned char)(height>> 8);
+ infoHeader[10] = (unsigned char)(height>>16);
+ infoHeader[11] = (unsigned char)(height>>24);
+ infoHeader[12] = (unsigned char)(1);
+ infoHeader[14] = (unsigned char)(3*8);
+
+ return infoHeader;
+}
+
t_bool bmp_write(t_image *image, t_bmp_header *header)
{
int fd;
- if ((fd = open(CAPTURE_FILENAME, O_WRONLY | O_CREAT)) < 0)
+ unsigned char padding[3] = {0, 0, 0};
+ int paddingSize = (4 - (image->width*3) % 4) % 4; //redundant?
+ unsigned char* fileHeader = createBitmapFileHeader(image->height, image->width, paddingSize);
+ unsigned char* infoHeader = createBitmapInfoHeader(image->height, image->width);
+
+ if ((fd = open(CAPTURE_FILENAME, O_WRONLY | O_CREAT, S_IRWXU)) < 0)
return (FALSE);
- printf("%d\n", fd);
- write(fd, &header, sizeof(t_bmp_header));
- write(fd, image->data, image->width * image->height * 4);
+
+ write(fd, fileHeader, 14);
+ write(fd, infoHeader, 40);
+ /* write(fd, &header, sizeof(t_bmp_header)); */
+ for (int i = 0; i < image->width; i++)
+ {
+ for (int j = 0; j < image->height; j++)
+ {
+ write(fd, &image->data[4 * (i * image->width + j)], 3);
+ }
+ write(fd, padding, paddingSize);
+ }
+ /* write(fd, image->data, image->width * image->height * 4); */
close(fd);
return (TRUE);
}
@@ -47,23 +113,25 @@ t_bool bmp_write(t_image *image, t_bmp_header *header)
void bmp_fill_header(t_image *image, t_bmp_header *header)
{
header->file_header.file_type = BM_FILE_TYPE;
- header->file_header.file_size = sizeof(t_bmp_header) + image->width * image->height * 4;
+ header->file_header.file_size = sizeof(t_bmp_header) + image->width * image->height * 3;
header->file_header.reserved1 = 0;
header->file_header.reserved1 = 0;
- header->file_header.offset = DATA_OFFSET;
- header->info_header.size = sizeof(t_bmp_header) + image->width * image->height * 4;
+ header->file_header.offset = sizeof(t_bmp_header);
+
+ header->info_header.size = sizeof(header->info_header);
header->info_header.width = image->width;
header->info_header.height = image->height;
header->info_header.planes = 0;
- header->info_header.depth = 8 * 4;
+ header->info_header.depth = 8 * 3;
header->info_header.compression = 0;
- header->info_header.size_image = 0;
+ header->info_header.size_image = image->width * image->height * 3;
header->info_header.w_pix_per_meter = 0;
header->info_header.h_pix_per_meter = 0;
header->info_header.color_used = 0;
header->info_header.color_important = 0;
- header->color_table.blue = 0xff;
- header->color_table.green = 0xff;
- header->color_table.red = 0xff;
- header->color_table.reserved = 0;
+
+ /* header->color_table.blue = 0xff; */
+ /* header->color_table.green = 0xff; */
+ /* header->color_table.red = 0xff; */
+ /* header->color_table.reserved = 0; */
}
diff --git a/src/event.c b/src/event.c
index 3517978..5a5b3e1 100644
--- a/src/event.c
+++ b/src/event.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/15 06:39:37 by cacharle #+# #+# */
-/* Updated: 2020/02/02 09:52:09 by cacharle ### ########.fr */
+/* Updated: 2020/02/03 00:44:39 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -38,8 +38,7 @@ int event_keydown(int key, t_state *state)
helper_rotate_player(state, -ROTATE_STEP);
else if (key == MLXK_RIGHT)
helper_rotate_player(state, ROTATE_STEP);
- if (state->map[(int)state->pos.y][(int)state->pos.x] == CELL_WALL ||
- state->map[(int)state->pos.y][(int)state->pos.x] == CELL_ITEM)
+ if (state->map[(int)state->pos.y][(int)state->pos.x] == CELL_WALL)
state->pos = saved_pos;
return (0);
}
diff --git a/src/helper.c b/src/helper.c
index 6fa7ffe..957a5de 100644
--- a/src/helper.c
+++ b/src/helper.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/11 07:32:20 by cacharle #+# #+# */
-/* Updated: 2020/02/01 11:32:15 by cacharle ### ########.fr */
+/* Updated: 2020/02/02 22:16:14 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -59,3 +59,33 @@ void helper_init_dir_plane(t_state *state, int y, int x)
state->plane = vector_scale(state->plane, 0.66);
state->plane = vector_apply(state->plane, &fabs);
}
+
+t_bool state_init_sprites(t_state *state)
+{
+ int i;
+ int j;
+ int counter;
+
+ counter = 0;
+ i = -1;
+ while (++i < state->map_height)
+ {
+ j = -1;
+ while (++j < state->map_width)
+ if (state->map[i][j] == CELL_ITEM)
+ counter++;
+ }
+ state->sprites_num = counter;
+ if ((state->sprites = (t_sprite*)malloc(sizeof(t_sprite) * counter)) == NULL)
+ return (FALSE);
+ counter = 0;
+ i = -1;
+ while (++i < state->map_height)
+ {
+ j = -1;
+ while (++j < state->map_width)
+ if (state->map[i][j] == CELL_ITEM)
+ state->sprites[counter++].pos = vector_new((double)j + 0.5, (double)i + 0.5);
+ }
+ return (TRUE);
+}
diff --git a/src/main.c b/src/main.c
index 0d9ecea..95256d2 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/15 06:39:39 by cacharle #+# #+# */
-/* Updated: 2020/02/01 14:00:48 by cacharle ### ########.fr */
+/* Updated: 2020/02/02 19:22:09 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -29,46 +29,44 @@ int main(int argc, char **argv)
return (0);
}
-/*
-int main(int argc, char **argv)
-{
- (void)argc;
- t_state *s = parse_check(parse(argv[1]));
- if (s == NULL)
- return (1);
- printf("R %d %d\n", s->window.width, s->window.height);
- printf("NO %s\n", s->textures_path[TEX_NORTH]);
- printf("SO %s\n", s->textures_path[TEX_SOUTH]);
- printf("WE %s\n", s->textures_path[TEX_WEST]);
- printf("EA %s\n\n", s->textures_path[TEX_EAST]);
- printf("S %s\n", s->textures_path[TEX_SPRITE]);
- printf("F %d,%d,%d\n", s->floor_color.rgb.r, s->floor_color.rgb.g, s->floor_color.rgb.b);
- printf("C %d,%d,%d\n\n", s->ceilling_color.rgb.r, s->ceilling_color.rgb.g, s->ceilling_color.rgb.b);
- printf("%dx%d\n", s->map_height, s->map_width);
- for (int i = 0; i < s->map_height; i++)
- {
- for (int j = 0; j < s->map_width; j++)
- {
- if (s->map[i][j] == CELL_WALL)
- printf("#");
- else if (s->map[i][j] == CELL_EMPTY)
- printf(" ");
- else
- printf("%d", s->map[i][j]);
- if (j != s->map_width - 1)
- printf(" ");
- }
- printf("\n");
- }
- printf("post state_new\n");
- if ((s = state_new(s)) == NULL)
- {
- printf("Error: state new");
- return 1;
- }
- printf("state->pos [%f %f]\n", s->pos.x, s->pos.y);
- state_destroy(s);
- return 0;
-}
-*/
+/* int main(int argc, char **argv) */
+/* { */
+/* (void)argc; */
+/* t_state *s = parse_check(parse(argv[1])); */
+/* if (s == NULL) */
+/* return (1); */
+/* printf("R %d %d\n", s->window.width, s->window.height); */
+/* printf("NO %s\n", s->textures_path[TEX_NORTH]); */
+/* printf("SO %s\n", s->textures_path[TEX_SOUTH]); */
+/* printf("WE %s\n", s->textures_path[TEX_WEST]); */
+/* printf("EA %s\n\n", s->textures_path[TEX_EAST]); */
+/* printf("S %s\n", s->textures_path[TEX_SPRITE]); */
+/* printf("F %d,%d,%d\n", s->floor_color.rgb.r, s->floor_color.rgb.g, s->floor_color.rgb.b); */
+/* printf("C %d,%d,%d\n\n", s->ceilling_color.rgb.r, s->ceilling_color.rgb.g, s->ceilling_color.rgb.b); */
+/* printf("%dx%d\n", s->map_height, s->map_width); */
+/* for (int i = 0; i < s->map_height; i++) */
+/* { */
+/* for (int j = 0; j < s->map_width; j++) */
+/* { */
+/* if (s->map[i][j] == CELL_WALL) */
+/* printf("#"); */
+/* else if (s->map[i][j] == CELL_EMPTY) */
+/* printf(" "); */
+/* else */
+/* printf("%d", s->map[i][j]); */
+/* if (j != s->map_width - 1) */
+/* printf(" "); */
+/* } */
+/* printf("\n"); */
+/* } */
+/* printf("post state_new\n"); */
+/* if ((s = state_new(s)) == NULL) */
+/* { */
+/* printf("Error: state new"); */
+/* return 1; */
+/* } */
+/* printf("state->pos [%f %f]\n", s->pos.x, s->pos.y); */
+/* state_destroy(s); */
+/* return 0; */
+/* } */
diff --git a/src/parse/parse_check.c b/src/parse/parse_check.c
index a65d74a..5fc2674 100644
--- a/src/parse/parse_check.c
+++ b/src/parse/parse_check.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/11 10:59:15 by cacharle #+# #+# */
-/* Updated: 2020/01/11 13:03:33 by cacharle ### ########.fr */
+/* Updated: 2020/02/02 19:42:34 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,8 +15,8 @@
t_state *parse_check(t_state *state)
{
int i;
- /* int j; */
- /* int player_count; */
+ int j;
+ int player_count;
i = -1;
while (++i < state->map_width)
@@ -30,17 +30,16 @@ t_state *parse_check(t_state *state)
|| state->map[i][state->map_width - 1] != CELL_WALL)
return (error_put_return_state_destroy(
"validate map without borders", state));
- // maybe not necessary
- /* player_count = 0; */
- /* i = -1; */
- /* while (++i < state->map_height) */
- /* { */
- /* j = -1; */
- /* while (++j < state->map_width) */
- /* if (helper_is_player_cell(state->map[i][j])) */
- /* player_count++; */
- /* } */
- /* if (player_count != 1) */
- /* return (state_destroy(state)); */
+ player_count = 0;
+ i = -1;
+ while (++i < state->map_height)
+ {
+ j = -1;
+ while (++j < state->map_width)
+ if (helper_is_player_cell(state->map[i][j]))
+ player_count++;
+ }
+ if (player_count != 1)
+ return (error_put_return_state_destroy("only one player allowed", state));
return (state);
}
diff --git a/src/parse/parse_textures.c b/src/parse/parse_textures.c
index a0fb8f6..f6ba1a0 100644
--- a/src/parse/parse_textures.c
+++ b/src/parse/parse_textures.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/11 09:47:53 by cacharle #+# #+# */
-/* Updated: 2020/01/11 09:51:03 by cacharle ### ########.fr */
+/* Updated: 2020/02/02 19:21:39 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/src/render.c b/src/render.c
index 5d3c5d4..87379a0 100644
--- a/src/render.c
+++ b/src/render.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/11 13:37:17 by cacharle #+# #+# */
-/* Updated: 2020/02/02 09:56:37 by cacharle ### ########.fr */
+/* Updated: 2020/02/02 23:30:17 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -23,33 +23,27 @@ int render_update(void *param)
exit(EXIT_SUCCESS);
return (0);
}
- state->surface = &state->window;
- render_update_window(state, CELL_WALL);
- mlx_put_image_to_window(state->mlx_ptr, state->window_ptr,
- state->surface->id, 0, 0);
- state->surface = &state->sprite_window;
- for (int i = 0; i < state->surface->width * state->surface->height; i++)
- ((unsigned int*)state->surface->data)[i] = 0xff000000;
- render_update_window(state, CELL_ITEM);
- mlx_put_image_to_window(state->mlx_ptr, state->window_ptr,
- state->surface->id, 0, 0);
+ render_update_window(state);
+ render_update_sprite(state);
+ mlx_put_image_to_window(state->mlx_ptr, state->window_ptr, state->window.id, 0, 0);
+ /* for (int i = 0; i < state->window.width * state->window.height; i++) */
+ /* ((unsigned int*)state->window.data)[i] = 0xff000000; */
return (0);
}
-void render_update_window(t_state *state, t_cell target)
+void render_update_window(t_state *state)
{
int x;
x = -1;
- while (++x < state->surface->width)
- render_column(state, x, target);
+ while (++x < state->window.width)
+ render_column(state, x);
}
-void render_column(t_state *state, int x, t_cell target)
+void render_column(t_state *state, int x)
{
t_render_state rstate;
- rstate.target = target;
rstate.x = x;
rstate_ray(state, &rstate);
rstate.map_pos = vector_new((double)((int)state->pos.x), (double)((int)state->pos.y));
@@ -60,19 +54,19 @@ void render_column(t_state *state, int x, t_cell target)
{
rstate.side = rstate.probe.x < rstate.probe.y ? SIDE_WE : SIDE_NS;
rstate_next_probe(&rstate);
- if (target == CELL_ITEM && state->map[(int)rstate.map_pos.y][(int)rstate.map_pos.x] == CELL_WALL)
- return ;
- if (state->map[(int)rstate.map_pos.y][(int)rstate.map_pos.x] == target)
+ if (state->map[(int)rstate.map_pos.y][(int)rstate.map_pos.x] == CELL_WALL)
break ;
}
rstate_perp_dist(state, &rstate);
+ state->z_buffer[x] = rstate.perp_dist;
+ /* printf("%f\n", state->z_buffer[x]); */
rstate_line_height(state, &rstate);
- rstate.draw_start = state->surface->height / 2 - rstate.line_height / 2;
- rstate.draw_end = state->surface->height / 2 + rstate.line_height / 2;
+ rstate.draw_start = state->window.height / 2 - rstate.line_height / 2;
+ rstate.draw_end = state->window.height / 2 + rstate.line_height / 2;
if (rstate.draw_start < 0)
rstate.draw_start = 0;
- if (rstate.draw_end > state->surface->height - 1)
- rstate.draw_end = state->surface->height - 1;
+ if (rstate.draw_end > state->window.height - 1)
+ rstate.draw_end = state->window.height - 1;
render_window_column(state, &rstate);
}
@@ -84,12 +78,11 @@ void render_window_column(t_state *state, t_render_state *rstate)
white.hexcode = 0x00ffffff;
i = 0;
while (i < rstate->draw_start)
- ((t_color*)state->surface->data)[i++ * state->surface->width + rstate->x] =
- state->ceilling_color;
+ ((t_color*)state->window.data)[i++ * state->window.width + rstate->x] = state->ceilling_color;
i--;
render_texture(state, rstate, &i);
- while (i < state->surface->height)
- ((t_color*)state->surface->data)[i++ * state->surface->width + rstate->x] =
+ while (i < state->window.height)
+ ((t_color*)state->window.data)[i++ * state->window.width + rstate->x] =
state->floor_color;
}
@@ -104,12 +97,12 @@ void render_texture(t_state *state, t_render_state *rstate, int *i)
texture = texture_select(state, rstate);
tex_x = texture_x(state, rstate, texture);
tex_step = (double)texture->height / (double)rstate->line_height;
- tex_pos = (rstate->draw_start - state->surface->height / 2 + rstate->line_height / 2) * tex_step;
+ tex_pos = (rstate->draw_start - state->window.height / 2 + rstate->line_height / 2) * tex_step;
while ((*i)++ < rstate->draw_end)
{
tex_y = (int)tex_pos & (texture->height - 1);
tex_pos += tex_step;
- ((t_color*)state->surface->data)[*i * state->surface->width + rstate->x] =
+ ((t_color*)state->window.data)[*i * state->window.width + rstate->x] =
((t_color*)texture->data)[texture->height * tex_y + tex_x];
}
}
diff --git a/src/render_sprite.c b/src/render_sprite.c
new file mode 100644
index 0000000..0570a71
--- /dev/null
+++ b/src/render_sprite.c
@@ -0,0 +1,84 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* render_sprite.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/02 17:39:07 by cacharle #+# #+# */
+/* Updated: 2020/02/02 23:52:29 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "cub3d.h"
+
+int sprite_compar(const void *a, const void *b)
+{
+ const t_sprite *sprite_a;
+ const t_sprite *sprite_b;
+
+ sprite_a = a;
+ sprite_b = b;
+ if (sprite_a->dist > sprite_b->dist)
+ return -1;
+ if (sprite_a->dist < sprite_b->dist)
+ return 1;
+ return 0;
+}
+
+void sprites_dist(t_state *state)
+{
+ int i;
+
+ i = -1;
+ while (++i < state->sprites_num)
+ state->sprites[i].dist = vector_norm(vector_sub(state->sprites[i].pos, state->pos));
+}
+
+void render_update_sprite(t_state *state)
+{
+ sprites_dist(state);
+ ft_qsort(state->sprites, state->sprites_num, sizeof(t_sprite), sprite_compar);
+ for(int i = 0; i < state->sprites_num; i++)
+ {
+ double sprite_x = state->sprites[i].pos.x - state->pos.x;
+ double sprite_y = state->sprites[i].pos.y - state->pos.y;
+
+ double inverse_det = 1.0 / (state->plane.x * state->dir.y - state->dir.x * state->plane.y);
+ double transform_x = inverse_det * (state->dir.y * sprite_x - state->dir.x * sprite_y);
+ double transform_y = inverse_det * (-state->plane.y * sprite_x + state->plane.x * sprite_y);
+ int sprite_window_x = (int)((state->window.width / 2) * (1 + transform_x / transform_y));
+
+ int sprite_height = abs((int)(state->window.height / (transform_y)));
+ int draw_start = -sprite_height / 2 + state->window.height / 2;
+ if(draw_start < 0)
+ draw_start = 0;
+ int draw_end_y = sprite_height / 2 + state->window.height / 2;
+ if(draw_end_y >= state->window.height)
+ draw_end_y = state->window.height - 1;
+
+ int sprite_width = abs( (int) (state->window.height / (transform_y)));
+ int draw_start_x = -sprite_width / 2 + sprite_window_x;
+ if (draw_start_x < 0)
+ draw_start_x = 0;
+ int draw_end_x = sprite_width / 2 + sprite_window_x;
+ if (draw_end_x >= state->window.width)
+ draw_end_x = state->window.width - 1;
+
+ for(int stripe = draw_start_x; stripe < draw_end_x; stripe++)
+ {
+ int tex_width = state->textures[TEX_SPRITE].width;
+ int tex_height = state->textures[TEX_SPRITE].height;
+ int tex_x = (int)(256 * (stripe - (-sprite_width / 2 + sprite_window_x)) * tex_width / sprite_width) / 256;
+ if(transform_y > 0 && stripe > 0 && stripe < state->window.width && transform_y < state->z_buffer[stripe])
+ for(int y = draw_start; y < draw_end_y; y++)
+ {
+ int d = y * 256 - state->window.height * 128 + sprite_height * 128;
+ int tex_y = ((d * tex_height) / sprite_height) / 256;
+ t_color color = ((t_color*)state->textures[TEX_SPRITE].data)[tex_width * tex_y + tex_x];
+ if ((color.hexcode & 0x00FFFFFF) != 0) // if not empty pixel
+ ((t_color*)state->window.data)[y * state->window.width + stripe] = color;
+ }
+ }
+ }
+}
diff --git a/src/render_state.c b/src/render_state.c
index 09afe25..904e564 100644
--- a/src/render_state.c
+++ b/src/render_state.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/15 14:40:14 by cacharle #+# #+# */
-/* Updated: 2020/02/02 09:00:38 by cacharle ### ########.fr */
+/* Updated: 2020/02/02 17:39:59 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -29,7 +29,7 @@ void rstate_ray(t_state *state, t_render_state *rstate)
{
double camera_x;
- camera_x = 2.0 * (double)rstate->x / (double)state->surface->width - 1.0;
+ camera_x = 2.0 * (double)rstate->x / (double)state->window.width - 1.0;
rstate->ray = vector_add(state->dir, vector_scale(state->plane, camera_x));
}
@@ -157,5 +157,5 @@ void rstate_perp_dist(t_state *state, t_render_state *rstate)
void rstate_line_height(t_state *state, t_render_state *rstate)
{
- rstate->line_height = (int)((double)state->surface->height / rstate->perp_dist);
+ rstate->line_height = (int)((double)state->window.height / rstate->perp_dist);
}
diff --git a/src/state.c b/src/state.c
index 267c6c9..ddab714 100644
--- a/src/state.c
+++ b/src/state.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/19 16:39:57 by cacharle #+# #+# */
-/* Updated: 2020/02/02 09:13:06 by cacharle ### ########.fr */
+/* Updated: 2020/02/02 22:16:47 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@