- fixed problem with last_menu not being able to handle menues deeper than two submenues
[supertux.git] / src / texture.h
1 //
2 // C Interface: texture
3 //
4 // Description: 
5 //
6 //
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12
13 #ifndef SUPERTUX_TEXTURE_H
14 #define SUPERTUX_TEXTURE_H
15
16 #include <SDL.h>
17 #include <string>
18 #ifndef NOOPENGL
19 #include <SDL_opengl.h>
20 #endif
21
22 #include <list>
23 #include "screen.h"
24
25 class SurfaceImpl;
26 class SurfaceSDL;
27 class SurfaceOpenGL;
28
29 /** This class holds all the data necessary to construct a surface */
30 class SurfaceData 
31 {
32 public:
33   enum ConstructorType { LOAD, LOAD_PART, SURFACE };
34   ConstructorType type;
35   SDL_Surface* surface;
36   std::string file;
37   int use_alpha;
38   int x;
39   int y;
40   int w;
41   int h;
42
43   SurfaceData(SDL_Surface* surf, int use_alpha_);
44   SurfaceData(const std::string& file_, int use_alpha_);
45   SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, int use_alpha_);
46   ~SurfaceData();
47
48   SurfaceSDL* create_SurfaceSDL();
49   SurfaceOpenGL* create_SurfaceOpenGL();
50   SurfaceImpl* create();
51 };
52
53 /** Container class that holds a surface, necessary so that we can
54     switch Surface implementations (OpenGL, SDL) on the fly */
55 class Surface
56 {
57 public:
58   SurfaceData data;
59   SurfaceImpl* impl;
60   int w; 
61   int h;
62   
63   typedef std::list<Surface*> Surfaces;
64   static Surfaces surfaces;
65 public:
66   static void reload_all();
67
68   Surface(SDL_Surface* surf, int use_alpha);  
69   Surface(const std::string& file, int use_alpha);  
70   Surface(const std::string& file, int x, int y, int w, int h, int use_alpha);
71   ~Surface();
72   
73   /** Reload the surface, which is necesarry in case of a mode swich */
74   void reload();
75
76   void draw(float x, float y, Uint8 alpha = 255, bool update = false);
77   void draw_bg(Uint8 alpha = 255, bool update = false);
78   void draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha = 255, bool update = false);
79 };
80
81 /** Surface implementation, all implementation have to inherit from
82     this class */
83 class SurfaceImpl
84 {
85 public:
86   SDL_Surface* sdl_surface;
87   int w;
88   int h;
89
90 public:
91   /** Return 0 on success, -2 if surface needs to be reloaded */
92   virtual int draw(float x, float y, Uint8 alpha, bool update) = 0;
93   virtual int draw_bg(Uint8 alpha, bool update) = 0;
94   virtual int draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, bool update) = 0;
95 };
96
97 class SurfaceSDL : public SurfaceImpl
98 {
99 public:
100   
101 public:
102   SurfaceSDL(SDL_Surface* surf, int use_alpha);
103   SurfaceSDL(const std::string& file, int use_alpha);  
104   SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha);
105   virtual ~SurfaceSDL();
106
107   int draw(float x, float y, Uint8 alpha, bool update);
108   int draw_bg(Uint8 alpha, bool update);
109   int draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, bool update);
110 };
111
112 class SurfaceOpenGL : public SurfaceImpl
113 {
114 public:
115   unsigned gl_texture;
116
117 public:
118   SurfaceOpenGL(SDL_Surface* surf, int use_alpha);
119   SurfaceOpenGL(const std::string& file, int use_alpha);  
120   SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha);
121   virtual ~SurfaceOpenGL();
122
123   int draw(float x, float y, Uint8 alpha, bool update);
124   int draw_bg(Uint8 alpha, bool update);
125   int draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, bool update);
126
127 private:
128   void create_gl(SDL_Surface * surf, GLuint * tex);
129 };
130
131 #endif /*SUPERTUX_TEXTURE_H*/
132
133 /* Local Variables: */
134 /* mode: c++ */
135 /* End: */