forked from cwbriscoe/NeHe-SFML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TODOsfml_window_time.cpp
61 lines (49 loc) · 1.78 KB
/
TODOsfml_window_time.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
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window.hpp>
////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
// Create the main window
sf::Window App(sf::VideoMode(800, 600, 32), "SFML Time");
// Disable vertical synchronization to get maximum framerate
App.UseVerticalSync(false);
// Create a clock for measuring time
sf::Clock Clock;
// Some dummy variables to simulate an object that moves
const float Speed = 50.f;
float Left = 0.f;
float Top = 0.f;
// Start main loop
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
// Escape key : exit
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
}
// Get elapsed time since last frame (we could as well use App.GetFrameTime())
float ElapsedTime = Clock.GetElapsedTime();
Clock.Reset();
// Make our virtual object move
if (App.GetInput().IsKeyDown(sf::Key::Left)) Left -= Speed * ElapsedTime;
if (App.GetInput().IsKeyDown(sf::Key::Right)) Left += Speed * ElapsedTime;
if (App.GetInput().IsKeyDown(sf::Key::Up)) Top -= Speed * ElapsedTime;
if (App.GetInput().IsKeyDown(sf::Key::Down)) Top += Speed * ElapsedTime;
// Display window on screen
App.Display();
}
return EXIT_SUCCESS;
}