Voici donc un code minimal :
#include "Scene.h"
#include "Tile.h"
#define SIZE 10
Scene::Scene()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "[V0.1] ISO CityDev", sf::Style::Default);
window.setFramerateLimit(60);
Tile tiles(11, 11);
tiles.setTexture(1, 2);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
{
window.close();
}
}
window.clear();
window.draw(tiles);
window.display();
}
}
#include "Tile.h"
#define TILESIZEX 80
#define TILESIZEY 40
Tile::Tile(int width, int height)
{
if (!texturex.loadFromFile("Resources/TileSetISO.png"))
std::cout << "Error Loading" << std::endl;
m_vertices.setPrimitiveType(sf::Quads);
m_vertices.resize(width * height * 4);
for (int i = 0; i < width; ++i)
{
for (int j = 0; j < height; ++j)
{
tile = &m_vertices[(i + j * width) * 4];
tile[0].position = sf::Vector2f(40 + (TILESIZEX*i), (TILESIZEY * j));
tile[1].position = sf::Vector2f(80 + (TILESIZEX*i), 20 +(TILESIZEY * j));
tile[2].position = sf::Vector2f(40 + (TILESIZEX*i), 40 +(TILESIZEY * j));
tile[3].position = sf::Vector2f((TILESIZEX*i), 20 +(TILESIZEY * j));
tile[0].texCoords = sf::Vector2f(40, 0);
tile[1].texCoords = sf::Vector2f(80, 20);
tile[2].texCoords = sf::Vector2f(40, 40);
tile[3].texCoords = sf::Vector2f(0, 20);
}
}
}
void Tile::setTexture(int IDTile, int ID)
{
int line = ID % 10;
int column = ID / 10;
///Permet de voir à quoi l'ID correspond sur le TILESET
//std::cout << "line : " << line << " | column : " << column << std::endl;
tile[4 * IDTile].texCoords = sf::Vector2f(40 + (80 * line), 0);
tile[(4 * IDTile) + 1].texCoords = sf::Vector2f(80 + (80 * line), 20);
tile[(4 * IDTile) + 2].texCoords = sf::Vector2f(40 + (80 * line), 40);
tile[(4 * IDTile) + 3].texCoords = sf::Vector2f((80 * line), 20);
}
#ifndef TILE_H
#define TILE_H
#include <iostream>
#include <SFML/Graphics.hpp>
class Tile : public sf::Drawable, public sf::Transformable
{
public:
Tile(int width, int height);
void setTexture(int IDTile, int ID);
protected:
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
states.texture = &texturex;
target.draw(m_vertices, states);
}
sf::Vertex* tile;
sf::VertexArray m_vertices;
sf::Texture texturex;
};
#endif // TILE_H
Quand je fais appel à mon .setTexture j'ai une erreur de std::bad_alloc "terminate called after throwing an instance of "std::bad_alloc" what():