wxSettlersGLCanvas

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.

Derived From

wxFrameworkGLCanvas

Project

GLCore

Include

SettlersGLCanvas.h

Summary
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.

Public

Summary
The wxSettlersGLCanvas constructor.
The wxSettlersGLCanvas destructor.
Converts an IDrawObject into a wxBitmap of the given size.

Constructors

wxSettlersGLCanvas

wxSettlersGLCanvas(wxWindow *parent, 
const wxWindowID id = -1,
const wxPoint &pos = wxDefaultPosition,
const wxSize &size = wxDefaultSize,
long style = 0,
const wxString &name = wxT("wxSettlersGLCanvas"),
wxInt32 *attrib = NULL)

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.

Parameters

parentThe parent window (must not be NULL).
idThe id for message handling.
posThe starting position.
sizeThe starting size.
styleThe window style, using wxWindow parameters.
nameThe name.
attribStarting OpenGL implementation attributes.

Destructor

~wxSettlersGLCanvas

virtual ~wxSettlersGLCanvas()

The wxSettlersGLCanvas destructor.  The destructor removes all receivers from the Controller, and then calls the DrawObjectEngine to release all obtained OpenGL resources.

Functions

ConvertGLtoWX

wxBitmap ConvertGLtoWX(const DrawObjectPtr &object,
const wxInt32 width,
const wxInt32 height,
const Vector &eye)

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.

Parameters

objectThe IDrawObject to render.
widthThe width of the desired wxBitmap.
heightThe height of the desired wxBitmap.
eyeThe eye position for the camera when rendering the IDrawObject.  ConvertGLtoWX assumes that the object will be centered on (0, 0, 0).

Returns

A wxBitmap containing the converted image.

Private

Summary
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.

Functions

Virtual Functions

DrawScene

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.

Select

virtual wxInt32 Select(const wxInt32 x,
const wxInt32 y) const

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.

Parameters

xThe x coordinate of the current mouse position.
yThe y coordinate of the current mouse position.

Returns

The OpenGL id of nearest object, or -1 if none.

SelectObject

virtual void SelectObject(wxInt32 id)

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.

Parameters

idThe OpenGL id of the selected object.

Game Event Functions

OnGameObjects

void OnGameObjects(AggregatorObjectPtr 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.

OnSelectionObject

void OnSelectionObject(SelectionObjectPtr object)

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.

Parameters

objectThe new SelectionObject.

Variables

mPriorityMap

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.

mSelectionObject

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.

wxSettlersGLCanvas(wxWindow *parent, 
const wxWindowID id = -1,
const wxPoint &pos = wxDefaultPosition,
const wxSize &size = wxDefaultSize,
long style = 0,
const wxString &name = wxT("wxSettlersGLCanvas"),
wxInt32 *attrib = NULL)
The wxSettlersGLCanvas constructor.
virtual ~wxSettlersGLCanvas()
The wxSettlersGLCanvas destructor.
wxBitmap ConvertGLtoWX(const DrawObjectPtr &object,
const wxInt32 width,
const wxInt32 height,
const Vector &eye)
Converts an IDrawObject into a wxBitmap of the given size.
The interface for objects that are drawn on screen.
virtual void DrawScene()
Renders the scene.
virtual wxInt32 Select(const wxInt32 x,
const wxInt32 y) const
Does object selection checking based on the given window coordinates.
virtual void SelectObject(wxInt32 id)
Handles game object selection.
void OnGameObjects(AggregatorObjectPtr object)
Creates IDrawObjects for new IGameObjects and stores them in the priority draw map.
An interface for all game objects.
void OnSelectionObject(SelectionObjectPtr object)
Stores the given SelectionObject and then prepares the canvas for selection.
The base class for all selection-related IGameObjects.
PriorityMap mPriorityMap
A std::multimap of the IDrawObjects to render.
SelectionObjectPtr mSelectionObject
The current SelectionObject, if any.
The default implementation of IRule for normal Game actions.
The communication mechanism between the View and the Model.
A generic implementation of the Observer design pattern.
The default implementation of the IRuleEngine interface.
A class that provides mouse handling, camera, and selection for an OpenGL window.
GLCore provides the underlying window classes and tools needed to create and use an OpenGL window with a camera, lighting, selection, mouse/keyboard handling, and IDrawObject rendering.
The standard implementation of the IDrawObjectEngine interface.
A generic container for passing any kind of data around.
Aggregates IGameObjects so they can be sent to the renderer in one batch.