- added percentage support for min / map hp on damage particles
- bugfixes for png support for pixmap3d (water textures)
This commit is contained in:
parent
850825695e
commit
7c996e3296
|
@ -2191,9 +2191,17 @@ void Unit::checkCustomizedParticleTriggers(bool force) {
|
|||
|
||||
if(force == true || (pst != NULL && pst->getMinmaxEnabled() == true)) {
|
||||
bool stopParticle = force;
|
||||
if(force == false && pst->getMinmaxIsPercent() == false) {
|
||||
if(hp < pst->getMinHp() || hp > pst->getMaxHp()) {
|
||||
stopParticle = true;
|
||||
if(force == false) {
|
||||
if(pst->getMinmaxIsPercent() == false) {
|
||||
if(hp < pst->getMinHp() || hp > pst->getMaxHp()) {
|
||||
stopParticle = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
int hpPercent = (hp / type->getTotalMaxHp(&totalUpgrade) * 100);
|
||||
if(hpPercent < pst->getMinHp() || hpPercent > pst->getMaxHp()) {
|
||||
stopParticle = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2222,6 +2230,12 @@ void Unit::checkCustomizedParticleTriggers(bool force) {
|
|||
showParticle = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
int hpPercent = (hp / type->getTotalMaxHp(&totalUpgrade) * 100);
|
||||
if(hpPercent >= pst->getMinHp() && hpPercent <= pst->getMaxHp()) {
|
||||
showParticle = true;
|
||||
}
|
||||
}
|
||||
|
||||
//printf("CHECKING to START customized particle trigger by HP [%d to %d percentbased = %d] current hp = %d showParticle = %d\n",pst->getMinHp(),pst->getMaxHp(),pst->getMinmaxIsPercent(),hp,showParticle);
|
||||
|
||||
|
|
|
@ -158,7 +158,8 @@ static inline T* readFromFileReaders(vector<FileReader<T> const *>* readers, con
|
|||
try {
|
||||
FileReader<T> const * reader = *i;
|
||||
ret = reader->read(file, filepath); //It is guaranteed that at least the filepath matches ...
|
||||
} catch (...) { //TODO: Specific exceptions
|
||||
}
|
||||
catch (...) { //TODO: Specific exceptions
|
||||
continue;
|
||||
}
|
||||
if (ret != NULL) {
|
||||
|
@ -237,8 +238,8 @@ T* FileReader<T>::readPath(const string& filepath) {
|
|||
}
|
||||
T* ret = readFromFileReaders(&(getFileReaders()), filepath); //Try all other
|
||||
if (ret == NULL) {
|
||||
std::cerr << "Could not parse filepath: " << filepath << std::endl;
|
||||
ret = readFromFileReaders(&(getLowPriorityFileReaders()), filepath); //Try to get dummy file
|
||||
std::cerr << "ERROR #1 - Could not parse filepath: " << filepath << std::endl;
|
||||
throw runtime_error(string("Could not parse ") + filepath + " as object of type " + typeid(T).name());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
@ -260,7 +261,7 @@ T* FileReader<T>::readPath(const string& filepath, T* object) {
|
|||
}
|
||||
T* ret = readFromFileReaders(&(getFileReaders()), filepath, object); //Try all other
|
||||
if (ret == NULL) {
|
||||
std::cerr << "Could not parse filepath: " << filepath << std::endl;
|
||||
std::cerr << "ERROR #2 - Could not parse filepath: " << filepath << std::endl;
|
||||
ret = readFromFileReaders(&(getLowPriorityFileReaders()), filepath); //Try to get dummy file
|
||||
if (ret == NULL) {
|
||||
throw runtime_error(string("Could not parse ") + filepath + " as object of type " + typeid(T).name());
|
||||
|
|
|
@ -29,6 +29,13 @@ public:
|
|||
Pixmap2D* read(ifstream& in, const string& path, Pixmap2D* ret) const;
|
||||
};
|
||||
|
||||
class PNGReader3D: FileReader<Pixmap3D> {
|
||||
public:
|
||||
PNGReader3D();
|
||||
|
||||
Pixmap3D* read(ifstream& in, const string& path, Pixmap3D* ret) const;
|
||||
};
|
||||
|
||||
|
||||
}} //end namespace
|
||||
|
||||
|
|
|
@ -29,6 +29,12 @@ public:
|
|||
Pixmap2D* read(ifstream& in, const string& path, Pixmap2D* ret) const;
|
||||
};
|
||||
|
||||
class TGAReader3D: FileReader<Pixmap3D> {
|
||||
public:
|
||||
TGAReader3D();
|
||||
|
||||
Pixmap3D* read(ifstream& in, const string& path, Pixmap3D* ret) const;
|
||||
};
|
||||
|
||||
}} //end namespace
|
||||
|
||||
|
|
|
@ -303,6 +303,7 @@ protected:
|
|||
int w;
|
||||
int d;
|
||||
int components;
|
||||
int slice;
|
||||
uint8 *pixels;
|
||||
string path;
|
||||
|
||||
|
@ -323,6 +324,7 @@ public:
|
|||
void loadSlicePng(const string &path, int slice);
|
||||
|
||||
//get
|
||||
int getSlice() const { return slice; }
|
||||
int getW() const {return w;}
|
||||
int getH() const {return h;}
|
||||
int getD() const {return d;}
|
||||
|
|
|
@ -23,7 +23,9 @@ bool ImageRegisterer::registerImageReaders() {
|
|||
static BMPReader imageReaderBmp;
|
||||
static JPGReader imageReaderJpg;
|
||||
static PNGReader imageReaderPng;
|
||||
static PNGReader3D imageReader3DPng;
|
||||
static TGAReader imageReaderTga;
|
||||
static TGAReader3D imageReader3DTga;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -192,4 +192,146 @@ Pixmap2D* PNGReader::read(ifstream& is, const string& path, Pixmap2D* ret) const
|
|||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
PNGReader3D::PNGReader3D(): FileReader<Pixmap3D>(getExtensionsPng()) {}
|
||||
|
||||
Pixmap3D* PNGReader3D::read(ifstream& is, const string& path, Pixmap3D* ret) const {
|
||||
//Read file
|
||||
is.seekg(0, ios::end);
|
||||
size_t length = is.tellg();
|
||||
is.seekg(0, ios::beg);
|
||||
uint8 *buffer = new uint8[8];
|
||||
is.read((char*)buffer, 8);
|
||||
|
||||
if (png_sig_cmp(buffer, 0, 8) != 0) {
|
||||
delete [] buffer;
|
||||
return NULL; //This is not a PNG file - could be used for fast checking whether file is supported or not
|
||||
}
|
||||
|
||||
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
||||
if (!png_ptr) {
|
||||
delete [] buffer;
|
||||
return NULL;
|
||||
}
|
||||
png_infop info_ptr = png_create_info_struct(png_ptr);
|
||||
if (!info_ptr) {
|
||||
png_destroy_read_struct(&png_ptr, (png_infopp)NULL,(png_infopp)NULL);
|
||||
delete [] buffer;
|
||||
return NULL;
|
||||
}
|
||||
if (setjmp(png_jmpbuf(png_ptr))) {
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr,(png_infopp)NULL);
|
||||
delete [] buffer;
|
||||
return NULL; //Error during init_io
|
||||
}
|
||||
png_set_read_fn(png_ptr, &is, user_read_data);
|
||||
png_set_sig_bytes(png_ptr, 8);
|
||||
png_read_info(png_ptr, info_ptr);
|
||||
|
||||
int width = info_ptr->width;
|
||||
int height = info_ptr->height;
|
||||
int color_type = info_ptr->color_type;
|
||||
int bit_depth = info_ptr->bit_depth;
|
||||
|
||||
//We want RGB, 24 bit
|
||||
if (color_type == PNG_COLOR_TYPE_PALETTE || (color_type == PNG_COLOR_TYPE_GRAY && info_ptr->bit_depth < 8) || (info_ptr->valid & PNG_INFO_tRNS)) {
|
||||
png_set_expand(png_ptr);
|
||||
}
|
||||
|
||||
if (color_type == PNG_COLOR_TYPE_GRAY) {
|
||||
png_set_gray_to_rgb(png_ptr);
|
||||
}
|
||||
|
||||
int number_of_passes = png_set_interlace_handling(png_ptr);
|
||||
png_read_update_info(png_ptr, info_ptr);
|
||||
png_bytep* row_pointers = new png_bytep[height];
|
||||
|
||||
if (setjmp(png_jmpbuf(png_ptr))) {
|
||||
delete[] row_pointers;
|
||||
delete [] buffer;
|
||||
return NULL; //error during read_image
|
||||
}
|
||||
for (int y = 0; y < height; ++y) {
|
||||
row_pointers[y] = new png_byte[info_ptr->rowbytes];
|
||||
}
|
||||
png_read_image(png_ptr, row_pointers);
|
||||
size_t fileComponents = info_ptr->rowbytes/info_ptr->width;
|
||||
size_t picComponents = (ret->getComponents()==-1)?fileComponents:ret->getComponents();
|
||||
//std::cout << "PNG-Components: Pic: " << picComponents << " old: " << (ret->getComponents()) << " File: " << fileComponents << std::endl;
|
||||
//picComponents = 4;
|
||||
//Copy image
|
||||
//printf("pixmap3d loading path [%s] w = %d h = %d d = %d comp = %d\n",path.c_str(),width,height,ret->getD(),picComponents);
|
||||
ret->init(width,height,ret->getD(),(int)picComponents);
|
||||
uint8* pixels = ret->getPixels();
|
||||
if(ret->getSlice() > 0) {
|
||||
pixels = &pixels[ret->getSlice()*width*height*picComponents];
|
||||
}
|
||||
|
||||
const size_t rowbytes = info_ptr->rowbytes;
|
||||
size_t location = 0;
|
||||
for (int y = height-1; y >= 0; --y) { //you have to somehow invert the lines
|
||||
if (picComponents == fileComponents) {
|
||||
memcpy(pixels+location,row_pointers[y],rowbytes);
|
||||
} else {
|
||||
int r,g,b,a,l;
|
||||
for (size_t xPic = 0, xFile = 0; xFile < rowbytes; xPic+= picComponents, xFile+= fileComponents) {
|
||||
switch(fileComponents) {
|
||||
case 3:
|
||||
r = row_pointers[y][xFile];
|
||||
g = row_pointers[y][xFile+1];
|
||||
b = row_pointers[y][xFile+2];
|
||||
l = (r+g+b+2)/3;
|
||||
a = 255;
|
||||
break;
|
||||
case 4:
|
||||
r = row_pointers[y][xFile];
|
||||
g = row_pointers[y][xFile+1];
|
||||
b = row_pointers[y][xFile+2];
|
||||
l = (r+g+b+2)/3;
|
||||
a = row_pointers[y][xFile+3];
|
||||
break;
|
||||
default:
|
||||
//TODO: Error
|
||||
case 1:
|
||||
r = g = b = l = row_pointers[y][xFile];
|
||||
a = 255;
|
||||
break;
|
||||
}
|
||||
switch (picComponents) {
|
||||
case 1:
|
||||
pixels[location+xPic] = l;
|
||||
break;
|
||||
case 4:
|
||||
pixels[location+xPic+3] = a; //Next case
|
||||
case 3:
|
||||
pixels[location+xPic] = r;
|
||||
pixels[location+xPic+1] = g;
|
||||
pixels[location+xPic+2] = b;
|
||||
break;
|
||||
default:
|
||||
//just so at least something works
|
||||
for (unsigned int i = 0; i < picComponents; ++i) {
|
||||
pixels[location+xPic+i] = l;
|
||||
}
|
||||
//TODO: Error
|
||||
}
|
||||
}
|
||||
}
|
||||
location += picComponents * width;
|
||||
}
|
||||
|
||||
for (int y = 0; y < height; ++y) {
|
||||
delete [] row_pointers[y];
|
||||
}
|
||||
delete[] row_pointers;
|
||||
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
|
||||
|
||||
delete [] buffer;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
}} //end namespace
|
||||
|
|
|
@ -63,6 +63,101 @@ static inline std::vector<string> getExtensionStrings() {
|
|||
return extensions;
|
||||
}
|
||||
|
||||
TGAReader3D::TGAReader3D(): FileReader<Pixmap3D>(getExtensionStrings()) {}
|
||||
|
||||
Pixmap3D* TGAReader3D::read(ifstream& in, const string& path, Pixmap3D* ret) const {
|
||||
//read header
|
||||
TargaFileHeader fileHeader;
|
||||
in.read((char*)&fileHeader, sizeof(TargaFileHeader));
|
||||
if (!in.good()) {
|
||||
throw runtime_error(path + " could not be read");
|
||||
}
|
||||
|
||||
//check that we can load this tga file
|
||||
if(fileHeader.idLength!=0){
|
||||
throw runtime_error(path + ": id field is not 0");
|
||||
}
|
||||
|
||||
if(fileHeader.dataTypeCode!=tgaUncompressedRgb && fileHeader.dataTypeCode!=tgaUncompressedBw){
|
||||
throw runtime_error(path + ": only uncompressed BW and RGB targa images are supported");
|
||||
}
|
||||
|
||||
//check bits per pixel
|
||||
if(fileHeader.bitsPerPixel!=8 && fileHeader.bitsPerPixel!=24 && fileHeader.bitsPerPixel!=32){
|
||||
throw runtime_error(path + ": only 8, 24 and 32 bit targa images are supported");
|
||||
}
|
||||
|
||||
const int h = fileHeader.height;
|
||||
const int w = fileHeader.width;
|
||||
const int fileComponents= fileHeader.bitsPerPixel/8;
|
||||
const int picComponents = (ret->getComponents()==-1)?fileComponents:ret->getComponents();
|
||||
//std::cout << "TGA-Components: Pic: " << picComponents << " old: " << (ret->getComponents()) << " File: " << fileComponents << " slice:" << ret->getSlice() << std::endl;
|
||||
ret->init(w,h,ret->getD(), picComponents);
|
||||
uint8* pixels = ret->getPixels();
|
||||
if(ret->getSlice() > 0) {
|
||||
pixels = &pixels[ret->getSlice()*w*h*picComponents];
|
||||
}
|
||||
//read file
|
||||
for(int i=0; i<h*w*picComponents; i+=picComponents){
|
||||
uint8 r, g, b, a, l;
|
||||
|
||||
if(fileComponents==1){
|
||||
in.read((char*)&l,1);
|
||||
r= l;
|
||||
g= l;
|
||||
b= l;
|
||||
a= 255;
|
||||
}
|
||||
else{
|
||||
in.read((char*)&b, 1);
|
||||
in.read((char*)&g, 1);
|
||||
in.read((char*)&r, 1);
|
||||
if(fileComponents==4){
|
||||
in.read((char*)&a, 1);
|
||||
} else {
|
||||
a= 255;
|
||||
}
|
||||
l= (r+g+b)/3;
|
||||
}
|
||||
if (!in.good()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
switch(picComponents){
|
||||
case 1:
|
||||
pixels[i]= l;
|
||||
break;
|
||||
case 3:
|
||||
pixels[i]= r;
|
||||
pixels[i+1]= g;
|
||||
pixels[i+2]= b;
|
||||
break;
|
||||
case 4:
|
||||
pixels[i]= r;
|
||||
pixels[i+1]= g;
|
||||
pixels[i+2]= b;
|
||||
pixels[i+3]= a;
|
||||
break;
|
||||
}
|
||||
}
|
||||
/*for(int i = 0; i < w*h*picComponents; ++i) {
|
||||
if (i%39 == 0) std::cout << std::endl;
|
||||
int first = pixels[i]/16;
|
||||
if (first < 10)
|
||||
std:: cout << first;
|
||||
else
|
||||
std::cout << (char)('A'+(first-10));
|
||||
first = pixels[i]%16;
|
||||
if (first < 10)
|
||||
std:: cout << first;
|
||||
else
|
||||
std::cout << (char)('A'+(first-10));
|
||||
std::cout << " ";
|
||||
}*/
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
TGAReader::TGAReader(): FileReader<Pixmap2D>(getExtensionStrings()) {}
|
||||
|
||||
Pixmap2D* TGAReader::read(ifstream& in, const string& path, Pixmap2D* ret) const {
|
||||
|
|
|
@ -1215,15 +1215,18 @@ Pixmap3D::Pixmap3D() {
|
|||
d= -1;
|
||||
components= -1;
|
||||
pixels = NULL;
|
||||
slice=0;
|
||||
}
|
||||
|
||||
Pixmap3D::Pixmap3D(int w, int h, int d, int components){
|
||||
pixels = NULL;
|
||||
slice=0;
|
||||
init(w, h, d, components);
|
||||
}
|
||||
|
||||
Pixmap3D::Pixmap3D(int d, int components){
|
||||
pixels = NULL;
|
||||
slice=0;
|
||||
init(d, components);
|
||||
}
|
||||
|
||||
|
@ -1265,6 +1268,7 @@ Pixmap3D::~Pixmap3D() {
|
|||
}
|
||||
|
||||
void Pixmap3D::loadSlice(const string &path, int slice) {
|
||||
this->slice = slice;
|
||||
string extension= path.substr(path.find_last_of('.') + 1);
|
||||
if(extension == "png") {
|
||||
loadSlicePng(path, slice);
|
||||
|
@ -1284,24 +1288,28 @@ void Pixmap3D::loadSlice(const string &path, int slice) {
|
|||
void Pixmap3D::loadSlicePng(const string &path, int slice) {
|
||||
this->path = path;
|
||||
|
||||
Pixmap3D *pixmap = FileReader<Pixmap3D>::readPath(path);
|
||||
if(pixmap != NULL) {
|
||||
this->path = path;
|
||||
deletePixels();
|
||||
|
||||
w= pixmap->getW();
|
||||
h= pixmap->getH();
|
||||
if(components==-1){
|
||||
components= pixmap->getComponents();
|
||||
}
|
||||
Pixmap3D *pixmap = FileReader<Pixmap3D>::readPath(path,this);
|
||||
//printf("Loading 3D pixmap PNG [%s] pixmap [%p] this [%p]\n",path.c_str(),pixmap, this);
|
||||
|
||||
if(pixels==NULL) {
|
||||
pixels= new uint8[(std::size_t)pixmap->getPixelByteCount()];
|
||||
}
|
||||
|
||||
for(unsigned int i = 0; i < pixmap->getPixelByteCount(); ++i) {
|
||||
pixels[i] = pixmap->getPixels()[i];
|
||||
}
|
||||
}
|
||||
// if(pixmap != NULL) {
|
||||
// this->path = path;
|
||||
//
|
||||
// w= pixmap->getW();
|
||||
// h= pixmap->getH();
|
||||
// if(components==-1){
|
||||
// components= pixmap->getComponents();
|
||||
// }
|
||||
//
|
||||
// if(pixels==NULL) {
|
||||
// pixels= new uint8[(std::size_t)pixmap->getPixelByteCount()];
|
||||
// }
|
||||
//
|
||||
// for(unsigned int i = 0; i < pixmap->getPixelByteCount(); ++i) {
|
||||
// pixels[i] = pixmap->getPixels()[i];
|
||||
// }
|
||||
// }
|
||||
|
||||
// PixmapIoP plt;
|
||||
// plt.openRead(path);
|
||||
|
@ -1346,6 +1354,38 @@ void Pixmap3D::loadSliceBmp(const string &path, int slice){
|
|||
void Pixmap3D::loadSliceTga(const string &path, int slice){
|
||||
this->path = path;
|
||||
|
||||
deletePixels();
|
||||
FileReader<Pixmap3D>::readPath(path,this);
|
||||
//printf("Loading 3D pixmap TGA [%s] this [%p]\n",path.c_str(),this);
|
||||
|
||||
|
||||
// Pixmap3D *pixmap = new Pixmap3D();
|
||||
// pixmap->h = this->h;
|
||||
// pixmap->w = this->w;
|
||||
// pixmap->d = this->d;
|
||||
// pixmap->components = this->components;
|
||||
// pixmap->slice = this->slice;
|
||||
// FileReader<Pixmap3D>::readPath(path,pixmap);
|
||||
// printf("Loading 3D pixmap TGA [%s] pixmap [%p] this [%p]\n",path.c_str(),pixmap, this);
|
||||
//
|
||||
// //init
|
||||
// w= pixmap->getW();
|
||||
// h= pixmap->getH();
|
||||
// if(components==-1){
|
||||
// components= pixmap->getComponents();
|
||||
// }
|
||||
// if(pixels==NULL){
|
||||
// pixels= new uint8[(std::size_t)getPixelByteCount()];
|
||||
// }
|
||||
//
|
||||
// //read data
|
||||
// for(unsigned int i = slice*w*h*components; i < h*w*components; ++i) {
|
||||
// pixels[i] = pixmap->getPixels()[i];
|
||||
// }
|
||||
//
|
||||
// delete pixmap;
|
||||
|
||||
/*
|
||||
PixmapIoTga plt;
|
||||
plt.openRead(path);
|
||||
|
||||
|
@ -1364,6 +1404,7 @@ void Pixmap3D::loadSliceTga(const string &path, int slice){
|
|||
|
||||
//read data
|
||||
plt.read(&pixels[slice*w*h*components], components);
|
||||
*/
|
||||
}
|
||||
|
||||
// =====================================================
|
||||
|
|
Loading…
Reference in New Issue