-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameMain.cpp
More file actions
71 lines (58 loc) · 1.59 KB
/
GameMain.cpp
File metadata and controls
71 lines (58 loc) · 1.59 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
64
65
66
67
68
69
70
71
#include <SFML/Graphics.hpp>
#include "Constants.h"
#include "Game.h"
void HandleWindowEvents(sf::RenderWindow& window, SnakeGame::Game& game)
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
// Handle window resize
if (event.type == sf::Event::Resized)
{
// Update the view to the new size of the window
sf::FloatRect visibleArea(0, 0, (float)event.size.width, (float)event.size.height);
window.setView(sf::View(visibleArea));
}
}
}
int main()
{
using namespace SnakeGame;
// Init random seed
srand((unsigned int)time(nullptr));
// Init window
sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Snake Game!");
window.setFramerateLimit(60);
Game game;
IniteGame(game);
// Init game clock
sf::Clock gameClock;
float lastTime = gameClock.getElapsedTime().asSeconds();
// Main loop
while (window.isOpen())
{
// Check exit
if (game.shouldExit)
{
window.close();
break;
}
// Calculate delta time
float currentTime = gameClock.getElapsedTime().asSeconds();
float deltaTime = currentTime - lastTime;
lastTime = currentTime;
// Handle window events
HandleWindowEvents(window, game);
// Update game
UpdateGame(game, deltaTime);
// Draw game
window.clear();
DrawGame(game, window);
window.display();
}
return 0;
}