Added filter to horizontal flip Surfaces.
[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   void apply_filter_to_surface(SDL_Surface *surface, int filter, int value);
40   SDL_Surface* sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, bool use_alpha);
41   SDL_Surface* sdl_surface_from_nothing();
42
43   class SurfaceImpl;
44   class SurfaceSDL;
45   class SurfaceOpenGL;
46   class DrawingContext;
47   
48   /// bitset for drawing effects
49   enum {
50     /** Don't apply anything */
51     NONE_EFFECT       = 0x0000,
52     /** Draw the Surface upside down */
53     VERTICAL_FLIP     = 0x0001,
54     /** Draw the Surface from left to down */
55     HORIZONTAL_FLIP   = 0x0002,
56     /** Draw the Surface with alpha equal to 128 */
57     SEMI_TRANSPARENT  = 0x0004
58   };
59
60   /// types of filters
61   enum {
62     HORIZONTAL_FLIP_FILTER,
63     MASK_FILTER,
64     NONE_FILTER
65   };
66
67   /** This class holds all the data necessary to construct a surface */
68   class SurfaceData
69     {
70     public:
71       enum ConstructorType { LOAD, LOAD_PART, SURFACE, GRADIENT };
72       ConstructorType type;
73       SDL_Surface* surface;
74       std::string file;
75       bool use_alpha;
76       int x;
77       int y;
78       int w;
79       int h;
80       Color top_gradient;
81       Color bottom_gradient;
82
83       SurfaceData(SDL_Surface* surf, bool use_alpha_);
84       SurfaceData(const std::string& file_, bool use_alpha_);
85       SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, bool use_alpha_);
86       SurfaceData(Color top_gradient_, Color bottom_gradient_, int w_, int h_);
87       ~SurfaceData();
88
89       SurfaceSDL* create_SurfaceSDL();
90       SurfaceOpenGL* create_SurfaceOpenGL();
91       SurfaceImpl* create();
92     };
93
94
95   /// Surface
96   /** Container class that holds a surface, necessary so that we can
97       switch Surface implementations (OpenGL, SDL) on the fly */
98   class Surface
99     {
100     public:
101       SurfaceData data;
102       SurfaceImpl* impl;
103       int w;
104       int h;
105
106       typedef std::list<Surface*> Surfaces;
107       static Surfaces surfaces;
108     public:
109       static void reload_all();
110       static void debug_check();
111
112       Surface(SDL_Surface* surf, bool use_alpha);
113       Surface(const std::string& file, bool use_alpha);
114       Surface(const std::string& file, int x, int y, int w, int h, bool use_alpha);
115       Surface(Color top_gradient, Color bottom_gradient, int w_, int h_);
116       ~Surface();
117
118       /** Reload the surface, which is necesarry in case of a mode swich */
119       void reload();
120
121       void resize(int widht, int height);
122
123       void apply_filter(int filter, Color color = Color(0,0,0));
124     };
125
126   /** Surface implementation, all implementation have to inherit from
127       this class */
128   class SurfaceImpl
129     {
130     protected:
131       SDL_Surface* sdl_surface;
132
133     public:
134       int w;
135       int h;
136
137     public:
138       SurfaceImpl();
139       virtual ~SurfaceImpl();
140
141       /** Return 0 on success, -2 if surface needs to be reloaded */
142       virtual int draw(float x, float y, Uint8 alpha, Uint32 effect = NONE_EFFECT) = 0;
143       virtual int draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, Uint32 effect = NONE_EFFECT) = 0;
144       virtual int draw_stretched(float x, float y, int w, int h, Uint8 alpha, Uint32 effect = NONE_EFFECT) = 0;
145
146
147       int resize(int w_, int h_);
148
149       SDL_Surface* get_sdl_surface() const; // @evil@ try to avoid this function
150
151       virtual void apply_filter(int filter, Color color = Color(0,0,0)) = 0;
152     };
153
154   class SurfaceSDL : public SurfaceImpl
155     {
156     public:
157       SurfaceSDL(SDL_Surface* surf, bool use_alpha);
158       SurfaceSDL(const std::string& file, bool use_alpha);
159       SurfaceSDL(const std::string& file, int x, int y, int w, int h, bool use_alpha);
160       SurfaceSDL(Color top_gradient, Color bottom_gradient, int w, int h);
161       virtual ~SurfaceSDL();
162
163       int draw(float x, float y, Uint8 alpha, Uint32 effect = NONE_EFFECT);
164       int draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, Uint32 effect = NONE_EFFECT);
165       int draw_stretched(float x, float y, int w, int h, Uint8 alpha, Uint32 effect = NONE_EFFECT);
166
167       void apply_filter(int filter, Color color);
168     };
169
170 #ifndef NOOPENGL
171   class SurfaceOpenGL : public SurfaceImpl
172     {
173     public:
174       GLuint gl_texture;
175
176     public:
177       SurfaceOpenGL(SDL_Surface* surf, bool use_alpha);
178       SurfaceOpenGL(const std::string& file, bool use_alpha);
179       SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, bool use_alpha);
180       SurfaceOpenGL(Color top_gradient, Color bottom_gradient, int w, int h);
181
182       virtual ~SurfaceOpenGL();
183
184       int draw(float x, float y, Uint8 alpha, Uint32 effect = NONE_EFFECT);
185       int draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, Uint32 effect = NONE_EFFECT);
186       int draw_stretched(float x, float y, int w, int h, Uint8 alpha, Uint32 effect = NONE_EFFECT);
187
188       void apply_filter(int filter, Color color);
189
190     private:
191       void create_gl(SDL_Surface * surf, GLuint * tex);
192     };
193 #endif
194
195 } //namespace SuperTux
196
197 #endif /*SUPERTUX_TEXTURE_H*/
198
199 /* Local Variables: */
200 /* mode: c++ */
201 /* End: */