- Allow custom leveldots
[supertux.git] / src / video / surface.hpp
1 //  $Id: surface.h 2175 2004-11-24 19:02:49Z sik0fewl $
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 #ifndef SUPERTUX_TEXTURE_H
21 #define SUPERTUX_TEXTURE_H
22
23 #include <string>
24 #include <list>
25
26 #include <SDL.h>
27 #include <GL/gl.h>
28
29 #include "math/vector.hpp"
30 #include "video/screen.hpp"
31
32 void apply_filter_to_surface(SDL_Surface *surface, int filter, int value);
33 SDL_Surface* sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf);
34 SDL_Surface* sdl_surface_from_nothing();
35
36 class SurfaceImpl;
37 class SurfaceOpenGL;
38 class DrawingContext;
39   
40 /// bitset for drawing effects
41 enum {
42   /** Don't apply anything */
43   NONE_EFFECT       = 0x0000,
44   /** Draw the Surface upside down */
45   VERTICAL_FLIP     = 0x0001,
46   /** Draw the Surface from left to down */
47   HORIZONTAL_FLIP   = 0x0002,
48   /** Draw the Surface with alpha equal to 128 */
49   SEMI_TRANSPARENT  = 0x0004
50 };
51
52 /// types of filters
53 enum {
54   HORIZONTAL_FLIP_FILTER,
55   MASK_FILTER,
56   NONE_FILTER
57 };
58
59 /** This class holds all the data necessary to construct a surface */
60 class SurfaceData
61 {
62 public:
63   enum ConstructorType { LOAD, LOAD_PART, SURFACE, GRADIENT };
64   ConstructorType type;
65   SDL_Surface* surface;
66   std::string file;
67   
68   struct Filter { int type; Color color; };
69   std::vector<Filter> applied_filters;
70   
71   bool use_alpha;
72   int x;
73   int y;
74   int w;
75   int h;
76   Color top_gradient;
77   Color bottom_gradient;
78   
79   SurfaceData(SDL_Surface* surf, bool use_alpha_);
80   SurfaceData(const std::string& file_, bool use_alpha_);
81   SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, bool use_alpha_);
82   SurfaceData(Color top_gradient_, Color bottom_gradient_, int w_, int h_);
83   ~SurfaceData();
84   
85   SurfaceOpenGL* create_SurfaceOpenGL();
86   SurfaceImpl* create();
87 };
88
89
90 /** Container class that holds a surface, necessary so that we can
91  *  switch Surface implementations (OpenGL, SDL) on the fly
92  */
93 class Surface
94 {
95 public:
96   SurfaceImpl* impl;
97   SurfaceData data;
98   int w;
99   int h;
100   
101   typedef std::list<Surface*> Surfaces;
102   static Surfaces surfaces;
103 public:
104   static void reload_all();
105   static void debug_check();
106   
107   Surface(SDL_Surface* surf, bool use_alpha);
108   Surface(const std::string& file, bool use_alpha);
109   Surface(const std::string& file, int x, int y, int w, int h, bool use_alpha);
110   Surface(Color top_gradient, Color bottom_gradient, int w_, int h_);
111   ~Surface();
112   
113   /** Reload the surface, which is necesarry in case of a mode swich */
114   void reload();
115   
116   void apply_filter(int filter, Color color = Color(0,0,0));
117 };
118
119 /** Surface implementation, all implementation have to inherit from
120     this class */
121 class SurfaceImpl
122 {
123 protected:
124   SDL_Surface* sdl_surface;
125   
126 public:
127   int w;
128   int h;
129   
130 public:
131   SurfaceImpl();
132   virtual ~SurfaceImpl();
133   
134   /** Return 0 on success, -2 if surface needs to be reloaded */
135   virtual int draw(float x, float y, Uint8 alpha, Uint32 effect = NONE_EFFECT) = 0;
136   virtual int draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, Uint32 effect = NONE_EFFECT) = 0;
137   virtual int draw_stretched(float x, float y, int w, int h, Uint8 alpha, Uint32 effect = NONE_EFFECT) = 0;
138   
139   
140   SDL_Surface* get_sdl_surface() const; // @evil@ try to avoid this function
141   
142   virtual void apply_filter(int filter, Color color = Color(0,0,0)) = 0;
143 };
144
145 class SurfaceOpenGL : public SurfaceImpl
146 {
147 public:
148   GLuint gl_texture;
149   
150 public:
151   SurfaceOpenGL(SDL_Surface* surf);
152   SurfaceOpenGL(const std::string& file);
153   SurfaceOpenGL(const std::string& file, int x, int y, int w, int h);
154   SurfaceOpenGL(Color top_gradient, Color bottom_gradient, int w, int h);
155   
156   virtual ~SurfaceOpenGL();
157   
158   int draw(float x, float y, Uint8 alpha, Uint32 effect = NONE_EFFECT);
159   int draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, Uint32 effect = NONE_EFFECT);
160   int draw_stretched(float x, float y, int w, int h, Uint8 alpha, Uint32 effect = NONE_EFFECT);
161   
162   void apply_filter(int filter, Color color);
163   
164 private:
165   void create_gl(SDL_Surface * surf, GLuint * tex);
166 };
167
168 #endif