/ fdf / fdf / srcs / draw.c
draw.c
  1  /* ************************************************************************** */
  2  /*                                                                            */
  3  /*                                                        :::      ::::::::   */
  4  /*   draw.c                                             :+:      :+:    :+:   */
  5  /*                                                    +:+ +:+         +:+     */
  6  /*   By: gychoi <gychoi@student.42seoul.kr>         +#+  +:+       +#+        */
  7  /*                                                +#+#+#+#+#+   +#+           */
  8  /*   Created: 2022/12/24 18:31:18 by gychoi            #+#    #+#             */
  9  /*   Updated: 2023/01/03 16:38:56 by gychoi           ###   ########.fr       */
 10  /*                                                                            */
 11  /* ************************************************************************** */
 12  
 13  #include "fdf.h"
 14  
 15  static void	my_mlx_pixel_put(t_fdf *fdf, int x, int y, int color)
 16  {
 17  	char	*dst;
 18  
 19  	if ((0 <= x && x < SCREEN_WIDTH) && (0 <= y && y < SCREEN_HEIGHT))
 20  	{
 21  		dst = fdf->addr + ((y * fdf->line_len) + (x * fdf->bpp / 8));
 22  		*(unsigned int *)dst = color;
 23  	}
 24  }
 25  
 26  static void	plot_dx_big(t_point s, t_point f, t_fdf *fdf, t_delta delta)
 27  {
 28  	int	plot;
 29  
 30  	plot = 2 * delta.dy - delta.dx;
 31  	while (1)
 32  	{
 33  		my_mlx_pixel_put(fdf, s.x, s.y, s.color);
 34  		if (s.x == f.x)
 35  			break ;
 36  		if (plot < 0)
 37  			plot += 2 * delta.dy;
 38  		else
 39  		{
 40  			if (s.y < f.y)
 41  				s.y++;
 42  			else
 43  				s.y--;
 44  			plot += 2 * (delta.dy - delta.dx);
 45  		}
 46  		if (s.x < f.x)
 47  			s.x++;
 48  		else
 49  			s.x--;
 50  	}
 51  }
 52  
 53  static void	plot_dy_big(t_point s, t_point f, t_fdf *fdf, t_delta delta)
 54  {
 55  	int	plot;
 56  
 57  	plot = 2 * delta.dx - delta.dy;
 58  	while (1)
 59  	{
 60  		my_mlx_pixel_put(fdf, s.x, s.y, s.color);
 61  		if (s.y == f.y)
 62  			break ;
 63  		if (plot < 0)
 64  			plot += 2 * delta.dx;
 65  		else
 66  		{
 67  			if (s.x < f.x)
 68  				s.x++;
 69  			else
 70  				s.x--;
 71  			plot += 2 * (delta.dx - delta.dy);
 72  		}
 73  		if (s.y < f.y)
 74  			s.y++;
 75  		else
 76  			s.y--;
 77  	}
 78  }
 79  
 80  static void	plot_line(t_point s, t_point f, t_fdf *fdf)
 81  {
 82  	t_delta	delta;
 83  
 84  	delta.dx = fdf_abs(f.x - s.x);
 85  	delta.dy = fdf_abs(f.y - s.y);
 86  	if (delta.dx >= delta.dy)
 87  		plot_dx_big(s, f, fdf, delta);
 88  	else
 89  		plot_dy_big(s, f, fdf, delta);
 90  }
 91  
 92  void	draw_frame(t_fdf *fdf)
 93  {
 94  	t_point	**points;
 95  	int		y;
 96  	int		x;
 97  
 98  	init_draw(fdf);
 99  	points = set_points(fdf);
100  	y = 0;
101  	while (y < fdf->map.height)
102  	{
103  		x = 0;
104  		while (x < fdf->map.width)
105  		{
106  			if (x < fdf->map.width - 1)
107  				plot_line(points[y][x], points[y][x + 1], fdf);
108  			if (y < fdf->map.height - 1)
109  				plot_line(points[y][x], points[y + 1][x], fdf);
110  			x++;
111  		}
112  		free(points[y]);
113  		y++;
114  	}
115  	free(points);
116  	mlx_put_image_to_window(fdf->mlx, fdf->win, fdf->img, 0, 0);
117  }