1 #pragma once
2 
3 #ifdef USE_TILE_LOCAL
4 
5 #include "tiletex.h"
6 
7 #include <stdlib.h>
8 #include <vector>
9 
10 namespace opengl
11 {
12     bool flush_opengl_errors();
13     bool check_texture_size(const char *name, int width, int height);
14 }
15 
16 struct coord_def;
17 
18 struct GLW_2VF
19 {
GLW_2VFGLW_2VF20     GLW_2VF() {}
GLW_2VFGLW_2VF21     GLW_2VF(float l, float m) : x(l), y(m) {}
22 
setGLW_2VF23     inline void set(float l, float m)
24     {
25         x = l;
26         y = m;
27     }
28 
setGLW_2VF29     inline void set(const GLW_2VF &in)
30     {
31         x = in.x;
32         y = in.y;
33     }
34 
35     float x;
36     float y;
37 };
38 
39 struct GLW_3VF
40 {
GLW_3VFGLW_3VF41     GLW_3VF() {}
GLW_3VFGLW_3VF42     GLW_3VF(float l, float m, float n) : x(l), y(m), z(n) {}
GLW_3VFGLW_3VF43     GLW_3VF(float l, float m) : x(l), y(m), z(0.0) {}
44 
45     inline void set(float l, float m, float n = 0.0)
46     {
47         x = l;
48         y = m;
49         z = n;
50     }
51 
setGLW_3VF52     inline void set(const GLW_3VF &in)
53     {
54         x = in.x;
55         y = in.y;
56         z = in.z;
57     }
58 
59     float x;
60     float y;
61     float z;
62 };
63 
64 enum drawing_modes
65 {
66     GLW_LINES,
67     GLW_RECTANGLE,
68 };
69 
70 // Convenience structure for passing around primitives (lines or quads)
71 class GLWPrim
72 {
73 public:
74     // Constructor assumes we're always going to have a position
75     GLWPrim(float sx, float sy, float ex, float ey, float z = 0.0f) :
pos_sx(sx)76             pos_sx(sx), pos_sy(sy), pos_ex(ex), pos_ey(ey), pos_z(z),
77             tex_sx(0.0f), tex_sy(0.0f), tex_ex(0.0f), tex_ey(0.0f),
78             col_s(VColour::white), col_e(VColour::white) {}
79 
set_tex(float sx,float sy,float ex,float ey)80     inline void set_tex(float sx, float sy, float ex, float ey)
81     {
82         tex_sx = sx;
83         tex_sy = sy;
84         tex_ex = ex;
85         tex_ey = ey;
86     }
87 
set_col(const VColour & vc)88     inline void set_col(const VColour &vc)
89     {
90         col_s = vc;
91         col_e = vc;
92     }
93 
set_col(const VColour & s,const VColour & e)94     inline void set_col(const VColour &s, const VColour &e)
95     {
96         col_s = s;
97         col_e = e;
98     }
99 
100     float pos_sx, pos_sy, pos_ex, pos_ey, pos_z;
101     float tex_sx, tex_sy, tex_ex, tex_ey;
102     VColour col_s, col_e;
103 };
104 
105 // This struct defines all of the state that any particular rendering needs.
106 // If other rendering states are needed, they should be added here so that
107 // they do not introduce unneeded side effects for other parts of the code
108 // that have not thought about turning that new state off.
109 
110 struct GLState
111 {
112     GLState();
113     GLState(const GLState &state);
114 
115     const GLState &operator=(const GLState &state);
116     bool operator==(const GLState &state) const;
117 
118     // vertex arrays
119     bool array_vertex;
120     bool array_texcoord;
121     bool array_colour;
122 
123     // render state
124     bool blend;
125     bool texture;
126     bool depthtest;
127     bool alphatest;
128     unsigned char alpharef;
129     VColour colour;
130 };
131 
132 class GLStateManager
133 {
134 public:
135     // To silence pre 4.3 g++ compiler warnings
~GLStateManager()136     virtual ~GLStateManager() {}
137 
138     // Note: the init/shutdown static functions should be written in the derived
139     // implementation-specific cc file, e.g. glwrapper-ogl.cc.
140     static void init();
141     static void shutdown();
142 
143     // State Manipulation
144     virtual void set(const GLState& state) = 0;
145     virtual void pixelstore_unpack_alignment(unsigned int bpp) = 0;
146     virtual void reset_view_for_redraw() = 0;
147     virtual void reset_view_for_resize(const coord_def &m_windowsz,
148                                        const coord_def &m_drawablesz) = 0;
149     virtual void set_transform(const GLW_3VF &trans, const GLW_3VF &scale) = 0;
150     virtual void reset_transform() = 0;
151     virtual void get_transform(GLW_3VF *trans, GLW_3VF *scale) = 0;
152     virtual void set_scissor(int x, int y, unsigned int w, unsigned int h) = 0;
153     virtual void reset_scissor() = 0;
154 #ifdef __ANDROID__
155     virtual void fixup_gl_state() = 0;
156 #endif
157 
158     // Texture-specific functinos
159     virtual void delete_textures(size_t count, unsigned int *textures) = 0;
160     virtual void generate_textures(size_t count, unsigned int *textures) = 0;
161     virtual void bind_texture(unsigned int texture) = 0;
162     virtual void load_texture(unsigned char *pixels, unsigned int width,
163                               unsigned int height, MipMapOptions mip_opt,
164                               int xoffset=-1, int yoffset=-1) = 0;
165 
166     virtual int logical_to_device(int n) const = 0;
167     virtual int device_to_logical(int n, bool round=true) const = 0;
168 
169     // Debug
170 #ifdef ASSERTS
171     static bool _valid(int num_verts, drawing_modes mode);
172 #endif
173 };
174 
175 class GenericTexture;
176 
177 class GLShapeBuffer
178 {
179 public:
~GLShapeBuffer()180     virtual ~GLShapeBuffer() {}
181 
182     // Static Constructors
183     // Note: the init/shutdown static functions should be written in the derived
184     // implementation-specific cc file, e.g. glwrapper-ogl.cc.
185     static GLShapeBuffer *create(bool texture = false, bool colour = false,
186                                  drawing_modes prim = GLW_RECTANGLE);
187 
188     virtual const char *print_statistics() const = 0;
189     virtual unsigned int size() const = 0;
190 
191     // Add a primitive to be drawn.
192     virtual void add(const GLWPrim &prim) = 0;
193 
194     // Draw all the primitives in the buffer.
195     virtual void draw(const GLState &state) = 0;
196 
197     // Clear all the primitives from the buffer.
198     virtual void clear() = 0;
199 };
200 
201 // Main interface for GL functions
202 extern GLStateManager *glmanager;
203 
204 #endif // use_tile
205