fdf - KimTaebin-ai/study_posts GitHub Wiki
κΉν μν€λ MathJax LaTeX μμμ μ§μ μ§μνμ§ μμμ μ 리λ₯Ό ν¬κΈ°ν¨..
μλμ° μ°½ λμ°κΈ°
#include "fdf.h"
int main(void)
{
void *mlx;
void *mlx_win;
mlx = mlx_init();
mlx_win = mlx_new_window(mlx, 1920, 1080, "Hello world!");
mlx_loop(mlx);
}
#include "mlx/mlx.h"
NAME = fdf.a
# μ΅μ’
μ μΌλ‘ μμ±λ μ€ν νμΌμ μ΄λ¦μ 'fdf.a'λ‘ μ§μ
SRCS = main.c
# main.c μμ€ νμΌ μ§μ
OBJS = $(SRCS:%.c=%.o)
# μμ€ νμΌ(.c)μ κ°μ²΄ νμΌ(.o)λ‘ λ³ν
# CFLAGS = -Wall -Wextra -Werror
INCLUDE_HEADER =-Imlx
# MLX λΌμ΄λΈλ¬λ¦¬μ ν€λ νμΌ μμΉλ₯Ό μ§μ
%.o: %.c
cc $(CFLAGS) -Imlx -c $< -o $@
$(NAME): $(OBJS)
$(MAKE) -C mlx
$(CC) -g $(OBJS) -Lmlx -lmlx_Linux -L/usr/lib -lXext -lX11 -lm -lz -o $(NAME)
# -Lmlx: MLX λΌμ΄λΈλ¬λ¦¬ κ²½λ‘ μ§μ
# -lmlx_Linux: MLX λΌμ΄λΈλ¬λ¦¬ λ§ν¬
# -lXext -lX11: X11 κ΄λ ¨ λΌμ΄λΈλ¬λ¦¬ λ§ν¬
# -lm: μν λΌμ΄λΈλ¬λ¦¬ λ§ν¬
# -lz: zlib μμΆ λΌμ΄λΈλ¬λ¦¬ λ§ν¬
μλμ° μ°½μ μ΄λ―Έμ§λ‘ μ μ°κΈ°
typedef struct s_data
{
void *img;
char *addr;
int bits_per_pixel;
int line_length;
int endian;
} t_data;
void my_mlx_pixel_put(t_data *data, int x, int y, int color)
{
char *dst;
dst = data->addr + (y * data->line_length + x * (data->bits_per_pixel / 8));
*(unsigned int *)dst = color;
}
int main(void)
{
void *mlx;
void *mlx_win;
t_data img;
mlx = mlx_init();
mlx_win = mlx_new_window(mlx, 1920, 1080, "Hello world!");
img.img = mlx_new_image(mlx, 1920, 1080);
img.addr = mlx_get_data_addr(img.img, &img.bits_per_pixel, &img.line_length,
&img.endian);
my_mlx_pixel_put(&img, 5, 5, 0x00FF0000);
mlx_put_image_to_window(mlx, mlx_win, img.img, 0, 0);
mlx_loop(mlx);
}
μ€μΌμ΄ μ°μ΅μ λλ¬κ³ μ΄μ 맡 νμΌ λΆλ¬μ€λ κ²λΆν° ν΄λ³΄μ
int main(int argc, char **argv)
{
file_load(argc, argv);
return (0);
}
static void file_load(int argc, char **argv)
{
int fd;
char *ext;
if (argc != 2)
{
// μλ¬ μ²λ¦¬
}
fd = open(argv[1], O_RDONLY);
if (fd < 0)
{
// μλ¬ μ²λ¦¬
}
close(fd);
ext = ft_strrchr(argv[1], '.');
if (!ext || ft_strncmp(ext, ".fdf", 4))
{
// μλ¬ μ²λ¦¬
}
}
μλ¬ μ²λ¦¬ ν¨μ λ§λ€κΈ°
void error_msg(char *msg, t_data *data)
{
ft_putstr_fd(msg, 2);
free(data->img.data);
free(data->img.img_ptr);
free(data->mlx_ptr);
exit(EXIT_FAILURE);
}
static void file_load(int ac, char **av)
{
int fd;
char *ext;
if (ac != 2)
error_msg("usage: ./fdf <filename>\n", NULL);
fd = open(av[1], O_RDONLY);
if (fd < 0)
error_msg("error: open() failed\n", NULL);
close(fd);
ext = ft_strrchr(av[1], '.');
if (!ext || ft_strncmp(ext, ".fdf", 4))
error_msg("error: invalid file extension\n", NULL);
}
μμμ νλ mlx_init() ν¨μλ λ§λ€μ΄λκ³
void init_mlx(t_data *data)
{
data->mlx_ptr = mlx_init();
if (!data->mlx_ptr)
error_msg("error: mlx_init() failed\n", data);
data->win_ptr = mlx_new_window(data->mlx_ptr, WIN_WIDTH, WIN_HEIGHT, "fdf");
if (!data->win_ptr)
error_msg("error: mlx_new_window() failed\n", data);
data->img.img_ptr = mlx_new_image(data->mlx_ptr, WIN_WIDTH, WIN_HEIGHT);
if (!data->img.img_ptr)
error_msg("error: mlx_new_image() failed\n", data);
data->img.data = mlx_get_data_addr(data->img.img_ptr, &data->img.bpp,
&data->img.size_line, &data->img.endian);
if (!data->img.data)
error_msg("error: mlx_get_data_addr() failed\n", data);
}
int main(int argc, char **argv)
{
t_data data;
init_data(&data);
file_load(argc, argv);
init_mlx(&data);
mlx_loop(data.mlx_ptr);
return (0);
}
ν μ€ μΆκ°νκΈ°
#include "fdf.h"
static void close_window(t_data *data)
{
if (data->win_ptr)
{
mlx_destroy_window(data->mlx_ptr, data->win_ptr);
data->win_ptr = NULL;
}
if (data->img.img_ptr)
{
mlx_destroy_image(data->mlx_ptr, data->img.img_ptr);
data->img.img_ptr = NULL;
}
if (data->mlx_ptr)
{
free(data->mlx_ptr);
data->mlx_ptr = NULL;
}
// TODO
// mlx, μκΉ, μ’ν free() ν¨μ νμ
exit(EXIT_SUCCESS);
}
int key_hook(int keycode, t_data *data)
{
int i;
i = 0;
if (!data->init_coords || !data->coords)
error_msg("Error: init_coords or coords is NULL", data);
while (i < data->map_height * data->map_width)
{
data->coords[i] = data->init_coords[i];
i++;
}
if (keycode == 53 || keycode == 65307)
close_window(data);
// TODO
// νμ , νλ λ±λ± 보λμ€μ© ν¨μ νμ
return (0);
}
int handle_x(t_data *data)
{
close_window(data);
return (0);
}
int main(int argc, char **argv)
{
t_data data;
init_data(&data);
file_load(argc, argv);
init_mlx(&data);
mlx_key_hook(data.win_ptr, key_hook, &data);
mlx_hook(data.win_ptr, 17, 0, handle_x, &data);
mlx_loop(data.mlx_ptr);
return (0);
}