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