본문 바로가기
[ChatGPT] Sample Code 샘플 코드

[ChatGPT][SFML][2D 게임 개발] C++ 및 SFML 라이브러리를 사용하여 360도 회전하는 로고 회전 애니메이션을 만드는 방법의 예

by byungwoo733 2023. 1. 13.
728x90
반응형
SMALL

C++ 및 SFML 라이브러리를 사용하여 360도 회전하는 로고 회전 애니메이션을 만드는 방법의 예

// Here's an example of how you can create a logo rotation animation that rotates 360 degrees using C++ and the SFML library
#include <SFML/Graphics.hpp>

int main()
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");

    // Load a sprite to display
    sf::Texture texture;
    if (!texture.loadFromFile("logo.png"))
        return EXIT_FAILURE;
    sf::Sprite sprite(texture);

    // Set the initial position of the sprite
    sprite.setPosition(200, 150);

    // Create a clock for measuring the elapsed time
    sf::Clock clock;

    // Start the game loop
    while (window.isOpen())
    {
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window: exit
            if (event.type == sf::Event::Closed)
                window.close();
        }

        // Clear screen
        window.clear();

        // Draw the sprite
        window.draw(sprite);

        // Update the window
        window.display();

        // Measure the elapsed time
        sf::Time elapsed = clock.restart();

        // Update the sprite's rotation
        float rotationSpeed = 360.f; // 360 degrees per second
        sprite.rotate(rotationSpeed * elapsed.asSeconds());

        // Check if the sprite has completed a full rotation
        if (sprite.getRotation() >= 360)
            sprite.setRotation(0);
    }

    return EXIT_SUCCESS;
}

이 코드는 sf::RenderWindow 클래스를 사용하여 창을 만들고 sf::Texture 클래스를 사용하여 이미지를 로드하고 sf::Sprite 클래스를 사용하여 표시합니다. 또한 sf::Clock 클래스를 사용하여 마지막 프레임 이후 경과 시간을 측정합니다. 그런 다음 경과 시간과 지정된 회전 속도 값을 기반으로 스프라이트의 회전을 업데이트합니다. 마지막으로 스프라이트가 완전히 회전했는지 확인하고 필요한 경우 회전 각도를 0으로 재설정합니다.

728x90
LIST

댓글