-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
127 lines (104 loc) · 3.89 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 <SFML/Window/WindowStyle.hpp>
#include <cmath>
#include "load.hpp"
#include "mouse_click_event.hpp"
#include "red_box.hpp"
#include "tester.hpp"
#include <iostream>
static constexpr const char *VERSION = "1.5";
#define FPS 0 // Show fps in console
int main() {
// Play_Test_Games(-1, 14); // Test if game crash.
ResourceHolder<sf::Drawable, sf::Texture> Resource;
map Game = load_map();
uint16_t record = check_for_record(), old_score = Game.get_score();
sf::Font font;
font = load_font("font/NeverMindHand/ttf/NeverMindHand-Regular.ttf");
GLOBAL::INIT(Resource, font, {record, old_score}, 1280,
720); // Load textures, init default
// settings, draw started object etc
RedBox redbox(
Resource,
GLOBAL::RED_BOX_TEXTURE()); // picked redbox (can be textured or not)
sf::RenderWindow window(sf::VideoMode(1280, 720), VERSION, sf::Style::Default,
sf::ContextSettings{0, 0, 8});
auto fps = load_fps();
window.setFramerateLimit(fps);
sf::Image icon;
bool iconLoaded = true;
if (babel::FILE_SYS::folder_exist("ball_texture"))
icon.loadFromFile("ball_texture/orange.png");
else if ((babel::FILE_SYS::folder_exist("../ball_texture")))
icon.loadFromFile("../ball_texture/orange.png");
else
iconLoaded = false;
if (iconLoaded)
window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());
// Operation on map is doing on another thread
std::pair<char, char> picked = {-1, -1}, new_pick = {-1, -1};
std::mutex Mutex;
Thread thread(Game, picked, new_pick, Mutex);
#if FPS == 1
float fps_f;
sf::Clock clock;
sf::Time prev = clock.getElapsedTime();
sf::Time curr;
#endif
sf::Event event; // NOLINT
// GAME LOOP
while (window.isOpen()) {
#if FPS == 1
curr = clock.getElapsedTime();
fps_f = 1.0f / (curr.asSeconds() - prev.asSeconds());
std::cout << "FPS : " << fps_f << '\n';
#endif
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
thread.close_thread();
save_map(Game);
window.close();
} else if (event.type == sf::Event::Resized ||
event.type == sf::Event::GainedFocus) {
GLOBAL::UPDATE_RESOLUTION(Resource,
static_cast<float>(window.getSize().x),
static_cast<float>(window.getSize().y));
thread.operation(OperationType::UPDATE); // Game need update here
sf::FloatRect visibleArea(0, 0, static_cast<float>(window.getSize().x),
static_cast<float>(window.getSize().y));
window.setView(sf::View(visibleArea));
MOUSE::right_click(thread, picked, redbox);
} else if (event.type == sf::Event::MouseButtonPressed) {
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
MOUSE::left_click(thread, window, new_pick, picked, redbox, Game,
Resource);
else if (sf::Mouse::isButtonPressed(
sf::Mouse::Right)) // Right mouse click reset picked ball
MOUSE::right_click(thread, picked, redbox);
}
}
if (Game.get_score() != old_score) // check if score need update
{
old_score = Game.get_score();
auto _str = std::to_string(old_score);
if (old_score > record) // If record were break, then save it
{
record = old_score;
save_record(record);
Resource.get_as<sf::Text>(ResourceType::RECORD).setString(_str);
}
// Refresh score and record points
Resource.get_as<sf::Text>(ResourceType::SCORE).setString(_str);
thread.operation(OperationType::UPDATE); // Game need update here
}
// Game.random_move();
// If game need update draw it
if (Game.need_update())
draw_window(window, Game, Resource);
window.display();
#if FPS == 1
prev = curr;
#endif
}
return 0;
}