Esempio n. 1
0
SDL_Surface * BitmapHandler::loadBitmapFromDir(std::string path, std::string fname, bool setKey)
{
    if(!fname.size())
    {
        logGlobal->warnStream() << "Call to loadBitmap with void fname!";
        return NULL;
    }
    if (!CResourceHandler::get()->existsResource(ResourceID(path + fname, EResType::IMAGE)))
    {
        return nullptr;
    }

    SDL_Surface * ret=NULL;

    auto readFile = CResourceHandler::get()->loadData(
                        ResourceID(path + fname, EResType::IMAGE));


    if (isPCX(readFile.first.get()))
    {   //H3-style PCX
        ret = loadH3PCX(readFile.first.get(), readFile.second);
        if (ret)
        {
            if(ret->format->BytesPerPixel == 1  &&  setKey)
            {
                const SDL_Color &c = ret->format->palette->colors[0];
                SDL_SetColorKey(ret,SDL_SRCCOLORKEY,SDL_MapRGB(ret->format, c.r, c.g, c.b));
            }
        }
        else
            logGlobal->errorStream()<<"Failed to open "<<fname<<" as H3 PCX!";
    }
    else
    {   //loading via SDL_Image
        CFileInfo info(CResourceHandler::get()->getResourceName(ResourceID(path + fname, EResType::IMAGE)));

        ret = IMG_LoadTyped_RW(
                  //create SDL_RW with our data (will be deleted by SDL)
                  SDL_RWFromConstMem((void*)readFile.first.get(), readFile.second),
                  1, // mark it for auto-deleting
                  &info.getExtension()[0] + 1); //pass extension without dot (+1 character)
        if (ret)
        {
            if (ret->format->palette)
            {
                //set correct value for alpha\unused channel
                for (int i=0; i< ret->format->palette->ncolors; i++)
                    ret->format->palette->colors[i].unused = 255;
            }
        }
        else
        {
            logGlobal->errorStream()<<"Failed to open "<<fname<<" via SDL_Image";
        }
    }
    SDL_SetColorKey(ret, SDL_SRCCOLORKEY, SDL_MapRGB(ret->format, 0, 255, 255));
    return ret;
}
Esempio n. 2
0
SDL_Surface * BitmapHandler::loadBitmapFromDir(std::string path, std::string fname, bool setKey)
{
    if(!fname.size())
    {
        logGlobal->warnStream() << "Call to loadBitmap with void fname!";
        return nullptr;
    }
    if (!CResourceHandler::get()->existsResource(ResourceID(path + fname, EResType::IMAGE)))
    {
        return nullptr;
    }

    SDL_Surface * ret=nullptr;

    auto readFile = CResourceHandler::get()->load(ResourceID(path + fname, EResType::IMAGE))->readAll();

    if (isPCX(readFile.first.get()))
    {   //H3-style PCX
        ret = loadH3PCX(readFile.first.get(), readFile.second);
        if (ret)
        {
            if(ret->format->BytesPerPixel == 1  &&  setKey)
            {
                const SDL_Color &c = ret->format->palette->colors[0];
                SDL_SetColorKey(ret,SDL_SRCCOLORKEY,SDL_MapRGB(ret->format, c.r, c.g, c.b));
            }
        }
        else
            logGlobal->errorStream()<<"Failed to open "<<fname<<" as H3 PCX!";
    }
    else
    {   //loading via SDL_Image
        ret = IMG_Load_RW(
                  //create SDL_RW with our data (will be deleted by SDL)
                  SDL_RWFromConstMem((void*)readFile.first.get(), readFile.second),
                  1); // mark it for auto-deleting
        if (ret)
        {
            if (ret->format->palette)
            {
                //set correct value for alpha\unused channel
                for (int i=0; i< ret->format->palette->ncolors; i++)
                    ret->format->palette->colors[i].unused = 255;
            }
        }
        else
        {
            logGlobal->errorStream()<<"Failed to open "<<fname<<" via SDL_Image";
            logGlobal->errorStream()<<"Reason: " << IMG_GetError();
        }
    }

    // When modifyin anything here please check two use cases:
    // 1) Vampire mansion in Necropolis (not 1st color is transparent)
    // 2) Battle background when fighting on grass/dirt, topmost sky part (NO transparent color)
    // 3) New objects that may use 24-bit images for icons (e.g. witchking arts)
    auto colorID = SDL_MapRGB(ret->format, 0, 255, 255);

    if (ret->format->palette)
    {
        auto & color = ret->format->palette->colors[colorID];

        // set color key only if exactly such color was found
        if (color.r == 0 && color.g == 255 && color.b == 255)
            SDL_SetColorKey(ret, SDL_SRCCOLORKEY, colorID);
    }
    else // always set
    {
        SDL_SetColorKey(ret, SDL_SRCCOLORKEY, colorID);
    }
    return ret;
}