-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
119 lines (102 loc) · 3.64 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
#include <SFML/Graphics.hpp>
#include "cube_graphic.hpp"
#include <iostream>
#include <string>
#include <thread>
// Function used to maintain the view aspect ratio as the window size changes
sf::View getLetterboxView(sf::View view, int windowWidth, int windowHeight);
// Function used to get input from the user and apply the appropriate
// operations on the Rubik's Cube
void input(sf::RenderWindow* window, CubeGraphic* cube, bool* quit);
int main() {
// Create a window
int res_x = 800;
int res_y = 600;
sf::RenderWindow window(sf::VideoMode(res_x, res_y), "Rubik's Cube", (sf::Style::Resize + sf::Style::Close));
window.setVerticalSyncEnabled(true);
window.setFramerateLimit(60);
// Create a view
sf::View view;
view.setSize(res_x, res_y);
view.setCenter(view.getSize().x / 2, view.getSize().y / 2);
// Create the Rubik's Cube visualizer
CubeGraphic cube(view);
// cube.applyAlgorithm(cube.superflipAlgorithm());
window.setActive(false);
// Used to close window from the user input thread
bool quit_from_input = false;
// Handle input in a separate thread
std::thread input_thread(input, &window, &cube, &quit_from_input);
while (window.isOpen()) {
if (quit_from_input) {
window.close();
break;
}
sf::Event event;
// Handle events
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
window.close();
break;
case sf::Event::Resized:
view = getLetterboxView(view, event.size.width, event.size.height);
break;
default:
break;
}
}
// Update the screen
window.clear();
window.setView(view);
window.draw(cube);
window.display();
}
input_thread.detach();
return 0;
}
// https://github.com/SFML/SFML/wiki/Source%3A-Letterbox-effect-using-a-view
sf::View getLetterboxView(sf::View view, int windowWidth, int windowHeight) {
// Compares the aspect ratio of the window to the aspect ratio of the view,
// and sets the view's viewport accordingly in order to archieve a letterbox effect.
// A new view (with a new viewport set) is returned.
float windowRatio = windowWidth / (float) windowHeight;
float viewRatio = view.getSize().x / (float) view.getSize().y;
float sizeX = 1;
float sizeY = 1;
float posX = 0;
float posY = 0;
bool horizontalSpacing = true;
if (windowRatio < viewRatio)
horizontalSpacing = false;
// If horizontalSpacing is true, the black bars will appear on the left and right side.
// Otherwise, the black bars will appear on the top and bottom.
if (horizontalSpacing) {
sizeX = viewRatio / windowRatio;
posX = (1 - sizeX) / 2.f;
}
else {
sizeY = windowRatio / viewRatio;
posY = (1 - sizeY) / 2.f;
}
view.setViewport(sf::FloatRect(posX, posY, sizeX, sizeY));
return view;
}
void input(sf::RenderWindow* window, CubeGraphic* cube, bool* quit) {
std::string algorithm;
while (window->isOpen()) {
std::cout << "Enter a move or algorithm to execute ('Q' to quit, '\\' to reset): ";
std::getline(std::cin, algorithm);
if (std::toupper(algorithm[0]) == 'Q') {
*quit = true;
break;
}
if (algorithm[0] == '\\') {
cube->resetCube();
continue;
}
if(!cube->applyAlgorithm(algorithm)) {
std::cout << "\033[1;31mInvalid move(s).\033[0m" << std::endl;
}
}
}