- Fixed gradient not working when cached (SDL mode);
[supertux.git] / lib / video / surface.h
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20
21 #ifndef SUPERTUX_TEXTURE_H
22 #define SUPERTUX_TEXTURE_H
23
24 #include <string>
25 #include <list>
26
27 #ifndef NOOPENGL
28 #include "SDL_opengl.h"
29 #endif
30
31 #include "SDL.h"
32
33 #include "../math/vector.h"
34 #include "../video/screen.h"
35
36 namespace SuperTux
37   {
38   
39   SDL_Surface* sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, bool use_alpha);
40   SDL_Surface* sdl_surface_from_nothing();
41
42   class SurfaceImpl;
43   class SurfaceSDL;
44   class SurfaceOpenGL;
45   class DrawingContext;
46   
47   /// bitset for drawing effects
48   enum {
49     /** Don't apply anything */
50     NONE_EFFECT       = 0x0000,
51     /** Draw the Surface upside down */
52     VERTICAL_FLIP     = 0x0001,
53     /** Draw the Surface with alpha equal to 128 */
54     SEMI_TRANSPARENT  = 0x0002
55   };
56
57   /** This class holds all the data necessary to construct a surface */
58   class SurfaceData
59     {
60     public:
61       enum ConstructorType { LOAD, LOAD_PART, SURFACE, GRADIENT };
62       ConstructorType type;
63       SDL_Surface* surface;
64       std::string file;
65       bool use_alpha;
66       int x;
67       int y;
68       int w;
69       int h;
70       Color top_gradient;
71       Color bottom_gradient;
72
73       SurfaceData(SDL_Surface* surf, bool use_alpha_);
74       SurfaceData(const std::string& file_, bool use_alpha_);
75       SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, bool use_alpha_);
76       SurfaceData(Color top_gradient_, Color bottom_gradient_, int w_, int h_);
77       ~SurfaceData();
78
79       SurfaceSDL* create_SurfaceSDL();
80       SurfaceOpenGL* create_SurfaceOpenGL();
81       SurfaceImpl* create();
82     };
83
84
85   /// Surface
86   /** Container class that holds a surface, necessary so that we can
87       switch Surface implementations (OpenGL, SDL) on the fly */
88   class Surface
89     {
90     public:
91       SurfaceData data;
92       SurfaceImpl* impl;
93       int w;
94       int h;
95
96       typedef std::list<Surface*> Surfaces;
97       static Surfaces surfaces;
98     public:
99       static void reload_all();
100       static void debug_check();
101
102       Surface(SDL_Surface* surf, bool use_alpha);
103       Surface(const std::string& file, bool use_alpha);
104       Surface(const std::string& file, int x, int y, int w, int h, bool use_alpha);
105       Surface(Color top_gradient, Color bottom_gradient, int w_, int h_);
106       ~Surface();
107
108       /** Reload the surface, which is necesarry in case of a mode swich */
109       void reload();
110
111       void resize(int widht, int height);
112     };
113
114   /** Surface implementation, all implementation have to inherit from
115       this class */
116   class SurfaceImpl
117     {
118     protected:
119       SDL_Surface* sdl_surface;
120
121     public:
122       int w;
123       int h;
124
125     public:
126       SurfaceImpl();
127       virtual ~SurfaceImpl();
128
129       /** Return 0 on success, -2 if surface needs to be reloaded */
130       virtual int draw(float x, float y, Uint8 alpha, Uint32 effect = NONE_EFFECT) = 0;
131       virtual int draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, Uint32 effect = NONE_EFFECT) = 0;
132       virtual int draw_stretched(float x, float y, int w, int h, Uint8 alpha, Uint32 effect = NONE_EFFECT) = 0;
133
134
135       int resize(int w_, int h_);
136
137       SDL_Surface* get_sdl_surface() const; // @evil@ try to avoid this function
138     };
139
140   class SurfaceSDL : public SurfaceImpl
141     {
142     public:
143       SurfaceSDL(SDL_Surface* surf, bool use_alpha);
144       SurfaceSDL(const std::string& file, bool use_alpha);
145       SurfaceSDL(const std::string& file, int x, int y, int w, int h, bool use_alpha);
146       SurfaceSDL(Color top_gradient, Color bottom_gradient, int w, int h);
147       virtual ~SurfaceSDL();
148
149       int draw(float x, float y, Uint8 alpha, Uint32 effect = NONE_EFFECT);
150       int draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, Uint32 effect = NONE_EFFECT);
151       int draw_stretched(float x, float y, int w, int h, Uint8 alpha, Uint32 effect = NONE_EFFECT);
152     };
153
154 #ifndef NOOPENGL
155   class SurfaceOpenGL : public SurfaceImpl
156     {
157     public:
158       GLuint gl_texture;
159
160     public:
161       SurfaceOpenGL(SDL_Surface* surf, bool use_alpha);
162       SurfaceOpenGL(const std::string& file, bool use_alpha);
163       SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, bool use_alpha);
164       SurfaceOpenGL(Color top_gradient, Color bottom_gradient, int w, int h);
165
166       virtual ~SurfaceOpenGL();
167
168       int draw(float x, float y, Uint8 alpha, Uint32 effect = NONE_EFFECT);
169       int draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, Uint32 effect = NONE_EFFECT);
170       int draw_stretched(float x, float y, int w, int h, Uint8 alpha, Uint32 effect = NONE_EFFECT);
171
172
173     private:
174       void create_gl(SDL_Surface * surf, GLuint * tex);
175     };
176 #endif
177
178 } //namespace SuperTux
179
180 #endif /*SUPERTUX_TEXTURE_H*/
181
182 /* Local Variables: */
183 /* mode: c++ */
184 /* End: */