This repository has been archived by the owner on Aug 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
127 lines (114 loc) · 2.78 KB
/
main.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
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <algorithm>
#include "main.h"
#include "animation.h"
#include "globals.h"
#include "actors.h"
#include "buildings.h"
#include "objects.h"
#include <future>
#include <mutex>
#include "gametext.h"
gameState currentGame;
void updateActorHelper(int i)
{
if(listOfActors[i].isInitialized())
{
listOfActors[i].update();
}
}
void updateObjectHelper(int i)
{
//listOfObjects[i].update();
}
void updateBuildingsHelper(int i)
{
listOfBuildings[i].update();
}
void routeHelper(int i)
{
if(listOfActors[i].isInitialized())
{
listOfActors[i].calculateRoute();
}
}
void updateActorsWorker()
{
//while(window.isOpen())
//{
if(!listOfActors.empty())
{
for(int i = 0; i < listOfActors.size(); i++)
{
std::async(std::launch::async, updateActorHelper, i);
}
}
//}
}
void updateObjectsWorker()
{
// while(window.isOpen())
// {
if(!listOfObjects.empty())
{
for(int i = 0; i < listOfObjects.size(); i++)
{
std::async(std::launch::async, updateObjectHelper, i);
}
}
//}
}
void updateBuildingsWorker()
{
//while(window.isOpen())
//{
if(!listOfBuildings.empty())
{
for(int i = 0; i < listOfBuildings.size(); i++)
{
std::async(std::launch::async, updateBuildingsHelper, i);
}
}
//}
}
void calculateRoutesWorker()
{
while(window.isOpen())
{
if(!listOfActorsWhoNeedAPath.empty())
{
for (std::list<int>::const_iterator iterator = listOfActorsWhoNeedAPath.begin(), end = listOfActorsWhoNeedAPath.end(); iterator != end; ++iterator)
{
std::async(std::launch::async, routeHelper,*iterator);
}
}
}
}
int main()
{
sf::Clock clockMain;
currentGame.loadGame();
//std::thread updateActorsThread(updateActorsWorker);
//std::thread updateObjectsThread(updateObjectsWorker);
//std::thread updateBuildingsThread(updateBuildingsWorker);
std::thread updateRoutesThread(calculateRoutesWorker);
while(window.isOpen())
{
sf::Time elapsedMain = clockMain.getElapsedTime();
currentGame.elapsedTime = elapsedMain.asSeconds();
gameText.throwOutOldMessages();
updateActorsWorker();
//calculateRoutesWorker();
updateBuildingsWorker();
//updateObjectsWorker();
currentGame.interact();
currentGame.drawGame();
}
//updateActorsThread.join();
//updateObjectsThread.join();
//updateBuildingsThread.join();
updateRoutesThread.join();
return 0;
}