C/C++ SFML animasyon dönmüyor

GT 145

Decapat
Katılım
29 Temmuz 2021
Mesajlar
723
Çözümler
1
İmagelerim :
turn_left_sprite.png
office_light_close.png

C++ kodum :
C++:
#include <SFML/Graphics.hpp>
#include <iostream>

typedef short int sint;
using namespace sf;
using namespace std;

string dirPath = "C:/Users/samil/Documents/Projelerim/FazGame/designes/office/";
string office_images[] = {
    dirPath + "office_light_close.jpeg", // Ofisin ortası olan image
    dirPath + "/right/turn_right_sprite.png",
    dirPath + "/left/turn_left_sprite.png",
};
sint frameN = 12;

int main() {
    RenderWindow pencere(VideoMode(800, 600), "FazBear Distribution Center");

    /* LOAD SPRİTES */
    Texture middleOffice, turnLeft, turnRight;
    middleOffice.loadFromFile(office_images[0]);
    turnRight.loadFromFile(office_images[1]);
    turnLeft.loadFromFile(office_images[2]);
    Sprite office(middleOffice);
    IntRect officeRect;

    officeRect.left = 0, officeRect.top = 0;
    officeRect.width = 2560, officeRect.height = 1440;

    float scaledX = static_cast<float>(pencere.getSize().x) / static_cast<float>(middleOffice.getSize().x);
    float scaledY = static_cast<float>(pencere.getSize().y) / static_cast<float>(middleOffice.getSize().y);
    office.setScale(scaledX, scaledY);
    /* LOAD SPRİTES */

    /* CREATE VARIABLES */
    bool isTurn = false;
    char turnDir = 'm'; // 'm' indicates the center initially
    char turnRot = NULL;
    sint turnFrame = 0;

    // MAİN - GAME
    while (pencere.isOpen()) {
        Event event;
        while (pencere.pollEvent(event)) {
            if (event.type == Event::Closed || Keyboard::isKeyPressed(Keyboard::Escape)) pencere.close();
        }

        if (Keyboard::isKeyPressed(Keyboard::A) && !isTurn && turnDir != 'l') {
            turnRot = 'l';
            isTurn = true;
            if (turnDir == 'm') {
                office.setTexture(turnLeft);
                office.setTextureRect(officeRect);
                turnDir = 'l';
            }
            else if (turnDir == 'r') {
                cout << "alert"; // Burası önceki bir hataydı, istediğiniz işlemi yapabilirsiniz.
                turnFrame = 12;
                turnDir = 'm';
            }
        }
        else if (Keyboard::isKeyPressed(Keyboard::D) && !isTurn && turnDir != 'r') {
            turnRot = 'r';
            isTurn = true;
            if (turnDir == 'm') {
                office.setTexture(turnRight);
                office.setTextureRect(officeRect);
                turnDir = 'r';
            }
            else if (turnDir == 'l') {
                turnFrame = 12;
                turnDir = 'm';
            }
        }

        // Logic for turning animation
        if (isTurn) {
            if (turnRot == 'l') {
                if (turnDir == 'l' && turnFrame < frameN) {
                    officeRect.left = turnFrame * 2560;
                    officeRect.width = 2560;
                    office.setTextureRect(officeRect);
                    ++turnFrame;
                }
                else if (turnDir == 'm' && turnFrame > 1) {
                    officeRect.left = (frameN - turnFrame) * 2560;
                    officeRect.width = 2560;
                    office.setTextureRect(officeRect);
                    --turnFrame;
                }
                else {
                    isTurn = false;
                    turnFrame = 0;
                    if (turnDir == 'm') office.setTexture(middleOffice);
                }
            }
            else if (turnRot == 'r') {
                if (turnDir == 'r' && turnFrame < frameN) {
                    officeRect.left = turnFrame * 2560;
                    officeRect.width = 2560;
                    office.setTextureRect(officeRect);
                    ++turnFrame;
                }
                else if (turnDir == 'm' && turnFrame > 1) {
                    officeRect.left = (frameN - turnFrame) * 2560;
                    officeRect.width = 2560;
                    office.setTextureRect(officeRect);
                    --turnFrame; cout << turnFrame << endl;
                }
                else {
                    isTurn = false;
                    turnFrame = 0;
                    if (turnDir == 'm') office.setTexture(middleOffice);
                }
            }
            sleep(milliseconds(33));
        }

        //printf("%c\n", turnDir);
        pencere.clear();
        pencere.draw(office);
        pencere.display();
    }

    return EXIT_SUCCESS;
}

İsterseniz çalıştırabilirsiniz. Soldan ve sağdan ortaya dönerken animasyon bozulup gidiyor 1-2 haftadır uğraşıyorum gerçekten bıktım. Şimdiden teşekkürler.
 

Dosya Ekleri

  • turn_right_sprite.png
    turn_right_sprite.png
    234,3 KB · Görüntüleme: 10

Geri
Yukarı