virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// apply the tileset texture
states.texture = &tileset;
// draw the vertex array
target.draw(m_vertices, states);
}
void populateVertexArray() // Fill the VertexArray
{
// resize the vertex array to fit the level size
m_vertices.setPrimitiveType(sf::Quads);
m_vertices.resize(mapwidth * mapheight * 4);
// populate the vertex array, with one quad per tile
for (unsigned int i = 0; i < mapwidth; ++i)//width of the map in number of tiles (10 for me)
{
for (unsigned int j = 0; j < mapheight; ++j)
{
// get the current tile number
int tileNumber = tiles[i + j * mapwidth]-1; //-1 because the editor im using starts with 1, not 0
// find its position in the tileset texture
int tu = tileNumber % (tileset.getSize().x / tilewidth); // Width of a tile in pixel (32 for me)
int tv = tileNumber / (tileset.getSize().x / tilewidth);
// get a pointer to the current tile's quad
sf::Vertex* quad = &m_vertices[(i + j * mapwidth) * 4];
// define its 4 corners
quad[0].position = sf::Vector2f(i * tilewidth, j * tileheight);
quad[1].position = sf::Vector2f((i + 1) * tilewidth, j * tileheight);
quad[2].position = sf::Vector2f((i + 1) * tilewidth, (j + 1) * tileheight);
quad[3].position = sf::Vector2f(i * tilewidth, (j + 1) * tileheight);
// define its 4 texture coordinates
quad[0].texCoords = sf::Vector2f(tu * tilewidth, tv * tileheight);
quad[1].texCoords = sf::Vector2f((tu + 1) * tilewidth, tv * tileheight);
quad[2].texCoords = sf::Vector2f((tu + 1) * tilewidth, (tv + 1) * tileheight);
quad[3].texCoords = sf::Vector2f(tu * tilewidth, (tv + 1) * tileheight);
}
}
}
};
int main()
{
sf::RenderWindow window(sf::VideoMode(512, 256),"RPG");
TileMap t("testmap.tmx");
t.populateVertexArray();
window.setFramerateLimit(20);
while(window.isOpen())
{
sf::Event evt;
//Loop through all window events
while(window.pollEvent(evt))
{
if(evt.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(t);
window.display();
}
return 0;
}
Reduced the code to the important parts. Assume all variables to have the right values (checked every single one).
Problem description:
Vertex Array is drawn but the image apparently is not attached to it which resulsts in a white blank square instead of the tilemap being drawn. If i only draw the tileset via window.draw(sf::Sprite(mytileset_tex)) it works.
hi, here is a compressed example:
//#include "tilemap.h"
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(512, 256),"RPG");
//TileMap t("testmap.tmx");
window.setFramerateLimit(20);
sf::VertexArray m_vertices;
sf::Texture m_tileset;
m_tileset.loadFromFile("Tileset.png");
m_vertices.setPrimitiveType(sf::Quads);
m_vertices.resize(4);
m_vertices[0].position = sf::Vector2f(0, 0);
m_vertices[1].position = sf::Vector2f(32,0);
m_vertices[2].position = sf::Vector2f(32,32);
m_vertices[3].position = sf::Vector2f(0,32);
m_vertices[0].texCoords = sf::Vector2f(0,0);
m_vertices[1].texCoords = sf::Vector2f(32,0);
m_vertices[2].texCoords = sf::Vector2f(32,32);
m_vertices[3].texCoords = sf::Vector2f(0,32);
sf::RenderStates states;
states.texture = &m_tileset;
while(window.isOpen())
{
sf::Event evt;
//Loop through all window events
while(window.pollEvent(evt))
{
if(evt.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(m_vertices,states);
window.display();
}
return 0;
}
The file is in the attachment.
Why sfml draws is this:
(http://up.picr.de/18450105wt.jpg)
Im using one of the more recent 2.1 SFML versions.
I tried your code on my PC and everything works fine. Try checking if Tileset is correctly load
if( !m_tileset.loadFromFile("Tileset.png") )
{
return -1; //error
}