Skip to content

Commit cfc74cf

Browse files
committed
feat: add window functions
1 parent 6589e86 commit cfc74cf

File tree

11 files changed

+256
-87
lines changed

11 files changed

+256
-87
lines changed

Makefile

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ MLX_DIR = $(LIB_DIR)/minilibx-linux
1010
LIBFT = $(LIBFT_DIR)/libft.a
1111
MLX = $(MLX_DIR)/libmlx.a
1212

13-
VPATH = src
14-
SRCS = main.c map.c utils.c
13+
VPATH = src:src/map:src/window
14+
SRCS = main.c utils.c \
15+
init_map.c parse_map.c read_map.c \
16+
init_win.c win_hooks.c win_utils.c
1517
OBJS = $(addprefix $(BUILD_DIR)/,$(SRCS:.c=.o))
1618

1719
CFLAGS = -Wall -Wextra -Werror -I$(INC_DIR) -I$(LIBFT_DIR) -I$(MLX_DIR)
1820
LDFLAGS = -L$(LIBFT_DIR) -L$(MLX_DIR)
19-
LDLIBS = -lX11 -lXext -lm -lmlx -lft
21+
LDLIBS = -lm -lmlx -lft -lX11 -lXext
2022
RM = rm -rf
2123

2224
all: $(NAME)
@@ -34,7 +36,7 @@ $(MLX):
3436
@make -C $(MLX_DIR)
3537

3638
$(NAME): $(OBJS) $(LIBFT) $(MLX)
37-
$(CC) $(CFLAGS) $(OBJS) -o $(NAME) $(LDFLAGS) $(LDLIBS)
39+
$(CC) $(CFLAGS) $(OBJS) $(LDFLAGS) $(LDLIBS) -o $(NAME)
3840

3941
clean:
4042
$(RM) $(BUILD_DIR)

inc/fdf.h

