Game Development Reference
In-Depth Information
nothing stopping you from including a string member in the debug build of the class.
You can solve this problem by writing a bit of debug code that detects multiple con-
trols with the same ID. Your code should simply assert so you can go find the prob-
lem and redefine the offending identifier.
Hit Testing and Focus Order
There are two ways that controls know they are the center of your attention. The first
way is via a hit test. This is where you use a pointer or a cursor and position it over
the control by an analog device such as a mouse. This method is prevalent in desktop
games, especially games that have a large number of controls on the screen.
The second method uses a focus order. Only one control has the focus at any one
time, and each control can get the focus by an appropriate movement of the input
device. If the right key or button is pressed, the control with focus sends a message
to the parent screen. This is how most console games are designed, and it clearly
limits the number and density of controls on each screen.
Hit testing usually falls into three categories: rectangular hit testing, polygonal hit
testing, and bitmap collision testing. Bitmap collision isn
t too hard, but it is a little
beyond the scope of this chapter. The other two are really easy. The rectangle hit test
is brain-dead simple. You just make sure your hit test includes the entire rectangle,
not just the inside. If a rectangle
'
s coordinates were (15,4) and (30,35), then a hit
should be registered both at (15,4) and (30,35).
The hit test for a 2D polygon is not too complicated. The following algorithm was
adapted from Graphics Gems and assumes the polygon is closed. This adaptation
uses a p oint structure and STL to clarify the original algorithm. It will work on
any arbitrary polygons, convex or concave:
'
#include <vector>
struct Point
{
int x, y;
Point() {x=y=0;}
Point(int _x, int _y) { x = _x; y = _y; }
};
typedef std::vector<Point> Polygon;
bool PointInPoly( Point const &test, const Polygon & polygon)
{
Point newPoint, oldPoint;
Point left, right;
 
 
Search WWH ::




Custom Search