| 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 pygame_sdl2.surface cimport Surface |
| 21 |
|
| 22 |
import pygame_sdl2.gfxdraw as gfxdraw |
| 23 |
from pygame_sdl2.rect import Rect |
| 24 |
from pygame_sdl2.error import error |
| 25 |
|
| 26 |
def rect(Surface surface, color, rect, width=0): |
| 27 |
if not isinstance(rect, Rect): |
| 28 |
rect = Rect(rect) |
| 29 |
|
| 30 |
if width == 0: |
| 31 |
gfxdraw.box(surface, rect, color) |
| 32 |
else: |
| 33 |
gfxdraw.rectangle(surface, rect, color) |
| 34 |
n = 1 |
| 35 |
while n < width: |
| 36 |
r = Rect(rect.x - n, rect.y - n, rect.w + (n*2), rect.h + (n*2)) |
| 37 |
gfxdraw.rectangle(surface, r, color) |
| 38 |
r = Rect(rect.x + n, rect.y + n, rect.w - (n*2), rect.h - (n*2)) |
| 39 |
gfxdraw.rectangle(surface, r, color) |
| 40 |
n += 1 |
| 41 |
dirty = Rect(rect.x - width, rect.y - width, rect.w + (width*2), rect.h + (width*2)) |
| 42 |
return dirty.clip(surface.get_rect()) |
| 43 |
|
| 44 |
def polygon(Surface surface, color, pointlist, width=0): |
| 45 |
if width == 0: |
| 46 |
gfxdraw.filled_polygon(surface, pointlist, color) |
| 47 |
dirty = Rect(pointlist[0], (1, 1)) |
| 48 |
n = 1 |
| 49 |
while n < len(pointlist): |
| 50 |
dirty.union_ip(Rect(pointlist[n], (1,1))) |
| 51 |
n += 1 |
| 52 |
return dirty.clip(surface.get_rect()) |
| 53 |
else: |
| 54 |
return lines(surface, color, True, pointlist, width) |
| 55 |
|
| 56 |
def circle(Surface surface, color, pos, radius, width=0): |
| 57 |
x, y = pos |
| 58 |
if width == 0: |
| 59 |
gfxdraw.filled_circle(surface, x, y, radius, color) |
| 60 |
dirty = Rect((x - radius, y - radius), (radius*2, radius*2)) |
| 61 |
return dirty.clip(surface.get_rect()) |
| 62 |
else: |
| 63 |
gfxdraw.circle(surface, x, y, radius, color) |
| 64 |
n = 1 |
| 65 |
while n < width: |
| 66 |
gfxdraw.circle(surface, x, y, radius - n, color) |
| 67 |
gfxdraw.circle(surface, x + 1, y, radius - n, color) |
| 68 |
gfxdraw.circle(surface, x - 1, y, radius - n, color) |
| 69 |
n += 1 |
| 70 |
dirty = Rect(x - radius - width, y - radius - width, (radius*2) + width, (radius*2) + width) |
| 71 |
return dirty.clip(surface.get_rect()) |
| 72 |
|
| 73 |
def ellipse(Surface surface, color, rect, width=0): |
| 74 |
x, y, rx, ry = rect |
| 75 |
if width == 0: |
| 76 |
gfxdraw.filled_ellipse(surface, x, y, rx, ry, color) |
| 77 |
dirty = Rect((x - rx, y - ry), (rx*2, ry*2)) |
| 78 |
else: |
| 79 |
gfxdraw.ellipse(surface, x, y, rx, ry, color) |
| 80 |
n = 1 |
| 81 |
while n < width: |
| 82 |
gfxdraw.ellipse(surface, x, y, rx - n, ry - n, color) |
| 83 |
gfxdraw.ellipse(surface, x + 1, y, rx - n, ry - n, color) |
| 84 |
gfxdraw.ellipse(surface, x - 1, y, rx - n, ry - n, color) |
| 85 |
n += 1 |
| 86 |
dirty = Rect(x - rx - width, y - ry - width, (rx*2) + width, (ry*2) + width) |
| 87 |
return dirty.clip(surface.get_rect()) |
| 88 |
|
| 89 |
def arc(Surface surface, color, rect, start_angle, stop_angle, width=1): |
| 90 |
raise error("Not implemented.") |
| 91 |
|
| 92 |
def line(Surface surface, color, start_pos, end_pos, width=1): |
| 93 |
gfxdraw.thick_line(surface, start_pos[0], start_pos[1], |
| 94 |
end_pos[0], end_pos[1], width, color) |
| 95 |
dirty = Rect(start_pos, (width, width)) |
| 96 |
dirty.union_ip(Rect(end_pos, (width, width))) |
| 97 |
return dirty.clip(surface.get_rect()) |
| 98 |
|
| 99 |
def lines(Surface surface, color, closed, pointlist, width=1): |
| 100 |
n = 0 |
| 101 |
dirty = Rect(pointlist[0], (width, width)) |
| 102 |
while n < len(pointlist) - 1: |
| 103 |
line(surface, color, pointlist[n], pointlist[n+1], width) |
| 104 |
dirty.union_ip(Rect(pointlist[n+1], (width, width))) |
| 105 |
n += 1 |
| 106 |
if closed: |
| 107 |
line(surface, color, pointlist[n], pointlist[0], width) |
| 108 |
return dirty.clip(surface.get_rect()) |
| 109 |
|
| 110 |
def aaline(Surface surface, color, startpos, endpos, blend=1): |
| 111 |
x1, y1 = startpos |
| 112 |
x2, y2 = endpos |
| 113 |
gfxdraw.aaline(surface, x1, y1, x2, y2, color) |
| 114 |
dirty = Rect(x1, y1, x2 - x1, y2 - y1) |
| 115 |
return dirty.clip(surface.get_rect()) |
| 116 |
|
| 117 |
def aalines(Surface surface, color, closed, pointlist, blend=1): |
| 118 |
n = 0 |
| 119 |
dirty = Rect(pointlist[0], (1,1)) |
| 120 |
while n < len(pointlist) - 1: |
| 121 |
r = aaline(surface, color, pointlist[n], pointlist[n+1]) |
| 122 |
dirty.union_ip(r) |
| 123 |
n += 1 |
| 124 |
if closed: |
| 125 |
aaline(surface, color, pointlist[n], pointlist[0]) |
| 126 |
return dirty.clip(surface.get_rect()) |