61
Graphics / SFML 2.0 Sprite animation
« on: August 27, 2011, 01:08:00 pm »
I never used THOR before, just got into SFML lol ill look into it thanks guys!
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Config.hpp>
// set some globals
char faceDirection = 'L'; // L = Left, R = Right
int minAni = 2; // Start at picture 2 for the walk animation
int maxAni = 5; // End at picture 5 for the walk animation
int curAni = 2; // What animation we start on
char wayAni = 'R'; // What way should we start counting first
int main()
{
// create a new window 800x600 resolution 32 bit
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "MyTest");
/////////////////////////////////// SPRITE 1 FRAME 1 //////////////////////
sf::Image character1;
character1.LoadFromFile("character_sprites/character_1.png");
sf::Texture charTexture1;
charTexture1.LoadFromImage(character1);
sf::Sprite charSprite1(charTexture1);
charSprite1.SetPosition(390, 400);
/////////////////////////////////// SPRITE 1 FRAME 2 //////////////////////
sf::Image character2;
character2.LoadFromFile("character_sprites/character_2.png");
sf::Texture charTexture2;
charTexture2.LoadFromImage(character2);
/////////////////////////////////// SPRITE 1 FRAME 3 //////////////////////
sf::Image character3;
character3.LoadFromFile("character_sprites/character_3.png");
sf::Texture charTexture3;
charTexture3.LoadFromImage(character3);
/////////////////////////////////// SPRITE 1 FRAME 4 //////////////////////
sf::Image character4;
character4.LoadFromFile("character_sprites/character_4.png");
sf::Texture charTexture4;
charTexture4.LoadFromImage(character4);
/////////////////////////////////// SPRITE 1 FRAME 5 //////////////////////
sf::Image character5;
character5.LoadFromFile("character_sprites/character_5.png");
sf::Texture charTexture5;
charTexture5.LoadFromImage(character5);
//////////////////////////////////////////////////////////////////////////
// timers so app runs on the same speed on every machine possible
sf::Clock clock; // timer for events
sf::Clock aniClock; // timer for character animation
while( App.IsOpened())
{
// events loop
sf::Event Event;
while (App.PollEvent(Event))
{
switch(Event.Type)
{
// if screen is closed
case sf::Event::Closed:
App.Close();
break;
}
} // end events
// move the character at a rate of 33 ms per second
if(clock.GetElapsedTime() >= 33)
{
if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Left))
{
charSprite1.Move( -2, 0 );
}
if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Right))
{
charSprite1.Move( 2, 0 );
}
// reset the clock for the event timer
clock.Reset();
}
if(aniClock.GetElapsedTime() >= 100)
{
if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Left))
{
// check if facing the opposit side then flip
if( faceDirection == 'R')
charSprite1.FlipX(false);
// set the direction we are facing
faceDirection = 'L';
// set to frame 2
if( curAni <= 2 && wayAni == 'R')
charSprite1.SetTexture(charTexture2);
curAni++;
// set to frame 3
if( curAni <= 3 && wayAni == 'R')
charSprite1.SetTexture(charTexture3);
curAni++;
// set to frame 4
if( curAni <= 4 && wayAni == 'R')
charSprite1.SetTexture(charTexture4);
curAni++;
// set to frame 5 and set the wayAni in the opposite way
if( curAni <= 5 && wayAni == 'R')
charSprite1.SetTexture(charTexture5);
curAni++;
wayAni = 'L';
}
else if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Right))
{
// check if facing the opposit side then flip
if( faceDirection == 'L')
charSprite1.FlipX(true);
// set the direction we are facing
faceDirection = 'R';
}
else
{
// we are not moving, set the sprite back to animation 1
charSprite1.SetTexture(charTexture1);
}
aniClock.Reset();
}
// clear the screen
App.Clear();
// draw the sprite
App.Draw(charSprite1);
// display the window with the new stuff
App.Display();
} // end while App is opened
return EXIT_SUCCESS;
}
// Create a 20x20 image filled with black color
sf::Image image;
if (!image.Create(20, 20, sf::Color::Black))
return -1;
#include <SFML/Window.hpp>
int main()
{
// create a new window 800x600 resolution 32 bit
sf::Window App(sf::VideoMode(800, 600, 32), "Zombie Madnezz");
// disable vertical synchronization for max framerate
App.EnableVerticalSync(false);
// attach the GetInput function to our window
//const sf::Input& Input = App.GetInput();
// as long as this window is running
while( App.IsOpened())
{
sf::Event Event;
while (App.PollEvent(Event))
{
// if user closes the window stop running the program
if(Event.Type == sf::Event::Closed)
{
App.Close();
}
// if user presses ESC button stop running the program
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Escape))
{
App.Close();
}
// set the objects for keyboard and mouse input
bool LeftKeyDown = sf::Keyboard::IsKeyPressed(sf::Keyboard::Left); // left arrow key
bool RightKeyDown = sf::Keyboard::IsKeyPressed(sf::Keyboard::Right); // right array key
bool SpacebarDown = sf::Keyboard::IsKeyPressed(sf::Keyboard::Space); // spacebar
bool LeftMouseButton = sf::Mouse::IsButtonPressed(sf::Mouse::Left); // left mouse button
bool RightMouseButton = sf::Mouse::IsButtonPressed(sf::Mouse::Right); // right mouse button
//unsigned int MouseCoordsX = Input.GetMouseX(); // mouse position x-as
//unsigned int MouseCoordsY = Input.GetMouseY(); // mouse position y-as
// configure some character settings
const float Speed = 50.f;
float Left = 0.f; // only left and top are needed as we can
float Top = 0.f; // calculate all with those three numbers
/* CHECK OUT THIS PART WITH CLOCK FUNCTION TO HAVE THE SAME TIMING
ON EVERY HARDWARE COMPUTER */
///////////////////////////////////////////////////////////////////////
// http://www.sfml-dev.org/tutorials/1.6/window-time.php
// execute a action according to the boolean (keyboard)
if ( LeftKeyDown == true ) Left -= Speed;
if ( RightKeyDown == true ) Left += Speed;
} // checks for events with PollEvent
//##############################
// Create a sprite and add music, but aint workin
///////////////////////////////////////////////////////////////////////
// http://www.sfml-dev.org/tutorials/1.6/graphics-sprite.php
// display the window
App.Display();
} // end while app is opened
return EXIT_SUCCESS;
}
#-------------------------------------------------
#
# Project created by QtCreator 2011-08-22T17:53:13
#
#-------------------------------------------------
QT += core gui
TARGET = game
TEMPLATE = app
SOURCES += main.cpp
HEADERS +=
FORMS +=
LIBS += -L"C:\Program Files (x86)\SFML\lib" -lsfml-window -lsfml-graphics -sfml-system -sfml-main -sfml-audio
INCLUDEPATH = "C:\Program Files (x86)\SFML\include"
if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Left))
{
// move left...
}
else if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Right))
{
// move right...
}
else if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Escape))
{
// quit...
}
#include <SFML/Window.hpp>
int main()
{
// create a new window 800x600 resolution 32 bit
sf::Window App(sf::VideoMode(800, 600, 32), "Zombie Madnezz");
// attach the GetInput function to our window
const sf::Input& Input = App.GetInput();
// as long as this window is running
while( App.IsOpened())
{
sf::Event Event;
while (App.PollEvent(Event))
{
// if user closes the window stop running the program
if(Event.Type == sf::Event::Closed)
{
App.Close();
}
// if user presses ESC button stop running the program
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Escape))
{
App.Close();
}
// set the objects for keyboard and mouse input
bool LeftKeyDown = Input.IsKeyDown(sf::Keyboard::Left); // left arrow key
bool RightKeyDown = Input.IsKeyDown(sf::Keyboard::Right); // right array key
bool SpacebarDown = Input.IsKeyDown(sf::Keyboard::Space); // spacebar
bool LeftMouseButton = Input.IsMouseButtonDown(sf::Mouse::Left); // left mouse button
bool RightMouseButton = Input.IsMouseButtonDown(sf::Mouse::Right); // right mouse button
unsigned int MouseCoordsX = Input.GetMouseX(); // mouse position x-as
unsigned int MouseCoordsY = Input.GetMouseY(); // mouse position y-as
// configure some character settings
const float Speed = 50.f;
float Left = 0.f; // only left and top are needed as we can
float Top = 0.f; // calculate all with those three numbers
// check if a key is pressed
sf::Clock Clock;
while (App.IsOpened())
{
float ElapsedTime = Clock.GetElapsedTime();
Clock.Reset();
if (App.GetInput().IsKeyDown(sf::Keyboard::Left)) Left -= Speed * ElapsedTime;
if (App.GetInput().IsKeyDown(sf::Keyboard::Right)) Left += Speed * ElapsedTime;
if (App.GetInput().IsKeyDown(sf::Keyboard::Up)) Top += Speed * ElapsedTime;
if (App.GetInput().IsKeyDown(sf::Keyboard::Down)) Top -= Speed * ElapsedTime;
}
}
// display the window
App.Display();
}
return EXIT_SUCCESS;
}
#-------------------------------------------------
#
# Project created by QtCreator 2011-08-22T17:53:13
#
#-------------------------------------------------
QT += core gui
TARGET = game
TEMPLATE = app
SOURCES += main.cpp
HEADERS +=
FORMS +=
LIBS += -L"C:\Program Files (x86)\SFML\lib" -lsfml-window -lsfml-graphics -sfml-system -sfml-main -sfml-audio
INCLUDEPATH = "C:\Program Files (x86)\SFML\include"