add player moves and test image
This commit is contained in:
@@ -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
18
srcs/draw/player_limits.c
Normal 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
25
srcs/draw/player_moves.c
Normal 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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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);
|
||||
* }
|
||||
*
|
||||
*/
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user