add player moves and test image

This commit is contained in:
Hugo LAMY
2022-03-30 12:55:39 +02:00
parent f645da7885
commit 08f7b89d82
10 changed files with 109 additions and 223 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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);
}

18
srcs/draw/player_limits.c Normal file
View File

@@ -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);
}

25
srcs/draw/player_moves.c Normal file
View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
* }
*
*/

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -1,162 +0,0 @@
#include "libs/libft/includes/libft.h"
#include <stdio.h>
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);
}