-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.cpp
69 lines (60 loc) · 1.77 KB
/
example.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
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
window.setFramerateLimit(60);
sf::Texture squareTexture, backgroundTexture;
sf::Sprite squareSprite, backgroundSprite, square2;
sf::Clock inputClock;
sf::Time timeSinceInputCheck;
if((!squareTexture.loadFromFile("images/square.png")) || (!backgroundTexture.loadFromFile("images/background.png")))
{
std::cout << "Problem loading texture." << std::endl;
return 1;
}
squareSprite.setTexture(squareTexture);
square2.setTexture(squareTexture);
square2.setColor(sf::Color(0,255,0));
backgroundSprite.setTexture(backgroundTexture);
square2.setPosition(50,50);
squareSprite.setPosition(10,50);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
timeSinceInputCheck += inputClock.getElapsedTime();
inputClock.restart();
if(timeSinceInputCheck.asMilliseconds() >= 10)
{
timeSinceInputCheck = sf::milliseconds(0);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
squareSprite.move(-2,0);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
squareSprite.move(2,0);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
squareSprite.move(0,-2);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
squareSprite.move(0,2);
}
}
window.clear();
window.draw(backgroundSprite);
window.draw(squareSprite);
window.draw(square2);
window.display();
}
return 0;
}