Debian Codegraph @

src/pygame_sdl2/gfxdraw.pyx

raw
1 # Copyright 2014 Patrick Dawson <[email protected]>
2 #
3 # This software is provided 'as-is', without any express or implied
4 # warranty. In no event will the authors be held liable for any damages
5 # arising from the use of this software.
6 #
7 # Permission is granted to anyone to use this software for any purpose,
8 # including commercial applications, and to alter it and redistribute it
9 # freely, subject to the following restrictions:
10 #
11 # 1. The origin of this software must not be misrepresented; you must not
12 # claim that you wrote the original software. If you use this software
13 # in a product, an acknowledgment in the product documentation would be
14 # appreciated but is not required.
15 # 2. Altered source versions must be plainly marked as such, and must not be
16 # misrepresented as being the original software.
17 # 3. This notice may not be removed or altered from any source distribution.
18
19 from sdl2 cimport *
20 from sdl2_gfx cimport *
21 from libc.stdlib cimport malloc, free
22 from pygame_sdl2.surface cimport Surface
23 from pygame_sdl2.color cimport Color
24
25 from pygame_sdl2.error import error
26 from pygame_sdl2.rect import Rect
27
28
29 def pixel(Surface surface, x, y, color):
30 cdef Color c = Color(color)
31 pixelRGBA(surface.surface, x, y, c.r, c.g, c.b, c.a)
32
33 def hline(Surface surface, x1, x2, y, color):
34 cdef Color c = Color(color)
35 hlineRGBA(surface.surface, x1, x2, y, c.r, c.g, c.b, c.a)
36
37 def vline(Surface surface, x, y1, y2, color):
38 cdef Color c = Color(color)
39 vlineRGBA(surface.surface, x, y1, y2, c.r, c.g, c.b, c.a)
40
41 def rectangle(Surface surface, rect, color):
42 cdef Color c = Color(color)
43 if not isinstance(rect, Rect):
44 rect = Rect(rect)
45 rectangleRGBA(surface.surface, rect.x, rect.y, rect.x + rect.w, rect.y + rect.h, c.r, c.g, c.b, c.a)
46
47 def rounded_rectangle(Surface surface, rect, rad, color):
48 cdef Color c = Color(color)
49 if not isinstance(rect, Rect):
50 rect = Rect(rect)
51 roundedRectangleRGBA(surface.surface, rect.x, rect.y, rect.x + rect.w, rect.y + rect.h, rad, c.r, c.g, c.b, c.a)
52
53 def box(Surface surface, rect, color):
54 cdef Color c = Color(color)
55 if not isinstance(rect, Rect):
56 rect = Rect(rect)
57 boxRGBA(surface.surface, rect.x, rect.y, rect.x + rect.w, rect.y + rect.h, c.r, c.g, c.b, c.a)
58
59 def rounded_box(Surface surface, rect, rad, color):
60 cdef Color c = Color(color)
61 if not isinstance(rect, Rect):
62 rect = Rect(rect)
63 roundedBoxRGBA(surface.surface, rect.x, rect.y, rect.x + rect.w, rect.y + rect.h, rad, c.r, c.g, c.b, c.a)
64
65 def line(Surface surface, x1, y1, x2, y2, color):
66 cdef Color c = Color(color)
67 lineRGBA(surface.surface, x1, y1, x2, y2, c.r, c.g, c.b, c.a)
68
69 def aaline(Surface surface, x1, y1, x2, y2, color):
70 cdef Color c = Color(color)
71 aalineRGBA(surface.surface, x1, y1, x2, y2, c.r, c.g, c.b, c.a)
72
73 def thick_line(Surface surface, x1, y1, x2, y2, width, color):
74 cdef Color c = Color(color)
75
76 # This locks up in c code when trying to draw a zero-length line. So make
77 # sure that doesn't happen.
78 cdef int x1int, y1int, x2int, y2int
79 x1int = x1
80 y1int = y1
81 x2int = x2
82 y2int = y2
83
84
85 if x1int == x2int and y1int == y2int:
86 return
87
88 thickLineRGBA(surface.surface, x1int, y1int, x2int, y2int, width, c.r, c.g, c.b, c.a)
89
90 def circle(Surface surface, x, y, r, color):
91 cdef Color c = Color(color)
92 circleRGBA(surface.surface, x, y, r, c.r, c.g, c.b, c.a)
93
94 def arc(Surface surface, x, y, r, start, end, color):
95 cdef Color c = Color(color)
96 arcRGBA(surface.surface, x, y, r, start, end, c.r, c.g, c.b, c.a)
97
98 def aacircle(Surface surface, x, y, r, color):
99 cdef Color c = Color(color)
100 aacircleRGBA(surface.surface, x, y, r, c.r, c.g, c.b, c.a)
101
102 def filled_circle(Surface surface, x, y, r, color):
103 cdef Color c = Color(color)
104 filledCircleRGBA(surface.surface, x, y, r, c.r, c.g, c.b, c.a)
105
106 def ellipse(Surface surface, x, y, rx, ry, color):
107 cdef Color c = Color(color)
108 ellipseRGBA(surface.surface, x, y, rx, ry, c.r, c.g, c.b, c.a)
109
110 def aaellipse(Surface surface, x, y, rx, ry, color):
111 cdef Color c = Color(color)
112 aaellipseRGBA(surface.surface, x, y, rx, ry, c.r, c.g, c.b, c.a)
113
114 def filled_ellipse(Surface surface, x, y, rx, ry, color):
115 cdef Color c = Color(color)
116 filledEllipseRGBA(surface.surface, x, y, rx, ry, c.r, c.g, c.b, c.a)
117
118 def pie(Surface surface, x, y, r, start, end, color):
119 cdef Color c = Color(color)
120 pieRGBA(surface.surface, x, y, r, start, end, c.r, c.g, c.b, c.a)
121
122 def filled_pie(Surface surface, x, y, r, start, end, color):
123 cdef Color c = Color(color)
124 filledPieRGBA(surface.surface, x, y, r, start, end, c.r, c.g, c.b, c.a)
125
126 def trigon(Surface surface, x1, y1, x2, y2, x3, y3, color):
127 cdef Color c = Color(color)
128 trigonRGBA(surface.surface, x1, y1, x2, y2, x3, y3, c.r, c.g, c.b, c.a)
129
130 def aatrigon(Surface surface, x1, y1, x2, y2, x3, y3, color):
131 cdef Color c = Color(color)
132 aatrigonRGBA(surface.surface, x1, y1, x2, y2, x3, y3, c.r, c.g, c.b, c.a)
133
134 def filled_trigon(Surface surface, x1, y1, x2, y2, x3, y3, color):
135 cdef Color c = Color(color)
136 filledTrigonRGBA(surface.surface, x1, y1, x2, y2, x3, y3, c.r, c.g, c.b, c.a)
137
138 def polygon(Surface surface, points, color):
139 cdef Color c = Color(color)
140 cdef Sint16 *vx
141 cdef Sint16 *vy
142 cdef size_t num_points = len(points)
143 vx = <Sint16*>malloc(num_points * sizeof(Sint16))
144 vy = <Sint16*>malloc(num_points * sizeof(Sint16))
145 for n, pt in zip(range(num_points), points):
146 vx[n], vy[n] = points[n]
147 polygonRGBA(surface.surface, vx, vy, num_points, c.r, c.g, c.b, c.a)
148 free(vx)
149 free(vy)
150
151 def aapolygon(Surface surface, points, color):
152 cdef Color c = Color(color)
153 cdef Sint16 *vx
154 cdef Sint16 *vy
155 cdef size_t num_points = len(points)
156 vx = <Sint16*>malloc(num_points * sizeof(Sint16))
157 vy = <Sint16*>malloc(num_points * sizeof(Sint16))
158 for n, pt in zip(range(num_points), points):
159 vx[n], vy[n] = points[n]
160 aapolygonRGBA(surface.surface, vx, vy, num_points, c.r, c.g, c.b, c.a)
161 free(vx)
162 free(vy)
163
164 def filled_polygon(Surface surface, points, color):
165 cdef Color c = Color(color)
166 cdef Sint16 *vx
167 cdef Sint16 *vy
168 cdef size_t num_points = len(points)
169 vx = <Sint16*>malloc(num_points * sizeof(Sint16))
170 vy = <Sint16*>malloc(num_points * sizeof(Sint16))
171 for n, pt in zip(range(num_points), points):
172 vx[n], vy[n] = points[n]
173 filledPolygonRGBA(surface.surface, vx, vy, num_points, c.r, c.g, c.b, c.a)
174 free(vx)
175 free(vy)
176
177 def textured_polygon(Surface surface, points, Surface texture not None, tx, ty):
178 cdef Sint16 *vx
179 cdef Sint16 *vy
180 cdef size_t num_points = len(points)
181 vx = <Sint16*>malloc(num_points * sizeof(Sint16))
182 vy = <Sint16*>malloc(num_points * sizeof(Sint16))
183 for n, pt in zip(range(num_points), points):
184 vx[n], vy[n] = points[n]
185 texturedPolygon(surface.surface, vx, vy, num_points, texture.surface, tx, ty)
186 free(vx)
187 free(vy)
188
189 def bezier(Surface surface, points, steps, color):
190 cdef Color c = Color(color)
191 cdef Sint16 *vx
192 cdef Sint16 *vy
193 cdef size_t num_points = len(points)
194 vx = <Sint16*>malloc(num_points * sizeof(Sint16))
195 vy = <Sint16*>malloc(num_points * sizeof(Sint16))
196 for n, pt in zip(range(num_points), points):
197 vx[n], vy[n] = points[n]
198 bezierRGBA(surface.surface, vx, vy, num_points, steps, c.r, c.g, c.b, c.a)
199 free(vx)
200 free(vy)