Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_renderer_gl.h
1 // ================================================================================
2 // == This file is a part of Turbo Badger. (C) 2011-2014, Emil SegerÃ¥s ==
3 // == See tb_core.h for more information. ==
4 // ================================================================================
5 
6 #ifndef TB_RENDERER_GL_H
7 #define TB_RENDERER_GL_H
8 
9 #include "tb_types.h"
10 
11 #if defined(TB_RENDERER_GL)
12 
13 #if defined(TB_RENDERER_GLES_1) && defined(TB_RENDERER_GLES_2)
14 #error "Both GLES_1 and GLES_2 defined"
15 #elif defined(TB_RENDERER_GLES_1) && defined(TB_RENDERER_GL3)
16 #error "Both GLES_1 and GL3 defined"
17 #elif defined(TB_RENDERER_GLES_2) && defined(TB_RENDERER_GL3)
18 #error "Both GLES_2 and GL3 defined"
19 #endif
20 
21 #ifdef TB_RENDERER_GLES_1
22 
23 #if defined(ANDROID) || defined(__ANDROID__)
24 #include <GLES/gl.h>
25 #else
26 #include <EGL/egl.h>
27 #include <GLES/gl.h>
28 #endif
29 
30 #elif defined(TB_RENDERER_GLES_2)
31 
32 #ifndef GL_GLEXT_PROTOTYPES
33 #define GL_GLEXT_PROTOTYPES 1
34 #endif
35 #if defined(__APPLE__)
36 #include <OpenGLES/ES2/gl.h>
37 #include <OpenGLES/ES2/glext.h>
38 #elif defined(__ANDROID__) || defined (__EMSCRIPTEN__)
39 #include <GLES2/gl2.h>
40 #include <GLES2/gl2ext.h>
41 #else
42 #include <GLES/gl.h>
43 #endif
44 #if defined(ANDROID)
45 #include <android/ndk-version.h>
46 #endif
47 #if defined(ANDROID) && __NDK_MAJOR__ < 19
48 extern PFNGLBINDVERTEXARRAYOESPROC glBindVertexArrayOESEXT;
49 extern PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArraysOESEXT;
50 extern PFNGLGENVERTEXARRAYSOESPROC glGenVertexArraysOESEXT;
51 #define glDeleteVertexArrays glDeleteVertexArraysOESEXT
52 #define glGenVertexArrays glGenVertexArraysOESEXT
53 #define glBindVertexArray glBindVertexArrayOESEXT
54 #else
55 #define glGenVertexArrays glGenVertexArraysOES
56 #define glBindVertexArray glBindVertexArrayOES
57 #define glDeleteVertexArrays glDeleteVertexArraysOES
58 #define glIsVertexArray glIsVertexArrayOES
59 #endif
60 
61 #elif defined(TB_RENDERER_GL3)
62 
63 #if defined(__APPLE__)
64 #include <OpenGL/gl3.h>
65 #elif defined (_WIN32)
66 #ifndef GLEW_STATIC
67 #error "GLEW_STATIC NOT DEFINED!!!"
68 #endif
69 #define GL3_PROTOTYPES 1
70 #include <GL/glew.h>
71 //#include <GL3/gl3.h>
72 #else
73 #define GL_GLEXT_PROTOTYPES 1
74 #include <GL/gl.h>
75 #endif
76 
77 #else // Standard GL1.1 Renderer
78 
79 #if defined(_WIN32)
80 #include <windows.h> // make gl.h compile
81 #include <GL/glew.h>
82 #include <GL/gl.h>
83 #elif defined(TB_SYSTEM_MACOSX) || defined(TB_SYSTEM_IOS)
84 #include <OpenGL/gl.h>
85 #else
86 #include <GL/gl.h>
87 #endif
88 
89 #endif
90 
91 #include "renderers/tb_renderer_batcher.h"
92 
93 namespace tb {
94 
95 class TBRendererGL;
96 
97 class TBBitmapGL : public TBBitmap
98 {
99 public:
100  TBBitmapGL(TBRendererGL *renderer);
101  ~TBBitmapGL();
102  bool Init(int width, int height, uint32_t *data);
103  virtual int Width() { return m_w; }
104  virtual int Height() { return m_h; }
105  virtual void SetData(uint32_t *data);
106 public:
107  TBRendererGL *m_renderer;
108  int m_w, m_h;
109  GLuint m_texture;
110 };
111 
112 class TBRendererGL : public TBRendererBatcher
113 {
114 public:
115  TBRendererGL();
116  virtual ~TBRendererGL();
117 
118  // == TBRenderer ====================================================================
119 
120  virtual void BeginPaint(int render_target_w, int render_target_h);
121  virtual void EndPaint();
122 
123  virtual TBBitmap *CreateBitmap(int width, int height, uint32_t *data);
124 
125  // == TBRendererBatcher ===============================================================
126 
127  virtual void RenderBatch(Batch *batch);
128  virtual void SetClipRect(const TBRect &rect);
129 
130 #if defined(TB_RENDERER_GLES_2) || defined(TB_RENDERER_GL3)
131 private:
132  static const GLuint _NUM_VBOS = 256;
133  GLuint LoadShader(GLenum type, const GLchar * shaderSrc);
134  GLuint m_program;
135  bool m_hasvao;
136  GLuint m_vao[_NUM_VBOS];
137  GLuint m_vbo[_NUM_VBOS];
138  float m_ortho[16];
139  GLuint _vboidx;
140  GLint m_orthoLoc;
141  GLint m_texLoc;
142  TBBitmapGL m_white;
143 #endif
144 };
145 
146 } // namespace tb
147 
148 #endif // TB_RENDERER_GL
149 #endif // TB_RENDERER_GL_H