PolitikerNEU jpg and png support! ( currently linux only ? )

Some little changes where I forgot to integrate the playername
Mousescroll is not longer super fast
This commit is contained in:
Titus Tscharntke
2010-03-23 23:32:25 +00:00
parent f18c73d371
commit ecc39ea911
24 changed files with 1454 additions and 100 deletions

View File

@@ -0,0 +1,34 @@
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2010 Martiño Figueroa and others
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#ifndef BMPREADER_H
#define BMPREADER_H
// =====================================
// class BMPReader
// =====================================
#include "FileReader.h"
#include "pixmap.h"
namespace Shared{ namespace Graphics{
class BMPReader: FileReader<Pixmap2D> {
public:
BMPReader();
Pixmap2D* read(ifstream& in, const string& path, Pixmap2D* ret) const;
};
}} //end namespace
#endif

View File

@@ -0,0 +1,274 @@
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2010 Martiño Figueroa and others
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#ifndef FILE_READER_H
#define FILE_READER_H
#include "platform_util.h"
#include <map>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <stdexcept>
using std::map;
using std::string;
using std::vector;
using std::ifstream;
using std::ios;
using std::runtime_error;
using Shared::Platform::extractExtension;
namespace Shared{
// =====================================================
// class FileReader
// =====================================================
template <class T>
class FileReader {
protected:
string const * extensions;
/**Creates a filereader being able to possibly load files
* from the specified extension
**/
FileReader(string const * extensions);
public:
/*Return the - existing and initialized - fileReadersMap
*/
static map<string, vector<FileReader<T> const * >* >& getFileReadersMap() {
static map<string, vector<FileReader<T> const * >* > fileReaderByExtension;
return fileReaderByExtension;
}
static vector<FileReader<T> const * > fileReaders;
public:
/**Tries to read a file
* This method tries to read the file with the specified filepath.
* If it fails, either <code>null</code> is returned or an exception
* is thrown*/
static T* readPath(const string& filepath);
/**Tries to read a file from an object
* This method tries to read the file with the specified filepath.
* If it fails, either <code>null</code> is returned or an exception
* is thrown*/
static T* readPath(const string& filepath, T* object);
/**Gives a quick estimation of whether the specified file
* can be read or not depending on the filename*/
virtual bool canRead(const string& filepath) const;
/**Gives a better estimation of whether the specified file
* can be read or not depending on the file content*/
virtual bool canRead(ifstream& file) const;
/**Reads a file
* This method tries to read the file with the specified filepath
* If it fails, either <code>null</code> is returned or an exception
* is thrown
*/
virtual T* read(const string& filepath) const;
/**Reads a file to an object
* This method tries to read the file with the specified filepath
* If it fails, either <code>null</code> is returned or an exception
* is thrown
*/
virtual T* read(const string& filepath, T* object) const;
/**Reads a file
* This method tries to read the specified file
* If it failes, either <code>null</code> is returned or an exception
* Default implementation generates an object using T()
* is thrown
*/
virtual T* read(ifstream& file, const string& path) const {
T* obj = new T();
T* ret = read(file,path,obj);
if (obj != ret) {
delete obj;
}
return ret;
}
/**Reads a file onto the specified object
* This method tries to read the specified file
* If it failes, either <code>null</code> is returned or an exception
* is thrown
*/
virtual T* read(ifstream& file, const string& path, T* former) const = 0;
virtual ~FileReader() {
/*for (typename vector<FileReader<T> const * >::const_iterator i = fileReaders.begin(); i != fileReaders.end(); ++i) {
delete const_cast<FileReader<T>* >(*i); //Segfault
}*/
}; //Well ... these objects aren't supposed to be destroyed
};
template <typename T>
static inline T* readFromFileReaders(vector<FileReader<T> const *>* readers, const string& filepath) {
for (typename vector<FileReader<T> const *>::const_iterator i = readers->begin(); i != readers->end(); ++i) {
try {
FileReader<T> const * reader = *i;
T* ret = reader->read(filepath); //It is guaranteed that at least the filepath matches ...
if (ret != NULL) {
return ret;
}
} catch (...) { //TODO: Specific exceptions
}
}
return NULL;
}
template <typename T>
static inline T* readFromFileReaders(vector<FileReader<T> const *>* readers, const string& filepath, T* object) {
for (typename vector<FileReader<T> const *>::const_iterator i = readers->begin(); i != readers->end(); ++i) {
try {
FileReader<T> const * reader = *i;
T* ret = reader->read(filepath, object); //It is guaranteed that at least the filepath matches ...
if (ret != NULL) {
return ret;
}
} catch (...) { //TODO: Specific exceptions
}
}
std::cerr << "Could not parse filepath: " << filepath << std::endl;
return NULL;
}
/**Tries to read a file
* This method tries to read the file with the specified filepath.
* If it fails, either <code>null</code> is returned or an exception
* is thrown*/
template <typename T>
T* FileReader<T>::readPath(const string& filepath) {
const string& extension = extractExtension(filepath);
vector<FileReader<T> const * >* possibleReaders = (getFileReadersMap())[extension];
if (possibleReaders != NULL) {
//Search in these possible readers
T* ret = readFromFileReaders(possibleReaders, filepath);
if (ret != NULL) {
return ret;
}
}
T* ret = readFromFileReaders(&fileReaders, filepath); //Try all other
return ret;
}
/**Tries to read a file
* This method tries to read the file with the specified filepath.
* If it fails, either <code>null</code> is returned or an exception
* is thrown*/
template <typename T>
T* FileReader<T>::readPath(const string& filepath, T* object) {
const string& extension = extractExtension(filepath);
vector<FileReader<T> const * >* possibleReaders = (getFileReadersMap())[extension];
if (possibleReaders != NULL) {
//Search in these possible readers
T* ret = readFromFileReaders(possibleReaders, filepath, object);
if (ret != NULL) {
return ret;
}
}
T* ret = readFromFileReaders(&fileReaders, filepath, object); //Try all other
return ret;
}
template<typename T>
vector<FileReader<T> const * > FileReader<T>::fileReaders;
template <typename T>
FileReader<T>::FileReader(string const * extensions): extensions(extensions) {
fileReaders.push_back(this);
string const * nextExtension = extensions;
while (((*nextExtension) != "")) {
vector<FileReader<T> const* >* curPossibleReaders = (getFileReadersMap())[*nextExtension];
if (curPossibleReaders == NULL) {
(getFileReadersMap())[*nextExtension] = (curPossibleReaders = new vector<FileReader<T> const *>());
}
curPossibleReaders->push_back(this);
++nextExtension;
}
}
/**Gives a quick estimation of whether the specified file
* can be read or not depending on the filename*/
template <typename T>
bool FileReader<T>::canRead(const string& filepath) const {
const string& realExtension = extractExtension(filepath);
const string* haveExtension = extensions;
while (*haveExtension != "") {
if (realExtension == *haveExtension) {
return true;
}
++haveExtension;
}
return false;
}
/**Gives a better estimation of whether the specified file
* can be read or not depending on the file content*/
template <typename T>
bool FileReader<T>::canRead(ifstream& file) const {
try {
T* wouldRead = read(file,"unknown file");
bool ret = (wouldRead != NULL);
delete wouldRead;
return ret;
} catch (...) {
return false;
}
}
/**Reads a file
* This method tries to read the file with the specified filepath
* If it fails, either <code>null</code> is returned or an exception
* is thrown
*/
template <typename T>
T* FileReader<T>::read(const string& filepath) const {
ifstream file(filepath.c_str(), ios::in | ios::binary);
if (!file.is_open()) { //An error occured; TODO: Which one - throw an exception, print error message?
throw runtime_error("Could not open file " + filepath);
}
return read(file,filepath);
}
/**Reads a file
* This method tries to read the file with the specified filepath
* If it fails, either <code>null</code> is returned or an exception
* is thrown
*/
template <typename T>
T* FileReader<T>::read(const string& filepath, T* object) const {
ifstream file(filepath.c_str(), ios::in | ios::binary);
if (!file.is_open()) { //An error occured; TODO: Which one - throw an exception, print error message?
throw runtime_error("Could not open file " + filepath);
}
read(file,filepath,object);
}
} //end namespace
#endif

