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