SFML community forums

Help => Graphics => Topic started by: muffintastic on September 27, 2013, 07:21:40 pm

Title: Vertices and textures
Post by: muffintastic on September 27, 2013, 07:21:40 pm
Hello,

I'm new to SFML and only have a year of experience with programming in general. I'd like to create a grid of sprites to make a graphical roguelike game. However, this is costly on resources, which led me to wanting to use Vertex and VertexArray. I have one large texture with all of my 40x40 sprite graphics. I would like to create a 20x12 grid of simple objects which store a position on this texture sheet, and then draw the whole grid every cycle. However, I don't understand how I would do this... since the vectors only offer position, color and texture position (What does this even do? What texture is it referring to?) I tried reading this (https://github.com/SFML/SFML/wiki/Source%3A-TileMap) but it confused me. Surely there is a simple way to do what I want to do? I want to be able to call a function like "map[tileX][tileY].set(xOnSpriteSheet,yOnSpriteSheet)".

Thank you in advance for your help, I appreciate it.
Title: Re: Vertices and textures
Post by: zsbzsb on September 27, 2013, 07:33:31 pm
I will try to explain some of these things. When you draw a sprite what you are really drawing is a vertex array of 4 points. Each point is a corner of the sprite that you are drawing. Now, a vertex array is a simple a way to define OpenGL primitives (quads, triangles, trianglefan, ect). When you draw a sprite you are drawing a quad with a texture passed into the render states. Every 4 vertices is a new quad and can be placed anywhere on the map.

Now the position of each vertex is the world coordinates of where that point will be drawn. The color (generally set to white for drawing textures) is the color of that point and can be used to colorize the texture. The texture position determines from where on your texture the vertex will be mapped to (think here texture rect on the sprite).

Quote
Surely there is a simple way to do what I want to do? I want to be able to call a function like "map[tileX][tileY].set(xOnSpriteSheet,yOnSpriteSheet)".

You would need to write a class to manage a vertex array if you want something like this.
Title: Re: Vertices and textures
Post by: muffintastic on September 27, 2013, 07:44:13 pm
Thanks for the fast response! So if I should create a class containing a VertexArray primitive grid and a texture, how do I attach the texture to the primitive grid? Do I create a sprite, set its primitive to the grid I defined, then texture the sprite and draw it?

Edit: I guess my question is: how do I draw a VertexArray with a texture passed into the render states?
Title: Re: Vertices and textures
Post by: Laurent on September 27, 2013, 09:17:36 pm
A better question is: why don't you read the official tutorial, where all your questions are answered with even complete examples? ;)
Title: Re: Vertices and textures
Post by: muffintastic on September 27, 2013, 10:35:52 pm
Oh god I didn't see this (http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php) before. Feel free to delete this topic. Thanks for humoring me guys.