-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
71 lines (55 loc) · 1.23 KB
/
Game.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
#include <SFML/Graphics.hpp>
#include <memory>
#include "MenuScreen.h"
#include "Game.h"
using namespace sfSnake;
const sf::Time Game::TimePerFrame = sf::seconds(1.f / 10.f);
std::shared_ptr<Screen> Game::Screen = std::make_shared<MenuScreen>();
//default setting
sf::Color Game::BG_color = sf::Color::Black;
bool Game::grid_on = false;
sf::Color Game::grid_color = sf::Color::Black;
Game::Game()
: window_(sf::VideoMode(Game::Width, Game::Height), "sfSnake")
{
bgMusic_.openFromFile("Music/bg_music.wav");
bgMusic_.setLoop(true);
bgMusic_.play();
}
void Game::handleInput()
{
sf::Event event;
while (window_.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window_.close();
}
Game::Screen->handleInput(window_);
}
void Game::update(sf::Time delta)
{
Game::Screen->update(delta);
}
void Game::render()
{
window_.clear(BG_color);
Game::Screen->render(window_);
window_.display();
}
void Game::run()
{
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while (window_.isOpen())
{
sf::Time delta = clock.restart();
timeSinceLastUpdate += delta;
while (timeSinceLastUpdate > Game::TimePerFrame)
{
timeSinceLastUpdate -= TimePerFrame;
handleInput();
update(TimePerFrame);
}
render();
}
}