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