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
|
#include <SFML/Graphics.hpp>
int main(int argc, char** argv)
{
// Create the main rendering window
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
//Load the sprite image from a file
sf::Image Image;
if( !Image.LoadFromFile("sprite.bmp") )
return EXIT_FAILURE;
//create the sprite
sf::Sprite Sprite(Image);
//Change its properties
Sprite.SetColor(sf::Color( 0, 255, 255, 128 ) );
Sprite.SetPosition(200.f, 100.f );
Sprite.SetScale( 2.f, 2.f );
//Main game loop
while( App.IsOpened() )
{
//Handle Events
sf::Event Event;
while( App.GetEvent( Event ) )
{
//Close window : exit
if( Event.Type == sf::Event::Closed )
{
App.Close();
}
//Get elapsed time
float ElapsedTime = App.GetFrameTime();
//Move the spire
if( App.GetInput().IsKeyDown(sf::Key::Left) )
Sprite.Move( -100 * ElapsedTime, 0 );
if( App.GetInput().IsKeyDown(sf::Key::Right) )
Sprite.Move( 100 * ElapsedTime, 0 );
if( App.GetInput().IsKeyDown(sf::Key::Up ) )
Sprite.Move( 0, -100 * ElapsedTime);
if( App.GetInput().IsKeyDown(sf::Key::Down ) )
Sprite.Move(0, 100 * ElapsedTime);
//Rotate the sprite
if( App.GetInput().IsKeyDown(sf::Key::Add) )
Sprite.Rotate( -100 * ElapsedTime );
if( App.GetInput().IsKeyDown(sf::Key::Subtract ) )
Sprite.Rotate( +100 * ElapsedTime);
//Display sprite in window
App.Draw(Sprite);
//Display window contents on screen
App.Display();
}
}
return EXIT_SUCCESS;
}
|