diff --git a/Makefile b/Makefile index 61df04a..c467e11 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: tsuchen +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/08/15 12:02:08 by tsuchen #+# #+# # -# Updated: 2024/09/03 16:36:38 by tsuchen ### ########.fr # +# Updated: 2024/09/04 09:59:33 by tsuchen ### ########.fr # # # # **************************************************************************** # @@ -32,7 +32,7 @@ SRCS_RC = raycasting.c render_wall.c render_utils.c SRCS_VEC = vector.c vector_2.c vector_3.c -SRCS_FRM = lst_add_back.c lst_clear.c lst_last.c lst_new.c lst_size.c +SRCS_FRM = frame_lst_utils.c sprite.c PATH_M = srcs/ PATH_PS = srcs/parser/ diff --git a/includes/cub3d.h b/includes/cub3d.h index 7697107..e8f2d59 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,7 +6,7 @@ /* By: tsuchen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 00:19:44 by tsuchen #+# #+# */ -/* Updated: 2024/09/03 18:39:21 by tsuchen ### ########.fr */ +/* Updated: 2024/09/04 12:28:56 by tsuchen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,7 +54,7 @@ # define MINI_MAP_W 200 # define MINI_MAP_H 200 # define MINI_MAP_X 15 -# define MINI_MAP_Y 15 +# define MINI_MAP_Y 505 # define KEY_PRESS 2 # define MOUSE_PRESS 4 # define MOUSE_MOVE 6 @@ -77,10 +77,12 @@ typedef enum e_keys ARROW_UP = 65362, ARROW_DOWN = 65364, ESC_KEY = 65307, + SPACE_KEY = 32, W_KEY = 119, A_KEY = 97, S_KEY = 115, D_KEY = 100, + R_KEY = 114, M1 = 65307, P_KEY = 112, } t_keys; @@ -142,6 +144,7 @@ int key_events(int keycode, t_data *data); int mouse_move(int x, int y, t_data *data); void move_check(t_vec *step, t_data *data, int add_or_sub); int mouse_press(int button, int x, int y, t_data *data); +void interact_door(t_data *data); /* color utils*/ int create_trgb(int t, int r, int g, int b); int get_color(int trgb, char index); @@ -157,5 +160,8 @@ void rc_mlx_pixel_put(t_image *image, int x, int y, int color); void rc_stripe_pixel_put(t_data *data, int x, double ray_dist); /* Mini Map */ void put_mini_map(t_data *data); +/* Sprite */ +void play_sprite_forward(t_data *data, int map_x, int map_y); +void play_sprite_backward(t_data *data, int map_x, int map_y); #endif diff --git a/srcs/color_utils.c b/srcs/color_utils.c index ee0241b..65cc7e3 100644 --- a/srcs/color_utils.c +++ b/srcs/color_utils.c @@ -6,7 +6,7 @@ /* By: tsuchen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/05 15:01:46 by tsuchen #+# #+# */ -/* Updated: 2024/08/29 11:13:15 by tsuchen ### ########.fr */ +/* Updated: 2024/09/04 09:28:05 by tsuchen ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/srcs/events/key_events.c b/srcs/events/key_events.c index f3bf8fd..a1f81e3 100644 --- a/srcs/events/key_events.c +++ b/srcs/events/key_events.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* key_events.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jteissie +#+ +:+ +#+ */ +/* By: tsuchen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/27 18:40:50 by jteissie #+# #+# */ -/* Updated: 2024/09/02 17:28:10 by jteissie ### ########.fr */ +/* Updated: 2024/09/04 09:48:55 by tsuchen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,6 +54,12 @@ static void key_move_camera(t_data *data, t_rot dir) return ; } +static void turn_around(t_data *data) +{ + vec_mirror(&data->p_cam); + vec_mirror(&data->p_dir); +} + static void pause_game(t_data *data) { if (data->pause == TRUE) @@ -78,6 +84,10 @@ int key_events(int keycode, t_data *data) move_player(data, LEFT); else if (keycode == D_KEY && data->pause == FALSE) move_player(data, RIGHT); + else if (keycode == R_KEY && data->pause == FALSE) + turn_around(data); + else if (keycode == SPACE_KEY && data->pause == FALSE) + interact_door(data); else if (keycode == ESC_KEY) cleanup(data); return (SUCCESS); diff --git a/srcs/events/mouse_events.c b/srcs/events/mouse_events.c index 539991f..520db23 100644 --- a/srcs/events/mouse_events.c +++ b/srcs/events/mouse_events.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* mouse_events.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jteissie +#+ +:+ +#+ */ +/* By: tsuchen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/02 11:59:13 by jteissie #+# #+# */ -/* Updated: 2024/09/02 17:25:01 by jteissie ### ########.fr */ +/* Updated: 2024/09/04 12:25:22 by tsuchen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,12 +46,12 @@ static void change_door_state(t_data *data, t_vec *p_ray) door_x = data->p_pos.x + p_ray->x; door_y = data->p_pos.y + p_ray->y; if (data->map[door_x][door_y] == 'D') - data->map[door_x][door_y] = 'O'; + play_sprite_forward(data, door_x, door_y); else if (data->map[door_x][door_y] == 'O') - data->map[door_x][door_y] = 'D'; + play_sprite_backward(data, door_x, door_y); } -static void interact_door(t_data *data) +void interact_door(t_data *data) { t_vec p_ray; double dist; diff --git a/srcs/frames/lst_new.c b/srcs/frames/frame_lst_utils.c similarity index 51% rename from srcs/frames/lst_new.c rename to srcs/frames/frame_lst_utils.c index 5c4fd86..78c211c 100644 --- a/srcs/frames/lst_new.c +++ b/srcs/frames/frame_lst_utils.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* lst_new.c :+: :+: :+: */ +/* frame_lst_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: tsuchen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/05/10 11:28:30 by tsuchen #+# #+# */ -/* Updated: 2024/09/03 16:17:52 by tsuchen ### ########.fr */ +/* Created: 2024/05/10 11:39:29 by tsuchen #+# #+# */ +/* Updated: 2024/09/04 09:58:46 by tsuchen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,17 +23,58 @@ t_frame *lst_new(void) lst->prev = NULL; return (lst); } -/* -#include -int main(void) + +void lst_add_back(t_frame **lst, t_frame *new_node) +{ + t_frame *tmp; + + if (!new_node) + return ; + if (!(*lst)) + { + *lst = new_node; + return ; + } + tmp = *lst; + while (tmp->next) + tmp = tmp->next; + new_node->prev = tmp; + tmp->next = new_node; +} + +void lst_clear(t_frame **lst, void (*del)(void *)) { - int a = 7; - t_list *lst; - - lst = ft_lstnew(&a); - printf("val_lst: %d, add_lst: %p\n", *((int *)(lst->content)), lst->content); - printf("val_a : %d, add_a : %p\n", a, &a); - printf("add of nex in lst: %p\n", lst->next); - free(lst); - return (0); -}*/ + t_frame *tmp; + + while (*lst) + { + tmp = *lst; + *lst = (*lst)->next; + del(tmp); + } +} + +t_frame *lst_last(t_frame *lst) +{ + t_frame *tmp; + + tmp = lst; + while (tmp->next) + tmp = tmp->next; + return (tmp); +} + +int lst_size(t_frame *lst) +{ + int i; + t_frame *tmp; + + tmp = lst; + i = 0; + while (tmp) + { + i++; + tmp = tmp->next; + } + return (i); +} diff --git a/srcs/frames/lst_add_back.c b/srcs/frames/lst_add_back.c deleted file mode 100644 index aaa3fa5..0000000 --- a/srcs/frames/lst_add_back.c +++ /dev/null @@ -1,118 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* lstadd_back.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: tsuchen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/05/10 11:39:29 by tsuchen #+# #+# */ -/* Updated: 2024/09/03 16:11:30 by tsuchen ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "frame.h" - -void lst_add_back(t_frame **lst, t_frame *new_node) -{ - t_frame *tmp; - - if (!new_node) - return ; - if (!(*lst)) - { - *lst = new_node; - return ; - } - tmp = *lst; - while (tmp->next) - tmp = tmp->next; - new_node->prev = tmp; - tmp->next = new_node; -} -/* Note - * 1. if lst (or bgn_lst) is NULL, new will be the 1st lst - */ -/* -#include -void ft_print_result(t_list *elem) -{ - int len; - - len = 0; - while (((char *)elem->content)[len]) - len++; - write(1, elem->content, len); - write(1, "\n", 1); -} - -t_list *ft_lstnewone(void *content) -{ - t_list *elem; - - elem = (t_list *)malloc(sizeof(t_list)); - if (!elem) - return (NULL); - if (!content) - elem->content = NULL; - else - elem->content = content; - elem->next = NULL; - return (elem); -} - -int main(int argc, const char *argv[]) -{ - t_list *begin; - t_list *elem; - t_list *elem2; - t_list *elem3; - t_list *elem4; - char *str = strdup("lorem"); - char *str2 = strdup("ipsum"); - char *str3 = strdup("dolor"); - char *str4 = strdup("sit"); - - elem = ft_lstnewone(str); - elem2 = ft_lstnewone(str2); - elem3 = ft_lstnewone(str3); - elem4 = ft_lstnewone(str4); - alarm(5); - if (argc == 1 || !elem || !elem2 || !elem3 || !elem4) - return (0); - else if (atoi(argv[1]) == 1) - { - begin = NULL; - ft_lstadd_back(&begin, elem); - ft_lstadd_back(&begin, elem2); - ft_lstadd_back(&begin, elem3); - ft_lstadd_back(&begin, elem4); - while (begin) - { - ft_print_result(begin); - begin = begin->next; - } - } - return (0); -}*/ -/* -#include -t_list *ft_lstnew(void *content); -int main(void) -{ - int a = 4; - int b = 3; - t_list *tmp1; - t_list *tmp2; - - t_list *lst_0; - t_list *lst_1; - lst_0 = ft_lstnew(&a); - lst_1 = ft_lstnew(&b); - ft_lstadd_back(&lst_1, lst_0); - tmp1 = lst_1; - printf("Next content of lst_1 is: %d\n", *(int *)(tmp1->next->content)); - tmp2 = lst_1->next; - free(tmp2); - free(lst_1); - return (0); -}*/ diff --git a/srcs/frames/lst_clear.c b/srcs/frames/lst_clear.c deleted file mode 100644 index d6f7ab5..0000000 --- a/srcs/frames/lst_clear.c +++ /dev/null @@ -1,84 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* lst_clear.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: tsuchen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/05/10 13:09:48 by tsuchen #+# #+# */ -/* Updated: 2024/09/03 16:37:58 by tsuchen ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "frame.h" - -void lst_clear(t_frame **lst, void (*del)(void *)) -{ - t_frame *tmp; - - while (*lst) - { - tmp = *lst; - *lst = (*lst)->next; - del(tmp); - } -} - -/* Note - * 1. if *lst (or bgn_lst) is NULL, do nothing (already cleared) - * 2. if function "del" is NULL, it will crash at lstdelone - */ -/* -#include -t_list *ft_lstnew(void *content); -void ft_lstadd_back(t_list **lst, t_list *new); - -void del_content(void *content) -{ - free(content); -} - -int main(void) -{ - t_list *begin_list; - int a = 3; - int b = 4; - int i = 0; - t_list *tmp; - t_list *tmp2; - t_list *o_next; - int *o_data; - - begin_list = ft_lstnew(ft_lstnew(&a)); - tmp = begin_list; - while (i < 10) - { - ft_lstadd_back(&begin_list, ft_lstnew(ft_lstnew(&b))); - i++; - } - printf("content |current add |next add\n"); - while (tmp) - { - printf("%p |%p |%p\n", ((*tmp).content), tmp, (*tmp).next); - tmp = (*tmp).next; - } - // save original record - o_next = begin_list->next; - o_data = begin_list->content; - // clear the list - tmp2 = begin_list; - ft_lstclear(&begin_list, &del_content); - printf(" ==> after free <==\n"); - printf("bgn_lst %p, %p\n", begin_list, &begin_list); - printf("tmp2 %p, %p\n", tmp2, &tmp2); - printf(" ======================= \n"); - printf("data before: %p; after: %p\n", o_data, tmp2->content); - if (tmp2->content != o_data) - printf("==> list data no longer exists!\n"); - printf("next before: %p; after: %p\n", o_next, tmp2->next); - if (tmp2->next != o_next) - printf("==> list link no longer exists!\n"); - return (0); -} -// The 4 errors are due to tmp2->content and tmp2-> after free -*/ diff --git a/srcs/frames/lst_last.c b/srcs/frames/lst_last.c deleted file mode 100644 index f9afde5..0000000 --- a/srcs/frames/lst_last.c +++ /dev/null @@ -1,57 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* lst_last.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: tsuchen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/05/10 12:53:24 by tsuchen #+# #+# */ -/* Updated: 2024/09/03 17:27:23 by tsuchen ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "frame.h" - -t_frame *lst_last(t_frame *lst) -{ - t_frame *tmp; - - tmp = lst; - while (tmp->next) - tmp = tmp->next; - return (tmp); -} - -/* Note - * 1. if lst is NULL, it will return NULL - */ -/* -#include -void ft_lstadd_front(t_list **lst, t_list *new); -t_list *ft_lstnew(void *content); - -int main(void) -{ - t_list *lst; - t_list *last; - int a = 3; - int b = 4; - int i = 0; - t_list *tmp; - - lst = ft_lstnew(&a); - while (i < 10) - { - ft_lstadd_front(&lst, ft_lstnew(&b)); - i++; - } - last = ft_lstlast(lst); - printf("value of last list is: %d\n", *(int *)(*last).content); - while (lst) - { - tmp = lst; - lst = lst->next; - free(tmp); - } - return (0); -}*/ diff --git a/srcs/frames/lst_size.c b/srcs/frames/lst_size.c deleted file mode 100644 index b76498d..0000000 --- a/srcs/frames/lst_size.c +++ /dev/null @@ -1,55 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* lst_size.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: tsuchen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/05/10 12:42:49 by tsuchen #+# #+# */ -/* Updated: 2024/09/03 16:25:05 by tsuchen ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "frame.h" - -int lst_size(t_frame *lst) -{ - int i; - t_frame *tmp; - - tmp = lst; - i = 0; - while (tmp) - { - i++; - tmp = tmp->next; - } - return (i); -} -/* -#include -void ft_lstadd_front(t_list **lst, t_list *new); -t_list *ft_lstnew(void *content); - -int main(void) -{ - t_list *lst; - int a = 3; - int i = 0; - t_list *tmp1; - - lst = ft_lstnew(&a); - while (i < 10) - { - ft_lstadd_front(&lst, ft_lstnew(&a)); - i++; - } - printf("size of list is: %d\n", ft_lstsize(lst)); - while (lst) - { - tmp1 = lst; - lst = lst->next; - free(tmp1); - } - return (0); -}*/ diff --git a/srcs/frames/sprite.c b/srcs/frames/sprite.c new file mode 100644 index 0000000..ed02f08 --- /dev/null +++ b/srcs/frames/sprite.c @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sprite.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tsuchen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/04 09:59:13 by tsuchen #+# #+# */ +/* Updated: 2024/09/04 12:35:03 by tsuchen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +void play_sprite_forward(t_data *data, int map_x, int map_y) +{ + t_frame *beg_frame; + t_frame door_tmp; + + beg_frame = data->frames; + door_tmp.img = data->textures->text_img[D]; + door_tmp.addr = data->textures->text_addr[D]; + door_tmp.frm_h = data->textures->tex_h[D]; + door_tmp.frm_w = data->textures->tex_w[D]; + while (beg_frame) + { + data->textures->text_img[D] = beg_frame->img; + data->textures->text_addr[D] = beg_frame->addr; + data->textures->tex_h[D] = beg_frame->frm_h; + data->textures->tex_w[D] = beg_frame->frm_w; + rc_rendering(data); + mlx_do_sync(data->mlx); + beg_frame = beg_frame->next; + usleep(100000); + } + data->map[map_x][map_y] = 'O'; + data->textures->text_img[D] = door_tmp.img; + data->textures->text_addr[D] = door_tmp.addr; + data->textures->tex_h[D] = door_tmp.frm_h; + data->textures->tex_w[D] = door_tmp.frm_h; +} + +void play_sprite_backward(t_data *data, int map_x, int map_y) +{ + t_frame *beg_frame; + t_frame door_tmp; + + beg_frame = lst_last(data->frames); + door_tmp.img = data->textures->text_img[D]; + door_tmp.addr = data->textures->text_addr[D]; + door_tmp.frm_h = data->textures->tex_h[D]; + door_tmp.frm_w = data->textures->tex_w[D]; + data->map[map_x][map_y] = 'D'; + while (beg_frame) + { + data->textures->text_img[D] = beg_frame->img; + data->textures->text_addr[D] = beg_frame->addr; + data->textures->tex_h[D] = beg_frame->frm_h; + data->textures->tex_w[D] = beg_frame->frm_w; + rc_rendering(data); + mlx_do_sync(data->mlx); + beg_frame = beg_frame->prev; + usleep(100000); + } + data->textures->text_img[D] = door_tmp.img; + data->textures->text_addr[D] = door_tmp.addr; + data->textures->tex_h[D] = door_tmp.frm_h; + data->textures->tex_w[D] = door_tmp.frm_h; +} diff --git a/srcs/main.c b/srcs/main.c index 0b2b87a..84fa48c 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: tsuchen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 12:03:33 by tsuchen #+# #+# */ -/* Updated: 2024/09/03 18:58:08 by tsuchen ### ########.fr */ +/* Updated: 2024/09/04 09:42:34 by tsuchen ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/srcs/map/map.c b/srcs/map/map.c index 51d519b..64d143a 100644 --- a/srcs/map/map.c +++ b/srcs/map/map.c @@ -6,7 +6,7 @@ /* By: tsuchen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 12:13:17 by tsuchen #+# #+# */ -/* Updated: 2024/09/03 19:22:23 by tsuchen ### ########.fr */ +/* Updated: 2024/09/04 09:27:49 by tsuchen ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/srcs/raycasting/raycasting.c b/srcs/raycasting/raycasting.c index 61a197e..ee9b5c8 100644 --- a/srcs/raycasting/raycasting.c +++ b/srcs/raycasting/raycasting.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* raycasting.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jteissie +#+ +:+ +#+ */ +/* By: tsuchen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 12:13:34 by tsuchen #+# #+# */ -/* Updated: 2024/09/02 17:25:33 by jteissie ### ########.fr */ +/* Updated: 2024/09/04 12:37:01 by tsuchen ### ########.fr */ /* */ /* ************************************************************************** */