I just decided to drop sprites and applied little bit of opengl.
# -*- coding: utf-8 -*-
from PySFML import sf
from OpenGL.GL import (
glBegin,
glDisable,
glEnd,
glTexCoord2f,
glTranslatef,
glVertex2f,
GL_TEXTURE_2D,
GL_QUADS,
)
class Patch(sf.Drawable):
def __init__(self, image):
sf.Drawable.__init__(self)
self.SetImage(image)
def SetImage(self, image):
self.image = image
width = image.GetWidth()
height = image.GetHeight()
for x in range(1, width):
if image.GetPixel(x, 0).a > 0:
right = x + 1
if image.GetPixel(width - x, 0).a > 0:
left = width - x
for y in range(1, height):
if image.GetPixel(0, y).a > 0:
bottom = y + 1
if image.GetPixel(0, height - y).a > 0:
top = height - y
row = 1, left, right, width
col = 1, top, bottom, height
self.borders = top-1, height-bottom, left-1, width-right
self.subrects = [
sf.IntRect(
row[i%3],
col[i/3],
row[i%3+1],
col[i/3+1],
)
for i in range(9)
]
self.width = width
self.height = height
def Resize(self, width, height):
self.width = width
self.height = height
def Render(self, target):
image = self.image
width = self.width
height = self.height
top, bottom, left, right = self.borders
row = [0, left, self.width-right, self.width]
col = [0, top, self.height-bottom, self.height]
if image and image.GetWidth() and image.GetHeight():
image.Bind()
for i, subrect in enumerate(self.subrects):
x = i%3
y = i/3
rect = image.GetTexCoords(subrect)
glBegin(GL_QUADS)
glTexCoord2f(rect.Left, rect.Top); glVertex2f(row[x], col[y])
glTexCoord2f(rect.Left, rect.Bottom); glVertex2f(row[x], col[y+1])
glTexCoord2f(rect.Right, rect.Bottom); glVertex2f(row[x+1], col[y+1])
glTexCoord2f(rect.Right, rect.Top); glVertex2f(row[x+1], col[y])
glEnd()
else:
glBegin(GL_QUADS)
glVertex2f(0, 0)
glVertex2f(0, height)
glVertex2f(width, height)
glVertex2f(width, 0)
glEnd()