1 #pragma once
2 // Description:
3 //   A Bitmap Collection contains many bitmaps in a single PNG file. Bitmap names,
4 //   offsets, sizes, etc. are kept in a seperate data file.
5 //   Includes GL drawing methods.
6 //
7 // Copyright (C) 2001 Frank Becker
8 //
9 // This program is free software; you can redistribute it and/or modify it under
10 // the terms of the GNU General Public License as published by the Free Software
11 // Foundation;  either version 2 of the License,  or (at your option) any  later
12 // version.
13 //
14 // This program is distributed in the hope that it will be useful,  but  WITHOUT
15 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
17 //
18 
19 #include "GLTexture.hpp"
20 #include "HashString.hpp"
21 
22 #include "vmmlib/vector.hpp"
23 using namespace vmml;
24 
25 const unsigned int MAX_BITMAPS = 512;
26 
27 class GLBitmapCollection
28 {
29 
30 public:
31     struct BitmapInfo
32     {
33 	int xoff;   // offset from left top
34 	int yoff;   // offset from left top
35 	int xpos;   // position within texture
36 	int ypos;   // position within texture
37 	int width;  // width of bitmap
38 	int height; // height of bitmap
39         char name[32];
40     };
41 
GLBitmapCollection(void)42     GLBitmapCollection( void):
43         _bitmapCollection(0),
44         _bcNeedsCleanup(true),
45         _bitmapCount(0),
46         _textureSize(0.0),
47         _bitmapInfoMap()
48     {
49     }
50 
~GLBitmapCollection()51     virtual ~GLBitmapCollection()
52     {
53         //Note: A bit of a hack for iphone where all bitmap collections are merged into a single collection
54         if( _bcNeedsCleanup)
55         {
56             delete _bitmapCollection;
57         }
58     }
59 
60     //Load bitmap and data file
61     virtual bool Load( const char *bitmapFile, const char *dataFile);
62 
63     //Get index of bitmap with the given name
64     int getIndex( const std::string & name);
65     //Bind texture
bind(void)66     void bind( void){ _bitmapCollection->bind();}
67 
68     //Draw using bitmap index
69     void Draw(
70         unsigned int index,
71         const vec2f &ll,
72         const vec2f &ur);
73 
74     //Draw using bitmap index
75     void Draw(
76         unsigned int index,
77         const float &x,
78         const float &y,
79         const float &scalex,
80         const float &scaley,
81         const float &z = 0.0);
82 
83     //Draw using bitmap name
84     void Draw(
85         const std::string &name,
86         const float &x,
87         const float &y,
88         const float &scalex,
89         const float &scaley,
90         const float &z = 0.0);
91 
92     //Draw centred using bitmap name
93     void DrawC(
94         const std::string &name,
95         const float &x,
96         const float &y,
97         const float &scalex,
98         const float &scaley,
99         const float &z = 0.0);
100 
101     //Draw centred using bitmap index
102     void DrawC(
103         unsigned int index,
104         const float &x,
105         const float &y,
106         const float &scalex,
107         const float &scaley,
108         const float &z = 0.0);
109 
getWidth(unsigned int index)110     int getWidth( unsigned int index)
111     {
112 //	if( index >= _bitmapCount) return 0;
113         return _bitmapInfo[ index].width;
114     }
getHeight(unsigned int index)115     int getHeight( unsigned int index)
116     {
117 //	if( index >= _bitmapCount) return 0;
118         return _bitmapInfo[ index].height;
119     }
120 
reload(void)121     void reload( void)
122     {
123         _bitmapCollection->reload();
124     }
125 
126 #ifndef IPHONE
127 protected:
128 #endif
129     //Draw using bitmap info
130     inline void _Draw(
131         const BitmapInfo &bmInfo,
132         const float &x,
133         const float &y,
134         const float &z,
135         const float &scalex,
136         const float &scaley);
137 
138     //Draw centred using bitmap info
139     inline void _DrawC(
140         const BitmapInfo &bmInfo,
141         const float &x,
142         const float &y,
143         const float &z,
144         const float &scalex,
145         const float &scaley);
146 
147     GLTexture *_bitmapCollection;
148     bool _bcNeedsCleanup;
149     BitmapInfo _bitmapInfo[ MAX_BITMAPS];
150     unsigned int _bitmapCount;
151     float _textureSize;
152 
153 	hash_map< const std::string, BitmapInfo*, hash<const std::string>, std::equal_to<const std::string> > _bitmapInfoMap;
154 
155 private:
156     GLBitmapCollection( const GLBitmapCollection&);
157     GLBitmapCollection &operator=(const GLBitmapCollection&);
158 };
159