2013-05-23 2 views
0

타일 엔진 제작에 관한 약간 오래된 튜토리얼을 따르려고합니다.텍스처가 화면에 그려지지 않습니다.

문제는 화면에 그리려는 텍스처가 나타나지 않는다는 것입니다. 방금 검은 색 화면이 나타납니다.

나는 Engine.cpp의 대부분의 관련 부분 촬영했습니다 :

bool Engine::Init() 
{ 
    LoadTextures(); 
    window = new sf::RenderWindow(sf::VideoMode(800,600,32), "RPG"); 
    if(!window) 
     return false; 

    return true; 
} 

void Engine::LoadTextures() 
{ 
    sf::Texture sprite; 
    sprite.loadFromFile("C:\\Users\\Vipar\\Pictures\\sprite1.png"); 
    textureManager.AddTexture(sprite); 
    testTile = new Tile(textureManager.GetTexture(0)); 
} 

void Engine::RenderFrame() 
{ 
    window->clear(); 
    testTile->Draw(0,0,window); 
    window->display(); 
} 

void Engine::MainLoop() 
{ 
    //Loop until our window is closed 
    while(window->isOpen()) 
    { 
     ProcessInput(); 
     Update(); 
     RenderFrame(); 
    } 
} 

void Engine::Go() 
{ 
    if(!Init()) 
     throw "Could not initialize Engine"; 
    MainLoop(); 
} 

을 그리고 여기있는 TextureManager.cpp

#include "TextureManager.h" 
#include <vector> 
#include <SFML\Graphics.hpp> 

TextureManager::TextureManager() 
{ 

} 

TextureManager::~TextureManager() 
{ 

} 

void TextureManager::AddTexture(sf::Texture& texture) 
{ 
    textureList.push_back(texture); 
} 

sf::Texture& TextureManager::GetTexture(int index) 
{ 
    return textureList[index]; 
} 

를 튜토리얼에서는 자체는 Image 유형이 사용되었지만이 있었다 no Draw()Image에 대한 방법이므로 대신 Texture을 만들었습니다. Texture이 화면에 왜 렌더링되지 않습니까?

void Engine::LoadTextures() 
{ 
    sf::Texture sprite; 
    sprite.loadFromFile("C:\\Users\\Vipar\\Pictures\\sprite1.png"); 
    textureManager.AddTexture(sprite); 
    testTile = new Tile(textureManager.GetTexture(0)); 
} 

로컬 sf::Texture를 작성하고 TextureManager::AddTexture에 있음을 통과 :

+0

이 문제는 아직 해결되지 않았습니다. – OmniOwl

답변

1

문제가 될 것으로 보인다. 함수의 끝 부분에서 범위를 벗어날 가능성이 높습니다. 그러면 그릴 때 더 이상 유효하지 않습니다. 당신은 스마트 포인터를 사용하여이 문제를 해결 :

void Engine::LoadTextures() 
{ 
    textureManager.AddTexture(std::shared_ptr<sf::Texture>(
     new sf::Texture("C:\\Users\\Vipar\\Pictures\\sprite1.png"))); 

    testTile = new Tile(textureManager.GetTexture(0)); 
} 

그리고 TextureManager을 변경하면이 기능을 사용하려면

void TextureManager::AddTexture(std::shared_ptr<sf::Texture> texture) 
{ 
    textureList.push_back(texture); 
} 

sf::Texture& TextureManager::GetTexture(int index) 
{ 
    return *textureList[index]; 
} 

당신은 물론의 std::vector<std::shared_ptr<sf::Texture>textureList을 변경해야합니다.

+0

솔루션에 대해 무엇을 제안 하시겠습니까? 스프라이트를 전역으로 만드는 것은 전혀 효율적이지 못합니다. 아이디어는 참조로 전달하는 것입니다. 그래서 우리는 정보로 수백만 개의 객체를 참조하지 않고 그림 만 참조하도록합니다. – OmniOwl

+0

나는 최선의 방법은 아마 포인터를 사용하고 주위에 전달하는 것 같아요. 그래도 청소를해야합니다. (나는 스마트 포인터가 도움이 될 수 있다고 생각하지만 아직 그들과 놀지는 못했다.) – mwerschy

+0

답안에서 어쩌면 그렇게 할 수 있는지 예를 들어 주시겠습니까? 나는 아직도 약간 녹색이다 :) – OmniOwl

관련 문제