我试图将我所有的纹理存储在一个数组中。但是图片没有返回,这似乎是我的代码
我假设我的 loadTex 和 loadSpri 函数会返回并将它们的数据存储在我创建的数组中,但它似乎并没有那样工作,因为当我运行游戏时它们没有出现, 只是空白。我的代码可能看起来很糟糕,但我仍然是初学者,正在努力掌握一切
这里是 render.cpp
#include "render.h"
texandsprite render::textures[5];
sf::Texture loadTex(std::string);
sf::Sprite loadSpri(sf::Texture);
//Function to start the game
void render::start()
{
_mainWindow.create(sf::VideoMode(1024, 768, 32), "A New Life");
textures[1].tex = loadTex("civilian.png");
textures[1].spri = loadSpri(textures[1].tex);
GameLoop();
}
void render::GameLoop()
{
sf::Event event;
//Load(1, "civilian.png");
float x = 0;
float y = 0;
while(_mainWindow.isOpen())
{
while (_mainWindow.pollEvent(event))
{
//Check the type of event
switch (event.type)
{
//window closed
case sf::Event::Closed:
_mainWindow.close();
break;
case sf::Event::KeyPressed:
switch(event.key.code)
{
case sf::Keyboard::Up:
--y;
break;
case sf::Keyboard::Down:
y += 5;
break;
case sf::Keyboard::Right:
++x;
break;
case sf::Keyboard::Left:
--x;
break;
}
break;
default:
break;
}
}
_mainWindow.clear(sf::Color::White);
//Draw everything here
//_mainWindow.draw(...);
textures[1].spri.setPosition(x,y);
_mainWindow.draw(textures[1].spri);
//End the current frame
_mainWindow.display();
}
}
sf::RenderWindow render::_mainWindow;
sf::Texture render::loadTex(std::string filename)
{
sf::Texture temptex;
if(!temptex.loadFromFile(filename))
{
std::cout << "Error: could not load image" << filename << std::endl;
}
std::cout << filename << " Loaded" << std::endl;
return temptex;
}
sf::Sprite render::loadSpri(sf::Texture temptex)
{
sf::Sprite tempspri;
tempspri.setTexture(temptex);
return tempspri;
}
这里是 render.h
#ifndef RENDER_H_INCLUDED
#define RENDER_H_INCLUDED
#include "SFML/Window.hpp"
#include "SFML/Graphics.hpp"
#include <iostream>
struct texandsprite
{
sf::Texture tex;
sf::Sprite spri;
};
class render
{
public:
static void start();
//Here will go a list of all moving entities
static texandsprite textures[5];
//Here will go a list of all nonmoving entities
//static
private:
static void GameLoop();
static void Rendering();
static sf::Texture loadTex(std::string);
static sf::Sprite loadSpri(sf::Texture);
static sf::Texture texture;
static sf::Sprite sprite;
static sf::RenderWindow _mainWindow;
};
#endif // RENDER_H_INCLUDED
最佳答案
sf::Sprite render::loadSpri(sf::Texture temptex) { sf::Sprite tempspri; tempspri.setTexture(temptex); return tempspri; }
与sf::Texture as class member doesn't work?关系密切.看看my answer there也是。
发生的情况是您的 sprite 设置为显示 temptex
但是当函数返回时该纹理被销毁,因为它是按值传递给函数的。
您可以通过使用对 sf::Texture
的引用来解决这个问题。
有关以下评论的更多详细信息。我稍微简化了您的情况和代码,以突出显示真正需要理解的重要内容。
像下面这样使用 loadTexture()
生成纹理很好,因为用户只能访问返回的拷贝——他不能将 Sprite 设置为使用原始纹理,因此他不会患有白方综合症。
sf::Texture loadTexture(std::string name)
{
sf::Texture ret;
if (!ret.loadFromFile(name)) { doSomethingAboutIt(); }
return ret;
}
关于 loadSprite()
函数,您可以如下使用 sf::Texture
的 const ref 来防止任何复制,从而防止白方 block 问题。
sf::Sprite loadSprite(sf::Texture const& tex)
{
sf::Sprite ret(tex);
return ret;
}
但是仍然存在一个陷阱:您必须小心如何存储 loadTexture()
返回的纹理!这个纹理的生命周期应该和你的 Sprite 的生命周期一样长。你上面的代码(和下面的引用)应该可以工作:
void render::start()
{
_mainWindow.create(sf::VideoMode(1024, 768, 32), "A New Life");
textures[1].tex = loadTexture("civilian.png");
textures[1].spri = loadSprite(textures[1].tex);
GameLoop();
}
关于C++ 无法返回 sf::Texture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19546372/