View File

@@ -0,0 +1,39 @@
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2010 Martiño Figueroa and others
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#ifndef IMAGE_READERS_H
#define IMAGE_READERS_H
#include "FileReader.h"
#include "BMPReader.h"
#include "JPGReader.h"
#include "PNGReader.h"
#include "TGAReader.h"
//Initialize some objects
namespace Shared{ namespace Graphics{
// =====================================================
// namespace ImageRegisterer
// =====================================================
namespace ImageRegisterer {
//This function registers all image-readers, but only once (any further call is unnecessary)
bool registerImageReaders();
//Since you can't call void methods here, I have used a method doing nothing except initializing the image Readers
static bool readersRegistered = registerImageReaders(); //should always return true, this should guarantee that the readers are registered <--> ImageReaders is included anywhere
}
}} //end namespace
#endif

View File

@@ -0,0 +1,34 @@
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2010 Martiño Figueroa and others
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#ifndef JPGREADER_H
#define JPGREADER_H
// =====================================
// class JPGReader
// =====================================
#include "FileReader.h"
#include "pixmap.h"
namespace Shared{ namespace Graphics{
class JPGReader: FileReader<Pixmap2D> {
public:
JPGReader();
Pixmap2D* read(ifstream& in, const string& path, Pixmap2D* ret) const;
};
}} //end namespace
#endif

