bool Label::draw(int scrW, int scrH) { // Draw label // Center coords int lCX = alignX(x + width/2, scrW); int lCY = alignY(y + height/2, scrH); // Try Big size if(!drawText(lCX - textWidth(text, "big", font) / 2, lCY - textHeight(text, "big", font) / 2, animationText(), "big", font, alignX(x, scrW), alignY(y, scrH), alignX(width, scrW), alignY(height, scrH), indent)) { // Try Normal size if(!drawText(lCX - textWidth(text, "normal", font) / 2, lCY - textHeight(text, "normal", font) / 2, animationText(), "normal", font, alignX(x, scrW), alignY(y, scrH), alignX(width, scrW), alignY(height, scrH), indent)) { // Try Small size if(!drawText(lCX - textWidth(text, "small", font) / 2, lCY - textHeight(text, "small", font) / 2, animationText(), "small", font, alignX(x, scrW), alignY(y, scrH), alignX(width, scrW), alignY(height, scrH), indent)) { // Try Natural size return drawText(lCX - textWidth(text, "natural", font) / 2, lCY - textHeight(text, "natural", font) / 2, animationText(), "natural", font, alignX(x, scrW), alignY(y, scrH), alignX(width, scrW), alignY(height, scrH), indent); } } } return true; }
int main() { sf::RenderWindow window(sf::VideoMode(300, 200), "Thor Animation"); window.setVerticalSyncEnabled(true); window.setKeyRepeatEnabled(false); sf::Font font; if (!font.loadFromFile("Media/sansation.ttf")) return 1; // Instruction text sf::Text instructions( "W: Play walk animation (loop)\n" "A: Play attack animation\n" "S: Stop current animation\n" "Esc: Quit", font, 14u); sf::Text animationText("", font, 14u); animationText.setPosition(100.f, 150.f); // Load image that contains animation steps sf::Image image; if (!image.loadFromFile("Media/animation.png")) return 1; image.createMaskFromColor(sf::Color::White); // Create texture based on sf::Image sf::Texture texture; if (!texture.loadFromImage(image)) return 1; // Create sprite which is animated sf::Sprite sprite(texture); sprite.setPosition(100.f, 100.f); // Define walk animation thor::FrameAnimation walk; addFrames(walk, 0, 0, 7); // Frames 0..7 Right leg moves forward addFrames(walk, 0, 6, 0); // Frames 6..0 Right leg moves backward // Define attack animation thor::FrameAnimation attack; addFrames(attack, 1, 0, 3); // Frames 0..3 Lift gun addFrames(attack, 1, 4, 4, 5.f); // Frame 4 Aim (5 times normal frame duration) for (int i = 0; i < 3; ++i) addFrames(attack, 1, 5, 7); // Frame 5..7 Fire (repeat 3 times) addFrames(attack, 1, 4, 4, 5.f); // Frame 4 Wait addFrames(attack, 1, 3, 0); // Frame 3..1 Lower gun // Define static frame for stand animation thor::FrameAnimation stand; addFrames(stand, 0, 0, 0); // Register animations with their corresponding durations thor::Animator<sf::Sprite, std::string> animator; animator.addAnimation("walk", walk, sf::seconds(1.f)); animator.addAnimation("stand", stand, sf::seconds(1.f)); animator.addAnimation("attack", attack, sf::seconds(1.f)); // Create clock to measure frame time sf::Clock frameClock; // Main loop for (;;) { // Handle events sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::KeyPressed) { switch (event.key.code) { case sf::Keyboard::W: animator.playAnimation("walk", true); break; case sf::Keyboard::A: animator.playAnimation("attack"); break; case sf::Keyboard::S: animator.stopAnimation(); break; case sf::Keyboard::Escape: return 0; } } else if (event.type == sf::Event::Closed) { return 0; } } // If no other animation is playing, play stand animation if (!animator.isPlayingAnimation()) animator.playAnimation("stand"); // Output playing animation (general case; at the moment an animation is always playing) if (animator.isPlayingAnimation()) animationText.setString("Animation: " + animator.getPlayingAnimation()); else animationText.setString(""); // Update animator and apply current animation state to the sprite animator.update(frameClock.restart()); animator.animate(sprite); // Draw everything window.clear(sf::Color(50, 50, 50)); window.draw(instructions); window.draw(animationText); window.draw(sprite); window.display(); } }