Skip to content

Commit 1491759

Browse files
committed
feat: update to latest
1 parent 31d96e1 commit 1491759

12 files changed

+276
-18
lines changed

Makefile

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

13-
VPATH = src:src/map:src/window
13+
VPATH = src:src/map:src/render:src/window
1414
SRCS = main.c utils.c \
1515
init_map.c parse_map.c read_map.c \
16+
init_renderer.c render_map.c render_utils.c transformations.c \
1617
init_win.c win_hooks.c win_utils.c
1718
OBJS = $(addprefix $(BUILD_DIR)/,$(SRCS:.c=.o))
1819

inc/fdf.h

+29-9
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 14:29:13 by ebabaogl ### ########.fr */
9+
/* Updated: 2025/01/13 16:41:13 by ebabaogl ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -30,29 +30,49 @@ typedef struct s_vars
3030

3131
size_t anchor_x;
3232
size_t anchor_y;
33-
} t_vars;
33+
int map_x;
34+
int map_y;
35+
36+
double distance;
37+
double height;
38+
double zoom;
39+
double x_y_coef;
40+
double y_z_coef;
41+
double z_x_coef;
42+
} t_vars;
3443

3544
typedef struct s_point
3645
{
3746
int x;
3847
int y;
3948
int z;
40-
} t_point;
49+
} t_point;
4150

4251
int init_map(t_vars *vars, char *filename);
4352
char *get_raw_map(char *filename);
4453
unsigned long **split_raw_map(char *raw_map);
4554

46-
void free_str_arr(char **arr);
47-
void free_ulong_arr(unsigned long **arr);
48-
49-
unsigned long get_z(unsigned long point);
50-
unsigned long get_color(unsigned long point);
51-
unsigned int ft_atoi_hex(char *str);
55+
void init_renderer(t_vars *vars);
56+
void render_map(t_vars *vars);
57+
void set_camera_props(t_vars *vars);
58+
void set_point(t_point *point, int x, int y, int z);
59+
void render_point(t_vars *vars, t_point *point);
60+
void zoom(t_vars *vars, t_point *point);
61+
void translate(t_vars *vars, t_point *point);
62+
void rotate_z_x(t_vars *vars, t_point *point);
63+
void rotate_y_z(t_vars *vars, t_point *point);
64+
void rotate_x_y(t_vars *vars, t_point *point);
5265

5366
int init_win(t_vars *vars);
5467
void destroy_win(t_mlx *mlx);
5568
int destroy_handler(t_mlx *mlx);
5669
int key_handler(int keycode, t_mlx *mlx);
5770

71+
void free_str_arr(char **arr);
72+
void free_ulong_arr(unsigned long **arr);
73+
unsigned long get_z(unsigned long point);
74+
unsigned long get_color(unsigned long point);
75+
unsigned int atoi_hex(char *str);
76+
int abs(int n);
77+
5878
#endif

src/main.c

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

src/map/init_map.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ebabaogl <[email protected] +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/01/13 10:20:27 by ebabaogl #+# #+# */
9-
/* Updated: 2025/01/13 13:54:31 by ebabaogl ### ########.fr */
9+
/* Updated: 2025/01/13 16:04:21 by ebabaogl ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

src/map/parse_map.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ebabaogl <[email protected] +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/01/13 10:18:50 by ebabaogl #+# #+# */
9-
/* Updated: 2025/01/13 13:54:12 by ebabaogl ### ########.fr */
9+
/* Updated: 2025/01/13 16:28:45 by ebabaogl ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -39,7 +39,7 @@ static unsigned long get_point(char *point)
3939
if (!*(point_attrs + 1))
4040
color = DEFAULT_COLOR;
4141
else
42-
color = ft_atoi_hex(point_attrs[1] + 2);
42+
color = atoi_hex(point_attrs[1] + 2);
4343
ret = (((unsigned long)z) << 32) | (color);
4444
free_str_arr(point_attrs);
4545
return (ret);

src/map/read_map.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ebabaogl <[email protected] +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/01/13 10:15:51 by ebabaogl #+# #+# */
9-
/* Updated: 2025/01/13 14:39:55 by ebabaogl ### ########.fr */
9+
/* Updated: 2025/01/13 16:04:33 by ebabaogl ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -56,5 +56,6 @@ char *get_raw_map(char *filename)
5656
return (NULL);
5757
if (read_file(fd, &map, &buf) == -1)
5858
return (NULL);
59+
close(fd);
5960
return (map);
6061
}

src/render/init_renderer.c

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* init_renderer.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: ebabaogl <[email protected] +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2025/01/13 16:39:50 by ebabaogl #+# #+# */
9+
/* Updated: 2025/01/13 16:40:47 by ebabaogl ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "fdf.h"
14+
15+
void init_renderer(t_vars *vars)
16+
{
17+
set_camera_props(vars);
18+
render_map(vars);
19+
}

src/render/render_map.c

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* render_map.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: ebabaogl <[email protected] +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2025/01/13 15:56:40 by ebabaogl #+# #+# */
9+
/* Updated: 2025/01/13 16:51:33 by ebabaogl ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "fdf.h"
14+
#include "mlx.h"
15+
#include <limits.h>
16+
17+
static void put_pixel(t_vars *vars, int x, int y, unsigned long color)
18+
{
19+
int i;
20+
21+
if ((WIN_WIDTH / 2) + x >= 0
22+
&& abs((WIN_WIDTH / 2) + x) < WIN_WIDTH
23+
&& (WIN_HEIGHT / 2) + y >= 0
24+
&& abs((WIN_HEIGHT / 2)) + y < WIN_HEIGHT)
25+
{
26+
i = (((WIN_WIDTH / 2) + x) * vars->mlx->bits_per_pixel / 8)
27+
+ (((WIN_HEIGHT / 2) + y) * vars->mlx->size_line);
28+
vars->mlx->data_addr[i] = color;
29+
vars->mlx->data_addr[++i] = color >> 8;
30+
vars->mlx->data_addr[++i] = color >> 16;
31+
}
32+
}
33+
34+
static void draw_line(t_vars *vars, t_point p1, t_point p2, unsigned long color)
35+
{
36+
double x0;
37+
double y0;
38+
int steps;
39+
int i;
40+
41+
x0 = p1.x;
42+
y0 = p1.y;
43+
steps = abs(p2.y - p1.y);
44+
if (abs(p2.x - p1.x) > abs(p2.y - p1.y))
45+
steps = abs(p2.x - p1.x);
46+
i = 0;
47+
while (i <= steps)
48+
{
49+
put_pixel(vars, (int)x0, (int)y0, color);
50+
x0 += (p2.x - p1.x) / (double)steps;
51+
y0 += (p2.y - p1.y) / (double)steps;
52+
i++;
53+
}
54+
}
55+
56+
static void set_lines(t_vars *vars, int x, int y, unsigned long point)
57+
{
58+
t_point left_point;
59+
t_point top_point;
60+
t_point curr_point;
61+
62+
set_point(&curr_point, x, y, get_z(point));
63+
render_point(vars, &curr_point);
64+
if (x > 0)
65+
{
66+
set_point(&left_point, x - 1, y, get_z(vars->map[y][x - 1]));
67+
render_point(vars, &left_point);
68+
draw_line(vars, curr_point, left_point, get_color(vars->map[y][x - 1]));
69+
}
70+
if (y > 0)
71+
{
72+
set_point(&top_point, x, y - 1, get_z(vars->map[y - 1][x]));
73+
render_point(vars, &top_point);
74+
draw_line(vars, curr_point, top_point, get_color(vars->map[y - 1][x]));
75+
}
76+
}
77+
78+
void render_map(t_vars *vars)
79+
{
80+
size_t i;
81+
size_t j;
82+
83+
i = 0;
84+
while (vars->map[i])
85+
{
86+
j = 0;
87+
while (vars->map[i][j] != ULONG_MAX)
88+
{
89+
set_lines(vars, j, i, vars->map[i][j]);
90+
j++;
91+
}
92+
i++;
93+
}
94+
mlx_put_image_to_window(vars->mlx->mlx_ptr, vars->mlx->win_ptr,
95+
vars->mlx->img_ptr, 0, 0);
96+
}
97+

src/render/render_utils.c

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* render_utils.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: ebabaogl <[email protected] +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2025/01/13 16:15:19 by ebabaogl #+# #+# */
9+
/* Updated: 2025/01/13 16:17:23 by ebabaogl ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "fdf.h"
14+
15+
void set_camera_props(t_vars *vars)
16+
{
17+
double width_distance;
18+
double height_distance;
19+
20+
width_distance = (double)(PADDED_WIDTH) / (vars->row_size);
21+
height_distance = (double)(PADDED_HEIGHT) / (vars->col_size);
22+
if (width_distance > height_distance)
23+
vars->distance = height_distance;
24+
else
25+
vars->distance = width_distance;
26+
vars->height = 5;
27+
vars->zoom = 1;
28+
vars->x_y_coef = 0;
29+
vars->y_z_coef = 0;
30+
vars->z_x_coef = 0;
31+
vars->map_x = 0;
32+
vars->map_y = 0;
33+
}
34+
35+
void set_point(t_point *point, int x, int y, int z)
36+
{
37+
point->x = x;
38+
point->y = y;
39+
point->z = z;
40+
}
41+
42+
void render_point(t_vars *vars, t_point *point)
43+
{
44+
point->x = (point->x - vars->anchor_x) * vars->distance;
45+
point->y = (point->y - vars->anchor_y) * vars->distance;
46+
point->z = point->z * vars->height;
47+
rotate_x_y(vars, point);
48+
rotate_y_z(vars, point);
49+
rotate_z_x(vars, point);
50+
translate(vars, point);
51+
zoom(vars, point);
52+
}

src/render/transformations.c

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* transformations.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: ebabaogl <[email protected] +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2025/01/13 15:21:11 by ebabaogl #+# #+# */
9+
/* Updated: 2025/01/13 16:11:13 by ebabaogl ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "fdf.h"
14+
#include <math.h>
15+
16+
void zoom(t_vars *vars, t_point *point)
17+
{
18+
point->x *= vars->zoom;
19+
point->y *= vars->zoom;
20+
point->z *= vars->zoom;
21+
}
22+
23+
void translate(t_vars *vars, t_point *point)
24+
{
25+
point->x += vars->map_x;
26+
point->y -= vars->map_y;
27+
}
28+
29+
void rotate_z_x(t_vars *vars, t_point *point)
30+
{
31+
double original_z;
32+
33+
original_z = point->z;
34+
point->z = original_z * cos(vars->z_x_coef)
35+
+ point->x * -sin(vars->z_x_coef);
36+
point->x = original_z * sin(vars->z_x_coef)
37+
+ point->x * cos(vars->z_x_coef);
38+
}
39+
40+
void rotate_y_z(t_vars *vars, t_point *point)
41+
{
42+
double original_y;
43+
44+
original_y = point->y;
45+
point->y = point->z * sin(vars->y_z_coef)
46+
+ original_y * cos(vars->y_z_coef);
47+
point->z = point->z * cos(vars->y_z_coef)
48+
+ original_y * -sin(vars->y_z_coef);
49+
}
50+
51+
void rotate_x_y(t_vars *vars, t_point *point)
52+
{
53+
double original_x;
54+
55+
original_x = point->x;
56+
point->x = original_x * cos(vars->x_y_coef)
57+
+ point->y * -sin(vars->x_y_coef);
58+
point->y = original_x * sin(vars->x_y_coef)
59+
+ point->y * cos(vars->x_y_coef);
60+
}

src/utils.c

+9-2
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 13:53:41 by ebabaogl ### ########.fr */
9+
/* Updated: 2025/01/13 16:28:33 by ebabaogl ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -15,7 +15,7 @@
1515
#include <stdlib.h>
1616
#include <stddef.h>
1717

18-
unsigned int ft_atoi_hex(char *str)
18+
unsigned int atoi_hex(char *str)
1919
{
2020
unsigned int res;
2121
int val;
@@ -70,3 +70,10 @@ unsigned long get_color(unsigned long point)
7070
{
7171
return (point & COLOR_MASK);
7272
}
73+
74+
int abs(int n)
75+
{
76+
if (n < 0)
77+
return (-n);
78+
return (n);
79+
}

0 commit comments

Comments
 (0)