Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我的sfml ImageManager类仅在c++;_C++_Sprite_Sfml - Fatal编程技术网

C++ 我的sfml ImageManager类仅在c++;

C++ 我的sfml ImageManager类仅在c++;,c++,sprite,sfml,C++,Sprite,Sfml,我知道这个话题在论坛上很丰富,但我需要在这个具体问题上的帮助。下面我将为一个tic tac toe游戏创建一个基本的sfml ImageManager类。但是我在理解如何在我的游戏引擎中实现这个类时遇到了困难。我将发布大量代码,因为涉及多个类: 我的井字游戏板 #pragma once #include "stdafx.h" #include <vector> #include "Box.h" #include "SFML/Graphics.hpp" #include "ImageM

我知道这个话题在论坛上很丰富,但我需要在这个具体问题上的帮助。下面我将为一个tic tac toe游戏创建一个基本的sfml ImageManager类。但是我在理解如何在我的游戏引擎中实现这个类时遇到了困难。我将发布大量代码,因为涉及多个类: 我的井字游戏板

#pragma once
#include "stdafx.h"
#include <vector>
#include "Box.h"
#include "SFML/Graphics.hpp"
#include "ImageManager.h"

class Board{
public:
Board();
~Board();

std::vector<Box> &GetBoxes();

sf::Sprite GetGameBoard();
private:
sf::Sprite gameBoard;

std::vector<Box> boxes;
};

我的窗口只显示了一个完整的白色背景,但它在其他方面仍然可以正常工作。有人能看出我做错了什么吗?

我假设
纹理
ImageManager
类的私人成员?在这种情况下,因为
ImageManager::GetImage
返回对存储在已销毁容器中的数据的引用(请记住
imgr
是构造函数中的局部变量)。

好的,我有它。这是一个相当尴尬的解决方案,但是在
板的构造函数中声明我的
ImageManager imgr
会立即破坏我的ImageManager,所以我只是剪切该实例并将其粘贴到头文件中。出于某种原因,返回一个引用而不是实际的纹理值是必要的,这也让我感到困惑。

确实是:
private:std::map textures\uz。所以应该
const-sf::Texture和GetImage(const-std::string和filename)刚好是
const sf::Texture GetImage(const std::string&filename)而不是?编辑:仅此一点并不能解决问题。还有什么需要改变的?@jburn7我对SFML没有太多经验(尤其是他们的
Sprint
类),但它似乎是一个可能的罪魁祸首。如果是,那么是的,按值返回将解决问题。好的,您对
std::map
的了解程度如何?这可能是问题所在:
sf::纹理;如果(texture.loadFromFile(filename)){//在贴图纹理中创建字符串和匹配图像\[filename]=texture;std::cout@jburn7这部分代码应该可以工作。实际上,您可以尝试将图像管理器设置为单例类,或者至少将
纹理
变量设置为
静态
。谢谢,我理解这个问题,但我不理解修复方法。我需要在哪里设置纹理的图像以避免丢失?
#include "stdafx.h"
#include "Board.h"
#include "SFML/Graphics.hpp"

Board::Board(){
ImageManager imgr;
imgr.AddResourceDirectory("images/");
gameBoard.setTexture(imgr.GetImage("images/t3board.png"));
}

Board::~Board(){

}

std::vector<Box> &Board::GetBoxes(){
return boxes;
}

sf::Sprite Board::GetGameBoard(){
return gameBoard;
}
const sf::Texture &ImageManager::GetImage(const std::string &filename){
    for(std::map<std::string, sf::Texture>::const_iterator it = textures_.begin(); it != textures_.end(); ++it){
        if(filename == it->first){
            std::cout << "DEBUG_MESSAGE: " << filename << " using existing image.\n";
            return it->second;
        }
    }

    //if image doesn't exist (no need for else since it will return if filename is found
    sf::Texture texture;
    if(texture.loadFromFile(filename)){
        //create both a string and matching image in the map
        textures_[filename] = texture;
        std::cout << "DEBUG_MESSAGE: " << filename << " loading image.\n";
        return textures_[filename];
    }

    // If the image has still not been found, search all registered directories
    //of course, it will be empty until I specify extra directories by adding them into the vector
    for(std::vector< std::string >::iterator it = resourceDirectories_.begin(); it != resourceDirectories_.end(); ++it){
        if(texture.loadFromFile((*it) + filename )){
            textures_[filename] = texture;
            std::cout << "DEBUG_MESSAGE: " << filename << " loading image 2.\n";
            return textures_[filename];
        }
    }

    //again, notice there's no elses because returns serve as breaks
    std::cout << "GAME_ERROR: Image was not found. It is filled with an empty image.\n";
    textures_[filename] = texture;
    return textures_[filename];
}
void Engine::Render(){
    window.clear();
    window.draw(board.GetGameBoard());
    window.display();
}