Ok cool, I was thinking that same thing as well, but wasn't sure whats better performance wise. Also I'm confused on what to do next on the VertexArray Texture; I have this:
list<sf::VertexArray> setTile;
list<sf::VertexArray>::iterator iter;
sf::VertexArray Square(sf::Quads, 4);
Square[0].texCoords = sf::Vector2f(0, 0);
Square[0].texCoords = sf::Vector2f(32, 0);
Square[0].texCoords = sf::Vector2f(32, 32);
Square[0].texCoords = sf::Vector2f(0, 32);
But I don't know what to put after that, I'm following the tutorial in the docs but i'm just confused as to how to apply it to my current program as i'm not using a class and I dont want to right now.
Full code:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <list>
using namespace std;
sf::Texture spriteSheetTexture;
sf::Sprite spriteSheet;
//if(!spriteSheetTexture.loadFromFile(""))
//{
//}
void LoadSpriteSheet()
{
}
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Tile Editor");
sf::Event event;
sf::Mouse mouse;
sf::View scrollScreen;
list<sf::VertexArray> setTile;
list<sf::VertexArray>::iterator iter;
sf::VertexArray Square(sf::Quads, 4);
Square[0].texCoords = sf::Vector2f(0, 0);
Square[0].texCoords = sf::Vector2f(32, 0);
Square[0].texCoords = sf::Vector2f(32, 32);
Square[0].texCoords = sf::Vector2f(0, 32);
scrollScreen.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
scrollScreen.setViewport(sf::FloatRect(0, 0, 1.0f, 1.0f));
while(window.isOpen())
{
while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
{
window.close();
}break;
case sf::Event::Resized:
{
sf::FloatRect viewArea(0, 0, event.size.width, event.size.height);
window.setView(sf::View(viewArea));
}
}
}
window.clear(sf::Color::White);
//Convert Pixels to new screen size
sf::Vector2i pixel_pos = sf::Mouse::getPosition(window);
sf::Vector2f coord_pos = window.mapPixelToCoords(pixel_pos);
sf::Texture blankGridTexture;
sf::Sprite blankGridSprite;
if(!blankGridTexture.loadFromFile("Resources/Tiles/Sign.png"))
{
}
blankGridSprite.setTexture(blankGridTexture);
blankGridSprite.setOrigin(16, 16);
//Load Grass
sf::Texture tileTexture;
sf::Sprite tileSprite;
if(!tileTexture.loadFromFile("Resources/Tiles/Grass.png"))
{
}
tileSprite.setTexture(tileTexture);
tileSprite.setOrigin(16, 16);
sf::Vector2f Grid(30, 30);
Grid.x = 30;
Grid.y = 30;
if(sf::Event::KeyPressed)
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
scrollScreen.move(-1, 0);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
scrollScreen.move(1, 0);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
scrollScreen.move(0, -1);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
scrollScreen.move(0, 1);
}
}
//Draw Grid
for(int i = 0; i < Grid.x; i++)
{
for(int j = 0; j < Grid.y; j++)
{
blankGridSprite.setPosition(j * 32, i * 32); // <- this is causing 1 grass tile and 1 sign tile to appear randomly
window.draw(blankGridSprite);
}
}
//Set tiles on grid - setMapTileSprite
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
//Get Position of the mouse then divide by 32 and round down to integer, then times it by 32 to set grid.
tileSprite.setPosition(rint(coord_pos.x / 32) * 32, rint(coord_pos.y / 32) * 32);
blankGridSprite.setTexture(tileTexture);
}
setTile.push_back(Square);
iter = setTile.begin();
while(iter != setTile.end())
{
window.draw(*iter);
iter++;
}
window.setView(scrollScreen);
window.draw(Square, &tileTexture);
window.display();
}
return 0;
}