Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
116 changes: 116 additions & 0 deletions get_next_line/get_next_line.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hyko <hyko@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/27 20:59:49 by hyko #+# #+# */
/* Updated: 2022/02/21 15:03:04 by hyko ### ########.fr */
/* */
/* ************************************************************************** */

#include "get_next_line.h"

char *get_next_line(int fd)
{
static char *backup;
char *return_line;
int nl_idx;
int pre_len;

if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
nl_idx = gnl_find_newline(backup);
if (nl_idx == -1)
{
pre_len = ft_strlen(backup);
backup = gnl_expand_backup(backup, fd);
if ((int)ft_strlen(backup) == pre_len)
nl_idx = ft_strlen(backup);
}
if (nl_idx != -1)
{
return_line = ft_substr(backup, 0, nl_idx + 1);
backup = gnl_cut_backup(backup, return_line);
return (return_line);
}
return (get_next_line(fd));
}

int gnl_find_newline(char *backup)
{
int idx;
int len;

if (backup == NULL)
return (-1);
idx = 0;
len = ft_strlen(backup);
while (idx < len)
{
if (backup[idx] == '\n')
return (idx);
idx++;
}
return (-1);
}

char *gnl_read_line(int fd)
{
char *read_line;
int read_size;

read_line = (char *)malloc(BUFFER_SIZE + 1);
if (read_line == NULL)
return (NULL);
read_size = read(fd, read_line, BUFFER_SIZE);
if (read_size == -1)
{
free(read_line);
return (NULL);
}
read_line[read_size] = '\0';
return (read_line);
}

char *gnl_expand_backup(char *backup, int fd)
{
char *new_backup;
char *read_line;

read_line = gnl_read_line(fd);
if (read_line == NULL)
return (NULL);
if (read_line[0] == 0)
{
free(read_line);
return (backup);
}
if (backup == NULL)
return (read_line);
new_backup = ft_strjoin(backup, read_line);
free(backup);
free(read_line);
return (new_backup);
}

char *gnl_cut_backup(char *backup, char *return_line)
{
char *new_backup;
int backup_len;
int return_len;

if (backup == NULL || return_line == NULL)
return (NULL);
backup_len = ft_strlen(backup);
return_len = ft_strlen(return_line);
if (backup_len == return_len)
{
free(backup);
return (NULL);
}
new_backup = ft_substr(backup, return_len, backup_len - return_len);
free(backup);
return (new_backup);
}
33 changes: 33 additions & 0 deletions get_next_line/get_next_line.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hyko <hyko@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/27 21:00:30 by hyko #+# #+# */
/* Updated: 2022/12/20 15:10:57 by hyko ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef GET_NEXT_LINE_H

# define GET_NEXT_LINE_H

# include <unistd.h>
# include <stdlib.h>

#define BUFFER_SIZE 42

char *get_next_line(int fd);
char *gnl_expand_backup(char *backup, int fd);
char *gnl_cut_backup(char *backup, char *return_line);
char *gnl_read_line(int fd);
int gnl_find_newline(char *read_line);

size_t ft_strlen(const char *s);
char *ft_strdup(const char *s1);
char *ft_substr(char const *s, unsigned int start, size_t len);
char *ft_strjoin(char const *s1, char const *s2);

#endif
102 changes: 102 additions & 0 deletions get_next_line/get_next_line_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hyko <hyko@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/27 21:00:08 by hyko #+# #+# */
/* Updated: 2022/02/20 18:47:46 by hyko ### ########.fr */
/* */
/* ************************************************************************** */

#include "get_next_line.h"

size_t ft_strlen(const char *s)
{
size_t i;

i = 0;
if (s == NULL)
return (0);
while (s[i] != '\0')
i++;
return (i);
}

char *ft_strdup(const char *s1)
{
int i;
int len;
char *s2;

len = ft_strlen(s1);
s2 = (char *)malloc(sizeof(char) * (len + 1));
if (s2 == NULL)
return (NULL);
i = 0;
while (s1[i] != '\0')
{
s2[i] = s1[i];
i++;
}
s2[i] = '\0';
return (s2);
}

char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t i;
size_t j;
char *substr;

i = 0;
j = 0;
if (s == 0)
return (NULL);
if (start >= ft_strlen(s))
return (ft_strdup(""));
substr = (char *)malloc(sizeof(char) * (len + 1));
if (substr == NULL)
return (NULL);
while (s[i] != '\0')
{
if (i >= start && j < len)
{
substr[j] = s[i];
j++;
}
i++;
}
substr[j] = '\0';
return (substr);
}

