window 上没有显示 SFML Sprite,但它没有给我任何错误

SFML Sprite isn't showing on the window but it isn't giving me any errors

我正在使用 SFML 创建一个用于练习的小游戏,这是我小时候在某种类型的控制台上玩过的旧坦克游戏的复制品。

我在尝试绘制播放器时遇到了一个奇怪的事情。我说 st运行ge 是因为一开始它显示出来了,所以我不认为我的函数有什么问题,但是在我为敌方坦克做了一个派生的 class 并做了一个新的为此,我在将所有文件链接在一起时遇到了一些嵌套问题(5 个文件,1 个 cpp 和 4 headers)。在我弄明白之后,我 运行 陷入了这个问题,但找不到任何解决方案。它在那个问题之前是有效的,现在它不再有效了。

我检查了纹理,如果它正在加载,我设置的位置,它们都可以。

这是玩家坦克class

class tank{

protected:
sf::FloatRect boundingBox;
sf::Sprite tankSprite;

bullet *mBullet;
public :
tank(); // constructor

bullet * get_ptr(){return mBullet;}
sf::FloatRect get_boundingBox(){return boundingBox;}

void setRotation(float);
void show(){game.draw(tankSprite);}
void update();
void shoot(){mBullet = new bullet(tankSprite);}
};

这是我更新世界并利用 window

的主要代码
if(!stop)
    Me.update();

if(Me.get_ptr()->isFired()==true)
    Me.get_ptr()->update();

// Output on the window
game.clear();
Me.show();
if(Me.get_ptr()->isFired()==true)
    Me.get_ptr()->show();
game.display();

旁注:游戏是渲染器 window 并且是全局声明的(我知道这是不好的做法,我正在慢慢改掉这个坏习惯)

main.cpp

#include <SFML/Graphics.hpp>
#include "Tank.h"

int main()
{
tank Me;

init();
while (game.isOpen())
{
    sf::Event event;
    while (game.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
            game.close();
        else if (event.type == sf::Event::KeyPressed)
        {
            stop=false;
            switch(event.key.code)
            {
                case sf::Keyboard::W:
                {
                    Me.setRotation(0);
                    break;
                }
                case sf::Keyboard::D:
                {
                    Me.setRotation(90);
                    break;
                }
                case sf::Keyboard::S:
                {
                    Me.setRotation(180);
                    break;
                }
                case sf::Keyboard::A:
                {
                    Me.setRotation(270);
                    break;
                }
                case sf::Keyboard::Escape:
                {
                    game.close();
                    break;
                }
                case sf::Keyboard::Space:
                {
                    if(Me.get_ptr()->isFired()==false)Me.shoot();
                    break;
                }
                case sf::Keyboard::LControl:
                {
                    stop=true;
                    break;
                }
                default:
                    break;
            }
        }
    }

    if(!stop)
        Me.update();

    if(Me.get_ptr()->isFired()==true)
        Me.get_ptr()->update();

    // Output on the window
    game.clear();
    Me.show();
    if(Me.get_ptr()->isFired()==true)
        Me.get_ptr()->show();
    game.display();
}
return 0;
}

init.h

#include<iostream>

bool stop;

sf::RenderWindow game(sf::VideoMode(400, 400), "SFML");

sf::Texture myTankTexture;
sf::Texture bulletTexture;

void init(){

srand(time(NULL));

stop = true;

game.setVerticalSyncEnabled(true);
game.setFramerateLimit(60);

if(!myTankTexture.loadFromFile("tank.jpg"))
{
    std::cout<<"eroare la textura tank"<<std::endl;
}
myTankTexture.setSmooth(true);

if(!bulletTexture.loadFromFile("bullet.jpg"))
{
    std::cout<<"bullet texture error"<<std::endl;
}
bulletTexture.setSmooth(true);
}

sf::Vector2f rotationToDirection(int rotation){

sf::Vector2f dir;
switch (rotation)
{
    case 0:
    {
        dir.x=0.0;
        dir.y=-1.0;
        break;
    }
    case 90:
    {
        dir.x=1.0;
        dir.y=0.0;
        break;
    }
    case 180:
    {
        dir.x=0.0;
        dir.y=1.0;
        break;
    }
    case 270:
    {
        dir.x=-1.0;
        dir.y=0.0;
        break;
    }
}
return dir;
}

bullet.h

#include "init.h"

class bullet{

protected:
sf::Sprite bulletSprite;
sf::FloatRect boundingBox;
bool isBFired = false;
public:
bullet(sf::Sprite); // constructor
~bullet(){isBFired=false;}

sf::FloatRect get_boundingBox(){return boundingBox;}
bool isFired(){if(isBFired)return true;else return false;}
int collision();

void del(){delete this;}
void update();
void show(){game.draw(bulletSprite);}
};

bullet::bullet(sf::Sprite sprite){

isBFired = true;

bulletSprite.setTexture(bulletTexture);
bulletSprite.setOrigin(2.5,5.0);
bulletSprite.setRotation(sprite.getRotation());
     bulletSprite.setPosition(sprite.getPosition().x+rotationToDirection(bulletSprite.getRotation()).x*5.0,sprite.getPosition().y+rotationToDirection(bulletSprite.getRotation()).y*5.0);

boundingBox = bulletSprite.getLocalBounds();
}

int bullet::collision(){

if(bulletSprite.getPosition().x < 0
   || bulletSprite.getPosition().x > 400
   || bulletSprite.getPosition().y > 400
   || bulletSprite.getPosition().y < 0 )return 1;
else
    return 0;
}

void bullet::update(){

           bulletSprite.move(rotationToDirection(bulletSprite.getRotation()).x*6.0,rotationToDirection(bulletSprite.getRotation()).y*6.0);

if(collision()==1)
    delete this;
else
    boundingBox = bulletSprite.getLocalBounds();
}

tank.h

#include "bullet.h"

class tank{

protected:
sf::FloatRect boundingBox;
sf::Sprite tankSprite;

bullet *mBullet;
public :
tank(); // constructor

bullet * get_ptr(){return mBullet;}
sf::FloatRect get_boundingBox(){return boundingBox;}

void setRotation(float);
void show(){game.draw(tankSprite);}
void update();
void shoot(){mBullet = new bullet(tankSprite);}
};

tank::tank(){

tankSprite.setTexture(myTankTexture);
tankSprite.setOrigin(20,20);
tankSprite.setRotation(0);
tankSprite.setPosition(200,200);

boundingBox = tankSprite.getLocalBounds();
}

void tank::update(){

    tankSprite.move(rotationToDirection(tankSprite.getRotation()).x*3,rotationToDirection(tankSprite.getRotation()).y*3);

boundingBox = tankSprite.getLocalBounds();
}

void tank::setRotation(float rotation){

tankSprite.setRotation(rotation);
}

据我所知,您没有设置纹理矩形,看看这是否对您有帮助。当你设置你的坦克精灵时尝试:

tankSprite.setTexture(myTankTexture, true);

传递 true 会将您的纹理矩形重置为精灵的大小,请参阅 SFML docs 了解更多信息。

此外,您在调用 init() 之前创建 Tank class,myTankTexture 将为空,因此 tank 构造函数将使用它,稍后加载您的纹理。

正如他们用您的语言所说的那样,"multa bafta" :)