Crazy Eddies GUI System  0.6.2
openglrenderer.h
1 /***********************************************************************
2  filename: openglrenderer.h
3  created: 9/4/2004
4  author: Mark Strom
5  mwstrom@gmail.com
6 
7  purpose: Interface to Renderer implemented via Opengl
8 *************************************************************************/
9 /***************************************************************************
10  * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining
13  * a copy of this software and associated documentation files (the
14  * "Software"), to deal in the Software without restriction, including
15  * without limitation the rights to use, copy, modify, merge, publish,
16  * distribute, sublicense, and/or sell copies of the Software, and to
17  * permit persons to whom the Software is furnished to do so, subject to
18  * the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be
21  * included in all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
27  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
28  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29  * OTHER DEALINGS IN THE SOFTWARE.
30  ***************************************************************************/
31 #ifndef _openglrenderer_h_
32 #define _openglrenderer_h_
33 
34 #include "CEGUIBase.h"
35 
36 #if (defined( __WIN32__ ) || defined( _WIN32 )) && !defined(CEGUI_STATIC)
37 # ifdef OPENGL_GUIRENDERER_EXPORTS
38 # define OPENGL_GUIRENDERER_API __declspec(dllexport)
39 # else
40 # define OPENGL_GUIRENDERER_API __declspec(dllimport)
41 # endif
42 #else
43 # define OPENGL_GUIRENDERER_API
44 #endif
45 
46 
47 #if defined(_WIN32)// All this taken from glut.h
48 # ifndef APIENTRY
49 # define GLUT_APIENTRY_DEFINED
50 # if (_MSC_VER >= 800) || defined(_STDCALL_SUPPORTED) || defined(__BORLANDC__) || defined(__LCC__) || defined(__GNUC__)
51 # define APIENTRY __stdcall
52 # else
53 # define APIENTRY
54 # endif
55 # endif
56 /* XXX This is from Win32's <winnt.h> */
57 # ifndef CALLBACK
58 # if (defined(_M_MRX000) || defined(_M_IX86) || defined(_M_ALPHA) || defined(_M_PPC)) && !defined(MIDL_PASS) || defined(__LCC__) || defined(__GNUC__)
59 # define CALLBACK __stdcall
60 # else
61 # define CALLBACK
62 # endif
63 # endif
64 /* XXX Hack for lcc compiler. It doesn't support __declspec(dllimport), just __stdcall. */
65 # if defined( __LCC__ )
66 # undef WINGDIAPI
67 # define WINGDIAPI __stdcall
68 # else
69 /* XXX This is from Win32's <wingdi.h> and <winnt.h> */
70 # ifndef WINGDIAPI
71 # define GLUT_WINGDIAPI_DEFINED
72 # define WINGDIAPI __declspec(dllimport)
73 # endif
74 # endif
75 /* XXX This is from Win32's <ctype.h> */
76 # ifndef _WCHAR_T_DEFINED
77 typedef unsigned short wchar_t;
78 # define _WCHAR_T_DEFINED
79 # endif
80 # endif //win32 end glut.h stuff
81 
82 /* XXX Hack for finding headers in Apple's OpenGL framework. */
83 #if defined( __APPLE__ )
84 #include <OpenGL/gl.h>
85 #include <OpenGL/glu.h>
86 #else /* __APPLE__ */
87 #include <GL/gl.h>
88 #include <GL/glu.h>
89 #endif /* __APPLE__ */
90 #include <list>
91 #include <set>
92 
93 #include "CEGUIRenderer.h"
94 #include "CEGUITexture.h"
95 
96 
97 // #if defined(_WIN32)
98 // # if defined(_DEBUG)
99 // # pragma comment(lib, "CEGUIBase_d.lib")
100 // # else
101 // # pragma comment(lib, "CEGUIBase.lib")
102 // # endif
103 // #endif
104 
105 #if defined(_MSC_VER)
106 # pragma warning(push)
107 # pragma warning(disable : 4251)
108 #endif
109 
110 #define OGLRENDERER_VBUFF_CAPACITY 4096
111 
112 
113 // Start of CEGUI namespace section
114 namespace CEGUI
115 {
116  /*************************************************************************
117  Forward refs
118  *************************************************************************/
119  class OpenGLTexture;
120  class ImageCodec;
121  class DynamicModule;
122 
127  class OPENGL_GUIRENDERER_API OpenGLRenderer : public Renderer
128  {
129  public:
141  OpenGLRenderer(uint max_quads, ImageCodec* codec = 0);
159  OpenGLRenderer(uint max_quads,int width, int height, ImageCodec* codec = 0);
160 
161 
166  virtual ~OpenGLRenderer(void);
167 
168  // add's a quad to the list to be rendered
169  virtual void addQuad(const Rect& dest_rect, float z, const Texture* tex, const Rect& texture_rect, const ColourRect& colours, QuadSplitMode quad_split_mode);
170 
171  // perform final rendering for all queued renderable quads.
172  virtual void doRender(void);
173 
174  // clear the queue
175  virtual void clearRenderList(void);
176 
177 
193  virtual void setQueueingEnabled(bool setting) {d_queueing = setting;}
194 
195 
196  // create an empty texture
197  virtual Texture* createTexture(void);
198 
199  // create a texture and load it with the specified file.
200  virtual Texture* createTexture(const String& filename, const String& resourceGroup);
201 
202  // create a texture and set it to the specified size
203  virtual Texture* createTexture(float size);
204 
205  // destroy the given texture
206  virtual void destroyTexture(Texture* texture);
207 
208  // destroy all textures still active
209  virtual void destroyAllTextures(void);
210 
218  virtual bool isQueueingEnabled(void) const {return d_queueing;}
219 
220 
228  virtual float getWidth(void) const {return d_display_area.getWidth();}
229 
230 
238  virtual float getHeight(void) const {return d_display_area.getHeight();}
239 
240 
248  virtual Size getSize(void) const {return d_display_area.getSize();}
249 
250 
259  virtual Rect getRect(void) const {return d_display_area;}
260 
261 
269  virtual uint getMaxTextureSize(void) const {return d_maxTextureSize;}
270 
271 
279  virtual uint getHorzScreenDPI(void) const {return 96;}
280 
281 
289  virtual uint getVertScreenDPI(void) const {return 96;}
290 
291 
309  void setDisplaySize(const Size& sz);
310 
311 
318  void grabTextures(void);
319 
320 
325  void restoreTextures(void);
326 
331  ImageCodec& getImageCodec(void);
332 
333 
338  void setImageCodec(const String& codecName);
339 
349  void setImageCodec(ImageCodec* codec);
350 
351 
356  static void setDefaultImageCodecName(const String& codecName);
357 
362  static const String& getDefaultImageCodecName();
363 
364 
365  private:
366  /************************************************************************
367  Implementation Constants
368  ************************************************************************/
369  static const int VERTEX_PER_QUAD;
370  static const int VERTEX_PER_TRIANGLE;
371  static const int VERTEXBUFFER_CAPACITY;
372 
373  /*************************************************************************
374  Implementation Structs & classes
375  *************************************************************************/
376  struct MyQuad
377  {
378  float tex[2];
379  uint32 color;
380  float vertex[3];
381  };
382 
387  struct QuadInfo
388  {
389  GLuint texid;
390  Rect position;
391  float z;
392  Rect texPosition;
393  uint32 topLeftCol;
394  uint32 topRightCol;
395  uint32 bottomLeftCol;
396  uint32 bottomRightCol;
397 
398  QuadSplitMode splitMode;
399 
400  bool operator<(const QuadInfo& other) const
401  {
402  // this is intentionally reversed.
403  return z > other.z;
404  }
405 
406  };
407 
408 
409  /*************************************************************************
410  Implementation Methods
411  *************************************************************************/
412  // setup states etc
413  void initPerFrameStates(void);
414 
415  // restore states
416  void exitPerFrameStates(void);
417 
418  // render whatever is in the vertex buffer
419  void renderVBuffer(void);
420 
421  // sort quads list according to texture
422  void sortQuads(void);
423 
424  // render a quad directly to the display
425  void renderQuadDirect(const Rect& dest_rect, float z, const Texture* tex, const Rect& texture_rect, const ColourRect& colours, QuadSplitMode quad_split_mode);
426 
427  // convert colour value to whatever the OpenGL system is expecting.
428  uint32 colourToOGL(const colour& col) const;
429 
430  // set the module ID string
431  void setModuleIdentifierString();
432 
433  // setup image codec
434  void setupImageCodec(const String& codecName);
435 
436  // cleanup image codec
437  void cleanupImageCodec();
438 
439  /*************************************************************************
440  Implementation Data
441  *************************************************************************/
442  typedef std::multiset<QuadInfo> QuadList;
443  QuadList d_quadlist;
444 
445  Rect d_display_area;
446 
447  MyQuad myBuff[OGLRENDERER_VBUFF_CAPACITY];
448 
449  bool d_queueing;
450  uint d_currTexture;
451  int d_bufferPos;
452  bool d_sorted;
453 
454  std::list<OpenGLTexture*> d_texturelist;
455  GLint d_maxTextureSize;
456 
457  ImageCodec* d_imageCodec;
458  DynamicModule* d_imageCodecModule;
459 
460  static String d_defaultImageCodecName;
461 
462 
463  };
464 
465 } // End of CEGUI namespace section
466 
467 #if defined(_MSC_VER)
468 # pragma warning(pop)
469 #endif
470 
471 #endif // end of guard _openglrenderer_h_