char *ft_strjoin(char const *s1, char const *s2)
{
char *new_str;
size_t s1_len;
size_t s2_len;
size_t i;
size_t j;

if (s1 == NULL && s2 == NULL)
return (NULL);
if (s1 == NULL)
return ((char *)s2);
if (s2 == NULL)
return ((char *)s1);
s1_len = ft_strlen(s1);
s2_len = ft_strlen(s2);
i = -1;
j = 0;
new_str = (char *)malloc(sizeof(char) * (s1_len + s2_len + 1));
if (new_str == NULL)
return (NULL);
while (++i < s1_len)
new_str[i] = s1[i];
while (j < s2_len)
new_str[i++] = s2[j++];
new_str[i] = '\0';
return (new_str);
}
27 changes: 23 additions & 4 deletions jk/3d_render.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
#include "cub3d.h"


void init_img_data(t_all* p_all)
{
int x;
int y;

y = 0;
while (y < WINDOW_HEI)
{
x = 0;
while (x < WINDOW_WID)
{
p_all->img.data[WINDOW_WID * y + x] = 0;
x++;
}
y++;
}
}

void draw_ceiling(t_all *p_all, int ray_num, int wall_top_pixel, int color)
{
int x;
Expand All @@ -11,7 +30,7 @@ void draw_ceiling(t_all *p_all, int ray_num, int wall_top_pixel, int color)
x = 0;
while (x < WALL_STRIP_WIDTH)
{
if (p_all->img.data[WINDOW_WID * y + (x + ray_num * WALL_STRIP_WIDTH)] == PINK)
if (!p_all->img.data[WINDOW_WID * y + (x + ray_num * WALL_STRIP_WIDTH)])
p_all->img.data[WINDOW_WID * y + (x + ray_num * WALL_STRIP_WIDTH)] = color;
x++;
}
Expand All @@ -30,7 +49,7 @@ void draw_floor(t_all *p_all, int ray_num, int wall_bottom_pixel, int color)
x = 0;
while (x < WALL_STRIP_WIDTH)
{
if (p_all->img.data[WINDOW_WID * y + (x + ray_num * WALL_STRIP_WIDTH)] == PINK)
if (!p_all->img.data[WINDOW_WID * y + (x + ray_num * WALL_STRIP_WIDTH)])
p_all->img.data[WINDOW_WID * y + (x + ray_num * WALL_STRIP_WIDTH)] = color;
x++;
}
Expand Down Expand Up @@ -78,7 +97,7 @@ void render_3d_wall(t_all *p_all, int ray_num)
x = 0;
while (x < WALL_STRIP_WIDTH)
{
if (p_all->img.data[WINDOW_WID * y + (x + ray_num * WALL_STRIP_WIDTH)] == PINK)
if (!p_all->img.data[WINDOW_WID * y + (x + ray_num * WALL_STRIP_WIDTH)])
p_all->img.data[WINDOW_WID * y + (x + ray_num * WALL_STRIP_WIDTH)] = color;
//현재위치픽셀 색이 초기설정색이면 색을 덧입히기
x++;
Expand All @@ -89,4 +108,4 @@ void render_3d_wall(t_all *p_all, int ray_num)
draw_floor(p_all, ray_num, wall_bottom_pixel, GROUND);
}//ray_num의 순번을 잘 넘겨주기 위해 draw_ray() 수정함
//화면~player 거리
//y축 기준으로 wall_top_pixel~wall_bottom_pixel 그려줌.
//y축 기준으로 wall_top_pixel~wall_bottom_pixel 그려줌.
12 changes: 8 additions & 4 deletions jk/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ SRC = main.c \
playeronmap.c \
draw_ray.c \
key.c \
map_parse.c \
get_next_line.c \
get_next_line_util.c \
3d_render.c
3d_render.c \
parse_map.c \
map_info.c


OBJ = $(SRC:.c=.o)
MLX_FLAGS = -I. -L../mlx -lmlx -framework OpenGL -framework Appkit

LIB = -L../libft -lft

all : $(NAME)

.c.o :
Expand All @@ -25,7 +29,7 @@ all : $(NAME)
# $(CC) $(CCFLAGS) $(MLX_FLAGS) -c $< -o $@

$(NAME) : $(OBJ)
$(CC) $(CCFLAGS) $(MLX_FLAGS) $(OBJ) -o $(NAME)
$(CC) $(CCFLAGS) $(MLX_FLAGS) $(OBJ) -o $(NAME) $(LIB)
# $(CC_M1) $(CCFLAGS) $(MLX_FLAGS) $(OBJ) -o $(NAME)

clean :
Expand All @@ -38,4 +42,4 @@ re :
make fclean
make all

.PHONY : all clean fclean re
.PHONY : all clean fclean re
Loading