C++/SFML2 Sprite 没有显示

标签 c++ sprite sfml

基本上,我正在尝试使用 C++ 和 SFML2 API 制作井字游戏。我在我的主要功能中加载了背景和 spritesheet,并让背景显示得很好。之后我继续创建一个如下所示的 Tile 类。

瓷砖.h

#ifndef TILE_H
#define TILE_H

#include <SFML\Graphics.hpp>

enum TileStatus
{
    FREE    = 0,
    CROSS   = 1,
    CIRCLE  = 2
};

class Tile
{

private:

    float   xPos,
            yPos;

    sf::Sprite tileSprite;


    int     height,
            width;

    TileStatus tileStatus;

public:

    //Constructors
    Tile();
    Tile( sf::Texture tileTexture , TileStatus tileStatus , float xPos , float yPos );

    //Methods
    sf::Sprite getTileSprite();
    void setTileStatus( TileStatus tileStatus );
    TileStatus getTileStatus();
};

#endif

和Tile.cpp

#include "tile.h"

Tile::Tile()
{
    xPos = 0;
    yPos = 0;

    height  = 0;
    width   = 0;

    tileStatus = FREE;

}

Tile::Tile( sf::Texture tileTexture , TileStatus tileStatus , float xPos , float yPos )
{
    this->tileStatus = tileStatus;

    height  = 126;
    width   = 126;

    this->xPos = xPos;
    this->yPos = yPos;

    this->tileSprite.setTexture( tileTexture );
    this->tileSprite.setTextureRect( sf::IntRect( 0,0,126,126 ) );
    this->tileSprite.setPosition( this->xPos , this->yPos );

    if( this->tileStatus == FREE)
    {
        tileSprite.setTextureRect( sf::IntRect( 0,0,126,126 ) );
    }
    else if( this->tileStatus == CROSS )
    {
        tileSprite.setTextureRect( sf::IntRect( 126,0,126,126 ) );
    }
    else if( this->tileStatus == CIRCLE )
    {
        tileSprite.setTextureRect( sf::IntRect( 252,0,126,126 ) );
    }
}

void Tile::setTileStatus( TileStatus tileStatus )
{
    this->tileStatus = tileStatus;

    if( this->tileStatus == FREE)
    {
        tileSprite.setTextureRect( sf::IntRect( 0,0,width,height ) );
    }
    else if( this->tileStatus == CROSS )
    {
        tileSprite.setTextureRect( sf::IntRect( 126,0,width,height ) );
    }
    else if( this->tileStatus == CIRCLE )
    {
        tileSprite.setTextureRect( sf::IntRect( 252,0,width,height ) );
    }
}

sf::Sprite Tile::getTileSprite()
{
    return tileSprite;
}

TileStatus Tile::getTileStatus()
{
    return this->tileStatus;
}

基本上我加载了this spritesheet 作为我的主要函数中的纹理,然后我将它传递给 Tile 函数构造函数,然后我可以设置和更改它的子矩形并执行其他操作。

现在这里的问题是,即使它编译没有任何错误, Sprite 也没有显示。但是,当我将 Tile 类中的代码写入主类时,而不是图像显示。谁能告诉我我做错了什么?

这是main.cpp

#include <SFML\System.hpp>
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>

#include "tile.h"

int main()
{
    sf::RenderWindow window( sf::VideoMode( 800,600,32 ) , "Tic - Tac - Toe" );

    // Load background image and display it
    sf::Texture BackgroundTexture;
    if(!BackgroundTexture.loadFromFile( "background.jpeg" ))
    {
        return EXIT_FAILURE;
    }
    sf::Sprite Background(BackgroundTexture);
    Background.setTextureRect( sf::IntRect( 0,0,800,600 ) );
    Background.setPosition( 0,0 );

    // Load the spritesheet
    sf::Texture Spritesheet;
    if(!Spritesheet.loadFromFile( "sprites.png" ))
    {
        return EXIT_FAILURE;
    }


    // Load font and create graphical text
    sf::Font font;
    if (!font.loadFromFile( "arial.ttf" ))
    {
        return EXIT_FAILURE;
    }
    sf::Text TurnNotification;

    //limit the framerate to 30FPS
    window.setFramerateLimit(30);

    /*********************************************/
    //THIS WORKS BUT IT'S NOT WHAT I NEED
    //sf::Sprite tileSprite;
    //tileSprite.setTexture( Spritesheet );
    //tileSprite.setTextureRect( sf::IntRect(126,0,126,126) );
    //tileSprite.setPosition(126,126);


    //This doesn't work
    Tile cTile(Spritesheet, CROSS, 0 , 0 );

    while(window.isOpen())
    {

        window.draw(Background);
        window.draw(cTile.getTileSprite());
        //window.draw(tileSprite);

        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
            {
                window.close();
            }
            if((event.type == sf::Event::KeyPressed) && (sf::Keyboard::Escape))
            {
                window.close();
            }
        }
        window.display();
    }

    return EXIT_SUCCESS;
}

我希望我的代码不会太乱。我还是新手

最佳答案

似乎如果您将 Tile 构造函数更改为采用 sf::Texture&const sf::Texture& 它会起作用,也许 sf::Texture 具有特殊的复制语义。

关于C++/SFML2 Sprite 没有显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12028854/

相关文章:

c++ - 创建一个包含要绘制的项目的类(段错误)

c# - 带有引用位 native dll 的托管包装器 dll 的 NuGet 包

c++ - C++ 模板参数中的三元表达式

html - 为什么这个图像 sprite 菜单不能正确显示,为什么链接不起作用?

css - 背景在 Sprite 图像中重复平铺 bgImage?

c++ - while循环困惑

C++/SDL 渲染文本

c++ - 几个函数的指针

c++ - Cocos2d-x 去除指定矩形区域的 Sprite

c++ - 比较 Sprite ?