-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlineDrawer.cpp
More file actions
179 lines (155 loc) · 3.79 KB
/
lineDrawer.cpp
File metadata and controls
179 lines (155 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
Copyright (c) 2013 Auston Sterling
See license.txt for copying permission.
-----LineDrawer Class Implementation-----
Auston Sterling
austonst@gmail.com
Contains the implementation of the LineDrawer class, which draws (currently) Bresenham
lines on an assigned SDL_Surface*.
*/
#ifndef _linedrawer_cpp_
#define _linedrawer_cpp_
#include "lineDrawer.h"
#include <cmath>
//Default constructor
LineDrawer::LineDrawer() : surf_(NULL)
{}
//Better constructor
LineDrawer::LineDrawer(SDL_Surface* insurf) : surf_(insurf)
{}
//Draws a line on the SDL_Surface from p1 to p2 with color c
void LineDrawer::line(Vec2f p1, Vec2f p2, SDL_Color c)
{
//Find if the surface is longer in X or Y direction
bool steep = abs(p2.y() - p1.y()) > abs(p2.x() - p1.x());
if (steep)
{
float swapper = p2.y();
p2.sety(p2.x());
p2.setx(swapper);
swapper = p1.y();
p1.sety(p1.x());
p1.setx(swapper);
}
//Ensure the line increases from p1 to p2
if (p1.x() > p2.x())
{
float swapper = p2.y();
p2.sety(p1.y());
p1.sety(swapper);
swapper = p2.x();
p2.setx(p1.x());
p1.setx(swapper);
}
//Find the change in x and y and initialize variables
int dx = p2.x() - p1.x();
int dy = abs(p2.y() - p1.y());
int error = dx/2;
int ystep = (p1.y() < p2.y())?1:-1;
int y = p1.y();
//Set up the SDL_Surface
if (SDL_MUSTLOCK(surf_)) SDL_LockSurface(surf_);
Uint32* pixels = (Uint32*)surf_->pixels;
//Loop over line
for (int x = p1.x(); x < p2.x(); x++)
{
//Plot the point
if (!steep)
{
pixels[x + y*surf_->w] = SDL_MapRGB(surf_->format, c.r, c.g, c.b);
}
else
{
pixels[y + x*surf_->w] = SDL_MapRGB(surf_->format, c.r, c.g, c.b);
}
//Modify the error
error -= dy;
if (error < 0)
{
y += ystep;
error += dx;
}
}
//Unlock the surface if needed
if (SDL_MUSTLOCK(surf_)) SDL_UnlockSurface(surf_);
}
//Draws a line on the SDL_Surface from p1 (with color c1) to p2 (with color c2)
void LineDrawer::line(Vec2f p1, Vec2f p2, SDL_Color c1, SDL_Color c2)
{
//Find if the surface is longer in X or Y direction
bool steep = abs(p2.y() - p1.y()) > abs(p2.x() - p1.x());
if (steep)
{
float swapper = p2.y();
p2.sety(p2.x());
p2.setx(swapper);
swapper = p1.y();
p1.sety(p1.x());
p1.setx(swapper);
}
//Ensure the line increases from p1 to p2
if (p1.x() > p2.x())
{
float swapper = p2.y();
p2.sety(p1.y());
p1.sety(swapper);
swapper = p2.x();
p2.setx(p1.x());
p1.setx(swapper);
SDL_Color colorswap = c1;
c1 = c2;
c2 = colorswap;
}
//Find the change in x and y and initialize variables
int dx = p2.x() - p1.x();
int dy = abs(p2.y() - p1.y());
int error = dx/2;
int ystep = (p1.y() < p2.y())?1:-1;
int y = p1.y();
//Find the change in color per x step
float cr, cg, cb;
cr = c1.r;
cg = c1.g;
cb = c1.b;
float dcr, dcg, dcb;
if (dx != 0)
{
dcr = float(c2.r - c1.r) / float(dx);
dcg = float(c2.g - c1.g) / float(dx);
dcb = float(c2.b - c1.b) / float(dx);
}
else
{
dcr = dcg = dcb = 0;
}
//Set up the SDL_Surface
if (SDL_MUSTLOCK(surf_)) SDL_LockSurface(surf_);
Uint32* pixels = (Uint32*)surf_->pixels;
//Loop over line
for (int x = p1.x(); x < p2.x(); x++)
{
//Plot the point
if (!steep)
{
pixels[x + y*surf_->w] = SDL_MapRGB(surf_->format, cr, cg, cb);
}
else
{
pixels[y + x*surf_->w] = SDL_MapRGB(surf_->format, cr, cg, cb);
}
//Modify the error
error -= dy;
if (error < 0)
{
y += ystep;
error += dx;
}
//Modify color
cr += dcr;
cg += dcg;
cb += dcb;
}
//Unlock the surface if needed
if (SDL_MUSTLOCK(surf_)) SDL_UnlockSurface(surf_);
}
#endif