tgrekov-fdf
HIVE fdf May 2024
Loading...
Searching...
No Matches
fdf_mlx.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* fdf_mlx.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: tgrekov <tgrekov@student.hive.fi> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/06/13 07:32:01 by tgrekov #+# #+# */
9/* Updated: 2024/06/24 08:50:27 by tgrekov ### ########.fr */
10/* */
11/* ************************************************************************** */
12
20#include <stdlib.h>
21#include <MLX42.h>
22#include <ft_printf.h>
23#include "../fdf.h"
24#include "map.h"
25
26void draw_line(mlx_image_t *img, int x1, int y1, int *p2);
27int project_map(t_map map, int *size);
28
36static void keyhook(mlx_key_data_t key_data, void *arg)
37{
38 if (key_data.key == MLX_KEY_ESCAPE)
39 mlx_close_window((mlx_t *) arg);
40}
41
51static void lines_from_point(mlx_image_t *img, t_map map, int x, int y)
52{
53 if (x < (map.width - 1))
54 draw_line(img,
55 map.point[y][x].projected[0], map.point[y][x].projected[1],
56 map.point[y][x + 1].projected);
57 if (y < (map.height - 1))
58 draw_line(img,
59 map.point[y][x].projected[0], map.point[y][x].projected[1],
60 map.point[y + 1][x].projected);
61}
62
68static void fdf_row(void *data)
69{
70 int x;
71 int init_y;
72 t_loop_data *d;
73
74 d = (t_loop_data *) data;
75 if (d->y == d->map.height)
76 return ;
77 init_y = d->y;
78 while ((d->y < d->map.height) && (d->y - init_y) < FDF_ROWS_PER_ITERATION)
79 {
80 x = 0;
81 while (x < d->map.width)
82 lines_from_point(d->img, d->map, x++, d->y);
83 d->y++;
84 }
85}
86
96static int fdf_mlx(mlx_t *mlx, t_loop_data d, int *size)
97{
98 mlx_set_window_size(mlx, size[0], size[1]);
99 mlx_set_window_pos(mlx, 0, 0);
100 d.img = mlx_new_image(mlx, size[0], size[1]);
101 if (!d.img)
102 {
103 ft_printf("%>mlx_new_image() failed: %s\n", 2, mlx_strerror(mlx_errno));
104 return (1);
105 }
106 if (mlx_image_to_window(mlx, d.img, 0, 0) == -1)
107 {
108 ft_printf("%>mlx_image_to_window() failed: %s\n",
109 2, mlx_strerror(mlx_errno));
110 mlx_delete_image(mlx, d.img);
111 return (1);
112 }
113 mlx_loop_hook(mlx, fdf_row, &d);
114 mlx_key_hook(mlx, keyhook, mlx);
115 d.y = 0;
116 mlx_loop(mlx);
117 mlx_delete_image(mlx, d.img);
118 if (mlx_errno)
119 ft_printf("%>MLX error with unknown source: %s\n",
120 2, mlx_strerror(mlx_errno));
121 return (mlx_errno);
122}
123
131int fdf(t_map map)
132{
133 mlx_t *mlx;
134 t_loop_data d;
135 int size[2];
136 int status;
137
138 mlx = mlx_init(1, 1, FDF_WINDOW_TITLE, 0);
139 if (!mlx)
140 {
141 ft_printf("%>mlx_init() failed:%s\n", 2, mlx_strerror(mlx_errno));
142 return (0);
143 }
144 d.map = map;
145 if (project_map(map, size))
146 {
147 ft_printf("%>Map too big\n", 2);
148 status = 1;
149 }
150 else
151 status = fdf_mlx(mlx, d, size);
152 mlx_terminate(mlx);
153 return (status);
154}
int ft_printf(const char *format,...)
Prints and formats a variable set of arguments.
Definition ft_printf.c:252
static void keyhook(mlx_key_data_t key_data, void *arg)
Runs when a key is pressed to check whether the escape key was used, so the window can be closed.
Definition fdf_mlx.c:36
int fdf(t_map map)
Initialize mlx, run map projection and scaling, execute mlx, and cleanup.
Definition fdf_mlx.c:131
void draw_line(mlx_image_t *img, int x1, int y1, int *p2)
Multi-directional implementation of the Bresenham incremental error line drawing algorithm.
Definition draw_line.c:32
static int fdf_mlx(mlx_t *mlx, t_loop_data d, int *size)
Bulk of mlx functions used in fdf, to set up the key press and drawing hooks, set up the image,...
Definition fdf_mlx.c:96
static void lines_from_point(mlx_image_t *img, t_map map, int x, int y)
Checks if adjacent points are available, and draws lines to them.
Definition fdf_mlx.c:51
int project_map(t_map map, int *size)
Project map and attempt to automatically scale to viewport.
static void fdf_row(void *data)
Draw rows in between frames if the image isn't complete.
Definition fdf_mlx.c:68
Definition MLX42.h:361
Data structure passed to the drawing loop.
Definition map.h:73
int y
Starting row index for the drawing loop.
Definition map.h:76
t_map map
Map
Definition map.h:74
mlx_image_t * img
Pointer to mlx image on which to draw lines.
Definition map.h:75
Map data.
Definition map.h:54
int width
Number of columns in the map.
Definition map.h:55
int height
Number of rows in the map.
Definition map.h:56
t_point ** point
2d array of points in [y][x] order
Definition map.h:57
int projected[2]
Projected coordinates for this point.
Definition map.h:38