2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
#include "../lib/filesystem/Filesystem.h"
|
2008-06-12 09:45:51 +03:00
|
|
|
#include "SDL.h"
|
|
|
|
#include "SDL_image.h"
|
|
|
|
#include "CBitmapHandler.h"
|
2010-12-20 23:22:53 +02:00
|
|
|
#include "CDefHandler.h"
|
2013-04-07 14:52:07 +03:00
|
|
|
#include "gui/SDL_Extensions.h"
|
2011-10-08 04:23:46 +03:00
|
|
|
#include "../lib/vcmi_endian.h"
|
2009-04-15 17:03:31 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-08-01 15:02:54 +03:00
|
|
|
bool isPCX(const ui8 *header)//check whether file can be PCX according to header
|
2008-06-12 09:45:51 +03:00
|
|
|
{
|
2012-08-01 15:02:54 +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
|
|
|
}
|
2009-09-27 14:37:15 +03:00
|
|
|
|
2012-08-01 15:02:54 +03:00
|
|
|
enum Epcxformat
|
2008-06-12 09:45:51 +03:00
|
|
|
{
|
2012-08-01 15:02:54 +03:00
|
|
|
PCX8B,
|
|
|
|
PCX24B
|
|
|
|
};
|
2008-06-12 09:45:51 +03:00
|
|
|
|
2012-08-01 15:02:54 +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;
|
|
|
|
|
2012-08-01 15:02:54 +03:00
|
|
|
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;
|
2012-08-01 15:02:54 +03:00
|
|
|
else
|
|
|
|
return nullptr;
|
2011-10-08 08:44:37 +03:00
|
|
|
|
2008-06-12 09:45:51 +03:00
|
|
|
if (format==PCX8B)
|
|
|
|
{
|
2011-11-25 22:41:23 +03:00
|
|
|
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
|
|
|
|
2012-08-01 15:02:54 +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++];
|
2015-09-02 17:49:29 +02:00
|
|
|
tp.a = SDL_ALPHA_OPAQUE;
|
2011-11-25 22:41:23 +03:00
|
|
|
ret->format->palette->colors[i] = tp;
|
2008-06-12 09:45:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-06 10:34:37 +03:00
|
|
|
#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
|
|
|
|
2012-08-01 15:02:54 +03:00
|
|
|
//it == 0xC;
|
2011-11-25 22:41:23 +03:00
|
|
|
for (int i=0; i<height; i++)
|
2008-06-12 09:45:51 +03:00
|
|
|
{
|
2011-11-25 22:41:23 +03:00
|
|
|
memcpy((char*)ret->pixels + ret->pitch * i, pcx + it, width*3);
|
|
|
|
it+= width*3;
|
2008-06-12 09:45:51 +03:00
|
|
|
}
|
2011-11-25 22:41:23 +03:00
|
|
|
|
2008-06-12 09:45:51 +03:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-08-01 15:02:54 +03:00
|
|
|
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
|
|
|
{
|
2016-03-12 03:41:27 +02:00
|
|
|
logGlobal->warnStream() << "Call to loadBitmap with void fname!";
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2009-02-14 16:37:13 +02:00
|
|
|
}
|
2012-08-01 15:02:54 +03:00
|
|
|
if (!CResourceHandler::get()->existsResource(ResourceID(path + fname, EResType::IMAGE)))
|
2008-06-12 09:45:51 +03:00
|
|
|
{
|
2012-08-01 15:02:54 +03:00
|
|
|
return nullptr;
|
2008-06-12 09:45:51 +03:00
|
|
|
}
|
2011-07-18 18:21:16 +03:00
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
SDL_Surface * ret=nullptr;
|
2012-08-01 15:02:54 +03:00
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
auto readFile = CResourceHandler::get()->load(ResourceID(path + fname, EResType::IMAGE))->readAll();
|
2012-08-01 15:02:54 +03:00
|
|
|
|
|
|
|
if (isPCX(readFile.first.get()))
|
2010-11-09 13:27:58 +02:00
|
|
|
{//H3-style PCX
|
2012-08-01 15:02:54 +03:00
|
|
|
ret = loadH3PCX(readFile.first.get(), readFile.second);
|
|
|
|
if (ret)
|
2008-06-12 09:45:51 +03:00
|
|
|
{
|
2012-08-01 15:02:54 +03:00
|
|
|
if(ret->format->BytesPerPixel == 1 && setKey)
|
|
|
|
{
|
2014-07-02 21:20:54 +03:00
|
|
|
CSDL_Ext::setColorKey(ret,ret->format->palette->colors[0]);
|
2012-08-01 15:02:54 +03:00
|
|
|
}
|
2008-06-12 09:45:51 +03:00
|
|
|
}
|
2012-08-01 15:02:54 +03:00
|
|
|
else
|
2013-11-07 15:48:41 +03:00
|
|
|
{
|
|
|
|
logGlobal->errorStream()<<"Failed to open "<<fname<<" as H3 PCX!";
|
|
|
|
return nullptr;
|
|
|
|
}
|
2008-06-12 09:45:51 +03:00
|
|
|
}
|
2010-11-09 13:27:58 +02:00
|
|
|
else
|
|
|
|
{ //loading via SDL_Image
|
2013-07-29 14:38:18 +03:00
|
|
|
ret = IMG_Load_RW(
|
2012-08-01 15:02:54 +03:00
|
|
|
//create SDL_RW with our data (will be deleted by SDL)
|
2012-08-06 10:34:37 +03:00
|
|
|
SDL_RWFromConstMem((void*)readFile.first.get(), readFile.second),
|
2013-07-29 14:38:18 +03:00
|
|
|
1); // mark it for auto-deleting
|
2012-09-16 17:36:31 +03:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (ret->format->palette)
|
|
|
|
{
|
|
|
|
//set correct value for alpha\unused channel
|
2014-05-23 13:51:38 +03:00
|
|
|
for (int i=0; i < ret->format->palette->ncolors; i++)
|
2015-09-02 17:49:29 +02:00
|
|
|
ret->format->palette->colors[i].a = SDL_ALPHA_OPAQUE;
|
2012-11-13 14:52:23 +03:00
|
|
|
}
|
2012-09-16 17:36:31 +03:00
|
|
|
}
|
|
|
|
else
|
2012-05-27 17:07:12 +03:00
|
|
|
{
|
2016-03-12 03:41:27 +02:00
|
|
|
logGlobal->errorStream() << "Failed to open " << fname << " via SDL_Image";
|
|
|
|
logGlobal->errorStream() << "Reason: " << IMG_GetError();
|
2013-11-07 15:48:41 +03:00
|
|
|
return nullptr;
|
2012-05-27 17:07:12 +03:00
|
|
|
}
|
2009-08-17 11:50:31 +03:00
|
|
|
}
|
2013-05-31 13:25:23 +03:00
|
|
|
|
2014-07-02 21:20:54 +03:00
|
|
|
// When modifying anything here please check two use cases:
|
2013-05-31 13:25:23 +03:00
|
|
|
// 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)
|
2013-05-31 13:25:23 +03:00
|
|
|
if (ret->format->palette)
|
|
|
|
{
|
2014-07-02 21:41:11 +03:00
|
|
|
CSDL_Ext::setDefaultColorKeyPresize(ret);
|
2013-05-31 13:25:23 +03:00
|
|
|
}
|
2013-05-31 14:46:19 +03:00
|
|
|
else // always set
|
|
|
|
{
|
2014-07-02 21:20:54 +03:00
|
|
|
CSDL_Ext::setDefaultColorKey(ret);
|
2013-05-31 14:46:19 +03:00
|
|
|
}
|
2008-06-16 17:56:48 +03:00
|
|
|
return ret;
|
2009-10-04 05:02:45 +03:00
|
|
|
}
|
2011-07-18 18:21:16 +03:00
|
|
|
|
|
|
|
SDL_Surface * BitmapHandler::loadBitmap(std::string fname, bool setKey)
|
|
|
|
{
|
2011-09-14 08:27:49 +03:00
|
|
|
SDL_Surface *bitmap;
|
2011-07-18 18:21:16 +03:00
|
|
|
|
2012-08-01 15:02:54 +03:00
|
|
|
if (!(bitmap = loadBitmapFromDir("DATA/", fname, setKey)) &&
|
|
|
|
!(bitmap = loadBitmapFromDir("SPRITES/", fname, setKey)))
|
2016-03-12 03:41:27 +02:00
|
|
|
{
|
|
|
|
logGlobal->errorStream() << "Error: Failed to find file " << fname;
|
|
|
|
}
|
2011-07-18 18:21:16 +03:00
|
|
|
|
2011-09-14 08:27:49 +03:00
|
|
|
return bitmap;
|
|
|
|
}
|