Yeah that all makes sense, the images really help a ton. I seem to be having trouble with my code again -_-, I decided that I want to load a sprite sheet in, instead of individual tiles, because i'm going to have to do that anyways so why not do it now. I need to get the width and height of it and for some reason I cant seem to do it. Also The sprite sheet is on top of everything else, in the sense that when i go over it with my mouse it goes underneath it. I need to check and see if the mouse is IN the tile sheets boundaries, and I just cant seem to get it.
#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;
vector<vector<int> > saveGrid;
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);
//Load TileSheet
sf::Texture tileSheetTexture;
sf::Sprite tileSheetSprite;
if(!tileSheetTexture.loadFromFile("Resources/Tile Sheets/Outside_A5.png"))
{
}
tileSheetSprite.setTexture(tileSheetTexture);
//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 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))
{
Grid[indexX][indexY].setTexture(tileTexture);
window.draw(editSquareSprite);
}
else
{
cout << "Out of bounds" << endl;
}
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
if((indexY > tileSheetSprite.getPosition().y) && (indexY > tileSheetSprite.getPosition().y) &&
(indexX > tileSheetSprite.getPosition().x) && (indexX > tileSheetSprite.getPosition().x))
{
cout << "On new tile sheet" << endl;
window.draw(editSquareSprite);
}
else
{
cout << "Out of bounds sprite sheet" << endl;
}
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Right))
{
if ((indexY < GridSizeY) && (indexY >= 0) && (indexX < GridSizeX) && (indexX >= 0))
{
Grid[indexX][indexY].setTexture(blankGridTexture);
window.draw(editSquareSprite);
}
else
{
cout << "Out of bounds" << endl;
}
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Middle))
{
if((indexY < GridSizeY) && (indexY >= 0) && (indexX < GridSizeX) && (indexX >= 0))
{
Grid[indexX][indexY].setTexture(flowerGrassTexture);
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"));
}
tileSheetSprite.setPosition(350, 0);
window.draw(tileSheetSprite);
window.setView(scrollScreen);
clock.restart();
window.display();
}
return 0;
}