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; }
SDL_Surface * BitmapHandler::loadBitmapFromLod(CLodHandler *lod, std::string fname, bool setKey) { if(!fname.size()) { tlog2 << "Call to loadBitmap with void fname!\n"; return NULL; } if (!lod->haveFile(fname, FILE_GRAPHICS)) { tlog2<<"Entry for file "<<fname<<" was not found"<<std::endl; return NULL; } SDL_Surface * ret=NULL; int size; unsigned char * file = 0; file = lod->giveFile(fname, FILE_GRAPHICS, &size); if (isPCX(file)) {//H3-style PCX CPCXConv cp; cp.openPCX((char*)file,size); ret = cp.getSurface(); if (!ret) tlog1<<"Failed to open "<<fname<<" as H3 PCX!\n"; 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 { //loading via SDL_Image std::string filename = lod->getFileName(fname, FILE_GRAPHICS); std::string ext; lod->convertName(filename, &ext); if (ext == ".TGA")//Special case - targa can't be loaded by IMG_Load_RW (no magic constants in header) { SDL_RWops *rw = SDL_RWFromMem((void*)file, size); ret = IMG_LoadTGA_RW( rw ); SDL_FreeRW(rw); } else ret = IMG_Load_RW( SDL_RWFromMem((void*)file, size), 1); if (!ret) tlog1<<"Failed to open "<<fname<<" via SDL_Image\n"; delete [] file; } return ret; }
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; }