- beginnings of a wingling
[supertux.git] / src / 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 <list>
31 #include "screen.h"
32 #include "vector.h"
33
34 SDL_Surface* sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha);
35
36 class SurfaceImpl;
37 class SurfaceSDL;
38 class SurfaceOpenGL;
39
40 /** This class holds all the data necessary to construct a surface */
41 class SurfaceData 
42 {
43 public:
44   enum ConstructorType { LOAD, LOAD_PART, SURFACE };
45   ConstructorType type;
46   SDL_Surface* surface;
47   std::string file;
48   int use_alpha;
49   int x;
50   int y;
51   int w;
52   int h;
53
54   SurfaceData(SDL_Surface* surf, int use_alpha_);
55   SurfaceData(const std::string& file_, int use_alpha_);
56   SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, int use_alpha_);
57   ~SurfaceData();
58
59   SurfaceSDL* create_SurfaceSDL();
60   SurfaceOpenGL* create_SurfaceOpenGL();
61   SurfaceImpl* create();
62 };
63
64 /** Container class that holds a surface, necessary so that we can
65     switch Surface implementations (OpenGL, SDL) on the fly */
66 class Surface
67 {
68 public:
69   SurfaceData data;
70   SurfaceImpl* impl;
71   int w; 
72   int h;
73   
74   typedef std::list<Surface*> Surfaces;
75   static Surfaces surfaces;
76 public:
77   static void reload_all();
78   static void debug_check();
79
80   Surface(SDL_Surface* surf, int use_alpha);  
81   Surface(const std::string& file, int use_alpha);  
82   Surface(const std::string& file, int x, int y, int w, int h, int use_alpha);
83   ~Surface();
84   
85   /** Captures the screen and returns it as Surface*, the user is expected to call the destructor. */
86   static Surface* CaptureScreen();
87   
88   /** Reload the surface, which is necesarry in case of a mode swich */
89   void reload();
90
91   void draw(float x, float y, Uint8 alpha = 255, bool update = false);
92   void draw_bg(Uint8 alpha = 255, bool update = false);
93   void draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha = 255, bool update = false);
94   void draw_stretched(float x, float y, int w, int h, Uint8 alpha, bool update = false);
95   void resize(int w_, int h_);
96
97   /// conveniance function
98   void draw(const Vector& pos, Uint8 alpha = 255, bool update = false)
99   {
100     draw(pos.x, pos.y, alpha, update);
101   }
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, bool update) = 0;
121   virtual int draw_bg(Uint8 alpha, bool update) = 0;
122   virtual int draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, bool update) = 0;
123   virtual int draw_stretched(float x, float y, int w, int h, Uint8 alpha, bool update) = 0;
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, bool update);
138   int draw_bg(Uint8 alpha, bool update);
139   int draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, bool update);
140   int draw_stretched(float x, float y, int w, int h, Uint8 alpha, bool update);
141 };
142
143 #ifndef NOOPENGL
144 class SurfaceOpenGL : public SurfaceImpl
145 {
146 public:
147   unsigned gl_texture;
148
149 public:
150   SurfaceOpenGL(SDL_Surface* surf, int use_alpha);
151   SurfaceOpenGL(const std::string& file, int use_alpha);  
152   SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha);
153   virtual ~SurfaceOpenGL();
154
155   int draw(float x, float y, Uint8 alpha, bool update);
156   int draw_bg(Uint8 alpha, bool update);
157   int draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, bool update);
158   int draw_stretched(float x, float y, int w, int h, Uint8 alpha, bool update);
159
160 private:
161   void create_gl(SDL_Surface * surf, GLuint * tex);
162 };
163 #endif 
164
165 #endif /*SUPERTUX_TEXTURE_H*/
166
167 /* Local Variables: */
168 /* mode: c++ */
169 /* End: */