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
|
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
#include <cctype>
#include <vector>
#include <sstream>
class character
{
public:
character()
{
//coords
x = 0;
y = 0;
}
int x;
int y;
void keymove();
sf::Sprite pSprite;
sf::Texture ptexture;
};
/*...*/
void LoadMap(const char*filename)
{
std::fstream openfile("map.txt");
sf::Texture tileTexture;
sf::Sprite tiles;
std::vector<std::vector<sf::Vector2i>> map;
std::vector<sf::Vector2i> tempMap;
if (openfile.is_open() && !openfile.eof())
{
std::string str;
std::getline(openfile, str);
tileTexture.loadFromFile(str);
tiles.setTexture(tileTexture);
while (!openfile.eof())
{
std::getline(openfile, str);
std::stringstream stream(str);
std::string value;
while (std::getline(stream, value, ' '))
{
if (value.length() > 0)
{
std::string x = value.substr(0, value.find(','));
std::string y = value.substr(value.find(',') + 1);
tempMap.push_back(sf::Vector2i(stoi(x), stoi(y)));
}
}
map.push_back(tempMap);
tempMap.clear();
}
}
}
int main()
{
std::vector<std::vector<sf::Vector2i>>map;
std::vector<sf::Vector2i> tempMap;
sf::Texture tileTexture;
sf::Sprite tiles;
LoadMap("map.txt");
sf::RenderWindow Window(sf::VideoMode(800, 600), "Window name");
Window.setKeyRepeatEnabled(false);
character midget;
sf::Texture ptexture;
if (!midget.ptexture.loadFromFile("player.png", sf::IntRect(32, 0, 32, 32)))
std::cout << "Error player.png";
ptexture.setSmooth(true);
while (Window.isOpen())
{
sf::Event Event;
while (Window.pollEvent(Event))
{
switch (Event.type)
{
case sf::Event::Closed:
Window.close();
break;
case sf::Event::KeyPressed:
if (Event.key.code == sf::Keyboard::Escape)
{
Window.close();
break;
}
else if(Event.key.code == sf::Keyboard::L)
{
LoadMap("map.txt");
break;
}
}
}
Window.clear();
midget.keymove();
sf::Sprite pSprite;
sf::Texture ptexture;
if (!midget.ptexture.loadFromFile("player.png", sf::IntRect(32, 0, 32, 32)))
std::cout << "Error player.png";
ptexture.setSmooth(true);
pSprite.setTextureRect(sf::IntRect(32, 0, 32, 32));
Window.draw(midget.pSprite);
Window.display();
}
}
|