C ++有麻烦返回sf :: Texture [英] C++ having trouble returning sf::Texture

查看:132
本文介绍了C ++有麻烦返回sf :: Texture的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将所有纹理存储在数组中。但是图片没有被返回,这似乎是我的代码



我是在假设我的loadTex和loadSpri函数将返回并存储他们的数据在数组我做,但它似乎没有工作,因为当我运行游戏,他们不会出现,只是空白。我的代码可能看起来不错,但我仍然是初学者仍然,并试图得到的事情的悬念



这里是render.cpp

  #includerender.h



texandsprite render :: textures [5]


sf :: Texture loadTex(std :: string);
sf :: Sprite loadSpri(sf :: Texture);

//开始游戏的函数
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 ::事件事件;

// Load(1,civilian.png);
float x = 0;
float y = 0;

while(_mainWindow.isOpen())
{

while(_mainWindow.pollEvent(event))
{
//事件类型
switch(event.type)
{
//窗口关闭
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;


默认值:

break;
}

}

_mainWindow.clear(sf :: Color :: White);

//在这里绘制所有内容
// _ mainWindow.draw(...);
textures [1] .spri.setPosition(x,y);

_mainWindow.draw(textures [1] .spri);

//结束当前框架
_mainWindow.display();


}
}
sf :: RenderWindow render :: _ mainWindow;

sf :: Texture render :: loadTex(std :: string filename)
{
sf :: Texture temptex;

if(!temptex.loadFromFile(filename))
{
std :: cout< 错误:无法加载图片< filename<< std :: endl;
}
std :: cout<< filename<< 加载<< 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

#includeSFML / Window.hpp
#includeSFML / Graphics。 hpp
#include< iostream>
struct texandsprite
{
sf :: Texture tex;
sf :: Sprite spri;
};


class render
{
public:
static void start();
//这里将列出所有移动实体
static texandsprite textures [5];
//这里将列出所有非移动实体
// static

private:

static void GameLoop();
static void Rendering();
static sf :: Texture loadTex(std :: string);
static sf :: Sprite loadSpri(sf :: Texture);

static sf ::纹理纹理;
static sf :: Sprite sprite;
static sf :: RenderWindow _mainWindow;
};


#endif // RENDER_H_INCLUDED


解决方案


  sf :: Sprite render :: loadSpri(sf :: Texture temptex)
{
sf :: Sprite温普斯

tempspri.setTexture(temptex);

return tempspri;
}


http://stackoverflow.com/questions/19429155/sftexture-as-class-member-doesnt-work\">sf::纹理作为类成员不工作?。请查看我的答案



会发生什么是你的sprite被设置为显示 temptex ,但是当该函数返回时,



你可以通过使用 sf :: Texture 来解决这个问题。 p>




有关您在下面的评论的更多详情。



使用 loadTexture()生成纹理像下面一个是很好,因为只有返回的副本是用户可访问的 - 他不能设置一个精灵使用原始的纹理,因此他不会遭受白方块症状。

  sf :: Texture loadTexture(std :: string name)
{
sf :: Texture ret;
if(!ret.loadFromFile(name)){doSomethingAboutIt(); }
return ret;
}

关于 loadSprite()函数,可以使用const ref sf :: Texture ,如下所示,以防止任何副本,从而避免白色方形问题。

  sf :: Sprite loadSprite(sf :: Texture const& tex)
{
sf :: Sprite ret(tex);
return ret;
}

但还有一个缺陷:你必须小心如何存储由 loadTexture()返回的纹理!这个纹理的生命时间应该和你的精灵的生命时间一样长。您的代码(以下为引用)应该工作,然后:

  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();
}


I'm trying to store all my textures in an array. but the images aren't getting returned it would seem here's my code

I was under the assumption that my loadTex and loadSpri functions would return and store their data in the array that i made, but it doesn't seem to be working that way because when i run the game they don't show up, just white space. My code probably looks bad, but i'm quite the beginner still and trying to get the hang of things

Here's 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;
}

Here's 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;
}

It's very related to sf::Texture as class member doesn't work?. Have a look at my answer there too.

What happens is that your sprite is set to display the temptex but this texture is destroyed when the function returns since it is passed by value to the function.

You can fix this by using reference to sf::Texture instead.


More details regarding your comment below. I simplify a little bit your situation and code to highlight what is really important to understand.

Generating a texture with a loadTexture() like the following one is fine since only the returned copy is accessible to the user – he cannot set a sprite to use the original texture hence he won't suffer from the white square syndrome.

sf::Texture loadTexture(std::string name)
{
    sf::Texture ret;
    if (!ret.loadFromFile(name)) { doSomethingAboutIt(); }
    return ret;
}

Regarding the loadSprite() function, you can use a const ref to sf::Texture as follow to prevent any copy and thus the white square problem.

sf::Sprite loadSprite(sf::Texture const& tex)
{
    sf::Sprite ret(tex);
    return ret;
}

But there is still one pitfall: you have to be careful how you store the texture returned by loadTexture()! The life time of this texture should be as long as the life time of your sprite. Your code above (and below for reference) should work then:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