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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include <iostream>
#include <SFML/Graphics.hpp>
int main()
{
const int velocity = 50;
// Six sprite frames will be displayed per second
const float timePerFrame = 1.0 / 6.0;
// Variables to store individual sprite dimensions
int pixelWidth = 0;
int pixelHeight = 0;
// Offset is incremented to determine what rectangle of the spritesheet is being displayed
int offset = 0;
// Render a window
sf::RenderWindow window(sf::VideoMode(960, 540), "Window");
// Instantiate Texture object to hold spritesheet
sf::Texture spritesheet;
// Instantiate Sprite object to hold texture
sf::Sprite player;
// Instantiate Time object to store elapsed time between frames,
sf::Time elapsedTime;
// Instantiate Clock object to keep track of time passed
sf::Clock clock;
// Load spritesheet onto texture
if(!spritesheet.loadFromFile("player.png"))
{
std::cout << "player.png could not be loaded." << std::endl;
}
else
{
// Apply loaded texture onto sprite
player.setTexture(spritesheet);
// Set sprite dimensions
pixelWidth = 32;
pixelHeight = 32;
// Set players starting position
player.setTextureRect(sf::IntRect(pixelWidth, pixelHeight * 0, pixelWidth, pixelHeight));
}
// Start main game loop (each loop is a full frame)
while(window.isOpen())// Beginning of new frame
{
// elapsedTime will accumulate the time that has passed so far
elapsedTime = clock.getElapsedTime();
// Instantiate Event object to process window events (closing, resizing...)
sf::Event event;
// While the window processes events
while(window.pollEvent(event))
{
// Close the window
if(event.type == sf::Event::Closed)
{
window.close();
}
if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
{
window.close();
}
}
// After the window has processed its events
if(elapsedTime.asSeconds() >= timePerFrame)
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
player.setTextureRect(sf::IntRect(pixelWidth * offset, pixelHeight * 3, pixelWidth, pixelHeight));
player.move(0, -1 * elapsedTime.asSeconds() * velocity);
offset++;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
player.setTextureRect(sf::IntRect(pixelWidth * offset, pixelHeight * 0, pixelWidth, pixelHeight));
player.move(0, 1 * elapsedTime.asSeconds() * velocity);
offset++;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
player.setTextureRect(sf::IntRect(pixelWidth * offset, pixelHeight * 1, pixelWidth, pixelHeight));
player.move(-1 * elapsedTime.asSeconds() * velocity, 0);
offset++;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
player.setTextureRect(sf::IntRect(pixelWidth * offset, pixelHeight * 2, pixelWidth, pixelHeight));
player.move(1 * elapsedTime.asSeconds() * velocity, 0);
offset++;
}
if(offset >= 3)
{
offset = 0;
}
// Reset elapsedTime
elapsedTime = clock.restart();
}
// Clear the window with the colour black
window.clear(sf::Color::Black);
// Draw the player to the screen
window.draw(player);
// Display the newly updated screen
window.display();
}// End of current frame
return 0;
}
|