-
Notifications
You must be signed in to change notification settings - Fork 0
/
fatpup-ui.cpp
112 lines (92 loc) · 3.49 KB
/
fatpup-ui.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
#include <iostream>
#include "SFML/Graphics.hpp"
#include "board.h"
#include "movepanel.h"
sf::View GetLetterboxView(sf::View view, int windowWidth, int windowHeight)
{
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;
}
int main()
{
fatpup::Engine* engine = fatpup::Engine::Create("minimax");
if (!engine)
{
std::cerr << "Can't create minimax engine, terminating...\n";
return -1;
}
const int windowHeight = (std::min(sf::VideoMode::getDesktopMode().width, sf::VideoMode::getDesktopMode().height) * 3 / 4) & 0xfff0;
const int windowWidth = windowHeight * 5 / 4;
const int targetWindowHeight = 1200;
const int targetWindowWidth = targetWindowHeight * 5 / 4;
// create the window
sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), "Fatpup Chess");
window.setFramerateLimit(10);
// SFML docs don't recommend setting setFramerateLimit and setVerticalSyncEnabled(true) simultaneously
//window.setVerticalSyncEnabled(true);
sf::View view;
view.setSize(targetWindowWidth, targetWindowHeight);
view.setCenter(view.getSize().x / 2, view.getSize().y / 2);
view = GetLetterboxView(view, windowWidth, windowHeight);
Board board(sf::Vector2u(targetWindowWidth, targetWindowHeight), true);
fatpup::Position initialPos;
initialPos.setInitial();
board.SetPosition(initialPos);
engine->SetPosition(initialPos);
board.SetEngine(engine);
MovePanel movePanel(board.GetSize().x, board.GetSize().x / 4, board.GetSize().y);
board.SetMovePanel(&movePanel);
// run the program as long as the window is open
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
else if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::Escape)
window.close();
else if (event.key.code == sf::Keyboard::R)
board.SetPosition(initialPos);
}
else if (event.type == sf::Event::Resized)
view = GetLetterboxView(view, event.size.width, event.size.height);
else if (event.type == sf::Event::MouseButtonPressed)
board.OnClick(window.mapPixelToCoords(sf::Vector2i(event.mouseButton.x, event.mouseButton.y)));
}
// clear the window with black color
window.clear(sf::Color::Black);
// draw everything here...
window.setView(view);
board.Draw(window);
movePanel.Draw(window);
// end the current frame
window.display();
}
delete engine;
return 0;
}