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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
"""2D drawing examples utilising the SDL2_gfx functions."""
import sys
import ctypes
from random import randint
import sdl2
import sdl2.sdlgfx
import sdl2.ext
# Draws random lines using the passed rendering context
def draw_lines(context, width, height):
# Reset the visible area with a black color.
context.clear(0)
# Split the visible area
whalf = width // 2 - 2
hhalf = height // 2 - 2
lw = 5
x0, x1 = whalf, whalf
y0, y1 = 0, height
sdl2.sdlgfx.thickLineColor(context.sdlrenderer, x0, y0, x1, y1, lw,
0xFFFFFFFF)
x0, x1 = 0, width
y0, y1 = hhalf, hhalf
sdl2.sdlgfx.thickLineColor(context.sdlrenderer, x0, y0, x1, y1, lw,
0xFFFFFFFF)
for x in range(15):
# In the first quadrant, draw normal lines
color = randint(0, 0xFFFFFFFF)
x0, x1 = randint(0, whalf), randint(0, whalf)
y0, y1 = randint(0, hhalf), randint(0, hhalf)
sdl2.sdlgfx.lineColor(context.sdlrenderer, x0, y0, x1, y1, color)
# In the second quadrant, draw aa lines
color = randint(0, 0xFFFFFFFF)
x0, x1 = randint(whalf + lw, width), randint(whalf + lw, width)
y0, y1 = randint(0, hhalf), randint(0, hhalf)
sdl2.sdlgfx.aalineColor(context.sdlrenderer, x0, y0, x1, y1, color)
# In the third quadrant, draw horizontal lines
color = randint(0, 0xFFFFFFFF)
x0, x1 = randint(0, whalf), randint(0, whalf)
y0 = randint(hhalf + lw, height)
sdl2.sdlgfx.hlineColor(context.sdlrenderer, x0, x1, y0, color)
# In the fourth quadrant, draw vertical lines
color = randint(0, 0xFFFFFFFF)
x0 = randint(whalf + lw, width)
y0, y1 = randint(hhalf + lw, height), randint(hhalf + lw, height)
sdl2.sdlgfx.vlineColor(context.sdlrenderer, x0, y0, y1, color)
# Draws random circles using the passed rendering context
def draw_circles(context, width, height):
# Reset the visible area with a black color.
context.clear(0)
# Split the visible area
wthird = width // 3 - 1
lw = 3
sdl2.sdlgfx.thickLineColor(context.sdlrenderer, wthird, 0, wthird, height,
lw, 0xFFFFFFFF)
sdl2.sdlgfx.thickLineColor(context.sdlrenderer, (2 * wthird + lw), 0,
(2 * wthird + lw), height, lw, 0xFFFFFFFF)
for x in range(15):
# In the first part, draw circles
color = randint(0, 0xFFFFFFFF)
x, y = randint(0, wthird), randint(0, height)
r = randint(1, max(min(x, wthird - x), 2))
sdl2.sdlgfx.circleColor(context.sdlrenderer, x, y, r, color)
# In the second part, draw aa circles
color = randint(0, 0xFFFFFFFF)
x, y = randint(0, wthird), randint(0, height)
r = randint(1, max(min(x, wthird - x), 2))
sdl2.sdlgfx.aacircleColor(context.sdlrenderer, x + wthird + lw, y, r,
color)
# In the third part, draw filled circles
color = randint(0, 0xFFFFFFFF)
x, y = randint(0, wthird), randint(0, height)
r = randint(1, max(min(x, wthird - x), 2))
sdl2.sdlgfx.filledCircleColor(context.sdlrenderer, x + 2 * (wthird + lw),
y, r, color)
# Draws random ellipsis using the passed rendering context
def draw_ellipsis(context, width, height):
# Reset the visible area with a black color.
context.clear(0)
# Split the visible area
wthird = width // 3 - 1
eheight = height // 4
lw = 3
sdl2.sdlgfx.thickLineColor(context.sdlrenderer, wthird, 0, wthird, height,
lw, 0xFFFFFFFF)
sdl2.sdlgfx.thickLineColor(context.sdlrenderer, (2 * wthird + lw), 0,
(2 * wthird + lw), height, lw, 0xFFFFFFFF)
for x in range(15):
# In the first part, draw ellipsis
color = randint(0, 0xFFFFFFFF)
x, y = randint(0, wthird), randint(0, height)
rx, ry = randint(1, max(min(x, wthird - x), 2)), randint(0, eheight)
sdl2.sdlgfx.ellipseColor(context.sdlrenderer, x, y, rx, ry, color)
# In the second part, draw aa ellipsis
color = randint(0, 0xFFFFFFFF)
x, y = randint(0, wthird), randint(0, height)
rx, ry = randint(1, max(min(x, wthird - x), 2)), randint(0, eheight)
sdl2.sdlgfx.aaellipseColor(context.sdlrenderer, x + wthird + lw, y,
rx, ry, color)
# In the third part, draw filled ellipsis
color = randint(0, 0xFFFFFFFF)
x, y = randint(0, wthird), randint(0, height)
rx, ry = randint(1, max(min(x, wthird - x), 2)), randint(0, eheight)
sdl2.sdlgfx.filledEllipseColor(context.sdlrenderer,
x + 2 * (wthird + lw), y, rx, ry,
color)
# Draws random rectangles using the passed rendering context
def draw_rects(context, width, height):
# Reset the visible area with a black color.
context.clear(0)
# Split the visible area
whalf = width // 2 - 2
hhalf = height // 2 - 2
lw = 5
x0, x1 = whalf, whalf
y0, y1 = 0, height
sdl2.sdlgfx.thickLineColor(context.sdlrenderer, x0, y0, x1, y1, lw,
0xFFFFFFFF)
x0, x1 = 0, width
y0, y1 = hhalf, hhalf
sdl2.sdlgfx.thickLineColor(context.sdlrenderer, x0, y0, x1, y1, lw,
0xFFFFFFFF)
for x in range(15):
# In the first quadrant, draw normal rectangles
color = randint(0, 0xFFFFFFFF)
x0, x1 = randint(0, whalf), randint(0, whalf)
y0, y1 = randint(0, hhalf), randint(0, hhalf)
sdl2.sdlgfx.rectangleColor(context.sdlrenderer, x0, y0, x1, y1, color)
# In the second quadrant, draw rounded rectangles
color = randint(0, 0xFFFFFFFF)
x0, x1 = randint(whalf + lw, width), randint(whalf + lw, width)
y0, y1 = randint(0, hhalf), randint(0, hhalf)
r = randint(0, max(x1 - x0, x0 - x1))
sdl2.sdlgfx.roundedRectangleColor(context.sdlrenderer, x0, y0, x1, y1, r,
color)
# In the third quadrant, draw horizontal lines
color = randint(0, 0xFFFFFFFF)
x0, x1 = randint(0, whalf), randint(0, whalf)
y0, y1 = randint(hhalf + lw, height), randint(hhalf + lw, height)
sdl2.sdlgfx.boxColor(context.sdlrenderer, x0, y0, x1, y1, color)
# In the fourth quadrant, draw vertical lines
color = randint(0, 0xFFFFFFFF)
x0, x1 = randint(whalf + lw, width), randint(whalf + lw, width)
y0, y1 = randint(hhalf + lw, height), randint(hhalf + lw, height)
r = randint(0, max(x1 - x0, x0 - x1))
sdl2.sdlgfx.roundedBoxColor(context.sdlrenderer, x0, y0, x1, y1, r,
color)
# Draws random triangles using the passed rendering context
def draw_trigons(context, width, height):
# Reset the visible area with a black color.
context.clear(0)
# Split the visible area
wthird = width // 3 - 1
lw = 3
sdl2.sdlgfx.thickLineColor(context.sdlrenderer, wthird, 0, wthird, height,
lw, 0xFFFFFFFF)
sdl2.sdlgfx.thickLineColor(context.sdlrenderer, (2 * wthird + lw), 0,
(2 * wthird + lw), height, lw, 0xFFFFFFFF)
for x in range(15):
# In the first part, draw triangles
color = randint(0, 0xFFFFFFFF)
x0, y0 = randint(0, wthird), randint(0, height)
x1, y1 = randint(0, wthird), randint(0, height)
x2, y2 = randint(0, wthird), randint(0, height)
sdl2.sdlgfx.trigonColor(context.sdlrenderer, x0, y0, x1, y1, x2, y2,
color)
# In the second part, draw aa triangles
color = randint(0, 0xFFFFFFFF)
x0, y0 = randint(0, wthird) + wthird + lw, randint(0, height)
x1, y1 = randint(0, wthird) + wthird + lw, randint(0, height)
x2, y2 = randint(0, wthird) + wthird + lw, randint(0, height)
sdl2.sdlgfx.aatrigonColor(context.sdlrenderer, x0, y0, x1, y1, x2, y2,
color)
# In the third part, draw filled triangles
color = randint(0, 0xFFFFFFFF)
x0, y0 = randint(0, wthird) + 2 * (wthird + lw), randint(0, height)
x1, y1 = randint(0, wthird) + 2 * (wthird + lw), randint(0, height)
x2, y2 = randint(0, wthird) + 2 * (wthird + lw), randint(0, height)
sdl2.sdlgfx.filledTrigonColor(context.sdlrenderer, x0, y0, x1, y1,
x2, y2, color)
# Draws random polygons using the passed rendering context
def draw_polygons(context, width, height):
# Reset the visible area with a black color.
context.clear(0)
# Split the visible area
wthird = width // 3 - 1
lw = 3
sdl2.sdlgfx.thickLineColor(context.sdlrenderer, wthird, 0, wthird, height,
lw, 0xFFFFFFFF)
sdl2.sdlgfx.thickLineColor(context.sdlrenderer, (2 * wthird + lw), 0,
(2 * wthird + lw), height, lw, 0xFFFFFFFF)
for x in range(5):
# In the first part, draw polygons
color = randint(0, 0xFFFFFFFF)
ptcount = randint(3, 10)
xlist, ylist = (sdl2.Sint16 * ptcount)(), (sdl2.Sint16 * ptcount)()
for k in range(ptcount):
xlist[k] = randint(0, wthird)
ylist[k] = randint(0, height)
xptr = ctypes.cast(xlist, ctypes.POINTER(sdl2.Sint16))
yptr = ctypes.cast(ylist, ctypes.POINTER(sdl2.Sint16))
sdl2.sdlgfx.polygonColor(context.sdlrenderer, xptr, yptr, ptcount, color)
# In the second part, draw aa polygons
color = randint(0, 0xFFFFFFFF)
ptcount = randint(3, 10)
xlist, ylist = (sdl2.Sint16 * ptcount)(), (sdl2.Sint16 * ptcount)()
for k in range(ptcount):
xlist[k] = randint(0, wthird) + wthird + lw
ylist[k] = randint(0, height)
xptr = ctypes.cast(xlist, ctypes.POINTER(sdl2.Sint16))
yptr = ctypes.cast(ylist, ctypes.POINTER(sdl2.Sint16))
sdl2.sdlgfx.aapolygonColor(context.sdlrenderer, xptr, yptr, ptcount,
color)
# In the third part, draw filled polygons
color = randint(0, 0xFFFFFFFF)
ptcount = randint(3, 10)
xlist, ylist = (sdl2.Sint16 * ptcount)(), (sdl2.Sint16 * ptcount)()
for k in range(ptcount):
xlist[k] = randint(0, wthird) + 2 * (wthird + lw)
ylist[k] = randint(0, height)
xptr = ctypes.cast(xlist, ctypes.POINTER(sdl2.Sint16))
yptr = ctypes.cast(ylist, ctypes.POINTER(sdl2.Sint16))
sdl2.sdlgfx.filledPolygonColor(context.sdlrenderer, xptr, yptr, ptcount,
color)
# Draw random elements using the passed rendering context
def draw_mixed(context, width, height):
# Reset the visible area with a black color.
context.clear(0)
# Split the visible area
whalf = width // 2 - 2
hhalf = height // 2 - 2
lw = 5
x0, x1 = whalf, whalf
y0, y1 = 0, height
sdl2.sdlgfx.thickLineColor(context.sdlrenderer, x0, y0, x1, y1, lw,
0xFFFFFFFF)
x0, x1 = 0, width
y0, y1 = hhalf, hhalf
sdl2.sdlgfx.thickLineColor(context.sdlrenderer, x0, y0, x1, y1, lw,
0xFFFFFFFF)
for x in range(15):
# In the first quadrant, draw arcs
color = randint(0, 0xFFFFFFFF)
x0, y0 = randint(0, whalf), randint(0, hhalf)
rad = randint(0, min(whalf - x0, hhalf - y0))
start, end = randint(0, 360), randint(0, 360)
sdl2.sdlgfx.arcColor(context.sdlrenderer, x0, y0, rad, start, end, color)
# In the second quadrant, draw bezier curves
color = randint(0, 0xFFFFFFFF)
ptcount = randint(3, 10)
xlist, ylist = (sdl2.Sint16 * ptcount)(), (sdl2.Sint16 * ptcount)()
for k in range(ptcount):
xlist[k] = randint(whalf, width)
ylist[k] = randint(0, hhalf)
steps = randint(2, 10)
xptr = ctypes.cast(xlist, ctypes.POINTER(sdl2.Sint16))
yptr = ctypes.cast(ylist, ctypes.POINTER(sdl2.Sint16))
sdl2.sdlgfx.bezierColor(context.sdlrenderer, xptr, yptr, ptcount, steps,
color)
# In the third quadrant, draw pies
color = randint(0, 0xFFFFFFFF)
x0, y0 = randint(0, whalf), randint(hhalf + lw, height)
rad = randint(0, min(whalf - x0, y0 - (hhalf + lw)))
start, end = randint(0, 360), randint(0, 360)
sdl2.sdlgfx.pieColor(context.sdlrenderer, x0, y0, rad, start, end, color)
# In the fourth quadrant, draw filled pies
color = randint(0, 0xFFFFFFFF)
x0, y0 = randint(whalf + lw, width), randint(hhalf + lw, height)
rad = randint(0, min(x0 - (whalf + lw), y0 - (hhalf + lw)))
start, end = randint(0, 360), randint(0, 360)
sdl2.sdlgfx.filledPieColor(context.sdlrenderer, x0, y0, rad, start, end,
color)
def run():
# You know those from the helloworld.py example.
# Initialize the video subsystem, create a window and make it visible.
sdl2.ext.init()
window = sdl2.ext.Window("sdlgfx drawing examples", size=(800, 600))
window.show()
# Create a rendering context for the window. The sdlgfx module requires it.
# NOTE: Defaults to software rendering to avoid flickering on some systems.
if "-hardware" in sys.argv:
renderflags = sdl2.render.SDL_RENDERER_ACCELERATED | sdl2.render.SDL_RENDERER_PRESENTVSYNC
else:
renderflags = sdl2.render.SDL_RENDERER_SOFTWARE
context = sdl2.ext.Renderer(window, flags=renderflags)
# Retrieve and display renderer + available renderer info
info = sdl2.render.SDL_RendererInfo()
sdl2.SDL_GetRendererInfo(context.sdlrenderer, info)
print("\nUsing renderer: {0}".format(info.name.decode('utf-8')))
print("\nAvailable renderers:")
num_drivers = sdl2.SDL_GetNumRenderDrivers()
for i in range(0, num_drivers):
info = sdl2.render.SDL_RendererInfo()
sdl2.SDL_GetRenderDriverInfo(i, info)
print(" - " + info.name.decode('utf-8'))
# We implement the functionality as it was done in colorpalettes.py and
# utilise a mapping table to look up the function to be executed, together
# with the arguments they should receive
functions = ((draw_lines, (context, 800, 600)),
(draw_circles, (context, 800, 600)),
(draw_ellipsis, (context, 800, 600)),
(draw_rects, (context, 800, 600)),
(draw_trigons, (context, 800, 600)),
(draw_polygons, (context, 800, 600)),
(draw_mixed, (context, 800, 600))
)
# A storage variable for the function we are currently on, so that we know
# which function to execute next.
curindex = 0
draw_lines(context, 800, 600)
context.present()
# The event loop is nearly the same as we used in colorpalettes.py. If you
# do not know, what happens here, take a look at colorpalettes.py for a
# detailed description.
running = True
while running:
events = sdl2.ext.get_events()
for event in events:
if event.type == sdl2.SDL_QUIT:
running = False
break
if event.type == sdl2.SDL_MOUSEBUTTONDOWN:
curindex += 1
if curindex >= len(functions):
curindex = 0
# In contrast to colorpalettes.py, our mapping table consists
# of functions and their arguments. Thus, we get the currently
# requested function and argument tuple and execute the
# function with the arguments.
func, args = functions[curindex]
func(*args)
break
context.present()
sdl2.ext.quit()
return 0
if __name__ == "__main__":
sys.exit(run())
|