예제 #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;
}