我知道這個主題在論壇中很豐富,但我需要幫助解決這個特定的問題。我正在關注this tutorial,爲tic tac toe遊戲製作一個基本的sfml ImageManager類。我無法理解如何在我的遊戲引擎中實現這個類。我會發布很多代碼,因爲涉及多個類: Board.h:我的井字遊戲板。我的sfml ImageManager類僅在C++中顯示白色方塊
#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;
};
而這裏的Board.cpp:
#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;
}
這裏是是從教程的ImageManager類相關:
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];
}
最後,在我的引擎,我得到gameBoard
和借鑑它到我的窗口:
void Engine::Render(){
window.clear();
window.draw(board.GetGameBoard());
window.display();
}
我的窗口只顯示一個完整的白色背景,但它仍然是功能,否則。任何人都可以看到我做錯了什麼嗎?
http://stackoverflow.com/a/19434823/520217 – Hiura 2014-09-02 15:42:35
謝謝,我明白這個問題,但我不明白這個問題。我需要在哪裏設置紋理的圖像,使其不會丟失? – jburn7 2014-09-02 21:36:25