1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00
vcmi/client/CBitmapHandler.cpp

194 lines
4.9 KiB
C++
Raw Normal View History

#include "StdInc.h"
#include "../lib/filesystem/Filesystem.h"
2013-04-07 13:48:07 +03:00
#include "../lib/filesystem/CFileInfo.h"
2008-06-12 09:45:51 +03:00
#include "SDL.h"
#include "SDL_image.h"
#include "CBitmapHandler.h"
#include "CDefHandler.h"
#include "gui/SDL_Extensions.h"
#include "../lib/vcmi_endian.h"
/*
* CBitmapHandler.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
bool isPCX(const ui8 *header)//check whether file can be PCX according to header
2008-06-12 09:45:51 +03:00
{
int fSize = read_le_u32(header + 0);
int width = read_le_u32(header + 4);
int height = read_le_u32(header + 8);
return fSize == width*height || fSize == width*height*3;
2008-06-12 09:45:51 +03:00
}
enum Epcxformat
2008-06-12 09:45:51 +03:00
{
PCX8B,
PCX24B
};
2008-06-12 09:45:51 +03:00
SDL_Surface * BitmapHandler::loadH3PCX(ui8 * pcx, size_t size)
2008-06-12 09:45:51 +03:00
{
SDL_Surface * ret;
Epcxformat format;
int it=0;
ui32 fSize = read_le_u32(pcx + it); it+=4;
ui32 width = read_le_u32(pcx + it); it+=4;
ui32 height = read_le_u32(pcx + it); it+=4;
2009-02-14 16:37:13 +02:00
if (fSize==width*height*3)
2008-06-12 09:45:51 +03:00
format=PCX24B;
2011-10-08 08:44:37 +03:00
else if (fSize==width*height)
2008-06-12 09:45:51 +03:00
format=PCX8B;
else
return nullptr;
2011-10-08 08:44:37 +03:00
2008-06-12 09:45:51 +03:00
if (format==PCX8B)
{
ret = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 8, 0, 0, 0, 0);
it = 0xC;
for (int i=0; i<height; i++)
{
memcpy((char*)ret->pixels + ret->pitch * i, pcx + it, width);
it+= width;
}
2011-10-08 08:44:37 +03:00
//palette - last 256*3 bytes
it = size-256*3;
2008-06-12 09:45:51 +03:00
for (int i=0;i<256;i++)
{
SDL_Color tp;
tp.r = pcx[it++];
tp.g = pcx[it++];
tp.b = pcx[it++];
CSDL_Ext::colorSetAlpha(tp,SDL_ALPHA_OPAQUE);
ret->format->palette->colors[i] = tp;
2008-06-12 09:45:51 +03:00
}
}
else
{
#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
int bmask = 0xff0000;
int gmask = 0x00ff00;
int rmask = 0x0000ff;
#else
int bmask = 0x0000ff;
int gmask = 0x00ff00;
int rmask = 0xff0000;
#endif
ret = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 24, rmask, gmask, bmask, 0);
2011-10-08 08:44:37 +03:00
//it == 0xC;
for (int i=0; i<height; i++)
2008-06-12 09:45:51 +03:00
{
memcpy((char*)ret->pixels + ret->pitch * i, pcx + it, width*3);
it+= width*3;
2008-06-12 09:45:51 +03:00
}
2008-06-12 09:45:51 +03:00
}
return ret;
}
SDL_Surface * BitmapHandler::loadBitmapFromDir(std::string path, std::string fname, bool setKey)
2008-06-12 09:45:51 +03:00
{
if(!fname.size())
2009-02-14 16:37:13 +02:00
{
logGlobal->warnStream() << "Call to loadBitmap with void fname!";
return nullptr;
2009-02-14 16:37:13 +02:00
}
if (!CResourceHandler::get()->existsResource(ResourceID(path + fname, EResType::IMAGE)))
2008-06-12 09:45:51 +03:00
{
return nullptr;
2008-06-12 09:45:51 +03:00
}
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)
2008-06-12 09:45:51 +03:00
{
if(ret->format->BytesPerPixel == 1 && setKey)
{
const SDL_Color &c = ret->format->palette->colors[0];
#ifdef VCMI_SDL1
uint32_t key = SDL_MapRGB(ret->format, c.r, c.g, c.b);
#else
uint32_t key = SDL_MapRGBA(ret->format, c.r, c.g, c.b, c.a);
#endif
SDL_SetColorKey(ret,SDL_SRCCOLORKEY,key);
}
2008-06-12 09:45:51 +03:00
}
else
{
logGlobal->errorStream()<<"Failed to open "<<fname<<" as H3 PCX!";
return nullptr;
}
2008-06-12 09:45:51 +03:00
}
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++)
CSDL_Ext::colorSetAlpha(ret->format->palette->colors[i],SDL_ALPHA_OPAQUE);
}
}
else
{
logGlobal->errorStream()<<"Failed to open "<<fname<<" via SDL_Image";
logGlobal->errorStream()<<"Reason: " << IMG_GetError();
return nullptr;
}
}
// 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)
2013-05-31 14:46:19 +03:00
// 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);
}
2013-05-31 14:46:19 +03:00
else // always set
{
SDL_SetColorKey(ret, SDL_SRCCOLORKEY, colorID);
}
return ret;
}
SDL_Surface * BitmapHandler::loadBitmap(std::string fname, bool setKey)
{
SDL_Surface *bitmap;
if (!(bitmap = loadBitmapFromDir("DATA/", fname, setKey)) &&
!(bitmap = loadBitmapFromDir("SPRITES/", fname, setKey)))
logGlobal->errorStream()<<"Error: Failed to find file "<<fname;
return bitmap;
}