- implemeted video-mode switching (this currently breaks shadow fonts, but shouldn...
[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
47   SurfaceSDL* create_SurfaceSDL();
48   SurfaceOpenGL* create_SurfaceOpenGL();
49   SurfaceImpl* create();
50 };
51
52 /** Container class that holds a surface, necessary so that we can
53     switch Surface implementations (OpenGL, SDL) on the fly */
54 class Surface
55 {
56 public:
57   SurfaceData data;
58   SurfaceImpl* impl;
59   int w; 
60   int h;
61   
62   typedef std::list<Surface*> Surfaces;
63   static Surfaces surfaces;
64 public:
65   static void reload_all();
66
67   Surface(SDL_Surface* surf, int use_alpha);  
68   Surface(const std::string& file, int use_alpha);  
69   Surface(const std::string& file, int x, int y, int w, int h, int use_alpha);
70   ~Surface();
71   
72   /** Reload the surface, which is necesarry in case of a mode swich */
73   void reload();
74
75   void draw(float x, float y, Uint8 alpha = 255, bool update = false);
76   void draw_bg(Uint8 alpha = 255, bool update = false);
77   void draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha = 255, bool update = false);
78 };
79
80 /** Surface implementation, all implementation have to inherit from
81     this class */
82 class SurfaceImpl
83 {
84 public:
85   SDL_Surface* sdl_surface;
86   int w;
87   int h;
88
89 public:
90   virtual void draw(float x, float y, Uint8 alpha, bool update) = 0;
91   virtual void draw_bg(Uint8 alpha, bool update) = 0;
92   virtual void draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, bool update) = 0;
93 };
94
95 class SurfaceSDL : public SurfaceImpl
96 {
97 public:
98   
99 public:
100   SurfaceSDL(SDL_Surface* surf, int use_alpha);
101   SurfaceSDL(const std::string& file, int use_alpha);  
102   SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha);
103   virtual ~SurfaceSDL();
104
105   void draw(float x, float y, Uint8 alpha, bool update);
106   void draw_bg(Uint8 alpha, bool update);
107   void draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, bool update);
108 };
109
110 class SurfaceOpenGL : public SurfaceImpl
111 {
112 public:
113   unsigned gl_texture;
114
115 public:
116   SurfaceOpenGL(SDL_Surface* surf, int use_alpha);
117   SurfaceOpenGL(const std::string& file, int use_alpha);  
118   SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha);
119   virtual ~SurfaceOpenGL();
120
121   void draw(float x, float y, Uint8 alpha, bool update);
122   void draw_bg(Uint8 alpha, bool update);
123   void draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, bool update);
124
125 private:
126   void create_gl(SDL_Surface * surf, GLuint * tex);
127 };
128
129 #endif /*SUPERTUX_TEXTURE_H*/
130
131 /* Local Variables: */
132 /* mode: c++ */
133 /* End: */