Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Vertices and textures  (Read 1651 times)

0 Members and 1 Guest are viewing this topic.

muffintastic

  • Newbie
  • *
  • Posts: 3
    • View Profile
Vertices and textures
« 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 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.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Vertices and textures
« Reply #1 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.
« Last Edit: September 27, 2013, 07:36:30 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

muffintastic

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Vertices and textures
« Reply #2 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?
« Last Edit: September 27, 2013, 08:44:12 pm by muffintastic »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Vertices and textures
« Reply #3 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? ;)
Laurent Gomila - SFML developer

muffintastic

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Vertices and textures
« Reply #4 on: September 27, 2013, 10:35:52 pm »
Oh god I didn't see this before. Feel free to delete this topic. Thanks for humoring me guys.