簡體   English   中英

如何使用C ++和SFML創建自己的類

[英]How to make my own classes with C++ and SFML

在學習C ++基礎知識之后,我目前已開始使用SFML。 我已經了解了數組,引用和之前的所有內容,但是一直難以掌握使用類的概念。

在SFML中,我創建了一個簡單的Sprite運動程序,但是,我想將此信息移到一個類中(可以說它稱為“ Player”)。 我已經搞砸了很多,但是我無法正常工作。

我試圖在一個類中創建一個函數來檢查播放器的輸入,但是我無法訪問在main中創建的sprite。 我想將與玩家相關的所有內容都移到Player類中,但需要一些建議。

正確的方法是什么? (請不要說回去學習課程,這是我要學習的課程!)

main.cpp

#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>

int main()
{
    //character position
    enum Direction{ Down, Left, Right, Up };
    sf::Vector2i source(1, Down);

    //window
    sf::RenderWindow window(sf::VideoMode(1200, 700), "Testing");
    window.setKeyRepeatEnabled(false);

    //player character
    sf::Texture pTexture;
    sf::Sprite pSprite;
    if(!pTexture.loadFromFile("image/playerSprite.png"))
        std::cout << "Texture Error" << std::endl;
    pSprite.setTexture(pTexture);
    pSprite.setScale(1.5f, 1.5f);

    //game loop
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) //move up
        {

            source.y = Up;
            pSprite.move(0, -0.2);

            //animation
            source.x++;
            if(source.x * 32 >= pTexture.getSize().x)
            {
                source.x = 0;
            }
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) //move down
        {
            source.y = Down;
            pSprite.move(0, 0.2);

            //animation
            source.x++;
            if(source.x * 32 >= pTexture.getSize().x)
            {
                source.x = 0;
            }
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) //move right
        {
            source.y = Right;
            pSprite.move(0.2, 0);

            //animation
            source.x++;
            if(source.x * 32 >= pTexture.getSize().x)
            {
                source.x = 0;
            }
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) //move left
        {
            source.y = Left;
            pSprite.move(-0.2, 0);

            //animation
            source.x++;
            if(source.x * 32 >= pTexture.getSize().x)
            {
                source.x = 0;
            }
        }

        pSprite.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));
        window.draw(pSprite);
        window.display();

    }

    return 0;
}

免責聲明:您不應該期望這樣的答案,您確實應該閱讀OOP以獲得更多信息,這與SFML無關,這只是基本的重構。

如何用OOP思考

首先,在編碼功能之前,您應該設計出真正適合這種情況的OOP結構。 將每個類視為整體的一部分,那就是您的程序。 實際上,類只是具有有用方法的數據的聚合,這些方法僅以有意義的方式影響類內部的數據(或通過方法參數提供的數據)。

請參閱C ++基礎知識 (更多有關OOP的內容),以了解如何使其在C ++中工作。 其他編程語言中的概念相似。

使用您提供的代碼

您需要的是Player類,從程序主邏輯中刪除播放器代碼是一個好主意。 您需要問自己:“我的播放器代碼需要做什么工作?”

玩家階層

基本上,您的播放器只是一個精靈和一個位置。 因此,您可以將這些數據作為私有成員封裝到Player類中。 這樣可以避免其他代碼干擾播放器數據。 要使用播放器數據,您需要在類中提供每個僅影響播放器的方法。

紋理和精靈

我故意將紋理保留在播放器之外。 紋理是沉重的對象,這就是為什么Sprite對象僅保留指向它的指針的原因。 精靈很輕巧,可以輕松更改和復制。 盡管這里是我自己的資源管理器代碼 ,但紋理對象和其他資產的管理是另一個主題。

可選的

我並沒有花太多時間來更改代碼,但是您可以更改處理運動的方式,以僅使一個采用Player::Direction具有參數的“移動”方法。

為了給您更多幫助,並為您提供更多有關該主題的指導,我使用了“ 前向聲明 ”,並將您的Direction枚舉移入了類中。 這可能不是實現所需目標的最佳方法,但我只是更改了自己的代碼,以免迷路。

編碼

無論如何,這是我的努力。

播放器

#ifndef PLAYER_H_
#define PLAYER_H_

#include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics/Sprite.hpp>

