Cub3D
Loading...
Searching...
No Matches
draw_line.c
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* draw_line.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/08/26 14:34:59 by kamitsui #+# #+# */
9/* Updated: 2024/09/30 17:57:29 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "cub3d.h"
14
15// propo is: Proportion of moving points to line span
16static int transition_color(int start, int end, int move_point, t_clr *color)
17{
18 int span;
19 int propo;
20 int tr_r;
21 int tr_g;
22 int tr_b;
23
24 span = end - start;
25 propo = (move_point - start) * 255 / span;
26 tr_r = color->start_r + ((color->end_r - color->start_r) * propo) / 255;
27 tr_g = color->start_g + ((color->end_g - color->start_g) * propo) / 255;
28 tr_b = color->start_b + ((color->end_b - color->start_b) * propo) / 255;
29 return ((tr_r << 16) | (tr_g << 8) | tr_b);
30}
31
32static int get_current_color(t_plot *plot, t_line *line, t_clr *color)
33{
34 int start;
35 int end;
36 int move_point;
37
38 if (plot->dx > 0)
39 {
40 start = line->x_start;
41 end = line->x_end;
42 move_point = plot->px;
43 }
44 else
45 {
46 start = line->y_start;
47 end = line->y_end;
48 move_point = plot->py;
49 }
50 return (transition_color(start, end, move_point, color));
51}
52
53//#include "ft_printf.h"
54//void debug_line(int x_start, int y_start, int color0,
55// int x_end, int y_end, int color1)
56//{
57// ft_printf("\n\n---- draw_line ---- start -> end\n");
58// ft_printf("start\tx_start:%d y_start:%d color0:%X\n",
59// x_start, y_start, color0);
60// ft_printf("end \tx_end:%d y_end:%d color1:%X\n", x_end, y_end, color1);
61//}
62//
63//void debug_count(int px, int py, int pc)
64//{
65// static int i = 0;
66// ft_printf("draw_loop : %d\tpx:%d\tpy:%d\tpc:%X\n", i++, px, py, pc);
67//}
68// debug_line(line->x_start, line->y_start, line->color0,
69// line->x_end, line->y_end, line->color1);
70// debug_count(plot->px, plot->py, plot->pc);
71
72static void draw_loop(t_plot *plot, t_line *line, t_clr *color, t_img *img)
73{
74 while (1)
75 {
76 if (plot->px >= IMG_2D_WIDTH || plot->px <= 0
77 || plot->py >= IMG_2D_HEIGHT || plot->py <= 0)
78 break ;
79 plot->pc = get_current_color(plot, line, color);
80 my_mlx_pixel_put(img, plot->px, plot->py, plot->pc);
81 if (plot->px == line->x_end && plot->py == line->y_end)
82 break ;
83 plot->err2 = 2 * plot->err;
84 if (plot->err2 > -plot->dy)
85 {
86 plot->err -= plot->dy;
87 plot->px += plot->sx;
88 }
89 if (plot->err2 < plot->dx)
90 {
91 plot->err += plot->dx;
92 plot->py += plot->sy;
93 }
94 }
95}
96
97// memo : develop unfinished
98// Bresenham's line algorithm
99void draw_line(t_img *img, t_line *line)
100{
101 t_plot plot;
102 t_clr color;
103
104 init_plot(&plot, line);
105 init_color(&color, line->color_start, line->color_end);
106 draw_loop(&plot, line, &color, img);
107}
3D or 2D or Texture image
Definition type_cub3d.h:82