Archived
1
0

started print map to window

This commit is contained in:
Adam Joly
2024-01-09 16:12:58 +01:00
parent f33d52e643
commit 0dc9dcf954
22 changed files with 349 additions and 27 deletions

View File

@ -6,7 +6,7 @@
# By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ # # By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/11/01 11:03:22 by adjoly #+# #+# # # Created: 2023/11/01 11:03:22 by adjoly #+# #+# #
# Updated: 2024/01/08 13:33:38 by adjoly ### ########.fr # # Updated: 2024/01/09 15:54:39 by adjoly ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -14,36 +14,40 @@ NAME = so_long
CC = cc CC = cc
SRCS = #main.c \ SRCS = main.c \
#ft_move_character.c \ get_map.c \
get_map.c print_map.c
OBJS = $(SRCS:.c=.o) OBJS = $(SRCS:.c=.o)
FLAGS = -Werror -Wall -Wextra -g FLAGS = -Werror -Wall -Wextra -g
HEADER = so_long.h HEADERS = so_long.h
MLX_OBJ = MacroLibX/libmlx.so \ LIB = MacroLibX/libmlx.so \
libft/libft.a libft/libft.a \
get_next_line/get_next_line.a
MLX_FLAGS = -lSDL2 MLX_FLAGS = -lSDL2
$(NAME): $(OBJS) $(NAME): $(OBJS)
$(CC) $(FLAGS) $(MLX_FLAGS) $(OBJS) $(MLX_OBJ) -o $(NAME) $(CC) $(FLAGS) $(MLX_FLAGS) $(OBJS) $(LIB) -o $(NAME)
%.o: %.c %.o: %.c
make -C libft make -C libft
$(CC) $(FLAGS) -I $(HEADER) $< -c -o $@ make -C get_next_line
$(CC) $(FLAGS) -I $(HEADERS) $< -c -o $@
all: $(NAME) all: $(NAME)
clean: clean:
make -C libft clean make -C libft clean
make -C get_next_line clean
rm -f $(OBJS) rm -f $(OBJS)
fclean: clean fclean: clean
make -C libft fclean make -C libft fclean
make -C get_next_line fclean
rm -f $(NAME) rm -f $(NAME)
re: fclean all re: fclean all

BIN
a.out Executable file

Binary file not shown.

BIN
assets/exit.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

BIN
assets/obj.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

View File

Before

Width:  |  Height:  |  Size: 613 B

After

