Final AI tweaks.
[NALCG.git] / include / OgreCEGUIRenderer.h
blobc7e34111359fabb86bb233fafc00f798958a8e1f
1 /************************************************************************
2 filename: OgreCEGUIRenderer.h
3 created: 11/5/2004
4 author: Paul D Turner
6 purpose: Interface for main Ogre GUI renderer class
7 *************************************************************************/
8 /*************************************************************************
9 Crazy Eddie's GUI System (http://www.cegui.org.uk)
10 Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 2.1 of the License, or (at your option) any later version.
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *************************************************************************/
26 /*************************************************************************
27 This file contains code that is specific to Ogre (http://www.ogre3d.org)
28 *************************************************************************/
29 #ifndef _OgreCEGUIRenderer_h_
30 #define _OgreCEGUIRenderer_h_
32 #include <CEGUI/CEGUIBase.h>
33 #include <CEGUI/CEGUIRenderer.h>
34 #include <CEGUI/CEGUITexture.h>
36 #include <OgreRenderQueueListener.h>
37 #include <OgreSceneManagerEnumerator.h>
38 #include <OgreTextureUnitState.h>
40 #if (OGRE_PLATFORM == OGRE_PLATFORM_WIN32) && !defined(OGRE_STATIC_LIB)
41 # ifdef OGRE_GUIRENDERER_EXPORTS
42 # define OGRE_GUIRENDERER_API __declspec(dllexport)
43 # else
44 # if defined(__MINGW32__)
45 # define OGRE_GUIRENDERER_API
46 # else
47 # define OGRE_GUIRENDERER_API __declspec(dllimport)
48 # endif
49 # endif
50 #elif defined ( OGRE_GCC_VISIBILITY )
51 # define OGRE_GUIRENDERER_API __attribute__ ((visibility("default")))
52 #else
53 # define OGRE_GUIRENDERER_API
54 #endif
57 // Start of CEGUI namespace section
58 namespace CEGUI
60 /*************************************************************************
61 Forward refs
62 *************************************************************************/
63 class OgreCEGUITexture;
64 class OgreCEGUIRenderer;
67 /*!
68 \brief
69 RenderQueueListener based class used to hook into the ogre rendering system
71 class _OgrePrivate CEGUIRQListener : public Ogre::RenderQueueListener
73 public:
74 CEGUIRQListener(OgreCEGUIRenderer* renderer, Ogre::uint8 queue_id, bool post_queue)
76 d_renderer = renderer;
77 d_queue_id = queue_id;
78 d_post_queue = post_queue;
81 virtual ~CEGUIRQListener() {}
83 virtual void renderQueueStarted(Ogre::uint8 id, const Ogre::String& invocation, bool& skipThisQueue);
84 virtual void renderQueueEnded(Ogre::uint8 id, const Ogre::String& invocation, bool& repeatThisQueue);
86 // methods for adjusting target queue settings
87 void setTargetRenderQueue(Ogre::uint8 queue_id) {d_queue_id = queue_id;}
88 void setPostRenderQueue(bool post_queue) {d_post_queue = post_queue;}
90 private:
91 /*************************************************************************
92 Implementation Data
93 *************************************************************************/
94 OgreCEGUIRenderer* d_renderer; //!< CEGUI renderer object for Ogre.
95 Ogre::uint8 d_queue_id; //!< ID of the queue that we are hooked into
96 bool d_post_queue; //!< true if we render after everything else in our queue.
101 \brief
102 Renderer class to interface with Ogre engine.
104 class OGRE_GUIRENDERER_API OgreCEGUIRenderer : public Renderer
106 public:
108 \brief
109 Constructor for renderer class that uses Ogre for rendering. Note that if
110 you use this option you must call setTargetSceneManager before rendering.
112 \param window
113 Pointer to an Ogre::RenderWindow object.
115 \param queue_id
116 Ogre::uint8 value that specifies where the GUI should appear in the ogre rendering output.
118 \param post_queue
119 set to true to have GUI rendered after render queue \a queue_id, or false to have the GUI rendered before render queue
120 \a queue_id.
122 \param max_quads
123 Obsolete. Set to 0.
126 OgreCEGUIRenderer(Ogre::RenderWindow* window,
127 Ogre::uint8 queue_id = Ogre::RENDER_QUEUE_OVERLAY,
128 bool post_queue = false, uint max_quads = 0);
132 \brief
133 Constructor for renderer class that uses Ogre for rendering.
135 \param window
136 Pointer to an Ogre::RenderWindow object.
138 \param queue_id
139 Ogre::uint8 value that specifies where the GUI should appear in the ogre rendering output.
141 \param post_queue
142 set to true to have GUI rendered after render queue \a queue_id, or false to have the GUI rendered before render queue
143 \a queue_id.
145 \param max_quads
146 Obsolete. Set to 0.
148 \param scene_manager
149 Pointer to an Ogre::SceneManager object that is to be used for GUI rendering.
151 OgreCEGUIRenderer(Ogre::RenderWindow* window, Ogre::uint8 queue_id, bool post_queue, uint max_quads, Ogre::SceneManager* scene_manager);
155 \brief
156 Destructor for Ogre renderer.
158 virtual ~OgreCEGUIRenderer(void);
162 // add's a quad to the list to be rendered
163 virtual void addQuad(const Rect& dest_rect, float z, const Texture* tex, const Rect& texture_rect, const ColourRect& colours, QuadSplitMode quad_split_mode);
165 // perform final rendering for all queued renderable quads.
166 virtual void doRender(void);
168 // clear the queue
169 virtual void clearRenderList(void);
173 \brief
174 Enable or disable the queueing of quads from this point on.
176 This only affects queueing. If queueing is turned off, any calls to addQuad will cause the quad to be rendered directly. Note that
177 disabling queueing will not cause currently queued quads to be rendered, nor is the queue cleared - at any time the queue can still
178 be drawn by calling doRender, and the list can be cleared by calling clearRenderList. Re-enabling the queue causes subsequent quads
179 to be added as if queueing had never been disabled.
181 \param setting
182 true to enable queueing, or false to disable queueing (see notes above).
184 \return
185 Nothing
187 virtual void setQueueingEnabled(bool setting) {d_queueing = setting;}
190 // create an empty texture
191 virtual Texture* createTexture(void);
193 // create a texture and load it with the specified file.
194 virtual Texture* createTexture(const String& filename, const String& resourceGroup = "General");
196 // create a texture and set it to the specified size
197 virtual Texture* createTexture(float size);
199 // create an OGRE resource provider.
200 virtual ResourceProvider* createResourceProvider(void);
202 // destroy the given texture
203 virtual void destroyTexture(Texture* texture);
205 // destroy all textures still active
206 virtual void destroyAllTextures(void);
210 \brief
211 Return whether queueing is enabled.
213 \return
214 true if queueing is enabled, false if queueing is disabled.
216 virtual bool isQueueingEnabled(void) const {return d_queueing;}
220 \brief
221 Return the current width of the display in pixels
223 \return
224 float value equal to the current width of the display in pixels.
226 virtual float getWidth(void) const {return d_display_area.getWidth();}
230 \brief
231 Return the current height of the display in pixels
233 \return
234 float value equal to the current height of the display in pixels.
236 virtual float getHeight(void) const {return d_display_area.getHeight();}
240 \brief
241 Return the size of the display in pixels
243 \return
244 Size object describing the dimensions of the current display.
246 virtual Size getSize(void) const {return d_display_area.getSize();}
250 \brief
251 Return a Rect describing the screen
253 \return
254 A Rect object that describes the screen area. Typically, the top-left values are always 0, and the size of the area described is
255 equal to the screen resolution.
257 virtual Rect getRect(void) const {return d_display_area;}
261 \brief
262 Return the maximum texture size available
264 \return
265 Size of the maximum supported texture in pixels (textures are always assumed to be square)
267 virtual uint getMaxTextureSize(void) const {return 2048;} // TODO: Change to proper value
271 \brief
272 Return the horizontal display resolution dpi
274 \return
275 horizontal resolution of the display in dpi.
277 virtual uint getHorzScreenDPI(void) const {return 96;}
281 \brief
282 Return the vertical display resolution dpi
284 \return
285 vertical resolution of the display in dpi.
287 virtual uint getVertScreenDPI(void) const {return 96;}
291 \brief
292 Set the scene manager to be used for rendering the GUI.
294 The GUI system will be unhooked from the current scene manager and attached to what ever
295 is specified here.
297 \param scene_manager
298 Pointer to an Ogre::SceneManager object that is the new target Ogre::SceneManager to be
299 used for GUI rendering.
301 \return
302 Nothing.
304 void setTargetSceneManager(Ogre::SceneManager* scene_manager);
308 \brief
309 Set the target render queue for GUI rendering.
311 \param queue_id
312 Ogre::uint8 value specifying the render queue that the GUI system should attach to.
314 \param post_queue
315 - true to specify that the GUI should render after everything else in render queue \a queue_id.
316 - false to specify the GUI should render before everything else in render queue \a queue_id.
318 \return
319 Nothing.
321 void setTargetRenderQueue(Ogre::uint8 queue_id, bool post_queue);
325 \brief
326 Create a texture from an existing Ogre::TexturePtr object.
328 \note
329 If you want to use an Ogre::RenderTexture (for putting rendered output onto Gui elements or other
330 advanced techniques), you can get the Ogre::TexturePtr to be used by calling Ogre::TextureManager::getByName()
331 passing the name returned from Ogre::RenderTexture::getName() (and casting the result as necessary).
333 \param texture
334 pointer to an Ogre::TexturePtr object to be used as the basis for the new CEGUI::Texture
336 \return
337 Pointer to the newly created CEGUI::TexturePtr object.
339 Texture* createTexture(Ogre::TexturePtr& texture);
343 \brief
344 Set the size of the display in pixels.
346 You do not have to call this method under normal operation as the system
347 will automatically extract the size from the current view port.
349 \note
350 This method will cause the EventDisplaySizeChanged event to fire if the
351 display size has changed.
353 \param sz
354 Size object describing the size of the display.
356 \return
357 Nothing.
359 void setDisplaySize(const Size& sz);
362 private:
363 /************************************************************************
364 Implementation Constants
365 ************************************************************************/
366 static const size_t VERTEX_PER_QUAD; //!< number of vertices per quad
367 static const size_t VERTEX_PER_TRIANGLE; //!< number of vertices for a triangle
368 static const size_t VERTEXBUFFER_INITIAL_CAPACITY; //!< initial capacity of the allocated vertex buffer
369 static const size_t UNDERUSED_FRAME_THRESHOLD; //!< number of frames to wait before shrinking buffer
371 /*************************************************************************
372 Implementation Structs & classes
373 *************************************************************************/
375 \brief
376 structure used for all vertices.
378 struct QuadVertex {
379 float x, y, z; //!< The position for the vertex.
380 Ogre::RGBA diffuse; //!< colour of the vertex
381 float tu1, tv1; //!< texture coordinates
385 \brief
386 structure holding details about a quad to be drawn
388 struct QuadInfo
390 Ogre::TexturePtr texture;
391 Rect position;
392 float z;
393 Rect texPosition;
394 uint32 topLeftCol;
395 uint32 topRightCol;
396 uint32 bottomLeftCol;
397 uint32 bottomRightCol;
399 QuadSplitMode splitMode;
401 bool operator<(const QuadInfo& other) const
403 // this is intentionally reversed.
404 return z > other.z;
409 /*************************************************************************
410 Implementation Methods
411 *************************************************************************/
412 // setup states etc
413 void initRenderStates(void);
415 // sort quads list according to texture
416 void sortQuads(void);
418 // render a quad directly to the display
419 void renderQuadDirect(const Rect& dest_rect, float z, const Texture* tex, const Rect& texture_rect, const ColourRect& colours, QuadSplitMode quad_split_mode);
421 // convert colour value to whatever the Ogre render system is expecting.
422 uint32 colourToOgre(const colour& col) const;
424 // perform main work of the constructor. This does everything except the final hook into the render system.
425 void constructor_impl(Ogre::RenderWindow* window, Ogre::uint8 queue_id, bool post_queue, uint max_quads);
428 /*************************************************************************
429 Implementation Data
430 *************************************************************************/
431 Rect d_display_area;
433 typedef std::multiset<QuadInfo> QuadList;
434 QuadList d_quadlist;
435 bool d_queueing; //!< setting for queueing control.
437 // Ogre specific bits.
438 Ogre::Root* d_ogre_root; //!< pointer to the Ogre root object that we attach to
439 Ogre::RenderSystem* d_render_sys; //!< Pointer to the render system for Ogre.
440 Ogre::uint8 d_queue_id; //!< ID of the queue that we are hooked into
441 Ogre::TexturePtr d_currTexture; //!< currently set texture;
442 Ogre::RenderOperation d_render_op; //!< Ogre render operation we use to do our stuff.
443 Ogre::HardwareVertexBufferSharedPtr d_buffer; //!< vertex buffer to queue sprite rendering
444 size_t d_underused_framecount; //!< Number of frames elapsed since buffer utilization was above half the capacity
445 Ogre::RenderOperation d_direct_render_op; //!< Renderop for cursor
446 Ogre::HardwareVertexBufferSharedPtr d_direct_buffer; //!< Renderop for cursor
447 Ogre::SceneManager* d_sceneMngr; //!< The scene manager we are hooked into.
448 Ogre::LayerBlendModeEx d_colourBlendMode; //!< Controls colour blending mode used.
449 Ogre::LayerBlendModeEx d_alphaBlendMode; //!< Controls alpha blending mode used.
450 Ogre::TextureUnitState::UVWAddressingMode d_uvwAddressMode;
452 CEGUIRQListener* d_ourlistener;
453 bool d_post_queue; //!< true if we render after everything else in our queue.
454 size_t d_bufferPos; //!< index into buffer where next vertex should be put.
455 bool d_sorted; //!< true when data in quad list is sorted.
456 Point d_texelOffset; //!< Offset required for proper texel mapping.
458 std::list<OgreCEGUITexture*> d_texturelist; //!< List used to track textures.
461 } // End of CEGUI namespace section
464 #endif // end of guard _OgreCEGUIRenderer_h_