~ubuntu-branches/ubuntu/precise/widelands/precise-backports

« back to all changes in this revision

Viewing changes to src/sw16_graphic.h

  • Committer: Bazaar Package Importer
  • Author(s): Martin Quinson
  • Date: 2005-02-14 10:41:12 UTC
  • Revision ID: james.westby@ubuntu.com-20050214104112-6v08iux9fptxpva9
Tags: upstream-build9
Import upstream version build9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2002-2004 by the Wide Lands Development Team
 
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 included_sw16_graphic_h
 
21
#define included_sw16_graphic_h
 
22
 
 
23
#include <SDL.h>
 
24
#include <map>
 
25
#include <string>
 
26
#include <vector>
 
27
#include "geometry.h"
 
28
#include "graphic.h"
 
29
#include "rendertarget.h"
 
30
#include "rgbcolor.h"
 
31
 
 
32
class AnimationData;
 
33
class EncodeData;
 
34
 
 
35
namespace Renderer_Software16
 
36
{
 
37
 
 
38
SDL_Surface* LoadImage(std::string filename);
 
39
 
 
40
/*
 
41
class AnimationGfx
 
42
 
 
43
The graphics belonging to an animation
 
44
*/
 
45
struct AnimFrame {
 
46
        int             width;
 
47
        int             height;
 
48
        Point           hotspot;
 
49
        ushort* data;
 
50
};
 
51
 
 
52
class AnimationGfx {
 
53
private:
 
54
        int                     m_nrframes;
 
55
        AnimFrame*      m_frames;
 
56
 
 
57
public:
 
58
        AnimationGfx(const AnimationData* data);
 
59
        ~AnimationGfx();
 
60
 
 
61
        inline int get_nrframes() const { return m_nrframes; }
 
62
        inline const AnimFrame* get_frame(int i) const
 
63
        { assert(i>=0 && i<m_nrframes); return &m_frames[i]; }
 
64
 
 
65
private:
 
66
        void encode(AnimFrame* frame, SDL_Surface* bmp, const EncodeData* encdata);
 
67
};
 
68
 
 
69
 
 
70
/*
 
71
struct Bitmap
 
72
 
 
73
Represents a simple bitmap without managing its memory.
 
74
The rendering functions do NOT perform any clipping; this is up to the caller.
 
75
*/
 
76
struct Bitmap {
 
77
        ushort* pixels;
 
78
        int             w, h;
 
79
        int             pitch;
 
80
        bool            hasclrkey;
 
81
        ushort  clrkey;
 
82
 
 
83
        void clear();
 
84
        void draw_rect(Rect rc, RGBColor clr);
 
85
        void fill_rect(Rect rc, RGBColor clr);
 
86
        void brighten_rect(Rect rc, int factor);
 
87
 
 
88
        void blit(Point dst, Bitmap* src, Rect srcrc);
 
89
 
 
90
        void draw_minimap(Editor_Game_Base* egbase, const std::vector<bool>* visibility, Rect rc, Coords viewpt, uint flags);
 
91
        void draw_animframe(Point dst, const AnimFrame* frame, Rect rc, const RGBColor* plrclrs);
 
92
 
 
93
        // sw16_terrain.cc
 
94
        void draw_field(Field * const f, Field * const rf, Field * const fl, Field * const rfl,
 
95
                        Field * const lf, Field * const ft,
 
96
                        const int posx, const int rposx, const int posy,
 
97
                        const int blposx, const int rblposx, const int blposy,
 
98
                        uchar roads, bool render_r, bool render_b);
 
99
};
 
100
 
 
101
 
 
102
/** class Colormap
 
103
*
 
104
* Colormap contains a palette and lookup table for use with ground textures.
 
105
*/
 
106
class Colormap {
 
107
//    friend class Texture;
 
108
 
 
109
private:
 
110
        SDL_Color palette[256];
 
111
 
 
112
        unsigned short *colormap;       // maps 8 bit color and brightness value to the shaded 16 bit color
 
113
                // NOTE: brightness is currently 8 bits. Restricting brightness
 
114
                // to 64 or less shades would greatly reduce the size of this
 
115
                // table, and thus improve memory cache impact inside the renderer.
 
116
 
 
117
public:
 
118
        Colormap (const SDL_Color *);
 
119
        ~Colormap ();
 
120
 
 
121
        SDL_Color* get_palette() { return palette; }
 
122
 
 
123
        unsigned short *get_colormap () const { return colormap; }
 
124
};
 
125
 
 
126
 
 
127
/** class Texture
 
128
*
 
129
* Texture represents are terrain texture, which is strictly
 
130
* TEXTURE_W by TEXTURE_H pixels in size. It uses 8 bit color, and a pointer
 
131
* to the corresponding palette and color lookup table is provided.
 
132
*
 
133
* Currently, this is initialized from a 16 bit bitmap. This should be
 
134
* changed to load 8 bit bitmaps directly.
 
135
*/
 
136
class Texture {
 
137
private:
 
138
        Colormap*                       m_colormap;
 
139
        uint                                    m_nrframes;
 
140
        unsigned char*          m_pixels;
 
141
        uint                                    m_frametime;
 
142
        unsigned char*          m_curframe;
 
143
   std::string                  m_texture_picture;
 
144
 
 
145
public:
 
146
        Texture (const char* fnametempl, uint frametime);
 
147
        ~Texture ();
 
148
 
 
149
   inline const char* get_texture_picture(void) { return m_texture_picture.c_str(); }
 
150
 
 
151
        unsigned char *get_pixels () const { return m_pixels; }
 
152
        unsigned char* get_curpixels() const { return m_curframe; }
 
153
        unsigned short *get_colormap () const { return m_colormap->get_colormap(); }
 
154
 
 
155
        unsigned short get_minimap_color(char shade);
 
156
 
 
157
        void animate(uint time);
 
158
};
 
159
 
 
160
/*
 
161
 * This contains all the road textures needed to render roads
 
162
 */
 
163
struct Road_Textures {
 
164
   uint pic_road_normal;
 
165
   uint pic_road_busy;
 
166
};
 
167
 
 
168
/*
 
169
class RenderTargetImpl
 
170
 
 
171
The 16-bit software renderer implementation of the RenderTarget interface
 
172
*/
 
173
class RenderTargetImpl : public RenderTarget {
 
174
        Bitmap* m_bitmap;               // the target surface
 
175
        Rect            m_rect;                 // the current clip rectangle
 
176
        Point           m_offset;               // drawing offset
 
177
 
 
178
public:
 
179
        RenderTargetImpl(Bitmap* bmp);
 
180
        virtual ~RenderTargetImpl();
 
181
 
 
182
        void reset();
 
183
 
 
184
        virtual void get_window(Rect* rc, Point* ofs) const;
 
185
        virtual void set_window(const Rect& rc, const Point& ofs);
 
186
        virtual bool enter_window(const Rect& rc, Rect* previous, Point* prevofs);
 
187
 
 
188
        virtual int get_w() const;
 
189
        virtual int get_h() const;
 
190
 
 
191
   virtual void draw_line(int x1, int y1, int x2, int y2, RGBColor color);
 
192
        virtual void draw_rect(int x, int y, int w, int h, RGBColor clr);
 
193
        virtual void fill_rect(int x, int y, int w, int h, RGBColor clr);
 
194
        virtual void brighten_rect(int x, int y, int w, int h, int factor);
 
195
        virtual void clear();
 
196
 
 
197
        void doblit(Point dst, Bitmap* src, Rect srcrc);
 
198
 
 
199
        virtual void blit(int dstx, int dsty, uint picture);
 
200
        virtual void blitrect(int dstx, int dsty, uint picture,
 
201
                              int srcx, int srcy, int w, int h);
 
202
        virtual void tile(int x, int y, int w, int h, uint picture, int ofsx, int ofsy);
 
203
 
 
204
        virtual void rendermap(Editor_Game_Base* egbase, const std::vector<bool>* visibility, Point viewofs);
 
205
        virtual void renderminimap(Editor_Game_Base* egbase, const std::vector<bool>* visibility, Coords viewpt, uint flags);
 
206
 
 
207
        virtual void drawanim(int dstx, int dsty, uint animation, uint time, const RGBColor* plrclrs = 0);
 
208
        virtual void drawanimrect(int dstx, int dsty, uint animation, uint time,
 
209
                                                                          const RGBColor* plrclrs, int srcx, int srcy, int w, int h);
 
210
};
 
211
 
 
212
 
 
213
/*
 
214
class GraphicImpl
 
215
 
 
216
The 16-bit software renderer implementation of the Graphic interface.
 
217
*/
 
218
#define MAX_RECTS 20
 
219
 
 
220
class GraphicImpl : public Graphic {
 
221
public:
 
222
        GraphicImpl(int w, int h, bool fullscreen);
 
223
        virtual ~GraphicImpl();
 
224
 
 
225
        // General management
 
226
        virtual int get_xres();
 
227
        virtual int get_yres();
 
228
        virtual RenderTarget* get_render_target();
 
229
        virtual void update_fullscreen();
 
230
        virtual void update_rectangle(int x, int y, int w, int h);
 
231
        virtual bool need_update();
 
232
        virtual void refresh();
 
233
 
 
234
        virtual void flush(int mod);
 
235
 
 
236
        // Pictures
 
237
        virtual uint get_picture(int mod, const char* fname, bool);
 
238
   virtual void use_clrkey(uint pic, bool);
 
239
   virtual bool has_clrkey(uint);
 
240
   virtual void flush_picture(uint pic);
 
241
        virtual void get_picture_size(uint pic, int* pw, int* ph);
 
242
        virtual uint create_surface(int w, int h);
 
243
        virtual uint create_surface(int w, int h, RGBColor clrkey);
 
244
        virtual void free_surface(uint pic);
 
245
        virtual RenderTarget* get_surface_renderer(uint pic);
 
246
   virtual void save_pic_to_file(uint, FileWrite*);
 
247
   virtual uint load_pic_from_file(FileRead*, int);
 
248
 
 
249
        Bitmap* get_picture_bitmap(uint id);
 
250
 
 
251
        // Map textures
 
252
        virtual uint get_maptexture(const char* fnametempl, uint frametime);
 
253
        virtual void animate_maptextures(uint time);
 
254
        Texture* get_maptexture_data(uint id);
 
255
 
 
256
   // Road textures
 
257
   Bitmap* get_road_texture( int );
 
258
      
 
259
        // Animations
 
260
        virtual void load_animations();
 
261
        AnimationGfx* get_animation(uint anim);
 
262
        virtual int get_animation_nr_frames(uint anim);
 
263
        virtual void get_animation_size(uint anim, uint time, int* w, int* h);
 
264
 
 
265
        // Misc functions
 
266
        virtual void screenshot(const char* fname);
 
267
 
 
268
        virtual uint get_picture(int mod, int w, int h, const ushort* data, RGBColor clrkey);
 
269
        virtual const char* get_maptexture_picture(uint id);
 
270
 
 
271
private:
 
272
        uint find_free_picture();
 
273
 
 
274
        struct Picture {
 
275
                int             mod; // 0 if unused, -1 for surfaces, PicMod_* bitmask for pictures
 
276
                Bitmap  bitmap;
 
277
 
 
278
                union {
 
279
                        char*                                   fname;
 
280
                        RenderTargetImpl*       rendertarget;
 
281
                } u;
 
282
 
 
283
                Picture() { mod = 0; u.fname = 0; bitmap.pixels = 0; }
 
284
        };
 
285
        typedef std::map<std::string, uint> picmap_t;
 
286
 
 
287
        SDL_Surface*                    m_sdlsurface;
 
288
        int                  m_screen_pixels_size_in_bytes;
 
289
   bool                 m_lock_sdl_surface;
 
290
        Bitmap                                  m_screen;
 
291
        RenderTargetImpl*               m_rendertarget;
 
292
   SDL_Rect                                     m_update_rects[MAX_RECTS];
 
293
        int                                             m_nr_update_rects;
 
294
        bool                                            m_update_fullscreen;
 
295
 
 
296
        std::vector<Picture>    m_pictures;
 
297
        picmap_t                                        m_picturemap; // hash of filename/picture ID pairs
 
298
 
 
299
   Road_Textures*        m_roadtextures;
 
300
        std::vector<Texture*>           m_maptextures;
 
301
        std::vector<AnimationGfx*>      m_animations;
 
302
};
 
303
 
 
304
#define get_graphicimpl() (static_cast<Renderer_Software16::GraphicImpl*>(g_gr))
 
305
 
 
306
 
 
307
}
 
308
 
 
309
#endif // included_sw16_graphic_h