13703f85d53f672c53b87fcc339a44028cf69395
[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
33 class SurfaceImpl;
34 class SurfaceSDL;
35 class SurfaceOpenGL;
36
37 /** This class holds all the data necessary to construct a surface */
38 class SurfaceData 
39 {
40 public:
41   enum ConstructorType { LOAD, LOAD_PART, SURFACE };
42   ConstructorType type;
43   SDL_Surface* surface;
44   std::string file;
45   int use_alpha;
46   int x;
47   int y;
48   int w;
49   int h;
50
51   SurfaceData(SDL_Surface* surf, int use_alpha_);
52   SurfaceData(const std::string& file_, int use_alpha_);
53   SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, int use_alpha_);
54   ~SurfaceData();
55
56   SurfaceSDL* create_SurfaceSDL();
57   SurfaceOpenGL* create_SurfaceOpenGL();
58   SurfaceImpl* create();
59 };
60
61 /** Container class that holds a surface, necessary so that we can
62     switch Surface implementations (OpenGL, SDL) on the fly */
63 class Surface
64 {
65 public:
66   SurfaceData data;
67   SurfaceImpl* impl;
68   int w; 
69   int h;
70   
71   typedef std::list<Surface*> Surfaces;
72   static Surfaces surfaces;
73 public:
74   static void reload_all();
75
76   Surface(SDL_Surface* surf, int use_alpha);  
77   Surface(const std::string& file, int use_alpha);  
78   Surface(const std::string& file, int x, int y, int w, int h, int use_alpha);
79   ~Surface();
80   
81   /** Reload the surface, which is necesarry in case of a mode swich */
82   void reload();
83
84   void draw(float x, float y, Uint8 alpha = 255, bool update = false);
85   void draw_bg(Uint8 alpha = 255, bool update = false);
86   void draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha = 255, bool update = false);
87 };
88
89 /** Surface implementation, all implementation have to inherit from
90     this class */
91 class SurfaceImpl
92 {
93 protected:
94   SDL_Surface* sdl_surface;
95
96 public:
97   int w;
98   int h;
99
100 public:
101   SurfaceImpl();
102   virtual ~SurfaceImpl();
103   
104   /** Return 0 on success, -2 if surface needs to be reloaded */
105   virtual int draw(float x, float y, Uint8 alpha, bool update) = 0;
106   virtual int draw_bg(Uint8 alpha, bool update) = 0;
107   virtual int draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, bool update) = 0;
108
109   SDL_Surface* get_sdl_surface() const; // @evil@ try to avoid this function
110 };
111
112 class SurfaceSDL : public SurfaceImpl
113 {
114 public:
115   SurfaceSDL(SDL_Surface* surf, int use_alpha);
116   SurfaceSDL(const std::string& file, int use_alpha);  
117   SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha);
118   virtual ~SurfaceSDL();
119
120   int draw(float x, float y, Uint8 alpha, bool update);
121   int draw_bg(Uint8 alpha, bool update);
122   int draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, bool update);
123 };
124
125 class SurfaceOpenGL : public SurfaceImpl
126 {
127 public:
128   unsigned gl_texture;
129
130 public:
131   SurfaceOpenGL(SDL_Surface* surf, int use_alpha);
132   SurfaceOpenGL(const std::string& file, int use_alpha);  
133   SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha);
134   virtual ~SurfaceOpenGL();
135
136   int draw(float x, float y, Uint8 alpha, bool update);
137   int draw_bg(Uint8 alpha, bool update);
138   int draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, bool update);
139
140 private:
141   void create_gl(SDL_Surface * surf, GLuint * tex);
142 };
143
144 #endif /*SUPERTUX_TEXTURE_H*/
145
146 /* Local Variables: */
147 /* mode: c++ */
148 /* End: */