Renders all game objects in OpenGL. The class wxSettlersGLCanvas is the top-level OpenGL canvas in the game. It maintains a map of IDrawObject objects, sorted by draw priority. Each IDrawObject is is responsible for drawing a single IGameObject. Each time the scene is rendered, each IDrawObject is asked if it can be deleted, which happens when the IGameObject it refers to is destroyed. If the IDrawObject can be deleted, it is removed from the map and destroyed. If it is valid, the IDrawObject is rendered to the screen.
This class also handles game object selection in OpenGL. When a Rule signals to the View that a user needs to make a selection of a game object, it sends a SelectionObject via the Controller with the selection information. wxSettlersGLCanvas listens for that Event and then maintains the given SelectionObject until a valid selection is made. It then executes the Rule associated with the SelectionObject via the RuleEngine and destroys the no longer needed SelectionObject. In this way, the wxSettlersGLCanvas is allowed to know absolutely nothing about any particular game object, but simply passes the information along to the appropriate Rule for it to decide what to do.
Also provided with wxSettlersGLCanvas is a utility function to convert any IDrawObject into a wxBitmap of that object. This allows for any game object to be represented in a bitmap image on the fly without having to create the bitmap externally and load it into the application. The object is rendered in the back buffer of the OpenGL canvas, read into the internal memory of a wxImage, scaled, and then converted into the outgoing wxBitmap.
SettlersGLCanvas.h
Renders all game objects in OpenGL. | |
The wxSettlersGLCanvas constructor. | |
The wxSettlersGLCanvas destructor. | |
Converts an IDrawObject into a wxBitmap of the given size. | |
Renders the scene. | |
Does object selection checking based on the given window coordinates. | |
Handles game object selection. | |
Creates IDrawObjects for new IGameObjects and stores them in the priority draw map. | |
Stores the given SelectionObject and then prepares the canvas for selection. | |
A std::multimap of the IDrawObjects to render. | |
The current SelectionObject, if any. |
The wxSettlersGLCanvas constructor. | |
The wxSettlersGLCanvas destructor. | |
Converts an IDrawObject into a wxBitmap of the given size. |
|
The wxSettlersGLCanvas constructor. Adds OnGameObjects as a receiver of the eventGameObjects Event, and OnSelectionObject as a receiver of the eventSelectionObject Event. Then calls the DrawObjectEngine to obtain all OpenGL resources needed.
| parent | The parent window (must not be NULL). |
| id | The id for message handling. |
| pos | The starting position. |
| size | The starting size. |
| style | The window style, using wxWindow parameters. |
| name | The name. |
| attrib | Starting OpenGL implementation attributes. |
| virtual ~wxSettlersGLCanvas() |
The wxSettlersGLCanvas destructor. The destructor removes all receivers from the Controller, and then calls the DrawObjectEngine to release all obtained OpenGL resources.
|
Converts an IDrawObject into a wxBitmap of the given size. ConvertGLtoWX takes a given IDrawObject, renders it in OpenGL in the back buffer of the current context, and then converts the rendered image into a wxImage object with the given width and height dimensions. The trick is that the OpenGL rendering area is created much larger than the desired dimensions. The pixels are then read from OpenGL into a wxImage of the larger size, which is then scaled down to meet the required size. This removes any jagged aliasing lines that might have existed in the rendered OpenGL image. Finally, the wxImage is converted into the outgoing wxBitmap.
| object | The IDrawObject to render. |
| width | The width of the desired wxBitmap. |
| height | The height of the desired wxBitmap. |
| eye | The eye position for the camera when rendering the IDrawObject. ConvertGLtoWX assumes that the object will be centered on (0, 0, 0). |
A wxBitmap containing the converted image.
Renders the scene. | |
Does object selection checking based on the given window coordinates. | |
Handles game object selection. | |
Creates IDrawObjects for new IGameObjects and stores them in the priority draw map. | |
Stores the given SelectionObject and then prepares the canvas for selection. | |
A std::multimap of the IDrawObjects to render. | |
The current SelectionObject, if any. |
| virtual void DrawScene() |
Renders the scene. First applies the world rotation to the current matrix and then traverses the entire priority map of IDrawObjects, rendering each one, unless it should be deleted, in which case it is removed from the map.
|
Does object selection checking based on the given window coordinates. Determines the origin point in world coordinates and the ray from that point into the world. Calls CanSelect on each IDrawObject, selecting the object nearest the origin. along the ray.
| x | The x coordinate of the current mouse position. |
| y | The y coordinate of the current mouse position. |
The OpenGL id of nearest object, or -1 if none.
|
Handles game object selection. If a valid object was selected, the current SelectionObject is reset, and the Rule that it referred to is called with the ID of the selected object as its DataObject parameter.
| id | The OpenGL id of the selected object. |
|
Creates IDrawObjects for new IGameObjects and stores them in the priority draw map. Calls the DrawObjectEngine for each IGameObject in the given AggregatorObject. If a valid IDrawObject is returned from the DrawObjectEngine, the IDrawObject is added into the priority draw map according to its priority and then drawn in the next render pass. Triggered by the eventGameObjects Event.
|
Stores the given SelectionObject and then prepares the canvas for selection. Calls OnGameObjects with the given SelectionObject so that any needed visual representation in the scene is created. Turns on selection handling by calling SetHitTest in the base class wxFrameworkGLCanvas. Triggered by the eventSelectionObject Event.
| object | The new SelectionObject. |
| PriorityMap mPriorityMap |
A std::multimap of the IDrawObjects to render. A std::multimap is ideal in this situation because it allows multiple objects with the same key. Since it does not matter which order objects of the same priority are drawn in, just adding new objects to the multimap with their priorities keeps them sorted in proper priority order.
| SelectionObjectPtr mSelectionObject |
The current SelectionObject, if any. It is maintained here because if it was not, it would go out of scope, be destroyed, and then any IDrawObject that referred to it would be destoryed in the next render pass, making it worthless. Therefore, it persists in the wxSettlersGLCanvas until something is selected, at which point it is destroyed.
|
| virtual ~wxSettlersGLCanvas() |
|
| virtual void DrawScene() |
|
|
|
|
| PriorityMap mPriorityMap |
| SelectionObjectPtr mSelectionObject |