put texture an id?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Virion
Competition winner
Posts: 2148
Joined: Mon Dec 18, 2006 5:04 am

put texture an id?

Post by Virion »

Any idea how to set an id for the texture loaded with:

Code: Select all

video::ITexture* images = driver->getTexture("../../media/2ddemo.bmp");
I wanna know if this is possible.
Thx.
AaronA
Posts: 55
Joined: Tue Sep 12, 2006 1:31 am

Post by AaronA »

I actually built a similar naming system for media with SDL, so it looked something like:

Code: Select all

class->loadMedia("Name of Media");
Of course, I made a function to call it by an ID (int) as well; the code should be easily translatable...

Code: Select all

#define LVIDEO_MAX_SURFACES 100

class LVideoManager
{
    public:
        void loadImage(const char *, const char *); // Load an image (name, filename)
        SDL_Surface * getImage(const char *); // Get an image by name
        SDL_Surface * getImage(int); // Get an image by ID
        void drawImage(const char *, int, int, int); // Draw an image, x, y, alpha
    private:
        SDL_Surface * LVideoSurfaceIndex[LVIDEO_MAX_SURFACES]; // Where surfaces are stored
        const char * LVideoSurfaceNames[LVIDEO_MAX_SURFACES]; // Names for our surfaces.
        int LVideoSurfaceIndexer; // Surface indexer for ids.
};

// ---- IMPLEMENTATIONS ----
void LVideoManager::loadImage(const char * LTitle, const char * LFilename)
{
    LVideoSurfaceIndex[LVideoSurfaceIndexer] = IMG_Load(LFilename);
    if (!LVideoSurfaceIndex[LVideoSurfaceIndexer])
    {
        printf("%s could not be loaded from %s\n", LTitle, LFilename);
        return;
    }
    LVideoSurfaceNames[LVideoSurfaceIndexer] = LTitle;
    LVideoSurfaceIndexer++;
    return;
}

SDL_Surface * LVideoManager::getImage(int LID)
{
    return LVideoSurfaceIndex[LID];
}

SDL_Surface * LVideoManager::getImage(const char * LTitle)
{
    for (int i = 0; i < LVIDEO_MAX_SURFACES; i++)
    {
        if (LVideoSurfaceNames[i] == LTitle)
        {
            return LVideoSurfaceIndex[i];
        }
    }

    printf("The image titled %s was not found.", LTitle);
    return 0;
}

void LVideoManager::drawImage(const char * LName, int LX, int LY, int LA = 255)
{
    SDL_Rect LSurfaceDestination = { LX, LY, 0, 0 };
    SDL_SetAlpha(getImage(LName), SDL_SRCALPHA, LA);
    SDL_BlitSurface(getImage(LName), NULL, LVideoScreen, &LSurfaceDestination);
}
Again, you'll see SDL code for drawing and images (SDL_Surface, etc) - but you can see the naming system (and id system) is just based on 3 variables...

LVideoSurfaces[max surfaces]; // Holds the images/textures
LVideoSurfaceNames[max surfaces]; // Holds the names of each image/texture
LvideoSurfaceIndexer (int); // Just tells us which slot to fill next.
Post Reply