View File

@@ -0,0 +1,34 @@
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2010 Martiño Figueroa and others
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#ifndef PNGREADER_H
#define PNGREADER_H
// =====================================
// class PNGReader
// =====================================
#include "FileReader.h"
#include "pixmap.h"
namespace Shared{ namespace Graphics{
class PNGReader: FileReader<Pixmap2D> {
public:
PNGReader();
Pixmap2D* read(ifstream& in, const string& path, Pixmap2D* ret) const;
};
}} //end namespace
#endif

View File

@@ -0,0 +1,34 @@
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2010 Martiño Figueroa and others
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#ifndef TGAREADER_H
#define TGAREADER_H
// =====================================
// class TGAReader
// =====================================
#include "FileReader.h"
#include "pixmap.h"
namespace Shared{ namespace Graphics{
class TGAReader: FileReader<Pixmap2D> {
public:
TGAReader();
Pixmap2D* read(ifstream& in, const string& path, Pixmap2D* ret) const;
};
}} //end namespace
#endif

View File

@@ -0,0 +1,269 @@
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2008 Marti<74>o Figueroa
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#ifndef _SHARED_GRAPHICS_PIXMAP_H_
#define _SHARED_GRAPHICS_PIXMAP_H_
#include <string>
#include "vec.h"
#include "types.h"
using std::string;
using Shared::Platform::int8;
using Shared::Platform::uint8;
using Shared::Platform::int16;
using Shared::Platform::uint16;
using Shared::Platform::int32;
using Shared::Platform::uint32;
using Shared::Platform::float32;
namespace Shared{ namespace Graphics{
// =====================================================
// class PixmapIo
// =====================================================
class PixmapIo{
protected:
int w;
int h;
int components;
public:
virtual ~PixmapIo(){}
int getW() const {return w;}
int getH() const {return h;}
int getComponents() const {return components;}
virtual void openRead(const string &path)= 0;
virtual void read(uint8 *pixels)= 0;
virtual void read(uint8 *pixels, int components)= 0;
virtual void openWrite(const string &path, int w, int h, int components)= 0;
virtual void write(uint8 *pixels)= 0;
};
// =====================================================
// class PixmapIoTga
// =====================================================
class PixmapIoTga: public PixmapIo{
private:
FILE *file;
public:
PixmapIoTga();
virtual ~PixmapIoTga();
virtual void openRead(const string &path);
virtual void read(uint8 *pixels);
virtual void read(uint8 *pixels, int components);
virtual void openWrite(const string &path, int w, int h, int components);
virtual void write(uint8 *pixels);
};
// =====================================================
// class PixmapIoBmp
// =====================================================
class PixmapIoBmp: public PixmapIo{
private:
FILE *file;
public:
PixmapIoBmp();
virtual ~PixmapIoBmp();
virtual void openRead(const string &path);
virtual void read(uint8 *pixels);
virtual void read(uint8 *pixels, int components);
virtual void openWrite(const string &path, int w, int h, int components);
virtual void write(uint8 *pixels);
};
// =====================================================
// class Pixmap1D
// =====================================================
class Pixmap1D{
protected:
int w;
int components;
uint8 *pixels;
public:
//constructor & destructor
Pixmap1D();
Pixmap1D(int components);
Pixmap1D(int w, int components);
void init(int components);
void init(int w, int components);
~Pixmap1D();
//load & save
void load(const string &path);
void loadTga(const string &path);
void loadBmp(const string &path);
//get
int getW() const {return w;}
int getComponents() const {return components;}
uint8 *getPixels() const {return pixels;}
};
// =====================================================
// class Pixmap2D
// =====================================================
class Pixmap2D{
protected:
int h;
int w;
int components;
uint8 *pixels;
public:
//constructor & destructor
Pixmap2D();
Pixmap2D(int components);
Pixmap2D(int w, int h, int components);
void init(int components);
void init(int w, int h, int components);
~Pixmap2D();
//load & save
static Pixmap2D* loadPath(const string& path);
void load(const string &path);
/*void loadTga(const string &path);
void loadBmp(const string &path);*/
void save(const string &path);
void saveBmp(const string &path);
void saveTga(const string &path);
//get
int getW() const {return w;}
int getH() const {return h;}
int getComponents() const {return components;}
uint8 *getPixels() const {return pixels;}
//get data
void getPixel(int x, int y, uint8 *value) const;
void getPixel(int x, int y, float32 *value) const;
void getComponent(int x, int y, int component, uint8 &value) const;
void getComponent(int x, int y, int component, float32 &value) const;
//vector get
Vec4f getPixel4f(int x, int y) const;
Vec3f getPixel3f(int x, int y) const;
float getPixelf(int x, int y) const;
float getComponentf(int x, int y, int component) const;
//set data
void setPixel(int x, int y, const uint8 *value);
void setPixel(int x, int y, const float32 *value);
void setComponent(int x, int y, int component, uint8 value);
void setComponent(int x, int y, int component, float32 value);
//vector set
void setPixel(int x, int y, const Vec3f &p);
void setPixel(int x, int y, const Vec4f &p);
void setPixel(int x, int y, float p);
//mass set
void setPixels(const uint8 *value);
void setPixels(const float32 *value);
void setComponents(int component, uint8 value);
void setComponents(int component, float32 value);
//operations
void splat(const Pixmap2D *leftUp, const Pixmap2D *rightUp, const Pixmap2D *leftDown, const Pixmap2D *rightDown);
void lerp(float t, const Pixmap2D *pixmap1, const Pixmap2D *pixmap2);
void copy(const Pixmap2D *sourcePixmap);
void subCopy(int x, int y, const Pixmap2D *sourcePixmap);
private:
bool doDimensionsAgree(const Pixmap2D *pixmap);
};
// =====================================================
// class Pixmap3D
// =====================================================
class Pixmap3D{
protected:
int h;
int w;
int d;
int components;
uint8 *pixels;
public:
//constructor & destructor
Pixmap3D();
Pixmap3D(int w, int h, int d, int components);
Pixmap3D(int d, int components);
void init(int w, int h, int d, int components);
void init(int d, int components);
~Pixmap3D();
//load & save
void loadSlice(const string &path, int slice);
void loadSliceBmp(const string &path, int slice);
void loadSliceTga(const string &path, int slice);
//get
int getW() const {return w;}
int getH() const {return h;}
int getD() const {return d;}
int getComponents() const {return components;}
uint8 *getPixels() const {return pixels;}
};
// =====================================================
// class PixmapCube
// =====================================================
class PixmapCube{
public:
enum Face{
fPositiveX,
fNegativeX,
fPositiveY,
fNegativeY,
fPositiveZ,
fNegativeZ
};
protected:
Pixmap2D faces[6];
public:
//init
void init(int w, int h, int components);
//load & save
void loadFace(const string &path, int face);
/*void loadFaceBmp(const string &path, int face);
void loadFaceTga(const string &path, int face);*/
//get
Pixmap2D *getFace(int face) {return &faces[face];}
const Pixmap2D *getFace(int face) const {return &faces[face];}
};
}}//end namespace
#endif