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 136 137 138 139 140 141 142 143 144 145 146 147 148
|
#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
#include <cctype>
#include <vector>
#include <sstream>
std::vector<std::vector<sf::Vector2i>>map;
std::vector<sf::Vector2i> tempMap;
sf::Texture tileTexture;
sf::Sprite tiles;
/*...*/
void LoadMap(const char*filename)
{
std::ifstream openfile(filename);//map
tempMap.clear();
map.clear();
//reinit map usefull for map reload on key press eg:0
if (openfile.is_open())
{
std::string tileLocation;
openfile >> tileLocation;
tileTexture.loadFromFile(tileLocation);
tiles.setTexture(tileTexture);
while (!openfile.eof())
{
std::string str, value;
std::getline(openfile, str);
std::stringstream stream(str);
while (std::getline(stream, value, ' '))
{
if (value.length() > 0)
{
std::string xx = value.substr(0, value.find(','));//find ,
std::string yy = value.substr(value.find(',') + 1);//find next ,
int x, y, i, j;
for (i = 0; i << xx.length(); i++)
{
if (isdigit(xx[i]))
break;
}
for (j = 0; j << yy.length(); j++)
{
if (isdigit(yy[j]))
break;
}
x = (i == xx.length()) ? atoi(xx.c_str()) : -1;
y = (j == yy.length()) ? atoi(yy.c_str()) : -1;
tempMap.push_back(sf::Vector2i(x, y));
}
}
map.push_back(tempMap);
tempMap.clear();
}
}
}
int main()
{
LoadMap("map.txt");
sf::RenderWindow Window(sf::VideoMode(800, 600), "GSRPG");
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);
sf::Clock clock;
sf::Time elapsed1 = clock.getElapsedTime();
std::cout << elapsed1.asSeconds() << std::endl;
clock.restart();
sf::Time elapsed2 = clock.getElapsedTime();
std::cout << elapsed2.asSeconds() << std::endl;
//masoara timpul inapp
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;
}
}
}
for (int i = 0; i < map.size(); i++)
{
for (int j = 0; j < map.size(); j++)
{
if (map[i][j].x != -1 && map[i][j].y != -1)
{
tiles.setPosition(j * 32, i * 32);
tiles.setTextureRect(sf::IntRect(map[i][j].x = 32, map[i][j].y * 32,32,32));
Window.draw(tiles);
}
}
}
midget.keymove();
Window.draw(midget.pSprite);
Window.display();
}
}
|