1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
#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;
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::VertexArray Square(sf::Quads, 4);
sf::RenderStates states;
Square[0].position = sf::Vector2f(0, 0);
Square[1].position = sf::Vector2f(32, 0);
Square[2].position = sf::Vector2f(32, 32);
Square[3].position = sf::Vector2f(0, 32);
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);
sf::Texture blankGridTexture;
sf::Sprite blankGridSprite;
if(!blankGridTexture.loadFromFile("Sign.png"))
{
}
blankGridSprite.setTexture(blankGridTexture);
blankGridSprite.setOrigin(0, 0);
//Load Grass
sf::Texture tileTexture;
sf::Sprite tileSprite;
if(!tileTexture.loadFromFile("Outside_A5.png"))
{
}
tileSprite.setTexture(tileTexture);
tileSprite.setOrigin(16, 16);
states.texture = &tileTexture;
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.
states.transform.translate(sf::Vector2f(rint(coord_pos.x / 32) * 32, rint(coord_pos.y / 32) * 32));
}
window.setView(scrollScreen);
window.draw(Square, states);
window.display();
}
return 0;
}
|