I think I almost got it, It gives me a tile on the spritesheet but it gives me 2 different halfs of a sprite in one grid. Sometimes it even makes it's own textures... I've been at this for hours, please help, I tried as hard as I could.
#include <SFML/Graphics.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Tile Editor");
sf::Event event;
sf::Mouse mouse;
sf::Clock clock;
sf::Time time;
sf::View scrollScreen;
sf::View zoomScreen;
vector<vector<sf::Sprite> > Grid;
int GridSizeX = 10; //Anything higher than 200x200 tiles is bad
int GridSizeY = 10;
window.setFramerateLimit(60);
scrollScreen.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
sf::Texture blankGridTexture;
sf::Sprite blankGridSprite;
if(!blankGridTexture.loadFromFile("Resources/Tiles/Grid.png"))
{
}
blankGridSprite.setTexture(blankGridTexture);
blankGridSprite.setOrigin(0, 0);
//Load Grass
sf::Texture tileTexture;
sf::Sprite tileSprite;
if(!tileTexture.loadFromFile("Resources/Tiles/Grass.png"))
{
cout << "Couldnt load resources" << endl;
}
tileSprite.setTexture(tileTexture);
tileSprite.setOrigin(16, 16);
//Load flower grass texture
sf::Texture flowerGrassTexture;
sf::Sprite flowerGrassSprite;
if(!flowerGrassTexture.loadFromFile("Resources/Tiles/FlowerGrass.png"))
{
cout << "Couldnt load flower tiles" << endl;
}
//Set mask for editing square
sf::Image mask;
mask.loadFromFile("Resources/Tiles/EditSquare.png");
mask.createMaskFromColor(sf::Color(200, 191, 231));
sf::Texture editSquareTexture;
sf::Sprite editSquareSprite;
editSquareTexture.loadFromImage(mask);
editSquareSprite.setTexture(editSquareTexture);
editSquareSprite.setOrigin(16, 16);
sf::Vector2i pixel_pos = sf::Mouse::getPosition(window);
sf::Vector2f coord_pos = window.mapPixelToCoords(pixel_pos, scrollScreen);
tileTexture.create(floor(coord_pos.x / 32), floor(coord_pos.y / 32));
//Input grid into vector
Grid.resize(GridSizeX);
for (int x = 0; x < GridSizeY; x++)//Column
{
for (int y = 0; y < GridSizeY; y++)//Row
{
blankGridSprite.setPosition(x * 32, y * 32);
Grid[x].push_back(blankGridSprite);
}
}
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));
scrollScreen.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
zoomScreen.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
break;
}
}
window.clear(sf::Color::White);
time = clock.getElapsedTime();
scrollScreen.setViewport(sf::FloatRect(0, 0, 1.0f, 1.0f));
sf::Vector2i pixel_pos = sf::Mouse::getPosition(window);
sf::Vector2f coord_pos = window.mapPixelToCoords(pixel_pos, scrollScreen);
cout << "Row: " << floor(coord_pos.y / 32) << ",";
cout << "Column: " << floor(coord_pos.x / 32) << endl;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
scrollScreen.move(-10, 0);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
scrollScreen.move(10, 0);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
scrollScreen.move(0, -10);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
scrollScreen.move(0, 10);
}
//Draw map
for(int x = 0; x < Grid.size(); x++)
{
for(int y = 0; y < Grid[x].size(); y++)
{
window.draw(Grid[x][y]);
}
}
int indexX = floor(coord_pos.x / 32);
int indexY = floor(coord_pos.y / 32);
//Set tile sheet in image
sf::Image tileSheet;
if(!tileSheet.loadFromFile("Resources/Tile Sheets/Outside_A5.png"))
{
cout << "Error" << endl;
}
//Set it in a sprite so i can see it on screen.
sf::Texture drawTSheet;
drawTSheet.loadFromImage(tileSheet);
sf::Sprite spr;
spr.setTexture(drawTSheet);
spr.setPosition(352, 0);
window.draw(spr);
//Create another image to copy tile from position on tilesheet.
sf::Image copyImg;
//copyImg.create(32, 32);
//Set position of edit square to the mouse
editSquareSprite.setPosition(floor(coord_pos.x / 32) * 32 + 16, floor(coord_pos.y / 32) * 32 + 16);
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
if((indexY < GridSizeY) && (indexY >= 0) && (indexX < GridSizeX) && (indexX >= 0))
{
//Copy tile from tilesheet at mouse position.
copyImg.copy(tileSheet, floor(coord_pos.x / 32), floor(coord_pos.y / 32));
tileTexture.loadFromImage(copyImg);
//Grid[indexX][indexY].setTexture(tileTexture);
window.draw(editSquareSprite);
}
else
{
cout << "Out of bounds" << endl;
}
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Right))
{
if((indexY < GridSizeY) && (indexY >= 0) && (indexX < GridSizeX) && (indexX >= 0))
{
//Set image to texture
//tileTexture.create(floor(coord_pos.x / 32) * 32 + 16, floor(coord_pos.y / 32) * 32 + 16);
//Set texture to grid
Grid[indexX][indexY].setTexture(tileTexture);
window.draw(editSquareSprite);
}
else
{
//cout << "Out of bounds" << endl;
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::F))
{
string mapName;
ofstream saveMap;
cout << "Enter a name for your map" << endl;
getline(cin, mapName);
saveMap.open(mapName.append(".txt"));
for(int x = 0; x < Grid.size(); x++)
{
for(int y = 0; y < Grid[x].size(); y++)
{
}
}
}
//window.draw(tileSheetSprite);
window.setView(scrollScreen);
clock.restart();
window.display();
}
return 0;
}