Skip to content

Commit c844269

Browse files
committed
put_pixel and fdf functions initialized
1 parent aeb118c commit c844269

File tree

1 file changed

+75
-23
lines changed

1 file changed

+75
-23
lines changed

fdf.c

+75-23
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
/* ::: :::::::: */
44
/* fdf.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: ebabaogl <ebabaogl@student.42kocaeli.co +#+ +:+ +#+ */
6+
/* By: obastug <obastug@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/01/02 11:39:32 by ebabaogl #+# #+# */
9-
/* Updated: 2025/01/03 16:55:03 by ebabaogl ### ########.fr */
9+
/* Updated: 2025/01/04 16:36:54 by obastug ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

13+
#include "mlx.h"
1314
#include "fdf.h"
1415
#include "window.h"
1516
#include "libft.h"
@@ -18,35 +19,86 @@
1819
#include <limits.h>
1920
#include <stdio.h>
2021
#include <unistd.h>
21-
unsigned int ft_atoi_hex(char *str);
2222

23-
int main()
23+
24+
void put_pixel(void *mlx_ptr, void *win_ptr, int x, int y, unsigned long color)
2425
{
25-
//t_vars vars;
26-
//t_mlx mlx;
27-
28-
// char *map = read_file("test_maps/42.fdf");
29-
// printf("%s", map);
30-
// free(map);
31-
//vars.mlx = &mlx;
32-
//if (init_win(vars.mlx) == -1)
33-
char *map_str = read_file("test_maps/42.fdf");
34-
unsigned long **map = map_string_to_arr_2d(map_str);
35-
write(1, "helloworld", 10);
36-
size_t i;
37-
size_t j;
26+
printf("pixel sent to mlx: %d, %d\n", (WIN_WIDTH / 2) + x, (WIN_HEIGHT / 2) - y);
27+
mlx_pixel_put(mlx_ptr, win_ptr, (WIN_WIDTH / 2) + x, (WIN_HEIGHT / 2) + y, color);
28+
}
29+
30+
void render_and_put_pixel(t_vars *vars, int x, int y, unsigned long point)
31+
{
32+
int final_x;
33+
int final_y;
34+
35+
final_x = (x - vars->anchor_x) * vars->distance;
36+
final_y = (y - vars->anchor_y) * vars->distance;
37+
put_pixel(vars->mlx->mlx_ptr, vars->mlx->win_ptr, final_x, final_y, get_color(point));
38+
printf("anchor point: %d %d\n", vars->anchor_x, vars->anchor_y);
39+
printf("coordinate: %d, %d\n", final_x, final_y);
40+
printf("distance: %d\n", vars->distance);
41+
}
42+
43+
void render_map(t_vars *vars)
44+
{
45+
int i;
46+
int j;
3847
i = 0;
39-
j = 0;
40-
while(map[i])
48+
while(vars->map[i])
4149
{
42-
while (map[i][j] != ULONG_MAX)
50+
j = 0;
51+
while (vars->map[i][j] != ULONG_MAX)
4352
{
44-
printf("i: %lu, ", i);
45-
printf("z: %lu, ", map[i][j] >> 32);
46-
printf("color: %lu\n", map[i][j] & COLOR_MASK);
53+
render_and_put_pixel(vars, i, j, vars->map[i][j]);
4754
j++;
4855
}
4956
i++;
5057
}
58+
}
59+
60+
void fdf(t_vars *vars)
61+
{
62+
// mlx_hook(vars->mlx->mlx_ptr, ON_DESTROY, 0, close_win, vars->mlx);
63+
reset_camera(vars);
64+
mlx_key_hook(vars->mlx->win_ptr, key_handler, vars);
65+
mlx_loop(vars->mlx->mlx_ptr);
66+
mlx_destroy_display(vars->mlx->mlx_ptr);
67+
free(vars->mlx->mlx_ptr);
68+
}
69+
70+
void set_map_props(t_vars *vars)
71+
{
72+
int i;
73+
74+
i = 0;
75+
while (vars->map[0][i] != ULONG_MAX)
76+
i++;
77+
vars->line_len = i;
78+
vars->anchor_x = i / 2;
79+
i = 0;
80+
while (vars->map[i])
81+
i++;
82+
vars->line_count = i;
83+
vars->anchor_y = i / 2;
84+
}
85+
86+
int main()
87+
{
88+
t_vars vars;
89+
t_mlx mlx;
90+
char *map_str;
91+
92+
map_str = read_file("test_maps/2x2.fdf");
93+
if (!map_str)
94+
return (write(1, "file couldn't read", 18), 1);
95+
vars.map = map_string_to_arr_2d(map_str);
96+
if (!vars.map)
97+
return (write(1, "invalid map", 11), 1);
98+
set_map_props(&vars);
99+
vars.mlx = &mlx;
100+
init_win(&mlx);
101+
fdf(&vars);
102+
51103
return (1);
52104
}

0 commit comments

Comments
 (0)