Initial commit
This commit is contained in:
9
srcs/error.c
Normal file
9
srcs/error.c
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "so_long.h"
|
||||
#include "unistd.h"
|
||||
|
||||
int
|
||||
ft_put_error(void)
|
||||
{
|
||||
write(2, "Error\n", 6);
|
||||
return (1);
|
||||
}
|
||||
124
srcs/game_loop.c
Normal file
124
srcs/game_loop.c
Normal file
@@ -0,0 +1,124 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* game_loop.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/06/09 18:20:48 by lejulien #+# #+# */
|
||||
/* Updated: 2021/06/12 04:03:37 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "so_long.h"
|
||||
|
||||
void
|
||||
handle_moves(t_game *game)
|
||||
{
|
||||
if (game->key_w && game->player_pos.y != 0 &&
|
||||
game->map[(game->player_pos.y - 1) * game->mapX + game->player_pos.x] != '1')
|
||||
{
|
||||
game->player_pos.y--;
|
||||
game->key_w = 0;
|
||||
game->moves++;
|
||||
if (game->moves > 0)
|
||||
printf("%d moves\n", game->moves);
|
||||
}
|
||||
if (game->key_a && game->player_pos.x != 0 &&
|
||||
game->map[game->player_pos.y * game->mapX + (game->player_pos.x - 1)] != '1')
|
||||
{
|
||||
game->player_pos.x--;
|
||||
game->key_a = 0;
|
||||
game->moves++;
|
||||
if (game->moves > 0)
|
||||
printf("%d moves\n", game->moves);
|
||||
}
|
||||
if (game->key_s && game->player_pos.y != game->mapY &&
|
||||
game->map[(game->player_pos.y + 1) * game->mapX + game->player_pos.x] != '1')
|
||||
{
|
||||
game->player_pos.y++;
|
||||
game->key_s = 0;
|
||||
game->moves++;
|
||||
if (game->moves > 0)
|
||||
printf("%d moves\n", game->moves);
|
||||
}
|
||||
if (game->key_d && game->player_pos.x != game->mapX &&
|
||||
game->map[game->player_pos.y * game->mapX + (game->player_pos.x + 1)] != '1')
|
||||
{
|
||||
game->player_pos.x++;
|
||||
game->key_d= 0;
|
||||
game->moves++;
|
||||
if (game->moves > 0)
|
||||
printf("%d moves\n", game->moves);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
get_can(t_game *game)
|
||||
{
|
||||
if (game->map[game->player_pos.y * game->mapX + game->player_pos.x] == 'C')
|
||||
{
|
||||
game->coll++;
|
||||
game->map[game->player_pos.y * game->mapX + game->player_pos.x] = '0';
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
draw_map(t_game *game)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
|
||||
y = -1;
|
||||
while (y++ < game->mapY - 1)
|
||||
{
|
||||
x = -1;
|
||||
while (x++ < game->mapX - 1)
|
||||
{
|
||||
put_image(game, game->img->bg_img, set_pos(x * 64, y * 64));
|
||||
if (game->map[y * game->mapX + x] == '1')
|
||||
put_image(game, game->img->bloc_img, set_pos(x * 64, y * 64));
|
||||
else if (game->map[y * game->mapX + x] == 'C')
|
||||
put_image(game, game->img->coll_img, set_pos(x * 64, y * 64));
|
||||
else if (game->map[y * game->mapX + x] == 'E')
|
||||
put_image(game, game->img->portal_img, set_pos(x * 64, y * 64));
|
||||
}
|
||||
}
|
||||
put_image(game, game->img->player_img, set_pos(game->player_pos.x * 64,
|
||||
game->player_pos.y * 64));
|
||||
}
|
||||
|
||||
void
|
||||
render_mouse(t_game *game)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
|
||||
mlx_mouse_get_pos(game->mlx->mlx_ptr, game->mlx->win_ptr, &x, &y);
|
||||
if (x < game->winX - game->img->mouse_img->width && x > 0 &&
|
||||
y < game->winY - game->img->mouse_img->height && y > 0)
|
||||
put_image(game, game->img->mouse_img, set_pos(x, y));
|
||||
}
|
||||
|
||||
void
|
||||
ft_exit(t_game *game)
|
||||
{
|
||||
if (game->map[game->player_pos.y * game->mapX + game->player_pos.x] == 'E')
|
||||
{
|
||||
if (game->coll == game->t_coll)
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
game_loop(t_game *game)
|
||||
{
|
||||
handle_moves(game);
|
||||
get_can(game);
|
||||
ft_exit(game);
|
||||
draw_map(game);
|
||||
render_mouse(game);
|
||||
mlx_put_image_to_window(game->mlx->mlx_ptr, game->mlx->win_ptr,
|
||||
game->img->canvas->img_ptr, 0, 0);
|
||||
return (0);
|
||||
}
|
||||
96
srcs/get_next_line.c
Normal file
96
srcs/get_next_line.c
Normal file
@@ -0,0 +1,96 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/11/29 01:57:21 by lejulien #+# #+# */
|
||||
/* Updated: 2021/06/10 15:29:34 by user42 ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line.h"
|
||||
|
||||
int ft_hasnl(char *str)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (!str || str[i] == '\0')
|
||||
return (-1);
|
||||
while (str[i] != '\0')
|
||||
{
|
||||
if (str[i] == '\n')
|
||||
return (i);
|
||||
i++;
|
||||
}
|
||||
return (-1);
|
||||
}
|
||||
|
||||
char *ft_read(int fd, char **buffer)
|
||||
{
|
||||
int len;
|
||||
char *whatiread;
|
||||
char *tmp;
|
||||
|
||||
len = BUFFER_SIZE;
|
||||
if (!(whatiread = malloc((BUFFER_SIZE + 1) * sizeof(char))))
|
||||
return (NULL);
|
||||
while (ft_hasnl(*buffer) == -1 && len == BUFFER_SIZE)
|
||||
{
|
||||
len = read(fd, whatiread, BUFFER_SIZE);
|
||||
whatiread[len] = 0;
|
||||
if (!(tmp = ft_strjoin(*buffer, whatiread)))
|
||||
return (NULL);
|
||||
if (*buffer != 0)
|
||||
free(*buffer);
|
||||
if (!(*buffer = ft_strdup(tmp)))
|
||||
return (NULL);
|
||||
free(tmp);
|
||||
}
|
||||
free(whatiread);
|
||||
return (*buffer);
|
||||
}
|
||||
|
||||
t_char *ft_cut(char *buffer)
|
||||
{
|
||||
t_char *cut;
|
||||
int nlpos;
|
||||
|
||||
if (!(cut = malloc(sizeof(t_char))))
|
||||
return (NULL);
|
||||
nlpos = ft_hasnl(buffer);
|
||||
buffer[nlpos] = '\0';
|
||||
if (!(cut->toline = ft_strdup(buffer)))
|
||||
return (NULL);
|
||||
if (!(cut->tobuff = ft_strdup(&buffer[nlpos + 1])))
|
||||
return (NULL);
|
||||
free(buffer);
|
||||
return (cut);
|
||||
}
|
||||
|
||||
int get_next_line(int fd, char **line)
|
||||
{
|
||||
static char *buffer = NULL;
|
||||
t_char *res;
|
||||
|
||||
if (line)
|
||||
*line = NULL;
|
||||
if (fd < 0 || BUFFER_SIZE < 1 || read(fd, NULL, 0) == -1
|
||||
|| !(buffer = ft_read(fd, &buffer)) || !line)
|
||||
return (-1);
|
||||
if (ft_hasnl(buffer) == -1)
|
||||
{
|
||||
*line = buffer;
|
||||
buffer = NULL;
|
||||
return (0);
|
||||
}
|
||||
if (!(res = ft_cut(buffer)))
|
||||
return (-1);
|
||||
*line = res->toline;
|
||||
buffer = res->tobuff;
|
||||
free(res);
|
||||
return (1);
|
||||
}
|
||||
|
||||
32
srcs/get_next_line.h
Normal file
32
srcs/get_next_line.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/11/29 00:35:55 by lejulien #+# #+# */
|
||||
/* Updated: 2021/06/10 15:30:19 by user42 ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef GET_NEXT_LINE_H
|
||||
# define GET_NEXT_LINE_H
|
||||
# include <unistd.h>
|
||||
# include <stdlib.h>
|
||||
|
||||
typedef struct s_char
|
||||
{
|
||||
char *tobuff;
|
||||
char *toline;
|
||||
} t_char;
|
||||
|
||||
size_t ft_strlen(const char *s);
|
||||
char *ft_strjoin(char const *s1, char const *s2);
|
||||
char *ft_strdup(const char *src);
|
||||
int get_next_line(int fd, char **line);
|
||||
t_char *ft_cut(char *buffer);
|
||||
char *ft_read(int fd, char **buffer);
|
||||
int ft_hasnl(char *str);
|
||||
#endif
|
||||
|
||||
69
srcs/get_next_line_utils.c
Normal file
69
srcs/get_next_line_utils.c
Normal file
@@ -0,0 +1,69 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/11/29 00:35:11 by lejulien #+# #+# */
|
||||
/* Updated: 2021/06/10 15:30:56 by user42 ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line.h"
|
||||
|
||||
char *ft_strjoin(char const *s1, char const *s2)
|
||||
{
|
||||
char *s3;
|
||||
int allocplace;
|
||||
char *writeptr;
|
||||
|
||||
if (!s1 && !s2)
|
||||
return (0);
|
||||
if (!s1)
|
||||
return (ft_strdup((char *)s2));
|
||||
if (!s2)
|
||||
return (ft_strdup((char *)s1));
|
||||
allocplace = ft_strlen(s1) + ft_strlen(s2) + 1;
|
||||
if (!(s3 = malloc(allocplace * sizeof(char))))
|
||||
return (0);
|
||||
writeptr = s3;
|
||||
while (*s1 != '\0')
|
||||
*writeptr++ = *s1++;
|
||||
while (*s2 != '\0')
|
||||
*writeptr++ = *s2++;
|
||||
*writeptr++ = '\0';
|
||||
return (s3);
|
||||
}
|
||||
|
||||
size_t ft_strlen(const char *s)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
if (!s)
|
||||
return (0);
|
||||
while (s[i] != '\0')
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
char *ft_strdup(const char *src)
|
||||
{
|
||||
char *dest;
|
||||
int size;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
size = ft_strlen(src) + 1;
|
||||
if (!(dest = (char *)malloc(size * sizeof(char))))
|
||||
return (NULL);
|
||||
while (src[i] != '\0')
|
||||
{
|
||||
dest[i] = src[i];
|
||||
i++;
|
||||
}
|
||||
dest[i] = '\0';
|
||||
return (dest);
|
||||
}
|
||||
|
||||
51
srcs/getmap.c
Normal file
51
srcs/getmap.c
Normal file
@@ -0,0 +1,51 @@
|
||||
|
||||
#include "so_long.h"
|
||||
#include "get_next_line.h"
|
||||
#include "fcntl.h"
|
||||
|
||||
void
|
||||
ft_getmap(t_game *game, char *path)
|
||||
{
|
||||
int fd;
|
||||
char *line;
|
||||
char *longmap;
|
||||
char *tmp;
|
||||
int length;
|
||||
int old_length;
|
||||
|
||||
old_length = 0;
|
||||
fd = open(path, O_RDONLY);
|
||||
longmap = NULL;
|
||||
if (fd == -1)
|
||||
{
|
||||
game->map = NULL;
|
||||
return ;
|
||||
}
|
||||
while (get_next_line(fd, &line))
|
||||
{
|
||||
length = ft_strlen(line);
|
||||
if (length)
|
||||
{
|
||||
tmp = ft_strjoin(longmap, line);
|
||||
free(longmap);
|
||||
longmap = ft_strdup(tmp);
|
||||
free(tmp);
|
||||
}
|
||||
if (length != 0)
|
||||
{
|
||||
if (old_length == 0 || length == old_length)
|
||||
old_length = length;
|
||||
else
|
||||
{
|
||||
free(line);
|
||||
game->map = NULL;
|
||||
return ;
|
||||
}
|
||||
}
|
||||
free(line);
|
||||
}
|
||||
free(line);
|
||||
game->mapX = old_length;
|
||||
game->mapY = ft_strlen(longmap) / old_length;
|
||||
game->map = longmap;
|
||||
}
|
||||
43
srcs/keys.c
Normal file
43
srcs/keys.c
Normal file
@@ -0,0 +1,43 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* keys.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/06/11 18:36:19 by lejulien #+# #+# */
|
||||
/* Updated: 2021/06/12 02:57:12 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "so_long.h"
|
||||
|
||||
int
|
||||
key_pressed(int keycode, t_game *game)
|
||||
{
|
||||
if (keycode == 119)
|
||||
game->key_w = 1;
|
||||
if (keycode == 97)
|
||||
game->key_a = 1;
|
||||
if (keycode == 115)
|
||||
game->key_s = 1;
|
||||
if (keycode == 100)
|
||||
game->key_d = 1;
|
||||
if (keycode == 65307)
|
||||
exit(0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
key_released(int keycode, t_game *game)
|
||||
{
|
||||
if (keycode == 119)
|
||||
game->key_w = 0;
|
||||
if (keycode == 97)
|
||||
game->key_a = 0;
|
||||
if (keycode == 115)
|
||||
game->key_s = 0;
|
||||
if (keycode == 100)
|
||||
game->key_d = 0;
|
||||
return (0);
|
||||
}
|
||||
133
srcs/main.c
Normal file
133
srcs/main.c
Normal file
@@ -0,0 +1,133 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/06/08 17:06:28 by lejulien #+# #+# */
|
||||
/* Updated: 2021/06/12 04:02:57 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "so_long.h"
|
||||
|
||||
int
|
||||
load_mlx(t_mlx *mlx, int width, int height)
|
||||
{
|
||||
|
||||
mlx->mlx_ptr = mlx_init();
|
||||
if (!mlx->mlx_ptr)
|
||||
return (1);
|
||||
mlx->win_ptr = mlx_new_window(mlx->mlx_ptr, width*64, height*64, "so_long");
|
||||
if (!mlx->win_ptr)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
init_game(t_game *game, t_mlx *mlx, int tileX, int tileY)
|
||||
{
|
||||
game->mlx = mlx;
|
||||
game->winX = tileX * 64;
|
||||
game->winY = tileY * 64;
|
||||
game->coll = 0;
|
||||
game->moves = -4;
|
||||
}
|
||||
|
||||
t_img_d
|
||||
*create_and_get_date(t_game *g, t_img *img, char *path)
|
||||
{
|
||||
t_img_d *ret;
|
||||
|
||||
ret = malloc(sizeof(t_img_d));
|
||||
if (!ret)
|
||||
return (NULL);
|
||||
if (!path)
|
||||
ret->img_ptr = mlx_new_image(g->mlx->mlx_ptr, g->winX, g->winY);
|
||||
else
|
||||
ret->img_ptr = mlx_xpm_file_to_image(g->mlx->mlx_ptr, path, &ret->width,
|
||||
&ret->height);
|
||||
if (!ret->img_ptr)
|
||||
return (NULL);
|
||||
ret->data = (int *)mlx_get_data_addr(ret->img_ptr, &ret->bpp, &ret->size_l,
|
||||
&ret->endian);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
int
|
||||
load_img(t_game *g, t_img *img)
|
||||
{
|
||||
img->canvas = create_and_get_date(g, img, NULL);
|
||||
img->bg_img = create_and_get_date(g, img, "./sprites/aqua.xpm");
|
||||
img->player_img = create_and_get_date(g, img, "./sprites/dolphin.xpm");
|
||||
img->bloc_img = create_and_get_date(g, img, "./sprites/bloc.xpm");
|
||||
img->coll_img = create_and_get_date(g, img, "./sprites/soda.xpm");
|
||||
img->mouse_img = create_and_get_date(g, img, "./sprites/cursor.xpm");
|
||||
img->portal_img = create_and_get_date(g, img, "./sprites/portal.xpm");
|
||||
if (!img->bg_img || !img->player_img || !img->bloc_img || !img->coll_img ||
|
||||
!img->mouse_img || !img->canvas || !img->portal_img)
|
||||
return (1);
|
||||
g->img = img;
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
load_coll(t_game *game)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
int count;
|
||||
|
||||
y = -1;
|
||||
count = 0;
|
||||
while (y++ < game->mapY - 1)
|
||||
{
|
||||
x = -1;
|
||||
while (x++ < game->mapX - 1)
|
||||
{
|
||||
if (game->map[y * game->mapX + x] == 'C')
|
||||
count++;
|
||||
}
|
||||
}
|
||||
game->t_coll = count;
|
||||
}
|
||||
|
||||
int
|
||||
close(int keycode, t_game *game)
|
||||
{
|
||||
if (keycode > 10 || keycode < 0) //free data later
|
||||
exit(0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
main(int ac, char **av)
|
||||
{
|
||||
t_mlx mlx;
|
||||
t_game game;
|
||||
t_img img;
|
||||
t_vec2 mapSize;
|
||||
|
||||
if (ac == 2)
|
||||
{
|
||||
ft_getmap(&game, av[1]);
|
||||
if (game.map == NULL)
|
||||
return (ft_put_error());
|
||||
if (mapSize.x == -1)
|
||||
return (ft_put_error());
|
||||
get_player_pos(&game);
|
||||
load_coll(&game);
|
||||
init_game(&game, &mlx, game.mapX, game.mapY);
|
||||
if (load_mlx(&mlx, game.mapX, game.mapY) || load_img(&game, &img))
|
||||
return (ft_put_error());
|
||||
mlx_mouse_hide(mlx.mlx_ptr, mlx.win_ptr);
|
||||
mlx_hook(mlx.win_ptr, 17, 0L, close, &game);
|
||||
mlx_loop_hook(mlx.mlx_ptr, game_loop, &game);
|
||||
mlx_hook(mlx.win_ptr, 02, (1L<<0), key_pressed, &game);
|
||||
mlx_hook(mlx.win_ptr, 03, (1L<<1), key_released, &game);
|
||||
mlx_loop(mlx.mlx_ptr);
|
||||
return (0);
|
||||
}
|
||||
return (ft_put_error());
|
||||
}
|
||||
35
srcs/player_info.c
Normal file
35
srcs/player_info.c
Normal file
@@ -0,0 +1,35 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* player_info.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/06/10 20:58:48 by lejulien #+# #+# */
|
||||
/* Updated: 2021/06/10 21:07:27 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "so_long.h"
|
||||
|
||||
void
|
||||
get_player_pos(t_game *game)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
j = -1;
|
||||
while (j++ < game->mapY - 1)
|
||||
{
|
||||
i = -1;
|
||||
while (i++ < game->mapX - 1)
|
||||
{
|
||||
if (game->map[j * game->mapX + i] == 'P')
|
||||
{
|
||||
game->player_pos = set_pos(i, j);
|
||||
return ;
|
||||
}
|
||||
}
|
||||
}
|
||||
game->player_pos = set_pos(-1, -1);
|
||||
}
|
||||
30
srcs/put_image.c
Normal file
30
srcs/put_image.c
Normal file
@@ -0,0 +1,30 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* put_image.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/06/09 19:26:10 by lejulien #+# #+# */
|
||||
/* Updated: 2021/06/11 20:42:27 by user42 ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "so_long.h"
|
||||
|
||||
void
|
||||
put_image(t_game *g, t_img_d *to_place, t_vec2 pos)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
|
||||
y = -1;
|
||||
while (y++ < to_place->height - 1)
|
||||
{
|
||||
x = -1;
|
||||
while (x++ < to_place->width - 1)
|
||||
if (to_place->data[y * to_place->width + x] != -16777216)
|
||||
g->img->canvas->data[(pos.y + y) * g->winX + (pos.x + x)] =
|
||||
to_place->data[y * to_place->width + x];
|
||||
}
|
||||
}
|
||||
80
srcs/so_long.h
Normal file
80
srcs/so_long.h
Normal file
@@ -0,0 +1,80 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* so_long.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/06/09 16:28:51 by lejulien #+# #+# */
|
||||
/* Updated: 2021/06/12 02:47:03 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef SO_LONG_H
|
||||
# define SO_LONG_H
|
||||
# include <stdlib.h>
|
||||
# include <stdio.h>
|
||||
# include "../mlx/mlx.h"
|
||||
|
||||
typedef struct s_mlx
|
||||
{
|
||||
void *mlx_ptr;
|
||||
void *win_ptr;
|
||||
} t_mlx;
|
||||
|
||||
typedef struct s_vec2
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
} t_vec2;
|
||||
|
||||
typedef struct s_img_d
|
||||
{
|
||||
void *img_ptr;
|
||||
int *data;
|
||||
int size_l;
|
||||
int bpp;
|
||||
int endian;
|
||||
int width;
|
||||
int height;
|
||||
} t_img_d;
|
||||
|
||||
typedef struct s_img
|
||||
{
|
||||
t_img_d *bg_img;
|
||||
t_img_d *player_img;
|
||||
t_img_d *bloc_img;
|
||||
t_img_d *coll_img;
|
||||
t_img_d *canvas;
|
||||
t_img_d *portal_img;
|
||||
t_img_d *mouse_img;
|
||||
} t_img;
|
||||
|
||||
typedef struct s_game
|
||||
{
|
||||
t_mlx *mlx;
|
||||
t_img *img;
|
||||
char *map;
|
||||
t_vec2 player_pos;
|
||||
int coll;
|
||||
int moves;
|
||||
int t_coll;
|
||||
int mapX;
|
||||
int mapY;
|
||||
int winX;
|
||||
int winY;
|
||||
int key_w;
|
||||
int key_a;
|
||||
int key_s;
|
||||
int key_d;
|
||||
} t_game;
|
||||
|
||||
int game_loop(t_game *game);
|
||||
t_vec2 set_pos(int x, int y);
|
||||
void put_image(t_game *g, t_img_d *to_place, t_vec2 pos);
|
||||
int ft_put_error(void);
|
||||
void ft_getmap(t_game *game, char *path);
|
||||
void get_player_pos(t_game *game);
|
||||
int key_pressed(int keycode, t_game *game);
|
||||
int key_released(int keycode, t_game *game);
|
||||
#endif
|
||||
23
srcs/vector2.c
Normal file
23
srcs/vector2.c
Normal file
@@ -0,0 +1,23 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* vector2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/06/10 02:25:59 by lejulien #+# #+# */
|
||||
/* Updated: 2021/06/10 02:28:09 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "so_long.h"
|
||||
|
||||
t_vec2
|
||||
set_pos(int x, int y)
|
||||
{
|
||||
t_vec2 ret;
|
||||
|
||||
ret.x = x;
|
||||
ret.y = y;
|
||||
return (ret);
|
||||
}
|
||||
Reference in New Issue
Block a user