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

[ChatGPT][SFML][커스텀 2D 게임 엔진 개발] SFML 기반 커스텀 2D게임 엔진용 스프라이트 편집기 생성 예제 코드 샘플

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

SFML 기반 커스텀 2D게임 엔진용 스프라이트 편집기 생성 예제 코드 샘플

/* Here's an example of how you could move the sprite using the arrow keys
*/
#include <SFML/Graphics.hpp>

int main() {
    sf::RenderWindow window(sf::VideoMode(800, 600), "Sprite Editor");
    sf::Texture sprite_texture;
    if (!sprite_texture.loadFromFile("sprite.png")) {
        return -1;
    }

    sf::Sprite sprite(sprite_texture);
    float speed = 200.0f; // pixels per second

    sf::Clock clock;
    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }

        // Get the elapsed time since the last frame
        float elapsed = clock.restart().asSeconds();

        // Move the sprite based on the arrow keys
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
            sprite.move(-speed * elapsed, 0);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
            sprite.move(speed * elapsed, 0);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
            sprite.move(0, -speed * elapsed);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
            sprite.move(0, speed * elapsed);
        }

        window.clear();
        window.draw(sprite);
        window.display();
    }

    return 0;
}

이 코드는 sf::Clock 클래스를 사용하여 프레임 사이의 시간을 추적하고 sf::Keyboard 클래스를 사용하여 왼쪽, 오른쪽, 위쪽 및 아래쪽 화살표 키를 눌렀는지 확인합니다. 또한 마지막 프레임 이후 경과된 시간과 사용자가 설정한 속도에 따라 스프라이트를 이동합니다.

728x90
LIST

댓글