Unfortunately OpenGL doesn't support concave shape
And by consequence, SFML neither
But you can build them from multiple convex shapes if you really need (be warned, it's pain in the ass). But tiles are convex shapes so for your tile engine using Vertex and VertexArray, you're fine.
In your snipped code, you tried to construct a concave shape which renders badly, so you should try with a rectangle to start. This shape is built from vertices which all have a color, so your shape is filled with a gradient color (interpolation). Then, you applied a texture on top of that which merges the texture and the gradient color and give you at the end a weird shape
. The third argument in Vertex constructor
tex_coords tells OpenGL what part of your texture is used to fill the shape.
To experiment:
1) Start with a rectangle without texture
2) Then, try with white color points and a texture
3) Make your shape convex (other than a rectangle)
3) Modify tex_coords to fill your shape with different part of your texture
#!/usr/bin/env python2
# coding: utf-8
import sfml
class Mapa(sfml.Drawable):
def __init__(self, texture):
sfml.Drawable.__init__(self)
self.vertexarray = sfml.VertexArray(sfml.PrimitiveType.QUADS)
self.texture = texture
def draw(self, target, states):
states.texture = self.texture
target.draw(self.vertexarray, states)
def main():
wn = sfml.RenderWindow(sfml.VideoMode(800, 800), "Prueba de Vertexs")
wn.vertical_synchronization = True
wn.framerate_limit = 60
textura = sfml.Texture.from_file("textura.png")
mapa = Mapa(textura)
# Creamos una serie de vertexs conforme al mapa
v1 = sfml.Vertex(sfml.Vector2(10, 10), sfml.Color.WHITE, sfml.Vector2(0, 0))
v2 = sfml.Vertex(sfml.Vector2(600, 50), sfml.Color.WHITE, sfml.Vector2(86, 0))
v3 = sfml.Vertex(sfml.Vector2(450, 450), sfml.Color.WHITE, sfml.Vector2(86, 86))
v4 = sfml.Vertex(sfml.Vector2(10, 300), sfml.Color.WHITE, sfml.Vector2(0, 86))
v5 = sfml.Vertex(sfml.Vector2(10, 10), sfml.Color.WHITE, sfml.Vector2(0, 0))
# Existen más baldosas pero lo dejare hasta ahí
for vertex in [v1, v2, v3, v4, v5]:
mapa.vertexarray.append(vertex)
while wn.is_open:
for event in wn.events:
if isinstance(event, sfml.CloseEvent):
wn.close()
wn.clear(sfml.Color.WHITE)
wn.draw(mapa)
wn.display()
if __name__ == "__main__":
main()