Si bien sûr!
Tenez:
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
class TileMap: public sf::Drawable, public sf::Transformable
{
public:
bool chargeCarte(const string& str)
{
ifstream file(str.c_str());
if(!file)
return false;
file>>m_TX;
file>>m_TY;
m_carte.resize(m_TY);
for (int i = 0; i < m_TY; ++i)
{
m_carte[i].resize(m_TX);
for (int j = 0; j < m_TX; ++j)
m_carte[i][j].resize(2);
}
for (int i(0);i<m_TY;++i){
for (int j(0);j<m_TX;++j)
{
file>>m_carte[i][j][0];
file>>m_carte[i][j][1];
//cout<<m_carte[i][j][0]<<","<<m_carte[i][j][1]<<endl;
}cout<<endl;}
m_empl=str;
file.close();
return true;
}
bool load(const string& tileset, sf::Vector2u tileSize)
{
if(!m_tileset_fond.loadFromFile(tileset))
return false;
m_vertices_fond.setPrimitiveType(sf::Quads);
m_vertices_fond.resize(m_TX*m_TY*4);
for(unsigned int i=0;i<m_TX;++i)
{
for(unsigned int j=0;j<m_TY;++j)
{
int tu,tv;
assignTexture(m_carte[i][j][0],&tu,&tv);
sf::Vertex* quad = &m_vertices_fond[(i + j * m_TX) * 4];
quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);
quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
}
}
m_vertices_devant.setPrimitiveType(sf::Quads);
m_vertices_devant.resize(m_TX*m_TY*4);
for(unsigned int i=0;i<m_TX;++i)
{
for(unsigned int j=0;j<m_TY;++j)
{
int tu,tv;
assignTexture(m_carte[i][j][1],&tu,&tv);
sf::Vertex* quad = &m_vertices_devant[(i + j * m_TX) * 4];
quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);
quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
}
}
return true;
}
private:
void assignTexture(int tile,int *tu,int *tv)
{
switch(tile)
{
case 0:*tu=1;*tv=15;break;
case 1:*tu=0;*tv=0;break;
case 2:*tu=0;*tv=1;break;
case 3:*tu=0;*tv=2;break;
case 4:*tu=0;*tv=3;break;
case 5:*tu=0;*tv=4;break;
case 6:*tu=1;*tv=0;break;
case 7:*tu=1;*tv=1;break;
case 8:*tu=1;*tv=2;break;
case 9:*tu=1;*tv=3;break;
case 10:*tu=1;*tv=4;break;
case 11:*tu=2;*tv=0;break;
case 12:*tu=2;*tv=1;break;
case 13:*tu=2;*tv=2;break;
case 14:*tu=2;*tv=3;break;
case 15:*tu=2;*tv=4;break;
case 16:*tu=3;*tv=0;break;
case 17:*tu=3;*tv=1;break;
case 18:*tu=3;*tv=2;break;
case 19:*tu=3;*tv=3;break;
case 20:*tu=3;*tv=4;break;
case 21:*tu=4;*tv=0;break;
case 22:*tu=4;*tv=1;break;
case 23:*tu=4;*tv=2;break;
case 24:*tu=4;*tv=3;break;
case 25:*tu=4;*tv=4;break;
case 26:*tu=4;*tv=5;break;
case 27:*tu=0;*tv=21;break;
case 28:*tu=0;*tv=22;break;
case 29:*tu=0;*tv=23;break;
case 30:*tu=1;*tv=21;break;
case 31:*tu=2;*tv=21;break;
}
}
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
states.texture = &m_tileset_fond;
target.draw(m_vertices_fond, states);
}
sf::VertexArray m_vertices_fond;
sf::Texture m_tileset_fond;
sf::VertexArray m_vertices_devant;
sf::Texture m_tileset_devant;
vector< vector< vector<int> > > m_carte;
string m_empl;
int m_TX;
int m_TY;
};
int main()
{
sf::RenderWindow window(sf::VideoMode(800,600),"Test");
TileMap carte;
carte.chargeCarte("carte1.txt");
carte.load("tileset1.png",sf::Vector2u(16,16));
while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
window.close();
break;
}
}
window.clear(sf::Color::Black);
window.draw(carte);
window.display();
}
return 0;
}