Prikaz jedne poruke
Stara 6.4.2012, 14:04   #74
Andross
Kekule Mekule
 
Avatar korisnika Andross
 
Član od: 8.12.2005.
Lokacija: Beograd
Poruke: 4.134
Zahvalnice: 649
Zahvaljeno 1.354 puta na 693 poruka
Slanje poruke preko Skypea korisniku Andross
Određen forumom Re: How to... C++

Resio sam to ovako (doduse ovo bi trebalo teoretski da radi, jos uvek nemam kod koji bi ovo testirao):

Kod:
#ifndef TEXTUREMANAGER_HPP_INCLUDED
#define TEXTUREMANAGER_HPP_INCLUDED

#include <IL/il.h>
#include <string>
#include <unordered_map>
#include "Texture.hpp"

class TextureManager
{
public:
    // Destructor
    ~TextureManager();

    // Public methods
    static bool Initialize();
    static void Shutdown();

    bool LoadTexture(std::string filename);
    void UnloadTexture(std::string filename);
    Texture* GetByFilename(std::string filename);

    static TextureManager* GetInstance() { return mInstance; }

private:
    // Member variables
    std::unordered_map<std::string, Texture*> mTextures;

    static TextureManager* mInstance;
};

#endif // TEXTUREMANAGER_HPP_INCLUDED
Kod:
#include "TextureManager.hpp"

// Initialization of static members
TextureManager* TextureManager::mInstance = nullptr;

// Destructor
TextureManager::~TextureManager()
{
    // Delete all textures
    for(auto iter = begin(mTextures); iter != end(mTextures); iter++)
        delete (*iter).second;
}

// Method implementation
bool TextureManager::Initialize()
{
    // Abort if instance is already created
    if(mInstance != nullptr)
        return false;

    // Create the instance
    mInstance = new TextureManager();

    // Initialize DevIL
    ilInit();

    // Check the version of the library
    if(ilGetInteger(IL_VERSION_NUM) < IL_VERSION)
        return false;

    return true;
}

void TextureManager::Shutdown()
{
    // Delete the instance
    delete mInstance;
    mInstance = nullptr;
}

bool TextureManager::LoadTexture(std::string filename)
{
    // Check if the filename is empty string
    if(filename.empty())
        return false;

    // Texture is already loaded
    if(mTextures.find(filename) != mTextures.end())
        return true;

    // Generate one image name
    ILuint texId;
    ilGenImages(1, &texId);

    // Bind the image name
    ilBindImage(texId);

    // Try to load the image
    if(!ilLoadImage(filename.c_str()))
        return false;

    // Get the width and height
    int width = ilGetInteger(IL_IMAGE_WIDTH);
    int height = ilGetInteger(IL_IMAGE_HEIGHT);

    // Create our texture
    Texture* texture = new Texture(width, height, filename);

    // Copy pixel data into our texture
    ilCopyPixels(0, 0, 0, width, height, 1, IL_RGB, IL_UNSIGNED_BYTE, texture->GetData());

    // Unbind the image and delete its data
    ilBindImage(0);
    ilDeleteImage(texId);

    // Store our texture
    mTextures[filename] = texture;

    return true;
}

void TextureManager::UnloadTexture(std::string filename)
{
    // Try to find the texture
    auto iter = mTextures.find(filename);

    // Return if texture is not found
    if(iter == mTextures.end())
        return;

    // Delete the texture and remove it
    delete (*iter).second;
    mTextures.erase(iter);
}

Texture* TextureManager::GetByFilename(std::string filename)
{
    // Try to find the texture
    auto iter = mTextures.find(filename);

    // Return nullptr if texture is not found
    if(iter == mTextures.end())
        return nullptr;

    return (*iter).second;
}
Andross je offline   Odgovor sa citatom ove poruke