-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTut33.cpp
140 lines (123 loc) · 3.67 KB
/
Tut33.cpp
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
// C++ Sfml Made Easy Tutorial 29 - Loading Maps in Real Time
// CodingMadeEasy
#include<SFML/Graphics.hpp>
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<sstream>
#define ScreenWidth 800
#define ScreenHeight 600
#define BLOCKSIZE 40
int loadCounterX, loadCounterY;
int mapSizeX, mapSizeY;
std::vector < std::vector <int> > MapVector;
std::vector <sf::Image> textureVector;
enum Identifier { MAP, TEXTURE };
int loadType = -1;
void LoadMap(const char *filename)
{
sf::Image image;
sf::Sprite sprite;
std::ifstream openfile(filename);
std::vector <int> tempVector;
std::string line;
if(openfile.is_open())
{
while(!openfile.eof())
{
std::getline(openfile, line);
std::cout << line << std::endl;
if(line.find("Map") != std::string::npos)
{
loadType = MAP;
continue;
}
else if (line.find("Texture") != std::string::npos)
{
loadType = TEXTURE;
continue;
}
std::string tempLine = line.erase(line.find_last_not_of(" ") + 1);
int space = tempLine.find_first_of(" ");
if(space == 0)
tempLine = tempLine.erase(0, space + 1);
std::stringstream str(tempLine);
switch(loadType)
{
case MAP:
while(!str.eof())
{
std::string value;
getline(str, value, ' ');
if(value.length() > 0)
tempVector.push_back(atoi(value.c_str()));
}
MapVector.push_back(tempVector);
tempVector.clear();
break;
case TEXTURE:
if(tempLine.length() > 0)
{
if(image.LoadFromFile(tempLine))
{
textureVector.push_back(image);
}
else
{
std::cout << "Could not load" << line << std::endl;
}
break;
}
}
}
}
}
void DrawMap(sf::RenderWindow &Window)
{
sf::Shape rect = sf::Shape::Rectangle(0, 0, BLOCKSIZE, BLOCKSIZE, sf::Color(255, 255, 255, 255));
sf::Color rectCol;
sf::Sprite sprite;
for(int i = 0; i < MapVector.size(); i++)
{
for(int j = 0; j < MapVector[i].size(); j++)
{
if(MapVector[i][j] < 0)
{
rectCol = sf::Color(44, 117, 255);
rect.SetPosition(j * BLOCKSIZE, i * BLOCKSIZE);
rect.SetColor(rectCol);
Window.Draw(rect);
}
else
{
sprite.SetImage(textureVector[MapVector[i][j]]);
sprite.SetPosition(j * BLOCKSIZE, i * BLOCKSIZE);
Window.Draw(sprite);
}
}
}
}
int main()
{
sf::RenderWindow Window(sf::VideoMode(ScreenWidth, ScreenHeight, 32), "SFML Made Easy");
LoadMap("Map1.txt");
while(Window.IsOpened())
{
sf::Event Event;
while(Window.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed || Event.Key.Code == sf::Key::Escape)
Window.Close();
if(Event.Key.Code == sf::Key::L)
{
MapVector.clear();
LoadMap("Map1.txt");
}
}
Window.Clear();
DrawMap(Window);
Window.Display();
}
return 0;
}