diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-02-03 02:27:54 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-02-03 02:27:54 +0100 |
| commit | 0ca2404b509ef4ddc4e19996cdbe3edbbba6d339 (patch) | |
| tree | 28321d75ebf590b491eb956a7481cf750f0f7d44 /src | |
| parent | 628f82dbb9d44dfda7ebde0a54b6559b275e204c (diff) | |
| download | cub3d-0ca2404b509ef4ddc4e19996cdbe3edbbba6d339.tar.gz cub3d-0ca2404b509ef4ddc4e19996cdbe3edbbba6d339.tar.bz2 cub3d-0ca2404b509ef4ddc4e19996cdbe3edbbba6d339.zip | |
sprite from lodev and capture from random stackoverflow thread, now lets understand this nightmare
Diffstat (limited to 'src')
| -rw-r--r-- | src/capture.c | 100 | ||||
| -rw-r--r-- | src/event.c | 5 | ||||
| -rw-r--r-- | src/helper.c | 32 | ||||
| -rw-r--r-- | src/main.c | 84 | ||||
| -rw-r--r-- | src/parse/parse_check.c | 29 | ||||
| -rw-r--r-- | src/parse/parse_textures.c | 2 | ||||
| -rw-r--r-- | src/render.c | 51 | ||||
| -rw-r--r-- | src/render_sprite.c | 84 | ||||
| -rw-r--r-- | src/render_state.c | 6 | ||||
| -rw-r--r-- | src/state.c | 42 | ||||
| -rw-r--r-- | src/texture.c | 6 | ||||
| -rw-r--r-- | src/vector.c | 9 |
12 files changed, 321 insertions, 129 deletions
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); +} @@ -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 */ /* */ /* ************************************************************************** */ @@ -20,6 +20,8 @@ t_state *state_new(t_state *state) { int i; + if (state == NULL) + return (NULL); state->running = TRUE; if ((state->mlx_ptr = mlx_init()) == NULL) return (state_destroy(state)); @@ -27,14 +29,17 @@ t_state *state_new(t_state *state) state->window.width, state->window.height, WINDOW_TITLE)) == NULL) return (state_destroy(state)); printf("init mlx and mlx window\n"); + i = -1; while (++i < TEXTURES_NUM) { + puts(state->textures_path[i]); load_texture(state->mlx_ptr, &state->textures[i], state->textures_path[i]); if (state->textures[i].id == NULL) return (error_put_return_state_destroy("load texture", state)); } + if ((state->window.id = mlx_new_image(state->mlx_ptr, state->window.width, state->window.height)) == NULL) return (state_destroy(state)); @@ -42,17 +47,22 @@ t_state *state_new(t_state *state) &state->window.depth, &state->window.size_line, &state->window.endian); printf("init mlx window image\n"); - state->sprite_window.width = state->window.width; - state->sprite_window.height = state->window.height; - if ((state->sprite_window.id = mlx_new_image(state->mlx_ptr, - state->window.width, state->window.height)) == NULL) - return (state_destroy(state)); - state->sprite_window.data = mlx_get_data_addr(state->sprite_window.id, - &state->sprite_window.depth, &state->sprite_window.size_line, - &state->sprite_window.endian); - printf("init mlx sprite image\n"); + /* state->sprite_window.width = state->window.width; */ + /* state->sprite_window.height = state->window.height; */ + /* if ((state->sprite_window.id = mlx_new_image(state->mlx_ptr, */ + /* state->window.width, state->window.height)) == NULL) */ + /* return (state_destroy(state)); */ + /* state->sprite_window.data = mlx_get_data_addr(state->sprite_window.id, */ + /* &state->sprite_window.depth, &state->sprite_window.size_line, */ + /* &state->sprite_window.endian); */ + /* printf("init mlx sprite image\n"); */ state_init_player(state); printf("init player\n"); + if ((state->z_buffer = (double*)malloc(sizeof(double) * state->window.width)) == NULL) + return (error_put_return_state_destroy("create z buffer", state)); + printf("init z_buffer\n"); + if (!state_init_sprites(state)) + return (error_put_return_state_destroy("create sprites pos", state)); return (state); } @@ -85,7 +95,7 @@ t_state *state_new_empty(void) state->mlx_ptr = NULL; state->window_ptr = NULL; state->window.id = NULL; - state->sprite_window.id = NULL; + /* state->sprite_window.id = NULL; */ i = -1; while (++i < TEXTURES_NUM) { @@ -97,6 +107,8 @@ t_state *state_new_empty(void) state->map = NULL; state->ceilling_color.hexcode = 0x0; state->floor_color.hexcode = 0x0; + state->z_buffer = NULL; + state->sprites = NULL; return (state); } @@ -107,6 +119,8 @@ void *state_destroy(t_state *state) if (state == NULL) return (NULL); i = -1; + free(state->z_buffer); + free(state->sprites); while (++i < TEXTURES_NUM) { free(state->textures_path[i]); @@ -116,9 +130,9 @@ void *state_destroy(t_state *state) printf("free window image\n"); if (state->mlx_ptr != NULL && state->window.id != NULL) mlx_destroy_image(state->mlx_ptr, state->window.id); - printf("free sprite window image\n"); - if (state->mlx_ptr != NULL && state->sprite_window.id != NULL) - mlx_destroy_image(state->mlx_ptr, state->sprite_window.id); + /* printf("free sprite window image\n"); */ + /* if (state->mlx_ptr != NULL && state->sprite_window.id != NULL) */ + /* mlx_destroy_image(state->mlx_ptr, state->sprite_window.id); */ printf("free window\n"); if (state->mlx_ptr && state->window_ptr) mlx_destroy_window(state->mlx_ptr, state->window_ptr); diff --git a/src/texture.c b/src/texture.c index ba95bd4..5be0ca1 100644 --- a/src/texture.c +++ b/src/texture.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/01 11:38:43 by cacharle #+# #+# */ -/* Updated: 2020/02/02 09:25:36 by cacharle ### ########.fr */ +/* Updated: 2020/02/02 18:09:00 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,8 @@ t_image *texture_select(t_state *state, t_render_state *rstate) { - if (rstate->target == CELL_ITEM) - return (state->textures + TEX_SPRITE); + /* if (rstate->target == CELL_ITEM) */ + /* return (state->textures + TEX_SPRITE); */ if (rstate->side == SIDE_NS) { if (rstate->map_pos.y < state->pos.y) diff --git a/src/vector.c b/src/vector.c index 44f784b..49a7344 100644 --- a/src/vector.c +++ b/src/vector.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/18 01:28:01 by cacharle #+# #+# */ -/* Updated: 2020/01/16 08:43:09 by cacharle ### ########.fr */ +/* Updated: 2020/02/02 22:29:59 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,13 @@ t_vector vector_add(t_vector a, t_vector b) return (a); } +t_vector vector_sub(t_vector a, t_vector b) +{ + a.x -= b.x; + a.y -= b.y; + return (a); +} + t_vector vector_scale(t_vector v, double scalar) { v.x *= scalar; |
