forked from pr061012/pr061012
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
103 lines (86 loc) · 2.7 KB
/
Copy pathmain.cpp
File metadata and controls
103 lines (86 loc) · 2.7 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
// Needed for std::this_thread::sleep_for.
#define _GLIBCXX_USE_NANOSLEEP
#include <iostream>
#include <chrono>
#include <thread>
#include <random>
#include "common/BasicDefines.h"
#include "common/Log/Log.h"
#include "common/Exceptions/EInvalidResPath.h"
#include "control/Controller/Controller.h"
#include "model/World.h"
#include "view/View.h"
#include "cli/CLI.h"
// Period (in milliseconds).
#define PERIOD (uint) 1000 / TM_TICKS_PER_SECOND
int main()
{
// Initializing random.
srand(time(0));
// Running game.
try
{
// Creating World, View and Controller.
World world(rand(), SZ_WORLD_HSIDE, false);
Controller control(&world);
View view(world);
CLI cli(&world, &control);
// Pausing game.
view.setPaused(true);
// Starting time point.
std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
while (true)
{
// Au revoir.
if (!view.continues())
{
break;
}
// Current time point and difference between current and previous
// points.
std::chrono::steady_clock::time_point t2 = std::chrono::steady_clock::now();
std::chrono::duration<double> time_span = std::chrono::duration_cast< std::chrono::duration<double> >(t2 - t1);
// Check whether we need to update world and picture.
if (time_span.count() * 1000 > PERIOD)
{
t1 = t2;
// Running CLI.
std::string cmd = view.getUserInput();
if (cmd != "")
{
if (cmd == "exit")
{
break;
}
std::cout << cli.runCommand(cmd);
}
// Running controller step (if needed).
if (!view.isPaused())
{
control.step();
}
// Redrawing picture.
view.redraw();
// Check if user wants to reset world.
/*
if (view.isReset())
{
view.setReset(false);
world.reset();
}
*/
// Sleeping.
std::this_thread::sleep_for(std::chrono::milliseconds(PERIOD));
}
}
}
// Catching exceptions.
catch (std::exception& exc)
{
Log::ERROR(exc.what());
std::cout << "An error occurred during game execution. Look for LOG " <<
"file for details." << std::endl;
return 1;
}
return 0;
}