1 // ==============================================================
2 //	This file is part of Glest Shared Library (www.glest.org)
3 //
4 //	Copyright (C) 2001-2008 Martiño Figueroa
5 //
6 //	You can redistribute this code and/or modify it under
7 //	the terms of the GNU General Public License as published
8 //	by the Free Software Foundation; either version 2 of the
9 //	License, or (at your option) any later version
10 // ==============================================================
11 
12 #ifndef _SHARED_GRAPHICS_TEXTURE_H_
13 #define _SHARED_GRAPHICS_TEXTURE_H_
14 
15 #include "data_types.h"
16 #include "pixmap.h"
17 #include <string>
18 #include "leak_dumper.h"
19 
20 using std::string;
21 using Shared::Platform::uint8;
22 
23 struct SDL_Surface;
24 
25 namespace Shared{ namespace Graphics{
26 
27 class TextureParams;
28 
29 
30 // =====================================================
31 //	class Texture
32 // =====================================================
33 
34 class Texture {
35 public:
36 	static const int defaultSize;
37 	static const int defaultComponents;
38 	static bool useTextureCompression;
39 
40 	enum WrapMode{
41 		wmRepeat,
42 		wmClamp,
43 		wmClampToEdge
44 	};
45 
46 	enum Filter{
47 		fBilinear,
48 		fTrilinear
49 	};
50 
51 	enum Format{
52 		fAuto,
53 		fAlpha,
54 		fLuminance,
55 		fRgb,
56 		fRgba
57 	};
58 
59 protected:
60 	string path;
61 	bool mipmap;
62 	WrapMode wrapMode;
63 	bool pixmapInit;
64 	Format format;
65 
66 	bool inited;
67 	bool forceCompressionDisabled;
68 	int textureSystemId;
69 
70 public:
71 	Texture();
~Texture()72 	virtual ~Texture(){};
73 
getMipmap()74 	bool getMipmap() const			{return mipmap;}
getWrapMode()75 	WrapMode getWrapMode() const	{return wrapMode;}
getPixmapInit()76 	bool getPixmapInit() const		{return pixmapInit;}
getFormat()77 	Format getFormat() const		{return format;}
getInited()78 	bool getInited() const 			{return inited;}
79 
getTextureSystemId()80 	int getTextureSystemId() const { return textureSystemId; }
setTextureSystemId(int id)81 	void setTextureSystemId(int id) { textureSystemId = id; }
82 
setMipmap(bool mipmap)83 	void setMipmap(bool mipmap)			{this->mipmap= mipmap;}
setWrapMode(WrapMode wrapMode)84 	void setWrapMode(WrapMode wrapMode)	{this->wrapMode= wrapMode;}
setPixmapInit(bool pixmapInit)85 	void setPixmapInit(bool pixmapInit)	{this->pixmapInit= pixmapInit;}
setFormat(Format format)86 	void setFormat(Format format)		{this->format= format;}
87 
88 	virtual void init(Filter filter= fBilinear, int maxAnisotropy= 1)=0;
89 	virtual void end(bool deletePixelBuffer=true)=0;
90 	virtual string getPath() const = 0;
91 	virtual void deletePixels() = 0;
92 	virtual std::size_t getPixelByteCount() const = 0;
93 
reseInitState()94 	virtual void reseInitState() { inited = false; }
95 
setForceCompressionDisabled(bool value)96 	virtual void setForceCompressionDisabled(bool value) { forceCompressionDisabled = value;}
getForceCompressionDisabled()97 	virtual bool getForceCompressionDisabled() const {return forceCompressionDisabled;}
98 
99 	virtual uint32 getCRC() = 0;
100 
101 };
102 
103 // =====================================================
104 //	class Texture1D
105 // =====================================================
106 
107 class Texture1D: public Texture {
108 protected:
109 	Pixmap1D pixmap;
110 
111 public:
112 	void load(const string &path);
113 
getPixmap()114 	Pixmap1D *getPixmap()				{return &pixmap;}
getPixmap()115 	const Pixmap1D *getPixmap() const	{return &pixmap;}
116 	virtual string getPath() const;
117 	virtual void deletePixels();
getPixelByteCount()118 	virtual std::size_t getPixelByteCount() const {return pixmap.getPixelByteCount();}
119 
getTextureWidth()120 	virtual int getTextureWidth() const {return pixmap.getW();}
getTextureHeight()121 	virtual int getTextureHeight() const {return -1;}
122 
getCRC()123 	virtual uint32 getCRC() { return pixmap.getCRC()->getSum(); }
124 };
125 
126 // =====================================================
127 //	class Texture2D
128 // =====================================================
129 
130 class Texture2D: public Texture {
131 protected:
132 	Pixmap2D pixmap;
133 
134 public:
135 	void load(const string &path);
136 
getPixmap()137 	Pixmap2D *getPixmap()			{return &pixmap;}
getPixmapConst()138 	const Pixmap2D *getPixmapConst() const	{return &pixmap;}
139 	virtual string getPath() const;
140 	virtual void deletePixels();
getPixelByteCount()141 	virtual std::size_t getPixelByteCount() const {return pixmap.getPixelByteCount();}
142 
getTextureWidth()143 	virtual int getTextureWidth() const {return pixmap.getW();}
getTextureHeight()144 	virtual int getTextureHeight() const {return pixmap.getH();}
145 
getCRC()146 	virtual uint32 getCRC() { return pixmap.getCRC()->getSum(); }
147 
148 	std::pair<SDL_Surface*,unsigned char*> CreateSDLSurface(bool newPixelData) const;
149 };
150 
151 // =====================================================
152 //	class Texture3D
153 // =====================================================
154 
155 class Texture3D: public Texture {
156 protected:
157 	Pixmap3D pixmap;
158 
159 public:
160 	void loadSlice(const string &path, int slice);
161 
getPixmap()162 	Pixmap3D *getPixmap()				{return &pixmap;}
getPixmap()163 	const Pixmap3D *getPixmap() const	{return &pixmap;}
164 	virtual string getPath() const;
165 	virtual void deletePixels();
getPixelByteCount()166 	virtual std::size_t getPixelByteCount() const {return pixmap.getPixelByteCount();}
167 
getTextureWidth()168 	virtual int getTextureWidth() const {return pixmap.getW();}
getTextureHeight()169 	virtual int getTextureHeight() const {return pixmap.getH();}
170 
getCRC()171 	virtual uint32 getCRC() { return pixmap.getCRC()->getSum(); }
172 };
173 
174 // =====================================================
175 //	class TextureCube
176 // =====================================================
177 
178 class TextureCube: public Texture{
179 protected:
180 	PixmapCube pixmap;
181 
182 public:
183 	void loadFace(const string &path, int face);
184 
getPixmap()185 	PixmapCube *getPixmap()				{return &pixmap;}
getPixmap()186 	const PixmapCube *getPixmap() const	{return &pixmap;}
187 	virtual string getPath() const;
188 	virtual void deletePixels();
getPixelByteCount()189 	virtual std::size_t getPixelByteCount() const {return pixmap.getPixelByteCount();}
190 
getTextureWidth()191 	virtual int getTextureWidth() const {return -1;}
getTextureHeight()192 	virtual int getTextureHeight() const {return -1;}
193 
getCRC()194 	virtual uint32 getCRC() { return pixmap.getCRC()->getSum(); }
195 };
196 
197 }}//end namespace
198 
199 #endif
200