-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinesweeper.hpp
89 lines (77 loc) · 2.82 KB
/
Minesweeper.hpp
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
#pragma once
#include "Map.hpp"
#include "MapView.hpp"
#include "MapController.hpp"
#include <iostream>
#include "IniFile.hpp"
class Minesweeper
{
sf::RenderWindow * window;
Map * map;
MapView mapView;
MapController mapController;
sf::Mouse mouse;
int downNotificationBarHeight = 10;
public:
Minesweeper()
{
IniFile config("conf.ini");
map = new Map(config.getInt("sizeY"), config.getInt("sizeX"), config.getInt("mines"));
mapView.handleMap(map);
mapView.setMarginBetweenSquares(config.getInt("SquareMargin"));
mapView.setSquareSize(config.getInt("SquareSize"));
mapView.setMainColor(sf::Color::Blue);
mapView.reload();
downNotificationBarHeight = mapView.getFontSize() + 5;
map->handleView(&mapView);
mapController.handleMap(map);
mapController.handleView(&mapView);
window = new sf::RenderWindow(sf::VideoMode(mapView.getSizeX(), mapView.getSizeY() + downNotificationBarHeight), "Minesweeper");
window->setFramerateLimit(config.getInt("framerate"));
}
void mainLoop()
{
while (window->isOpen())
{
sf::Event event;
while (window->pollEvent(event))
{
// Change the map size when user resize the window
if (event.type == event.Resized)
{
int sizeX = (window->getSize().x) / (mapView.getMargin() + mapView.getSquareSize()) + int(mapView.getMargin() > 0);
//int sizeX = (window->getSize().x + mapView.getMargin() * 2) / (mapView.getMargin() + mapView.getSquareSize());
int sizeY = (window->getSize().y - downNotificationBarHeight) / (mapView.getMargin() + mapView.getSquareSize()) + int(mapView.getMargin() > 0);
std::cout << "sizeX: " << sizeX << "==" << map->getSizeX() << " " << "sizeY: " << sizeY << "== " << map->getSizeY() << "\n";
if (sizeX != map->getSizeX() || sizeY != map->getSizeY())
{
int nextMines = mapController.getNextMineNumber();
if (nextMines > map->getMineUpperbound())
{
nextMines = map->getMineUpperbound();
mapController.setMineNumber(map->getMineUpperbound());
}
map->setSize(sizeY, sizeX, nextMines);
mapView.popup(
"Map " + std::to_string(map->getSizeX()) +
"x" + std::to_string(map->getSizeY()) + " with " +
std::to_string(map->getMineCount()) + " mines generated!", 3000);
}
window->setSize(sf::Vector2u(mapView.getSizeX(), mapView.getSizeY() + downNotificationBarHeight));
// Proper scaling
sf::View changedView(sf::FloatRect(0, 0, window->getSize().x, window->getSize().y));
window->setView(changedView);
}
if (event.type == sf::Event::Closed)
window->close();
mapController.preceedMouse(mouse.getPosition(*window), event);
mapController.preceedKeyboard(event);
}
mapController.continiousMouse(mouse.getPosition(*window));
mapView.updateBottomLine();
window->clear();
window->draw(mapView);
window->display();
}
}
};