~gunchleoc/widelands/bug-1818494-ingame-zoom-freezes

« back to all changes in this revision

Viewing changes to src/graphic.h

  • Committer: sirver
  • Date: 2002-02-05 20:54:08 UTC
  • Revision ID: git-v1:df384fd3a5e53be1f37803d1c0381fa993844bf5
Initial revision


git-svn-id: https://widelands.svn.sourceforge.net/svnroot/widelands/trunk@2 37b2a8de-5219-0410-9f54-a31bc463ab9c

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2001 by Holger Rapp 
 
3
 * 
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License
 
6
 * as published by the Free Software Foundation; either version 2
 
7
 * of the License, or (at your option) any later version.
 
8
 * 
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 * 
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
 *
 
18
 */
 
19
 
 
20
#ifndef __S__GRAPHIC_H
 
21
#define __S__GRAPHIC_H
 
22
 
 
23
#define         DEF_CLRKEY      Graph::pack_rgb(0,0,255)        
 
24
 
 
25
#include <SDL/SDL.h>
 
26
#include <stdlib.h>
 
27
#include "errors.h"
 
28
#include "singleton.h"
 
29
 
 
30
namespace Graph {
 
31
 
 
32
                  /** inline unsigned short Graph::pack_rgb(const unsigned char r, const unsigned char g, const unsigned char b);
 
33
                        * 
 
34
                        * This functions packs a RGB tribble into a short
 
35
                        *
 
36
                        * Args: r       red value
 
37
                        *               g       green value
 
38
                        *               b       blue value (SUPRISE!)
 
39
                        * Returns: packed value
 
40
                        */
 
41
                  inline unsigned short Graph::pack_rgb(const unsigned char r, const unsigned char g, const unsigned char b) {
 
42
                                         return ((b>>3) + ((g>>2)<<5)+ ((r>>3)<<11) );
 
43
                  }
 
44
 
 
45
                  /** inline void Graph::unpack_rgb(const unsigned short clr, unsigned char* r, unsigned char* g, unsigned char* b) ;
 
46
                        *       
 
47
                        * this unpacks a clr and returns the RGB tribble
 
48
                        *
 
49
                        * Args:         clr     clr to unpack
 
50
                  *             r               ptr to var to hold red value
 
51
                        *                       g               ptr to var to hold green value
 
52
                        *                       b               ptr to var to hold blue value
 
53
                        * Returns: Nothing
 
54
                        */
 
55
                  inline void Graph::unpack_rgb(const unsigned short clr, unsigned char* r, unsigned char* g, unsigned char* b) {
 
56
                                         *r= ((clr<<3)>>11);
 
57
                                         *g= ((clr<<2)>>5);
 
58
                                         *b= (clr<<3);
 
59
                  }
 
60
 
 
61
                  
 
62
                  /** class Pic
 
63
                        * 
 
64
                        * This class represents a picture  
 
65
                        */
 
66
                  class Pic {
 
67
                                         public:
 
68
                                         Pic(void) { pixels=NULL; w=h=lpix=clrkey=sh_clrkey=bhas_clrkey=0; }
 
69
                                         ~Pic(void) { if(pixels) free(pixels); }
 
70
 
 
71
                                         void set_size(const unsigned short, const unsigned short);
 
72
                                         void set_clrkey(const unsigned short);
 
73
                                         void set_clrkey(const unsigned char, const unsigned char, const unsigned char);
 
74
                                         int  load(const char*);
 
75
                                         void clear_all(void);
 
76
                                         Pic& operator=(const Pic&);
 
77
                                         Pic(const Pic&);
 
78
 
 
79
                                         /** inline int Pic::get_w(void) const 
 
80
                                          * 
 
81
                                          * This function returns the width
 
82
                                          * Args: none
 
83
                                          * returns: width
 
84
                                          */
 
85
                                         inline int get_w(void) const { return w; }
 
86
 
 
87
                                         /** inline int Pic::get_h(void) const
 
88
                                          * 
 
89
                                          * This function returns the height
 
90
                                          * Args: none
 
91
                                          * returns: height
 
92
                                          */
 
93
                                         inline int get_h(void) const { return h; }
 
94
 
 
95
                                         /** inline unsigned short get_clrkey(void) const
 
96
                                          *
 
97
                                          * this returns the current colorkey
 
98
                                          *
 
99
                                          * Args: none
 
100
                                          * returns: clrkey
 
101
                                          */
 
102
                                         inline unsigned short get_clrkey(void) const {
 
103
                                                                if(bhas_clrkey) return sh_clrkey;
 
104
                                                                return 0;
 
105
                                         }
 
106
 
 
107
                                         /** inline bool has_clrkey(void) const
 
108
                                          *
 
109
                                          * this indicates if the pixel has a valid clrkey
 
110
                                          *
 
111
                                          * Args: none
 
112
                                          * Returns: if the pixel has a clrkey or not
 
113
                                          */
 
114
                                         inline bool has_clrkey(void) { return bhas_clrkey; }
 
115
                                         
 
116
                                         // optimized pixel get functions
 
117
                                         // They are rather simple,
 
118
                                         // THEY DON'T CHECK FOR OVERFLOWS!!
 
119
                                         inline unsigned short get_pixel(const unsigned int x, const unsigned int y) {
 
120
                                                                lpix=(y)*(w) + (x);
 
121
                                                                return pixels[lpix];
 
122
                                         }
 
123
                                         inline unsigned short get_npixel(void) {
 
124
                                                                return pixels[++lpix];
 
125
                                         }
 
126
                                         inline unsigned short get_ppixel(void) {
 
127
                                                                return pixels[--lpix];
 
128
                                         }
 
129
                                         inline unsigned short get_fpixel(void) {
 
130
                                                                lpix=0;
 
131
                                                                return pixels[0];
 
132
                                         }
 
133
 
 
134
                                         // rewinding or forwarding without change
 
135
                                         inline void npixel(void) { ++lpix; }
 
136
                                         inline void ppixel(void) { --lpix; }
 
137
                                         inline void fpixel(void) { lpix=0; }
 
138
                                         inline void set_cpixel(const unsigned int x, const unsigned int y) { lpix=y*w + x; }
 
139
 
 
140
                                         // optimized pixel set functions declared inline
 
141
                                         // All those functions take either a rgb tribble or 3 chars
 
142
                                         // And they are rather simple,
 
143
                                         // THEY DON'T CHECK FOR OVERFLOWS!!
 
144
                                         inline void set_pixel(const unsigned int x, const unsigned int y, const unsigned char r, const unsigned char g, const unsigned char b) {
 
145
                                                                lpix=(y)*(w) + (x);
 
146
                                                                pixels[lpix] = pack_rgb(r, g, b);
 
147
                                         }
 
148
                                         inline void set_pixel(const unsigned int x, const unsigned int y, const unsigned short clr) {
 
149
                                                                lpix=(y)*(w) + (x);
 
150
                                                                pixels[lpix] = clr; 
 
151
                                         }
 
152
                                         inline void set_npixel(const unsigned short clr) {
 
153
                                                                pixels[++lpix] = clr;
 
154
                                         }
 
155
                                         inline void set_npixel(const unsigned char r, const unsigned char g, const unsigned char b) {
 
156
                                                                pixels[++lpix]= pack_rgb(r, g, b);
 
157
                                         }
 
158
                                         inline void set_ppixel(const unsigned short clr) {
 
159
                                                                pixels[--lpix]= clr;
 
160
                                         }
 
161
                                         inline void set_ppixel(const unsigned char r, const unsigned char g, const unsigned char b) {
 
162
                                                                pixels[--lpix]= pack_rgb(r, g, b);
 
163
                                         }
 
164
                                         inline void set_fpixel(const unsigned short clr) {
 
165
                                                                lpix=0;
 
166
                                                                pixels[0] = clr;
 
167
                                         }
 
168
                                         inline void set_fpixel(const unsigned char r, const unsigned char g, const unsigned char b) {
 
169
                                                                lpix=0;
 
170
                                                                pixels[0] = pack_rgb(r, g, b);
 
171
                                         }
 
172
                                         
 
173
                                         // this function really needs faaast blitting
 
174
                                         friend void copy_pic(Pic*, Pic*, const unsigned short, const unsigned short,  const unsigned short, const unsigned short, 
 
175
                                                                                  const unsigned short, const unsigned short);
 
176
                                         friend  void draw_pic(Pic*, const unsigned short, const unsigned short,  const unsigned short, const unsigned short, 
 
177
                                                                                  const unsigned short, const unsigned short);
 
178
 
 
179
                                         private:
 
180
                                         bool bhas_clrkey;
 
181
                                         unsigned long clrkey;
 
182
                                         unsigned short sh_clrkey;
 
183
                                         unsigned short* pixels;
 
184
                                         unsigned short w, h;
 
185
                                         unsigned int lpix;
 
186
                  };
 
187
 
 
188
                  /** class Graphic 
 
189
                        *
 
190
                        * This class is responsible for all graphics stuff. It's
 
191
                        * modified/optimized to work only for 16bit colordepth and nothing else
 
192
                        * 
 
193
                        * It's a singleton
 
194
                        */
 
195
                  class Graphic : public Singleton<Graphic> {
 
196
                                         // forbidden functions
 
197
                                         Graphic(const Graphic&);
 
198
                                         Graphic& operator=(const Graphic&);
 
199
 
 
200
                                         public:
 
201
                                         enum Mode {
 
202
                                                                MODE_FS,
 
203
                                                                MODE_WIN
 
204
                                         };
 
205
 
 
206
                                         enum State {
 
207
                                                                NOT_INIT,
 
208
                                                                OK,
 
209
                                                                ERROR
 
210
                                         };
 
211
 
 
212
                                         Graphic(void);
 
213
                                         ~Graphic(void);
 
214
 
 
215
                                         void set_mode(const unsigned short, const unsigned short, const Mode);
 
216
                                         void update_screen(void);
 
217
                                         void update_rect(const unsigned short, const unsigned short, const unsigned short, const unsigned short);
 
218
                                         void update_quarter(void);
 
219
 
 
220
                                         /** Graphic::State Graphic::get_state(void) 
 
221
                                          *
 
222
                                          * returns the current state of the graphics class
 
223
                                          *
 
224
                                          * Args: none
 
225
                                          * Returns: nothing
 
226
                                          */
 
227
                                         State get_state(void) const { return st; }
 
228
 
 
229
                                         /** inline bool Graphic::is_fs(void)
 
230
                                          *
 
231
                                          * graphic mode is fullscreen?
 
232
                                          *
 
233
                                          * Args: none
 
234
                                          * Returns: true if fs, false if window
 
235
                                          */
 
236
                                         inline bool is_fs(void) { return bis_fs; st=OK; }
 
237
 
 
238
                                         /** inline int Graphic::get_xres(void) const
 
239
                                          * 
 
240
                                          * This function returns the X Resoultion of the current screen
 
241
                                          * Args: none
 
242
                                          * returns: XRes
 
243
                                          */
 
244
                                         inline int get_xres(void) const { return xres; }
 
245
 
 
246
                                         /** inline void Graphic::get_yres(void) const
 
247
                                          * 
 
248
                                          * This function returns the Y Resoultion of the current screen
 
249
                                          * Args: none
 
250
                                          * returns: YRes
 
251
                                          */
 
252
                                         inline int get_yres(void) const { return yres; }
 
253
 
 
254
 
 
255
                                         // optimized pixel get functions
 
256
                                         // They are rather simple,
 
257
                                         // THEY DON'T CHECK FOR OVERFLOWS!!
 
258
                                         inline unsigned short get_pixel(const unsigned int x, const unsigned int y) {
 
259
                                                                lpix=(y)*(xres>>1) + (x);
 
260
                                                                return *((Uint16*) sc->pixels + lpix);
 
261
                                         }
 
262
                                         inline unsigned short get_npixel(void) {
 
263
                                                                return *((Uint16*) sc->pixels + ++lpix);
 
264
                                         }
 
265
                                         inline unsigned short get_ppixel(void) {
 
266
                                                                return *((Uint16*) sc->pixels + --lpix);
 
267
                                         }
 
268
                                         inline unsigned short get_fpixel(void) {
 
269
                                                                lpix=0;
 
270
                                                                return *((Uint16*) sc->pixels);
 
271
                                         }
 
272
 
 
273
                                         // rewinding or forwarding without change
 
274
                                         inline void npixel(void) { ++lpix; }
 
275
                                         inline void ppixel(void) { --lpix; }
 
276
                                         inline void fpixel(void) { lpix=0; }
 
277
                                         inline void set_cpixel(const unsigned int x, const unsigned int y) { lpix=y*xres + x; }
 
278
 
 
279
                                         // optimized pixel set functions declared inline
 
280
                                         // All those functions take either a rgb tribble or 3 chars
 
281
                                         // And they are rather simple,
 
282
                                         // THEY DON'T CHECK FOR OVERFLOWS!!
 
283
                                         inline void set_pixel(const unsigned int x, const unsigned int y, const unsigned char r, const unsigned char g, const unsigned char b) {
 
284
                                                                lpix=(y)*(xres) + (x);
 
285
                                                                pixels[lpix] = pack_rgb(r, g, b);
 
286
                                         }
 
287
                                         inline void set_pixel(const unsigned int x, const unsigned int y, const unsigned short clr) {
 
288
                                                                lpix=(y)*(xres) + (x);
 
289
                                                                pixels[lpix] = clr; 
 
290
                                         }
 
291
                                         inline void set_npixel(const unsigned short clr) {
 
292
                                                                pixels[++lpix] = clr;
 
293
                                         }
 
294
                                         inline void set_npixel(const unsigned char r, const unsigned char g, const unsigned char b) {
 
295
                                                                pixels[++lpix]= pack_rgb(r, g, b);
 
296
                                         }
 
297
                                         inline void set_ppixel(const unsigned short clr) {
 
298
                                                                pixels[--lpix]= clr;
 
299
                                         }
 
300
                                         inline void set_ppixel(const unsigned char r, const unsigned char g, const unsigned char b) {
 
301
                                                                pixels[--lpix]= pack_rgb(r, g, b);
 
302
                                         }
 
303
                                         inline void set_fpixel(const unsigned short clr) {
 
304
                                                                lpix=0;
 
305
                                                                pixels[0] = clr;
 
306
                                         }
 
307
                                         inline void set_fpixel(const unsigned char r, const unsigned char g, const unsigned char b) {
 
308
                                                                lpix=0;
 
309
                                                                pixels[0] = pack_rgb(r, g, b);
 
310
                                         }
 
311
                
 
312
                                         
 
313
                                         // this function really needs faaast blitting
 
314
                                         friend void draw_pic(Pic*, const unsigned short, const unsigned short,  const unsigned short, const unsigned short, 
 
315
                                                                                  const unsigned short, const unsigned short);
 
316
 
 
317
                                         private:
 
318
                                         unsigned short* pixels;
 
319
                                         unsigned short xres, yres;
 
320
                                         unsigned int lpix;
 
321
                                         bool   bis_fs;
 
322
                                         SDL_Surface* sc;
 
323
                                         State st;
 
324
                  };
 
325
 
 
326
                  void draw_pic(Pic*, const unsigned short, const unsigned short,  const unsigned short, const unsigned short, 
 
327
                                                                const unsigned short, const unsigned short);
 
328
                  void copy_pic(Pic*, Pic*, const unsigned short, const unsigned short,  const unsigned short, const unsigned short, 
 
329
                                                                const unsigned short, const unsigned short);
 
330
}
 
331
 
 
332
using   Graph::Graphic;
 
333
using   Graph::Pic;
 
334
#define         g_gr    Graph::Graphic::get_singleton()
 
335
 
 
336
#endif /* __S__GRAPHIC_H */