+18-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ebabaogl <[email protected] +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/01/12 22:28:32 by ebabaogl #+# #+# */
9-
/* Updated: 2025/01/13 02:14:26 by ebabaogl ### ########.fr */
9+
/* Updated: 2025/01/13 14:16:48 by ebabaogl ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -20,28 +20,39 @@
2020
# define COLOR_MASK 4294967295U
2121
# define DEFAULT_COLOR 0x00FFFFFF
2222

23-
typedef struct s_vars
23+
typedef struct s_vars
2424
{
2525
t_mlx *mlx;
2626

2727
unsigned long **map;
2828
size_t col_size;
2929
size_t row_size;
30+
31+
size_t anchor_x;
32+
size_t anchor_y;
3033
} t_vars;
3134

32-
typedef struct s_point
35+
typedef struct s_point
3336
{
3437
int x;
3538
int y;
3639
int z;
3740
} t_point;
3841

39-
int init_map(t_vars *vars, char *filename);
40-
void free_str_arr(char **arr);
41-
void free_ulong_arr(unsigned long **arr);
42-
size_t ulong_arr_len(unsigned long *arr);
42+
int init_map(t_vars *vars, char *filename);
43+
char *get_raw_map(char *filename);
44+
unsigned long **split_raw_map(char *raw_map);
45+
46+
void free_str_arr(char **arr);
47+
void free_ulong_arr(unsigned long **arr);
48+
4349
unsigned long get_z(unsigned long point);
4450
unsigned long get_color(unsigned long point);
4551
unsigned int ft_atoi_hex(char *str);
4652

53+
int init_win(t_vars *vars);
54+
void destroy_win(t_mlx *mlx);
55+
int destroy_handler(t_mlx *mlx);
56+
int key_handler(int keycode, t_mlx *mlx);
57+
4758
#endif

inc/window.h

+9-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ebabaogl <[email protected] +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/01/12 22:35:08 by ebabaogl #+# #+# */
9-
/* Updated: 2025/01/13 02:14:01 by ebabaogl ### ########.fr */
9+
/* Updated: 2025/01/13 14:16:54 by ebabaogl ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -15,16 +15,20 @@
1515

1616
# define WIN_WIDTH 1920
1717
# define WIN_HEIGHT 1080
18+
# define WIN_TITLE "FdF"
19+
1820
# define PADDED_WIDTH 1800
1921
# define PADDED_HEIGHT 900
2022

21-
typedef struct s_mlx
23+
typedef struct s_mlx
2224
{
2325
void *mlx_ptr;
2426
void *win_ptr;
25-
int *bits_per_pixel;
26-
int *size_line;
27-
int *endian;
27+
void *img_ptr;
28+
char *data_addr;
29+
int bits_per_pixel;
30+
int size_line;
31+
int endian;
2832
} t_mlx;
2933

3034
#endif

src/main.c

+4-12
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@
66
/* By: ebabaogl <[email protected] +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/01/12 19:39:45 by ebabaogl #+# #+# */
9-
/* Updated: 2025/01/13 02:14:55 by ebabaogl ### ########.fr */
9+
/* Updated: 2025/01/13 13:53:37 by ebabaogl ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "fdf.h"
1414
#include "libft.h"
1515

16-
#include <stdio.h>
17-
#include <limits.h>
18-
1916
int main(int argc, char **argv)
2017
{
2118
t_vars vars;
@@ -27,12 +24,7 @@ int main(int argc, char **argv)
2724
}
2825
if (init_map(&vars, argv[1]) == -1)
2926
return (1);
30-
for (int i = 0; vars.map[i]; i++)
31-
{
32-
for (int j = 0; vars.map[i][j] != ULONG_MAX; j++)
33-
{
34-
printf("i: %d, j: %d, z: %lu, color: %lu \n", i, j, get_z(vars.map[i][j]), get_color(vars.map[i][j]));
35-
}
36-
}
27+
if (init_win(&vars) == -1)
28+
return (1);
3729
free_ulong_arr(vars.map);
38-
}
30+
}

src/map/init_map.c

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* init_map.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: ebabaogl <[email protected] +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2025/01/13 10:20:27 by ebabaogl #+# #+# */
9+
/* Updated: 2025/01/13 13:54:31 by ebabaogl ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "fdf.h"
14+
#include <stdlib.h>
15+
#include <limits.h>
16+
17+
static void init_map_props(t_vars *vars)
18+
{
19+
size_t i;
20+
21+
i = 0;
22+
while (vars->map[0][i] != ULONG_MAX)
23+
i++;
24+
vars->row_size = i;
25+
vars->anchor_x = i / 2;
26+
i = 0;
27+
while (vars->map[i])
28+
i++;
29+
vars->col_size = i;
30+
vars->anchor_y = i / 2;
31+
}
32+
33+
int init_map(t_vars *vars, char *filename)
34+
{
35+
char *raw_map;
36+
37+
raw_map = get_raw_map(filename);
38+
if (!raw_map)
39+
return (-1);
40+
vars->map = split_raw_map(raw_map);
41+
if (!vars->map)
42+
return (-1);
43+
free(raw_map);
44+
init_map_props(vars);
45+
return (0);
46+
}

src/map.c src/map/parse_map.c

+10-44
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,28 @@
11
/* ************************************************************************** */
22
/* */
33
/* ::: :::::::: */
4-
/* map.c :+: :+: :+: */
4+
/* parse_map.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
66
/* By: ebabaogl <[email protected] +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
8-
/* Created: 2025/01/12 22:47:31 by ebabaogl #+# #+# */
9-
/* Updated: 2025/01/13 02:14:40 by ebabaogl ### ########.fr */
8+
/* Created: 2025/01/13 10:18:50 by ebabaogl #+# #+# */
9+
/* Updated: 2025/01/13 13:54:12 by ebabaogl ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "fdf.h"
1414
#include "libft.h"
15-
#include <fcntl.h>
16-
#include <unistd.h>
1715
#include <stdlib.h>
1816
#include <limits.h>
1917

20-
static char *get_raw_map(char *filename)
18+
static size_t ulong_arr_len(unsigned long *arr)
2119
{
22-
char *buf;
23-
char *map;
24-
int r_bytes;
25-
int fd;
20+
size_t i;
2621

27-
fd = open(filename, O_RDONLY);
28-
if (fd == -1)
29-
return (NULL);
30-
r_bytes = 1;
31-
map = NULL;
32-
while (r_bytes)
33-
{
34-
buf = ft_calloc(1, BUFFER_SIZE);
35-
if (!buf)
36-
return (free(map), NULL); // norm
37-
r_bytes = read(fd, buf, BUFFER_SIZE);
38-
if (r_bytes == -1)
39-
return (free(map), NULL); // norm
40-
buf[r_bytes] = '\0';
41-
map = ft_strjoin(map, buf);
42-
if (!map)
43-
return (NULL);
44-
}
45-
return (map);
22+
i = 0;
23+
while (arr[i])
24+
i++;
25+
return (i);
4626
}
4727

4828
static unsigned long get_point(char *point)
@@ -93,7 +73,7 @@ static unsigned long *get_map_row(char *map_row)
9373
return (row);
9474
}
9575

96-
static unsigned long **split_raw_map(char *raw_map)
76+
unsigned long **split_raw_map(char *raw_map)
9777
{
9878
unsigned long **map;
9979
char **str_map;
@@ -120,17 +100,3 @@ static unsigned long **split_raw_map(char *raw_map)
120100
free_str_arr(str_map);
121101
return (map);
122102
}
123-
124-
int init_map(t_vars *vars, char *filename)
125-
{
126-
char *raw_map;
127-
128-
raw_map = get_raw_map(filename);
129-
if (!raw_map)
130-
return (-1);
131-
vars->map = split_raw_map(raw_map);
132-
if (!vars->map)
133-
return (-1);
134-
free(raw_map);
135-
return (0);
136-
}

src/map/read_map.c

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* read_map.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: ebabaogl <[email protected] +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2025/01/13 10:15:51 by ebabaogl #+# #+# */
9+
/* Updated: 2025/01/13 13:57:10 by ebabaogl ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "fdf.h"
14+
#include "libft.h"
15+
#include <stdlib.h>
16+
#include <unistd.h>
17+
#include <fcntl.h>
18+
19+
char *get_raw_map(char *filename)
20+
{
21+
char *buf;
22+
char *map;
23+
int r_bytes;
24+
int fd;
25+
26+
fd = open(filename, O_RDONLY);
27+
if (fd == -1)
28+
return (NULL);
29+
r_bytes = 1;
30+
map = NULL;
31+
while (r_bytes)
32+
{
33+
buf = ft_calloc(1, BUFFER_SIZE);
34+
if (!buf)
35+
return (free(map), NULL);
36+
r_bytes = read(fd, buf, BUFFER_SIZE);
37+
if (r_bytes == -1)
38+
return (free(map), NULL);
39+
buf[r_bytes] = '\0';
40+
map = ft_strjoin(map, buf);
41+
if (!map)
42+
return (NULL);
43+
}
44+
return (map);
45+
}

src/utils.c

+2-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ebabaogl <[email protected] +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/01/13 00:10:17 by ebabaogl #+# #+# */
9-
/* Updated: 2025/01/13 02:11:17 by ebabaogl ### ########.fr */
9+
/* Updated: 2025/01/13 13:53:41 by ebabaogl ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -35,16 +35,6 @@ unsigned int ft_atoi_hex(char *str)
3535
return (res);
3636
}
3737

38-
size_t ulong_arr_len(unsigned long *arr)
39-
{
40-
size_t i;
41-
42-
i = 0;
43-
while (arr[i])
44-
i++;
45-
return (i);
46-
}
47-
4838
void free_ulong_arr(unsigned long **arr)
4939
{
5040
size_t i;
@@ -79,4 +69,4 @@ unsigned long get_z(unsigned long point)
7969
unsigned long get_color(unsigned long point)
8070
{
8171
return (point & COLOR_MASK);
82-
}
72+
}

0 commit comments

Comments
 (0)