C++ Animation (SFML)
In this page:
Moving a Shape
The simplest form of animation is updating a shape's stored coordinates each pass through the game loop -- for example, incrementing its X position by a small amount every frame -- and then calling draw() again, which SFML renders at the shape's new location.
Example: Moving a Shape
#include <SFML/Graphics.hpp>
int main() {
sf::CircleShape shape(20.f);
float x = 0.f;
for (int frame = 0; frame < 5; frame++) {
x += 2.f;
shape.setPosition(x, 100);
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Using Delta Time
If you move an object by a fixed number of pixels every frame, it will visibly move faster on a machine that renders more frames per second than on a slower one. Multiplying the movement amount by delta time (the real elapsed time since the last frame) normalizes speed so the animation looks the same regardless of frame rate.
Example: Using Delta Time
#include <SFML/Graphics.hpp>
int main() {
sf::Clock clock;
float x = 0.f;
float speed = 100.f;
float deltaTime = clock.restart().asSeconds();
x += speed * deltaTime;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Sprite Sheet Animation
A sprite sheet packs many animation frames into a single image file to avoid loading dozens of separate textures. sf::IntRect defines a rectangular sub-region of that sheet to display at any given moment, and cycling through a sequence of rects over time produces the animation.
Example: Sprite Sheet Animation
#include <SFML/Graphics.hpp>
int main() {
sf::Sprite sprite;
sf::IntRect frame1(0, 0, 32, 32);
sf::IntRect frame2(32, 0, 32, 32);
sprite.setTextureRect(frame1);
sprite.setTextureRect(frame2); // next animation frame
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Scale and Rotation Animation
Continuously updating a shape's rotation or scale value each frame -- rather than its position -- produces spinning or pulsing effects. Because these are separate transform properties, you can combine rotation, scale, and position animation on the same object simultaneously.
Example: Scale and Rotation Animation
#include <SFML/Graphics.hpp>
int main() {
sf::CircleShape shape(20.f);
for (int frame = 0; frame < 5; frame++) {
shape.rotate(5.f);
shape.setScale(1.f + frame * 0.1f, 1.f + frame * 0.1f);
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Boundary Collision Animation
To make an object bounce, check its position against the window's edges each frame; when it crosses a boundary, invert the relevant component of its velocity (X for left/right walls, Y for top/bottom) so it continues moving away from the edge instead of off-screen.
Example: Boundary Collision Animation
#include <iostream>
int main() {
float x = 95.f, velocityX = 3.f;
float windowWidth = 100.f;
if (x + velocityX > windowWidth || x + velocityX < 0)
velocityX = -velocityX; // bounce off the edge
std::cout << "New velocity: " << velocityX << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: