-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.cpp
More file actions
159 lines (112 loc) · 4.78 KB
/
Application.cpp
File metadata and controls
159 lines (112 loc) · 4.78 KB
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
//
// Created by Fabien on 24/01/2016.
//
#include "Application.hpp"
Application::Application() : mWindow(sf::VideoMode(1024, 768), "SWRPG"), mLoader("maps")
{
mScript.add(chaiscript::bootstrap::standard_library::map_type<std::map<std::string, std::string> >("ConfigMap"));
mScript.add(chaiscript::var(&mConfig), "application");
mScript.eval_file(CONFIG_FILE);
systems.add<KeyBoardSystem>(mWindow);
systems.add<SceneRenderingSystem>(mWindow,mLoader);
systems.add<MoveSystem>();
systems.add<AnimationSystem>();
systems.add<CollisionSystem>(mWindow,mLoader);
systems.add<ActionSystem>(mLoader);
systems.add<DialogSystem>(mWindow);
systems.configure();
mLoader.Load("test.tmx");
mTileset.loadFromFile("maps/tileset.png");
entityx::Entity player = entities.create();
for (auto &layer : mLoader.GetLayers())
{
if (layer.name == "logic")
{
for(auto& object : layer.objects)
{
if(object.GetName() == "playerStart")
{
player.assign<Drawable>(object.GetPosition());
player.assign<Movable>(sf::Vector2f(0,0));
player.assign<Animable>(constructPlayerAnimations(),constructPlayerStates(),"standdown");
player.assign<Playable>();
}
}
}
}
}
sf::RenderStates Application::constructPlayerStates()
{
sf::RenderStates retour;
retour.texture = &mTileset;
return retour;
}
std::map< std::string, std::unique_ptr<Animation> > Application::constructPlayerAnimations()
{
std::map< std::string, std::unique_ptr<Animation> > retour;
std::vector<sf::FloatRect> standDownRects = {{992,768,32,32}};
std::vector<sf::FloatRect> standLeftRects = {{64,1024,32,32}};
std::vector<sf::FloatRect> standRightRects = {{160,1024,32,32}};
std::vector<sf::FloatRect> standUpRects = {{256,1024,32,32}};
std::vector<sf::FloatRect> runDownRects = {{0,1024,32,32},{32,1024,32,32}};
std::vector<sf::FloatRect> runUpRects = {{288,1024,32,32},{320,1024,32,32}};
std::vector<sf::FloatRect> runLeftRects = {{96,1024,32,32},{128,1024,32,32}};
std::vector<sf::FloatRect> runRightRects = {{192,1024,32,32},{224,1024,32,32}};
std::unique_ptr<Animation> standdown (new Animation(1,standDownRects));
std::unique_ptr<Animation> standup (new Animation(1,standUpRects));
std::unique_ptr<Animation> standleft (new Animation(1,standLeftRects));
std::unique_ptr<Animation> standright (new Animation(1,standRightRects));
std::unique_ptr<Animation> runDown (new Animation(500,runDownRects));
std::unique_ptr<Animation> runUp (new Animation(500,runUpRects));
std::unique_ptr<Animation> runLeft (new Animation(500,runLeftRects));
std::unique_ptr<Animation> runRight (new Animation(500,runRightRects));
retour["standdown"] = std::move(standdown);
retour["standleft"] = std::move(standleft);
retour["standright"] = std::move(standright);
retour["standup"] = std::move(standup);
retour["rundown"] = std::move(runDown);
retour["runup"] = std::move(runUp);
retour["runleft"] = std::move(runLeft);
retour["runright"] = std::move(runRight);
return std::move(retour);
}
void Application::start()
{
sf::Int64 myNextTick = mClock.getElapsedTime().asMicroseconds();
unsigned int loops = 0;
sf::Int64 windowSize = 1000000 / std::stoi(mConfig["MAXFPS"]);
double lDt;
double rDt;
// run the program as long as the window is open
while (mWindow.isOpen())
{
loops = 0;
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (mWindow.pollEvent(event))
{
events.emit<sf::Event>(event);
}
// clear the window with black color
mWindow.clear(sf::Color::Black);
while (mClock.getElapsedTime().asMicroseconds() >= myNextTick && loops < 5)
{
myNextTick += windowSize;
loops++;
sf::Int32 currTime = mClock.getElapsedTime().asMilliseconds();
double diffTime = currTime - lDt;
systems.update<KeyBoardSystem>(diffTime);
systems.update<AnimationSystem>(diffTime);
systems.update<MoveSystem>(diffTime);
systems.update<CollisionSystem>(diffTime);
systems.update<ActionSystem>(diffTime);
lDt = currTime;
}
double interpolation = (double) (mClock.getElapsedTime().asMicroseconds() + windowSize - myNextTick) / windowSize;
systems.update<MoveSystem>(interpolation);
systems.update<SceneRenderingSystem>(interpolation);
systems.update<DialogSystem>(interpolation);
// end the current frame
mWindow.display();
}
}