/
primitive.go
202 lines (168 loc) · 4.68 KB
/
primitive.go
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Primitives are basic shapes which can be directly drawn to screen.
*
*/
package graphics
import "image/color"
import "github.com/banthar/Go-SDL/sdl"
// A Primitive is a basic shape which can be drawn directly by the artist.
type Primitive interface {
draw(s *sdl.Surface)
}
// A Point is as it sounds, a single point in space.
type Point struct {
x, y int
c color.Color
}
// Points are drawn by setting a single corresponding pixel.
func (p Point) draw(s *sdl.Surface) {
color := sdl.ColorFromGoColor(p.c)
safeSet(s, p.x, p.y, color)
}
// A Rectangle is... a rectangle.
type Rectangle struct {
x, y int16
w, h uint16
c color.Color
}
// Rectangles are drawn by directly calling FillRect on the surface.
func (r Rectangle) draw(s *sdl.Surface) {
format := s.Format
color := sdl.ColorFromGoColor(r.c)
colorVal := sdl.MapRGB(format, color.R, color.G, color.B)
s.FillRect(&sdl.Rect{r.x, r.y, r.w, r.h}, colorVal)
}
// Circles are, you guessed it. Circles.
type Circle struct {
x, y int16 // Location on screen
r uint16 // Radius
b int // Border thickness. For now only controls if there IS a border or not, not actually it's thickness.
c color.Color // Color
}
// Circles may be filled or not.
func (c Circle) draw(s *sdl.Surface) {
if c.b == 0 {
drawFilledCircle(c.x, c.y, c.r, c.c, s)
} else {
drawOutlineCircle(c.x, c.y, c.r, c.c, s)
}
}
// Specifies a line to be drawn.
type Line struct {
x0, y0, x1, y1 int16
c color.Color
}
func (l Line) draw(s *sdl.Surface) {
drawLine(l.x0, l.y0, l.x1, l.y1, l.c, s)
}
// drawFilledCircle uses the integer midpoint circle algorithm to draw a filled
// circle to the given surface.
func drawFilledCircle(x0, y0 int16, r uint16, c color.Color, s *sdl.Surface) {
format := s.Format
color := sdl.ColorFromGoColor(c)
colorVal := sdl.MapRGB(format, color.R, color.G, color.B)
x := int16(r)
y := int16(0)
e := 1 - x
for x >= y {
s.FillRect(&sdl.Rect{-x + x0, y + y0, uint16(2 * x), 1}, colorVal)
s.FillRect(&sdl.Rect{-x + x0, -y + y0, uint16(2 * x), 1}, colorVal)
s.FillRect(&sdl.Rect{-y + x0, x + y0, uint16(2 * y), 1}, colorVal)
s.FillRect(&sdl.Rect{-y + x0, -x + y0, uint16(2 * y), 1}, colorVal)
y++
if e < 0 {
e += 2*y + 1
} else {
x--
e += 2 * (y - x + 1)
}
}
}
// drawOutlineCircle uses the integer midpoint circle algorithm to draw the outline
// of a circle (1 px thick) to the given surface.
func drawOutlineCircle(x0, y0 int16, r uint16, c color.Color, s *sdl.Surface) {
s.Lock()
defer s.Unlock()
color := sdl.ColorFromGoColor(c)
x := int16(r)
y := int16(0)
e := 1 - x
for x >= y {
safeSet(s, int(x+x0), int(y+y0), color)
safeSet(s, int(x+x0), int(-y+y0), color)
safeSet(s, int(-x+x0), int(y+y0), color)
safeSet(s, int(-x+x0), int(-y+y0), color)
safeSet(s, int(y+x0), int(x+y0), color)
safeSet(s, int(y+x0), int(-x+y0), color)
safeSet(s, int(-y+x0), int(x+y0), color)
safeSet(s, int(-y+x0), int(-x+y0), color)
y++
if e < 0 {
e += 2*y + 1
} else {
x--
e += 2 * (y - x + 1)
}
}
}
// Uses Bresenham's algorithm to draw a line between two points.
func drawLine(x0, y0, x1, y1 int16, c color.Color, s *sdl.Surface) {
s.Lock()
defer s.Unlock()
color := sdl.ColorFromGoColor(c)
// Make sure the two ends are left-to-right.
if x1 < x0 {
x0, x1 = x1, x0
y0, y1 = y1, y0
}
// This algorithm only works for curves where dx > -dy and dy < 0
// So, prepare a coordinate transform to make this the case.
// We will then reverse the transform when we plot the points.
// There are 4 cases, all the transformations are self-inverse, which
// makes our lives a little easier.
dx := int(x1 - x0)
dy := int(y1 - y0)
var transform func(x, y int) (int, int)
var inverse func(x, y int) (int, int)
if dy < 0 {
if dx < -dy {
transform = func(x, y int) (int, int) { return -y, x }
inverse = func(x, y int) (int, int) { return y, -x }
} else {
transform = func(x, y int) (int, int) { return x, -y }
inverse = transform
}
} else {
if dx < dy {
transform = func(x, y int) (int, int) { return y, x }
inverse = transform
} else {
transform = func(x, y int) (int, int) { return x, y }
inverse = transform
}
}
// Transform coordinates.
tx0, ty0 := transform(int(x0), int(y0))
tx1, ty1 := transform(int(x1), int(y1))
// Recalculate dx and dy.
dx = tx1 - tx0
dy = ty1 - ty0
D := 2*dy - dx
safeSet(s, int(x0), int(y0), color)
y := ty0
for x := tx0 + 1; x <= tx1; x++ {
if D > 0 {
y += 1
D += 2*dy - 2*dx
} else {
D += 2 * dy
}
tx, ty := inverse(x, y)
safeSet(s, tx, ty, color)
}
}
func safeSet(s *sdl.Surface, x, y int, c sdl.Color) {
if x >= 0 && y >= 0 && x < int(s.W) && y < int(s.H) {
s.Set(x, y, c)
}
}