From 00b3612c1682f19c0f33a49a273aff392744656f Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Wed, 30 Mar 2022 12:55:39 +0200 Subject: [PATCH] add player moves and test image --- ' | 13 --- Makefile | 26 +++--- headers/cube3d_proto.h | 9 ++- srcs/draw/draw.c | 38 ++++++++- srcs/draw/player_limits.c | 18 +++++ srcs/draw/player_moves.c | 25 ++++++ srcs/hook/key_do_action.c | 10 +-- srcs/hook/keyhook.c | 36 --------- srcs/init/init_struct.c | 5 +- srcs/mem/memorybook.c | 3 +- test_lst.c | 162 -------------------------------------- 11 files changed, 109 insertions(+), 236 deletions(-) delete mode 100644 ' create mode 100644 srcs/draw/player_limits.c create mode 100644 srcs/draw/player_moves.c delete mode 100644 test_lst.c diff --git a/' b/' deleted file mode 100644 index f1fa8c1..0000000 --- a/' +++ /dev/null @@ -1,13 +0,0 @@ -#include "cube3d.h" - -void draw(t_game *game) -{ - unsigned int i; - -// mlx_pixel_put(game->mlx_ptr, game->win_ptr, game->plr_x, game->plr_y, 0x74db74); - i = -1; - while (i++ < 500) - game->img_data[i] = 0x00FF0000; - - mlx_put_image_to_window(game->mlx_ptr, game->win_ptr, game->img_ptr, 0, 0); -} diff --git a/Makefile b/Makefile index 51b7e6f..ce0f539 100644 --- a/Makefile +++ b/Makefile @@ -17,19 +17,21 @@ D_SRCS = srcs \ srcs/draw \ srcs/mem -SRCS = cube3d.c \ - \ - memorybook.c \ - memorybook_utils.c \ - \ - init_struct.c \ - init_parsing.c \ - \ - keyhook.c \ +SRCS = cube3d.c +# mem/ +SRCS += memorybook.c \ + memorybook_utils.c +# init/ +SRCS += init_struct.c \ + init_parsing.c +# hook/ +SRCS += keyhook.c \ key_do_action.c \ - key_is_action_1.c \ - \ - draw.c + key_is_action_1.c +# draw/ +SRCS += draw.c \ + player_moves.c \ + player_limits.c # headers D_HEADERS = headers diff --git a/headers/cube3d_proto.h b/headers/cube3d_proto.h index 8cc6fd2..2816e1e 100644 --- a/headers/cube3d_proto.h +++ b/headers/cube3d_proto.h @@ -26,7 +26,7 @@ void init_parsing(int ac, char **av, t_game *game); // ------------------------------- // SRC/HOOK // ------------------------------- -// key_hook.c +// keyhook.c int keypress(int keycode, t_game *game); int keyrelease(int keycode, t_game *game); // key_do_action.c @@ -44,5 +44,12 @@ int is_go_backward(int *k_hook); // ------------------------------- // draw.c void draw(t_game *game); +// player_moves.c +void plr_posx_decrement(t_game *game); +void plr_posy_decrement(t_game *game); +void plr_posx_increment(t_game *game); +void plr_posy_increment(t_game *game); +// player_limits.c +void plr_limits(t_game *game, int x, int y); #endif diff --git a/srcs/draw/draw.c b/srcs/draw/draw.c index 549b322..2b73b9f 100644 --- a/srcs/draw/draw.c +++ b/srcs/draw/draw.c @@ -1,15 +1,51 @@ #include "cube3d.h" +static int pxl_out_limits(t_game *game, int x, int y) +{ + int xmax; + int ymax; + + xmax = game->sizel / (game->bpp / 8); + ymax = game->win_size_y; + if (x < 0 || y < 0 || x > xmax || y > ymax) + return (1); + return (0); +} + static void draw_pixel(t_game *game, int x, int y, int color) { unsigned int position; + if (pxl_out_limits(game, x, y)) + return ; position = y * game->sizel + x * (game->bpp / 8); *(unsigned int*)(game->img_data + position) = color; } void draw(t_game *game) { - draw_pixel(game, game->plr_x, game->plr_y, 0x00FF0000); +// temp, draw a map of points + int x; + int y; + int x_size; +// unsigned int screen_size; + + x_size = game->sizel / (game->bpp / 8); +// screen_size = x_size * game->win_size_y / 5; + x = 0; + y = 0; + while (y <= game->win_size_y) + { +// y = x % x_size * 5; + draw_pixel(game, x, y, 0x00999999); + x += 5; + if (x > x_size) + { + x = 0; + y += 5; + } + } +// temp + draw_pixel(game, game->plr_x, game->plr_y, 0x0000FF00); mlx_put_image_to_window(game->mlx_ptr, game->win_ptr, game->img_ptr, 0, 0); } diff --git a/srcs/draw/player_limits.c b/srcs/draw/player_limits.c new file mode 100644 index 0000000..af4634c --- /dev/null +++ b/srcs/draw/player_limits.c @@ -0,0 +1,18 @@ +#include "cube3d.h" + +void plr_limits(t_game *game, int x, int y) +{ + int xmax; + int ymax; + + xmax = game->sizel / (game->bpp / 8); + ymax = game->win_size_y; + if (x < 0) + plr_posx_increment(game); + else if (y < 0) + plr_posy_increment(game); + else if (x > xmax) + plr_posx_decrement(game); + else if (y > ymax) + plr_posy_decrement(game); +} diff --git a/srcs/draw/player_moves.c b/srcs/draw/player_moves.c new file mode 100644 index 0000000..2e9e2c7 --- /dev/null +++ b/srcs/draw/player_moves.c @@ -0,0 +1,25 @@ +#include "cube3d.h" + +void plr_posx_decrement(t_game *game) +{ + (game->plr_x) -= 5; + plr_limits(game, game->plr_x, game->plr_y); +} + +void plr_posy_decrement(t_game *game) +{ + (game->plr_y) -= 5; + plr_limits(game, game->plr_x, game->plr_y); +} + +void plr_posx_increment(t_game *game) +{ + (game->plr_x) += 5; + plr_limits(game, game->plr_x, game->plr_y); +} + +void plr_posy_increment(t_game *game) +{ + (game->plr_y) += 5; + plr_limits(game, game->plr_x, game->plr_y); +} diff --git a/srcs/hook/key_do_action.c b/srcs/hook/key_do_action.c index c7c8fde..5257d1a 100644 --- a/srcs/hook/key_do_action.c +++ b/srcs/hook/key_do_action.c @@ -6,14 +6,12 @@ void keypress_do_action(t_game *game) if (is_esc(game->k_hook)) shut_down(game); if (is_go_left(game->k_hook)) - (game->plr_x) -= 5; + plr_posx_decrement(game); if (is_go_right(game->k_hook)) - (game->plr_x) += 5; + plr_posx_increment(game); if (is_go_forward(game->k_hook)) - (game->plr_y) -= 5; + plr_posy_decrement(game); if (is_go_backward(game->k_hook)) - (game->plr_y) += 5; + plr_posy_increment(game); draw(game); } - - diff --git a/srcs/hook/keyhook.c b/srcs/hook/keyhook.c index 4ea8526..a6e4e74 100644 --- a/srcs/hook/keyhook.c +++ b/srcs/hook/keyhook.c @@ -40,39 +40,3 @@ int keyrelease(int keycode, t_game *game) return (0); } - -/* - * first version, cannot combine key hook - */ - -/* - * static void keypress_action(int keycode, t_game *game) - * { - * // escape - * if (keycode == 65307) - * shut_down(game); - * // left - * if (keycode == 65361) - * (game->plr_x) -= 5; - * // right - * if (keycode == 65363) - * (game->plr_x) += 5; - * // up - * if (keycode == 65362) - * (game->plr_y) -= 5; - * // down - * if (keycode == 65364) - * (game->plr_y) += 5; - * mlx_pixel_put(game->mlx_ptr, game->win_ptr, game->plr_x, game->plr_y, 0x74db74); - * } - * - * int keypress(int keycode, t_game *game) - * { - * // temp - * print_keycode(keycode); - * - * keypress_action(keycode, game); - * return (0); - * } - * - */ diff --git a/srcs/init/init_struct.c b/srcs/init/init_struct.c index 726f781..29b9e5b 100644 --- a/srcs/init/init_struct.c +++ b/srcs/init/init_struct.c @@ -6,8 +6,8 @@ t_game *init_game(void) game = mb_alloc(sizeof(t_game)); // player first position - game->plr_x = 16; - game->plr_y = 16; + game->plr_x = 0; + game->plr_y = 0; // size window game->win_size_x = 500; game->win_size_y = 500; @@ -26,4 +26,3 @@ t_game *init_game(void) &(game->sizel), &(game->endian)); return (game); } - diff --git a/srcs/mem/memorybook.c b/srcs/mem/memorybook.c index 31c8b48..9c4fddb 100644 --- a/srcs/mem/memorybook.c +++ b/srcs/mem/memorybook.c @@ -44,7 +44,7 @@ void mb_free(void *addr) lst = mb_lst(); tmp = ft_lstfind((*lst), addr, mb_comp_addr); if (!tmp) - ft_putstr_fd(B_RED"failed free element"RESET"\n", 2); + ft_putstr_fd(B_RED"element to free doesn't exist (maybe it was already freed)"RESET"\n", 2); ft_lsterase(tmp, free); } @@ -61,4 +61,3 @@ void mb_exit(char *str) ft_lstfree((*lst), free); exit(0); } - diff --git a/test_lst.c b/test_lst.c deleted file mode 100644 index e92f8fb..0000000 --- a/test_lst.c +++ /dev/null @@ -1,162 +0,0 @@ -#include "libs/libft/includes/libft.h" -#include - -void lst_print(void *element) -{ - printf("lst->element : %s - %p\n",(char *)element, element); -} - -void lst_delete(void *element) -{ - free(element); -} - -int lst_comp_ptr(void *to_find, void *to_compare) -{ - return (to_find == to_compare); -} - -int lst_comp(void *to_find, void *to_compare) -{ - return (!ft_strcmp((char *)to_find, (char *)to_compare)); -} - -void *lst_copy(void *to_copy) -{ - char *copy; - - copy = ft_memalloc(sizeof(char) * (ft_strlen(to_copy) + 1)); - copy = ft_strcpy(copy, to_copy); - return (copy); -} - -// -// ft_lstnew -// ft_lstadd_back -// ft_lstadd_front -// ft_lstclear -// ft_lstdelone -// ft_lstiter -// ft_lstlast -// ft_lstmap -// ft_lstsize -// -// ft_lstfind -// ft_lstremove -// ft_lstremove_next -// -// ft_lstcreate <-- -// ft_lstpush_back <-- -// ft_lstpush_front <-- -// ft_lstloop <-- -// ft_lstloop_back <-- -// ft_lstbegin <-- -// ft_lstend <-- -// ft_lstfind <-- -// ft_lstinsert <-- -// ft_lsterase <-- -// ft_lstfree <-- -// ft_lstlen <-- -// ft_lstcopy <-- -// - -int main(void) -{ - t_list *toto = NULL; - t_list *tmp; - char *str; - char *str2; - int c = 'f'; - -/////////////////////////////////////////////////////////// - printf("\npush back ------------\n"); - for (int i = 0; i < 5; i++) - { - str = ft_memalloc(2 * sizeof(char)); - str[1] = '\0'; - str[0] = c++; - ft_lstpush_back(&toto, ft_lstcreate(str)); - } - printf("print forward --------\n"); - ft_lstloop(toto, lst_print); - printf("print backward -------\n"); - ft_lstloop_back(ft_lstend(toto), lst_print); - printf("len ------------------\n"); - printf("%i\n", ft_lstlen(toto)); - -/////////////////////////////////////////////////////////// - printf("\npush front -----------\n"); - c = 'e'; - for (int i = 0; i < 5; i++) - { - str = ft_memalloc(2 * sizeof(char)); - str[1] = '\0'; - str[0] = c--; - ft_lstpush_front(&toto, ft_lstcreate(str)); - } - printf("print forward --------\n"); - ft_lstloop(toto, lst_print); - printf("print backward -------\n"); - ft_lstloop_back(ft_lstend(toto), lst_print); - printf("len ------------------\n"); - printf("%i\n", ft_lstlen(toto)); - -/////////////////////////////////////////////////////////// - printf("\nfind -----------------\n"); - tmp = ft_lstfind(toto, "e", lst_comp); - if (tmp) - lst_print(tmp->content); - -/////////////////////////////////////////////////////////// - printf("\ninsert ---------------\n"); - str = ft_memalloc(2 * sizeof(char)); - str[1] = '\0'; - str[0] = 'E'; - ft_lstinsert(tmp, ft_lstcreate(str)); - printf("print from insert ----\n"); - ft_lstloop(tmp, lst_print); - printf("print from begin -----\n"); - ft_lstloop(toto, lst_print); - printf("print backward -------\n"); - ft_lstloop_back(ft_lstend(toto), lst_print); - printf("len ------------------\n"); - printf("%i\n", ft_lstlen(toto)); - -/////////////////////////////////////////////////////////// - printf("\nerase ----------------\n"); - ft_lsterase(ft_lstfind(toto, "E", lst_comp), lst_delete); - printf("print forward --------\n"); - ft_lstloop(toto, lst_print); - printf("print backward -------\n"); - ft_lstloop_back(ft_lstend(toto), lst_print); - printf("len ------------------\n"); - printf("%i\n", ft_lstlen(toto)); - -/////////////////////////////////////////////////////////// - printf("\nfree -----------------\n"); - tmp = ft_lstfind(toto, "e", lst_comp); - ft_lstfree(tmp, lst_delete); - printf("print forward --------\n"); - ft_lstloop(toto, lst_print); - printf("print backward -------\n"); - ft_lstloop_back(ft_lstend(toto), lst_print); - printf("len ------------------\n"); - printf("%i\n", ft_lstlen(toto)); - -/////////////////////////////////////////////////////////// - printf("\ncopy -----------------\n"); - tmp = ft_lstcopy(toto, lst_copy); - printf("print forward toto ---\n"); - ft_lstloop(toto, lst_print); - printf("print forward tmp ----\n"); - ft_lstloop(tmp, lst_print); - printf("len tmp --------------\n"); - printf("%i\n", ft_lstlen(tmp)); - ft_lstfree(tmp, lst_delete); - -/////////////////////////////////////////////////////////// - ft_lstfree(toto, lst_delete); - - return (0); -} -