03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

This sets up the font used to draw the tiles, and then draws each tile in turn. Originally, I put<br />

the UseFont() and DiscardFont() around each DrawText() call in my DrawTile()<br />

function below. This means that UseFont() and DiscardFont() are being called many<br />

times more than they need to be – a waste, because this is a surprisingly expensive call.<br />

When I took them out of the loop, as shown above, it speeded up redraw performance<br />

dramatically.<br />

DrawTile() itself works in stages:<br />

� First, determine the on-screen rectangle to use for the tile.<br />

� Then, determine the brush color, depending on whether the tile is unknown (white),<br />

known (blue), or hit, that is, shot at (red).<br />

� If the square is a ship (not sea or unknown), determine a character to use to represent<br />

it (depending on the ship type), a pen color to draw in (depending on whether it has<br />

been hit or not), and then draw the character in the square.<br />

� If the square is not a ship, then just draw the rectangle with the brush color and a null<br />

pen.<br />

� If the square is the cursor square, draw the cursor.<br />

Here it is: first, determine the rectangle:<br />

void CFleetView::DrawTile(TInt aX, TInt aY) const<br />

{<br />

CWindowGc& gc = SystemGc();<br />

TRect rect(iBoardRect.iTl.iX + aX*iTileSize,<br />

iBoardRect.iTl.iY + aY*iTileSize,<br />

iBoardRect.iTl.iX + (aX + 1)*iTileSize,<br />

iBoardRect.iTl.iY + (aY + 1)*iTileSize);<br />

This uses iBoardRect as the rectangle for the entire grid, and iTileSize (a TInt) as the<br />

size of a tile. The calculation is simple.<br />

Then, determine the brush color:<br />

// Set background color depending on whether known, hit or otherwise<br />

gc.SetBrushStyle(CGraphicsContext::ESolidBrush);<br />

if(!iFleet->IsKnown(aX, aY))<br />

gc.SetBrushColor(KRgbWhite);<br />

else if(iFleet->IsHit(aX, aY))<br />

gc.SetBrushColor(KRgbDarkRed);<br />

else gc.SetBrushColor(KRgbCyan);<br />

No problems here. Next, I decide whether I need to draw a letter (for part of a ship) or a<br />

blank square (for anything else):<br />

// Draw either plain square or text<br />

if(iFleet->IsShip(aX, aY))<br />

{<br />

// Set pen color depending on whether hit or not<br />

gc.SetPenStyle(CGraphicsContext::ESolidPen);<br />

if(iFleet->IsHit(aX, aY))

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!