// Forward Declaration
namespace sf {
class Texture;
}

// provide your namespace to avoid collision/ambiguities
namespace test {
/*
 *
 */
class Player: public sf::Drawable {
public:

    enum Direction {
        Down, Left, Right, Up
    };

    Player(const sf::Texture& playerTexture);
    virtual ~Player();

    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;

    void moveUp();
    void moveDown();
    void moveLeft();
    void moveRight();

private:

    sf::Sprite mSprite;
    sf::Vector2i mSource;

};

} /* end namespace test */
#endif /* PLAYER_H_ */

Player.cpp

#include "Player.h"

// you need this because of forward declaration
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Rect.hpp>
#include <SFML/Graphics/RenderTarget.hpp>

namespace test {
Player::Player(const sf::Texture& imagePath) :
                mSprite(imagePath),
                mSource(1, Player::Down) {

    // do not need that line anymore, thanks to initialiser list
    //pSprite.setTexture(pTexture);

    mSprite.setScale(1.5f, 1.5f);

}

Player::~Player() {
    // TODO Auto-generated destructor stub
}

void Player::draw(sf::RenderTarget& target, sf::RenderStates states) const {
    target.draw(mSprite, states);
}

void Player::moveUp() {
    mSource.y = Up;
    mSprite.move(0, -0.2);

    //animation
    mSource.x++;
    if (mSource.x * 32 >= (int) mSprite.getTexture()->getSize().x) {
        mSource.x = 0;
    }

    mSprite.setTextureRect(sf::IntRect(mSource.x * 32, mSource.y * 32, 32, 32));
}

void Player::moveDown() {
    mSource.y = Down;
    mSprite.move(0, 0.2);

    //animation
    mSource.x++;
    if (mSource.x * 32 >= (int) mSprite.getTexture()->getSize().x) {
        mSource.x = 0;
    }
}

void Player::moveLeft() {
    mSource.y = Left;
    mSprite.move(-0.2, 0);

    //animation
    mSource.x++;
    if (mSource.x * 32 >= (int) mSprite.getTexture()->getSize().x) {
        mSource.x = 0;
    }
}

void Player::moveRight() {
    mSource.y = Right;
    mSprite.move(0.2, 0);

    //animation
    mSource.x++;
    if (mSource.x * 32 >= (int) mSprite.getTexture()->getSize().x) {
        mSource.x = 0;
    }
}

} /* end namespace test */

main.cpp

#include <SFML/Graphics.hpp>
//#include <string> // not used for now
#include <iostream>

// don't forget to include your own header
#include "Player.h"

int main() {

    // just to save typing the "std::"
    using std::cout;
    using std::endl;
    using std::cerr;

    //window
    sf::RenderWindow window(sf::VideoMode(1200, 700), "Testing");
    window.setKeyRepeatEnabled(false);

    //player texture
    sf::Texture pTexture;
    if (!pTexture.loadFromFile("image/playerSprite.png")) {
        cerr << "Texture Error" << endl;
    }

    test::Player thePlayer(pTexture);

    //game loop
    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }

        window.clear();

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) //move up
                {
            thePlayer.moveUp();
        } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) //move down
                {
            thePlayer.moveDown();
        } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) //move right
                {
            thePlayer.moveRight();
        } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) //move left
                {
            thePlayer.moveLeft();
        }

        window.draw(thePlayer);
        window.display();

    }

    return 0;
}

其他良好做法

訪問器(即Getters / Setters )是成員函數,可以使一個成員訪問類的私有成員。

在您的代碼中,您可以執行以下操作:

 class Player {
    public: 
        Player(const sf::Texture& playerTexture);
        virtual ~Player();

       // to give access to a const reference of the sprite
       // One could call it like: sf::Sprite mySprite = myPlayerObject.getSprite();
       // notice also that the method itself is const, which assure you that
       // myPlayerObject won't change by calling getSprite()
       const sf::Sprite& getSprite() const{
           return mSprite;
       }

       // setSprite is not a const method, so it will change the data
       // inside myPlayerObject
       void setSprite(const sf::Sprite& newSprite){
           mSprite = newSprite;
       }

    private:
        sf::Sprite mSprite;
        sf::Vector2i mSource;
    };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM