Example #1
0
Surface * ResourceManager::surface(std::string filename, int posX, int posY, unsigned int direction, unsigned int frame)
{
    if (_surfaces.find(filename) != _surfaces.end())
    {
        return _surfaces.at(filename);
    }
    
    std::string ext = filename.substr(filename.length() - 4);

    Surface * surface;
    
    if (ext == ".rix")
    {
        libfalltergeist::RixFileType* rix = rixFileType(filename);
        if (!rix) return 0;
        surface = new Surface(rix);
    }
    else if (ext == ".frm")
    {
        libfalltergeist::FrmFileType* frm = frmFileType(filename);
        if (!frm) return 0;
        surface = new Surface(frm, direction, frame);        
    }
    else
    {
        throw Exception("ResourceManager::surface() - unknow image type:" + filename);
    }
    
    surface->setX(posX);
    surface->setY(posY);

    _surfaces.insert(std::pair<std::string, Surface*>(filename, surface));
    return surface;
}
Graphics::Texture* ResourceManager::texture(const string& filename)
{
    if (_textures.count(filename))
    {
        return _textures.at(filename).get();
    }

    string ext = filename.substr(filename.length() - 4);

    Graphics::Texture* texture = nullptr;

    if (ext == ".png")
    {
        // @fixme: this section looks quite ugly. we should try to do something with it someday
        SDL_Surface* tempSurface = IMG_Load(string(CrossPlatform::findFalltergeistDataPath() + "/" +filename).c_str());
        if (tempSurface == NULL)
        {
            throw Exception("ResourceManager::texture(name) - cannot load texture from file " + filename + ": " + IMG_GetError());
        }

        SDL_PixelFormat* pixelFormat = SDL_AllocFormat(SDL_PIXELFORMAT_RGBA8888);
        SDL_Surface* tempSurface2 = SDL_ConvertSurface(tempSurface, pixelFormat, 0);
        texture = new Graphics::Texture(tempSurface2);

        SDL_FreeFormat(pixelFormat);
        SDL_FreeSurface(tempSurface);
        SDL_FreeSurface(tempSurface2);

    }
    else if (ext == ".rix")
    {
        auto rix = rixFileType(filename);
        if (!rix) return nullptr;
        texture = new Graphics::Texture(rix->width(), rix->height());
        texture->loadFromRGBA(rix->rgba());
    }
    else if (ext == ".frm")
    {
        auto frm = frmFileType(filename);
        if (!frm) return nullptr;
        texture = new Graphics::Texture(frm->width(), frm->height());
        texture->loadFromRGBA(frm->rgba(palFileType("color.pal")));
        texture->setMask(*frm->mask(palFileType("color.pal")));
    }
    else
    {
        throw Exception("ResourceManager::surface() - unknown image type:" + filename);
    }

    _textures.insert(make_pair(filename, unique_ptr<Graphics::Texture>(texture)));
    return texture;
}
Example #3
0
libfalltergeist::FrmFileType * ResourceManager::frmFileType(unsigned int FID)
{
    std::string prefix;
    std::string lstFile;
    switch(FID >> 24)
    {
        case libfalltergeist::FrmFileType::TYPE_ITEM:
            prefix = "art/items/";
            lstFile = "items.lst";
            break;
        case libfalltergeist::FrmFileType::TYPE_CRITTER:
            prefix = "art/critters/";
            lstFile = "critters.lst";
            break;
        case libfalltergeist::FrmFileType::TYPE_SCENERY:
            prefix = "art/scenery/";
            lstFile = "scenery.lst";
            break;
        case libfalltergeist::FrmFileType::TYPE_WALL:
            prefix = "art/walls/";
            lstFile = "walls.lst";
            break;
        case libfalltergeist::FrmFileType::TYPE_TILE:
            prefix = "art/tiles/";
            lstFile = "tiles.lst";
            break;
        case libfalltergeist::FrmFileType::TYPE_BACKGROUND:
            prefix = "art/backgrnd/";
            lstFile = "backgrnd.lst";
            break;
        case libfalltergeist::FrmFileType::TYPE_INTERFACE:
            prefix = "art/intrface/";
            lstFile = "intrface.lst";
            break;
        case libfalltergeist::FrmFileType::TYPE_INVENTORY:
            prefix = "art/inven/";
            lstFile = "inven.lst";
            break;
    }
    libfalltergeist::LstFileType * lst = lstFileType(prefix + lstFile);
    unsigned int frmId = 0x0000FFFF & FID;

    if (frmId >= lst->strings()->size())
    {
        return 0;
    }

    return frmFileType(prefix + lst->strings()->at(frmId));
}
libfalltergeist::Frm::File* ResourceManager::frmFileType(unsigned int FID)
{
    if (FIDtoFrmName(FID) == "") return 0;
    return frmFileType(FIDtoFrmName(FID));
}