-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameScreen.cpp
More file actions
63 lines (56 loc) · 2.15 KB
/
GameScreen.cpp
File metadata and controls
63 lines (56 loc) · 2.15 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
#include <SFML/Graphics.hpp>
#include "Welcome.h"
#include "Tile.h"
#include <string>
#include <fstream>
using namespace std;
#include "GameScreen.h"
void GameScreen::runGameScreen(float width, float height) {
// Game Window Render:
// Create the window
sf::RenderWindow window;
window.create(sf::VideoMode(width, height), "demo window");
// Load the texture files
sf::Texture up_texture;
sf::Texture down_texture;
up_texture.loadFromFile("files/images/tile_hidden.png");
down_texture.loadFromFile("files/images/tile_revealed.png");
// Create the tiles
vector<Tile> tiles;
for (int x = 0; x < width; x += 32) { // Iterating by 32 because the tile PNGs are 32 pixels wide
for (int y = 0; y < height; y += 32) { // Iterating by 32 because the tile PNGs are 32 pixels tall
// x and y correspond to where on the screen the top-left corner of the tile sprite should be
tiles.emplace_back(up_texture, down_texture, x, y);
}
}
// Mainloop
while (window.isOpen()) {
// Event loop
sf::Event event;
while (window.pollEvent(event)) {
// Close event
if (event.type == sf::Event::EventType::Closed) {
window.close();
break;
}
// Click event
if (event.type == sf::Event::EventType::MouseButtonPressed) {
// Get click data
auto click = event.mouseButton;
// See if the click intersects any tiles. If so, run their click() function
for (Tile& tile: tiles) {
// getGlobalBounds().contains(x, y) tests if (x, y) touches the sprite
if (tile.get_sprite().getGlobalBounds().contains(click.x, click.y)) {
tile.click(); // Click function as defined in the tile class
}
}
}
}
// Render loop
window.clear(sf::Color::Blue);
for (Tile& tile: tiles) {
window.draw(tile.get_sprite());
}
window.display();
}
}