00001
00002
00003
00004
00005
00006
00007 #ifndef TILETEX_H
00008 #define TILETEX_H
00009
00010
00011 enum TextureID
00012 {
00013 TEX_FLOOR,
00014 TEX_WALL,
00015 TEX_FEAT,
00016 TEX_PLAYER,
00017 TEX_DEFAULT,
00018 TEX_GUI,
00019 TEX_ICONS,
00020 TEX_MAX
00021 };
00022
00023 enum MipMapOptions
00024 {
00025 MIPMAP_CREATE,
00026 MIPMAP_NONE,
00027 MIPMAP_MAX,
00028 };
00029
00030 struct tile_def
00031 {
00032 tile_def(tileidx_t _tile, TextureID _tex, int _ymax = TILE_Y)
00033 : tile(_tile), tex(_tex), ymax(_ymax){}
00034
00035 tileidx_t tile;
00036 TextureID tex;
00037 int ymax;
00038 };
00039
00040 TextureID get_dngn_tex(tileidx_t idx);
00041
00042
00043 typedef bool(*tex_proc_func)(unsigned char *pixels, unsigned int w,
00044 unsigned int h);
00045
00046 class GenericTexture
00047 {
00048 public:
00049 GenericTexture();
00050 virtual ~GenericTexture();
00051
00052 bool load_texture(const char *filename, MipMapOptions mip_opt,
00053 tex_proc_func proc = NULL,
00054 bool force_power_of_two = true);
00055 bool load_texture(unsigned char *pixels, unsigned int w, unsigned int h,
00056 MipMapOptions mip_opt);
00057 void unload_texture();
00058
00059 unsigned int width() const { return m_width; }
00060 unsigned int height() const { return m_height; }
00061 void bind() const;
00062
00063 unsigned int orig_width() const { return m_orig_width; }
00064 unsigned int orig_height() const { return m_orig_height; }
00065
00066 protected:
00067 unsigned int m_handle;
00068 unsigned int m_width;
00069 unsigned int m_height;
00070
00071 unsigned int m_orig_width;
00072 unsigned int m_orig_height;
00073 };
00074
00075 class TilesTexture : public GenericTexture
00076 {
00077 public:
00078 TilesTexture();
00079
00080 void set_info(int max, tile_info_func *info);
00081 inline const tile_info &get_info(tileidx_t idx) const;
00082 inline bool get_coords(tileidx_t idx, int ofs_x, int ofs_y,
00083 float &pos_sx, float &pos_sy,
00084 float &pos_ex, float &pos_ey,
00085 float &tex_sx, float &tex_sy,
00086 float &tex_ex, float &tex_ey,
00087 bool centre = true, int ymin = -1, int ymax = -1,
00088 float tile_x = TILE_X, float tile_y = TILE_Y) const;
00089
00090 protected:
00091 int m_tile_max;
00092 tile_info_func *m_info_func;
00093 };
00094
00095 inline const tile_info &TilesTexture::get_info(tileidx_t idx) const
00096 {
00097 ASSERT(m_info_func);
00098 return m_info_func(idx);
00099 }
00100
00101 inline bool TilesTexture::get_coords(tileidx_t idx, int ofs_x, int ofs_y,
00102 float &pos_sx, float &pos_sy,
00103 float &pos_ex, float &pos_ey,
00104 float &tex_sx, float &tex_sy,
00105 float &tex_ex, float &tex_ey,
00106 bool centre, int ymin, int ymax,
00107 float tile_x, float tile_y) const
00108 {
00109 const tile_info &inf = get_info(idx);
00110
00111 float fwidth = m_width;
00112 float fheight = m_height;
00113
00114
00115 int size_ox = centre ? TILE_X / 2 - inf.width / 2 : 0;
00116 int size_oy = centre ? TILE_Y - inf.height : 0;
00117
00118 int pos_sy_adjust = ofs_y + inf.offset_y + size_oy;
00119 int pos_ey_adjust = pos_sy_adjust + inf.ey - inf.sy;
00120
00121 int sy = pos_sy_adjust;
00122 if (ymin >= 0)
00123 sy = std::max(ymin, sy);
00124
00125 int ey = pos_ey_adjust;
00126 if (ymax >= 0)
00127 ey = std::min(ymax, ey);
00128
00129
00130 if (sy >= ey)
00131 return (false);
00132
00133 pos_sx += (ofs_x + inf.offset_x + size_ox) / tile_x;
00134 pos_sy += sy / tile_y;
00135 pos_ex = pos_sx + (inf.ex - inf.sx) / tile_x;
00136 pos_ey = pos_sy + (ey - sy) / tile_y;
00137
00138 tex_sx = inf.sx / fwidth;
00139 tex_sy = (inf.sy + sy - pos_sy_adjust) / fheight;
00140 tex_ex = inf.ex / fwidth;
00141 tex_ey = (inf.ey + ey - pos_ey_adjust) / fheight;
00142
00143 return (true);
00144 }
00145
00146 class ImageManager
00147 {
00148 public:
00149 ImageManager();
00150 virtual ~ImageManager();
00151
00152 bool load_textures(bool need_mips);
00153 void unload_textures();
00154
00155 static const char *filenames[TEX_MAX];
00156 FixedVector<TilesTexture, TEX_MAX> m_textures;
00157 };
00158
00159 #endif