-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTut35.cpp
174 lines (147 loc) · 4.58 KB
/
Tut35.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// C++ Sfml Made Easy Tutorial 35 - Gravity
// 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");
sf::Shape rect = sf::Shape::Rectangle(0, 0, 10, 10, sf::Color::Red);
LoadMap("Map1.txt");
sf::Vector2f playerPosition(100, 100);
sf::Vector2f playerVelocity(0, 0);
const float gravity = 0.1;
int groundHeight = (MapVector.size() - 2) * BLOCKSIZE;
float jumpSpeed = 10, moveSpeed = 5;
bool jump = false;
rect.SetPosition(playerPosition);
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(Window.GetInput().IsKeyDown(sf::Key::Right))
playerVelocity.x = moveSpeed;
else if(Window.GetInput().IsKeyDown(sf::Key::Left))
playerVelocity.x = -moveSpeed;
else
playerVelocity.x = 0;
if(Window.GetInput().IsKeyDown(sf::Key::Up) && jump)
{
playerVelocity.y = -jumpSpeed;
jump = false;
}
if(!jump)
playerVelocity.y += gravity;
else
playerVelocity.y = 0;
playerPosition += playerVelocity;
jump = playerPosition.y + 10 >= groundHeight;
if(jump)
playerPosition.y = groundHeight - 10;
rect.SetPosition(playerPosition);
Window.Clear();
DrawMap(Window);
Window.Draw(rect);
Window.Display();
}
return 0;
}