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