Width:  |  Height:  |  Size: 613 B

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/08 13:13:18 by adjoly #+# #+# */ /* Created: 2024/01/08 13:13:18 by adjoly #+# #+# */
/* Updated: 2024/01/08 13:22:56 by adjoly ### ########.fr */ /* Updated: 2024/01/09 15:25:08 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,13 +16,21 @@ int ft_countline_fd(int fd)
{ {
size_t line_count; size_t line_count;
ssize_t rd; ssize_t rd;
char *buf;
size_t i;
i = 0;
while () line_count = 0;
buf = ft_calloc(1, 1);
while (i < ULONG_MAX)
{ {
rd = read(fd, buf, 1);
if (rd == -1 || rd == 0)
break ;
else if (buf[0] == '\n')
line_count++;
i++;
} }
return (line_count); return (line_count);
} }
@ -30,8 +38,22 @@ char **ft_read_map(char *file_name)
{ {
char **map_read; char **map_read;
int fd; int fd;
int line_count;
size_t i;
map_read = ft_countline_fd(fd); i = 0;
fd = open(file_name, O_RDONLY);
return line_count = ft_countline_fd(fd);
close(fd);
fd = open(file_name, O_RDONLY);
map_read = ft_calloc(sizeof(char *), line_count);
while (i < ULONG_MAX)
{
map_read[i] = get_next_line(fd);
if (map_read[i] == NULL)
break ;
i++;
}
close(fd);
return (map_read);
} }

BIN
get_map.o Normal file

Binary file not shown.

42
get_next_line/Makefile Normal file
View File

@ -0,0 +1,42 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/01 11:03:22 by adjoly #+# #+# #
# Updated: 2024/01/08 14:31:46 by adjoly ### ########.fr #
# #
# **************************************************************************** #
NAME = get_next_line.a
CC = clang
SRCS = get_next_line.c \
get_next_line_utils.c
OBJS = $(SRCS:.c=.o)
FLAGS = -Werror -Wall -Wextra
HEADER = get_next_line.h
$(NAME): $(OBJS)
ar -rcs $(NAME) $(OBJS)
%.o: %.c
$(CC) $(FLAGS) -I $(HEADER) $< -c -o $@
all: $(NAME)
clean:
rm -f $(OBJS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: clean all re fclean

Binary file not shown.

View File

@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */
/* Updated: 2024/01/08 21:49:14 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <unistd.h>
char check_line(char *res, char *buf)
{
int i;
int j;
i = 0;
j = 0;
while (res[i] && res[i] != '\n')
i++;
if (res[i] == '\n')
{
i++;
while (res[i])
{
buf[j] = res[i];
i++;
j++;
}
buf[j] = 0;
res[i - j] = 0;
return (1);
}
return (0);
}
char *ft_read_error(char **buf, char *res)
{
free(*buf);
*buf = NULL;
if (res[0] != 0)
return (res);
free(res);
return (NULL);
}
char *get_next_line(int fd)
{
static char *buf;
char *res;
ssize_t bytes_read;
if (BUFFER_SIZE <= 0 || fd < 0 || fd > 1023)
return (NULL);
if (!buf)
buf = ft_calloc_gnl(sizeof(char), BUFFER_SIZE + 1);
res = ft_calloc_gnl(1, 1);
while (buf)
{
res = ft_strjoin_gnl(res, buf);
if (!res)
return (NULL);
if (check_line(res, buf))
return (res);
bytes_read = read(fd, buf, BUFFER_SIZE);
if (bytes_read < 1)
return (ft_read_error(&buf, res));
buf[bytes_read] = 0;
}
return (NULL);
}

View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:12:00 by adjoly #+# #+# */
/* Updated: 2024/01/08 21:47:06 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# include <unistd.h>
# include <stdlib.h>
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 5
# endif
char *get_next_line(int fd);
char *ft_strjoin_gnl(char *s1, char *s2);
size_t ft_strlen_gnl(char *s);
void *ft_calloc_gnl(size_t nmemb, size_t size);
#endif

Binary file not shown.

View File

@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:12:02 by adjoly #+# #+# */
/* Updated: 2024/01/08 21:48:30 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t ft_strlen_gnl(char *s)
{
size_t i;
i = 0;
while (s[i])
i++;
return (i);
}
char *ft_strjoin_gnl(char *s1, char *s2)
{
char *result;
size_t i;
size_t j;
i = 0;
j = 0;
if (!s2)
return (NULL);
result = ft_calloc_gnl((ft_strlen_gnl(s1) + ft_strlen_gnl(s2) + 1), sizeof(char));
if (result == NULL)
return (NULL);
while (s1[i])
{
result[i] = s1[i];
i++;
}
while (s2[j])
{
result[i] = s2[j];
i++;
j++;
}
free(s1);
result[i] = '\0';
return (result);
}
void *ft_calloc_gnl(size_t nmemb, size_t size)
{
char *result;
size_t i;
i = 0;
if (nmemb == 0 || size == 0)
return (malloc(1));
if (((unsigned long long)(size * nmemb) > 4294967295))
return (NULL);
if ((int)size < 0 && (int)nmemb < 0)
return (NULL);
result = malloc(size * nmemb);
if (!result)
return (NULL);
while (i < (size * nmemb))
{
*(unsigned char *)(result + i) = '\0';
i++;
}
return (result);
}

Binary file not shown.

18
main.c
View File

@ -6,11 +6,12 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/06 16:18:56 by adjoly #+# #+# */ /* Created: 2024/01/06 16:18:56 by adjoly #+# #+# */
/* Updated: 2024/01/08 13:09:20 by adjoly ### ########.fr */ /* Updated: 2024/01/09 16:06:47 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "MacroLibX/includes/mlx.h" #include "MacroLibX/includes/mlx.h"
#include "libft/libft.h"
#include "so_long.h" #include "so_long.h"
int key_close(int key, void *param) int key_close(int key, void *param)
@ -27,15 +28,15 @@ int win_close(int event, void *param)
return (0); return (0);
} }
int win_update(void *param) /*int win_update(void *param)
{ {
t_window *win; t_window *win;
int y;
int x;
win = (t_window *)param; win = (t_window *)param;
mlx_pixel_put(win->mlx, win->win, 100, 100, 0xFFFFFF);
mlx_clear_window(win->mlx, win->win);
return (0); return (0);
} }*/
int main(int ac, char **av) int main(int ac, char **av)
{ {
@ -46,12 +47,15 @@ int main(int ac, char **av)
win = ft_calloc(sizeof(t_window), 1); win = ft_calloc(sizeof(t_window), 1);
win->mlx = mlx_init(); win->mlx = mlx_init();
win->win = mlx_new_window(win->mlx, 1600, 900, "so_fluffy"); win->win = mlx_new_window(win->mlx, 1600, 900, "so_fluffy");
char **map = ft_read_map(av[1]);
mlx_on_event(win->mlx, win->win, MLX_KEYDOWN, key_close, win->mlx); mlx_on_event(win->mlx, win->win, MLX_KEYDOWN, key_close, win->mlx);
mlx_on_event(win->mlx, win->win, MLX_WINDOW_EVENT, win_close, win->mlx); mlx_on_event(win->mlx, win->win, MLX_WINDOW_EVENT, win_close, win->mlx);
mlx_on_event(win->mlx, win->win, MLX_KEYDOWN, ft_move_character, win->mlx); // mlx_on_event(win->mlx, win->win, MLX_KEYDOWN, ft_move_character, win->mlx);
mlx_loop_hook(win->mlx, win_update, win); // mlx_loop_hook(win->mlx, win_update, win);
ft_printmap(map, win);
mlx_loop(win->mlx); mlx_loop(win->mlx);
mlx_destroy_image(win->mlx, win->img);
mlx_destroy_window(win->mlx, win->win); mlx_destroy_window(win->mlx, win->win);
mlx_destroy_display(win->mlx); mlx_destroy_display(win->mlx);
free(win); free(win);

BIN
main.o

Binary file not shown.

5
map.ber Normal file
View File

@ -0,0 +1,5 @@
1111111111111
10010000000C1
1000011111001
1P0011E000001
1111111111111

57
print_map.c Normal file
View File

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/09 15:30:06 by adjoly #+# #+# */
/* Updated: 2024/01/09 16:08:20 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft/libft.h"
#include "so_long.h"
#include <stddef.h>
int ft_putimg(int x, int y, void *param, char *file_path)
{
t_window *win;
win = (t_window *)param;
win->img = mlx_png_file_to_image(win->mlx, file_path, &x, &y);
mlx_put_image_to_window(win->mlx, win->win, win->img, x * TEXTURE_SIZE, y * TEXTURE_SIZE);
return (0);
}
int ft_printmap(char **map, void *param)
{
size_t x;
size_t y;
t_window *win;
y = 0;
win = (t_window *)param;
while (map[y])
{
x = 0;
while (map[y][x])
{
ft_putstr_fd("??", 1);
if (map[y][x] == '1')
ft_putimg(x, y, param, "assets/wall.png");
else if (map[y][x] == '0')
ft_putimg(x, y, param, "assets/ground.png");
else if (map[y][x] == 'C')
ft_putimg(x, y, param, "assets/obj.png");
else if (map[y][x] == 'E')
ft_putimg(x, y, param, "assets/exit.png");
else if (map[y][x] == 'P')
ft_putimg(x, y, param, "assets/player.png");
x++;
}
y++;
}
return (0);
}

BIN
print_map.o Normal file

Binary file not shown.

BIN
so_long

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/06 16:19:42 by adjoly #+# #+# */ /* Created: 2024/01/06 16:19:42 by adjoly #+# #+# */
/* Updated: 2024/01/08 13:32:18 by adjoly ### ########.fr */ /* Updated: 2024/01/09 16:09:17 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,17 +15,27 @@
# include "MacroLibX/includes/mlx.h" # include "MacroLibX/includes/mlx.h"
# include "libft/libft.h" # include "libft/libft.h"
# include "get_next_line/get_next_line.h"
# include <unistd.h> # include <unistd.h>
# include <fcntl.h> # include <fcntl.h>
# include <stdlib.h> # include <stdlib.h>
# define TEXTURE_SIZE 64
typedef struct s_window typedef struct s_window
{ {
void *mlx; void *mlx;
void *win; void *win;
void *img;
} t_window; } t_window;
int ft_move_character(int key, void *param); int ft_move_character(int key, void *param);
int key_close(int key, void *param); int key_close(int key, void *param);
int ft_countline_fd(int fd);
char **ft_read_map(char *file_name);
int ft_putimg(int x, int y, void *param, char *file_path);
int ft_printmap(char **map, void *param);
#endif #endif

BIN
so_long.h.gch Normal file

Binary file not shown.