03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Calculate board size in twips, and hence scale factor<br />

TInt boardSizeInTwips =<br />

iCoeEnv->ScreenDevice()->HorizontalPixelsToTwips(boardSize);<br />

boardSizeInTwips = (boardSizeInTwips * iZoomFactor) / 1000; // Zoom<br />

TInt scaleFactor = (boardSizeInTwips * 1000) /<br />

KIdealBoardSizeInTwips;<br />

boardSize =<br />

iCoeEnv->ScreenDevice()-<br />

>HorizontalTwipsToPixels(boardSizeInTwips);<br />

The HorizontalPixelsToTwips() function converts a given number of twips into pixels.<br />

This is a device-specific thing: I use the screen device to give me the right answer. Then, I<br />

scale the board size (in twips) by the zoom factor to get the board size I will really be using.<br />

Next, I calculate a scale factor: the ratio of the board size I will be using (in twips) to the ideal<br />

board size (in twips). I will be applying the same scale factor to the tiles and border,<br />

individually.<br />

For both the scale factor and the zoom factor, I use the number 1000 to indicate one-to-one.<br />

This means I can do a scaling calculation using code such as<br />

scaled_number = (old_number * scale) / 1000;<br />

with integer values. This is preferable to doing the calculations in floating- point numbers.<br />

This kind of 1000-based scaling is used elsewhere in Symbian OS graphics programming,<br />

as we'll see when we look at the TZoomFactor class, in <strong>Chapter</strong> 15. Watch out, though, for<br />

two possible sources of error:<br />

� Always multiply by the scale first, and then divide by 1000 – otherwise, you will lose<br />

precision.<br />

� Ensure that the number to be scaled, combined with the scale factor, is less than the<br />

maximum possible integer – otherwise, you'll get an overflow.<br />

The number 1000 was chosen to be sufficiently large to enable reasonable precision, but<br />

sufficiently small to reduce the risk of overflow. With a zoom factor of ten to one, and a twips<br />

value the size of a Letter paper, scaling calculations will reach 15 840 (twips high) × 10<br />

000(ten-to-one scale) = 158 400 000, which still leaves a good deal of room before you hit<br />

the 2.1 billion maximum signed integer value.<br />

Finally, I calculate a board size in pixels by calling the opposite function to the one I had<br />

before – HorizontalTwipsToPixels().<br />

Note<br />

I am assuming that pixels on the screen are square. This assumption is<br />

pretty safe.<br />

Next, I calculate the real tile and border sizes, in twips, by applying the same scale factor as<br />

was used for the board (I'll be using these values later, to calculate the point sizes of fonts):<br />

// Tile and border sizes also<br />

TInt tileSizeInTwips = (KTileSizeInTwips*scaleFactor) / 1000;<br />

TInt borderSizeInTwips = (KBorderSizeInTwips*scaleFactor) / 1000;<br />

Then, I have to calculate the sizes, in pixels, of the tiles and border.

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

Saved successfully!

Ooh no, something went wrong!