Wiley.Games.on.Symbian.OS.A.Handbook.for.Mobile.Development.Apr.2008 (779888), страница 28
Текст из файла (страница 28)
An application is notifiedwhen the screen configuration changes from one of the standard screenconfigurations to another. The following code illustrates how a UIQ gamemay deal with ‘flip close’ and ‘flip open’ configuration change events.void CMyView::HandleUiConfigChangedL(){CQikViewBase::HandleUiConfigChangedL();TQikUiConfig config = CQUiConfigClient::Static().CurrentConfig();switch(config.ScreenMode()){case EQikUiConfigPortrait:DoFlipOpen();110GRAPHICS ON SYMBIAN OSbreak;case EQikUiConfigSmallPortrait:DoFlipClose();break;default:break;}}3.10.6 Drawing APIs and ScalabilityTable 3.5 summarizes the drawing methods described in the chapter andhow they deal with scalability and orientation.Table 3.5 The scalability of drawing methods on Symbian OSGraphics/UI typeScalabilityNotesDrawing via WSERVManualDrawing using a windowserver graphics context canbe made scalable and willwork with rotated screenmodes.Drawing via WSERV– off-screen bitmapManualIt’s possible to create anoff-screen bitmap of thesame size as the screen, orone of fixed size.
However,it’s usually very inefficientto scale a bitmap which isnot a multiple of the screensize.Direct Screen Access +CFbsScreenDeviceManualBypasses WSERV, but stillallows the graphics contextto be used for drawing.Code which scales using astandard graphics contextcan scale with this schemealso.Direct Screen Access +frame bufferManualA lot of effort is required tosupport multiple screendepths and rotation.RENDERING TEXT111Table 3.5 (continued )Graphics/UI typeScalabilityAnti-tearing APIManual(CDirectScreenBitmap)OpenVG/Flash andOpenGL ESNotesProvides a fast way ofdrawing, synchronized withthe refresh rate of thescreen, but it alsorepresents a significanteffort if support for differentscreen sizes, depths andorientations are required.Automatic Used for 2D/3D vectorgraphics.3.11 Rendering TextSymbian OS is internationalized, and it provides a fairly mature textrendering subsystem which supports Unicode text and bi-directionalrendering.
Device manufacturers usually provide plug-ins in order torender scalable fonts depending on the region in which a handset isdeployed (for example Europe, the Middle East, or Asia Pacific).However, the heritage of games means that it’s common to use customfonts tailored to the look and feel of the game. Rarely does a standardTimes New Roman font fit in with the fun and aesthetics of a game, so agraphic designer typically provides a bitmap font.There are two common ways of rendering text in games, which are asfollows:• using the Symbian text drawing services which support arbitraryscalable fonts, Unicode text, and bi-directional rendering• copying pre-drawn characters from an image created by a graphicsdesigner.The first option is the most scalable across locales and market segments.In addition, since scalable fonts can be installed onto a device after it hasleft the factory, it’s possible for a game’s SIS file to include the installationof a custom font in order to differentiate the game.
However, renderinglarge amounts of text in this way may have an impact on performancecost and variability, particularly if characters are rendered into a cacheand the cost of re-rendering a character is significant.112GRAPHICS ON SYMBIAN OSThe second option used to be very common in games. The graphicsdesigner creates a character set within an image by creating a grid andconstraining the character drawings to that grid. A character is rendered bycalculating the grid reference of the character and blitting the fixedsizedrectangle to the correct destination.Figure 3.19 shows part of a fixed-size font stored as a large widebitmap.
section 3.9.3 shows some example code for extracting parts ofa bitmap.Figure 3.19 Selecting the character ‘F’ from a hand drawn fontUsing pre-drawn characters allows graphics designers to have ultimatecontrol of how a font looks on screen, and effects such as colors, shadows,outlines, and texture can easily be applied. However, this technique maynot be scalable for widely localized and distributed games, since eachpossible character must be hand drawn. Some of the system fonts builtinto Chinese language phones have thousands of characters.11The choice of methods is usually made by comparing the effort andtarget market with the resulting aesthetics. If a game will be localized tomany languages, then it’s not practical to supply a bitmap for each glyph.The next section details the text rendering facilities provided by Symbian OS, which deal with the rendering of Unicode text.3.11.1 Font and Text RenderingText-rendering API OverviewLibrary to link againstcone.libHeader to includecoetextdrawer.hRequired platform security capabilities NoneKey classesCCoeTextDrawerBase,XCoeTextDrawer11For a good introduction to this subject, please see Characters: A Brief Introduction atwww.bellevuelinux.org/character.html.RENDERING TEXT113Left end of textSpeedball 3000TPoint aPosition(x,y)Figure 3.20BaselineDescending characterRendering text from a coordinate pointaBoxLeft end of text (aligned left)aBaselineOffsetSpeedball 3000BaselineFigure 3.21 Rendering text within a boxA graphics context provides two text drawing primitives to allowyou to:• draw text from a coordinate point, shown in Figure 3.20This primitive draws the text from the coordinate point toward theright, justified according to the graphics context settings.
The x coordinate aligns with the left side of the first character; the y coordinatewith the baseline of the string.• draw text within a box, shown in Figure 3.21This primitive draws text within a box, justified and aligned accordingto the graphics context settings and parameters to the function call,and clipped to the box. Any other area within the box is painted withthe brush color.The text is drawn in the pen color and with drawing mode set up inthe graphics context, but pen style and pen width are ignored.The code fragment below demonstrates rendering based on thex,y position.// In this example, we use one of the standard font stylesconst CFont& font = ScreenFont(TCoeFont::LegendFont);gc.UseFont(fontUsed);gc.SetPenColor(KRgbBlack);TPoint pos(50,50);_LIT(KGameText,"Speedball 3000");gc.DrawText(KGameText, pos);114GRAPHICS ON SYMBIAN OSDrawing text within a box is very similar, but a bounding rectanglemust be supplied with an offset and an alignment.// Draw some text left justified in a box,// Offset so text is just inside top of boxTRect box(20,20,250,100);const TInt baseline = box.Height() /2 + fontUsed->AscentInPixels()/2;const TInt margin=10; // left margin is ten pixelsgc.SetBrushStyle(CGraphicsContext::ESolidBrush);gc.SetBrushColor(KRgbDarkGray);gc.SetPenColor(KRgbWhite);_LIT(KGameText,"Speedball 3000");gc.DrawText(KGameText,box,baseline,CGraphicsContext::ELeft,margin);3.11.2 Bi-directional TextTo support left-to-right writing languages (common in Europe) as wellas right-to-left languages (for example, Arabic and Hebrew) controlsthat draw text to the screen can make use of the bi-directional renderingsupport provided by Symbian OS.
When targeting multiple world regions,the use of the DrawText() method of CGraphicsContext andderived classes is now strongly discouraged in favor of the followingapproach.When drawing text in scripts that run from right-to-left, much moretext preparation is required than when drawing left-to-right scripts likeEnglish. These preparations include reordering the characters in the textfrom logical left-to-right to visual right-to-left order, as well as finding theright forms for the characters. For example, in Arabic, the character formdepends on the character that follows.To support bi-directional text presentation, controls that draw text onthe screen itself store the text in a TBidiText object, and the XCoeTextDrawer class is used to render to a graphics context.
TBidiTextobjects are created and modified outside the drawing function. The content would usually be loaded from a localizable resource file so thatmultiple languages can be supported in a single game binary.The following fragment shows how to set up two lines of text:_LIT(KTextToRender, "Speedball\n3000");TBidiText* iText = TBidiText::NewL(KTextToRender,2);The TBidiText class provides methods for setting the number of lines,line breaking characters and rendering direction of the text. AssumingPLAYING VIDEO CLIPS115iText is a TBidiText object, the code below demonstrates how torender the text using one of the stock fonts available.void CMyControl::Draw(const TRect& aRect){const CCoeFontProvider& fontProvider = FindFontProvider();const CFont& font = fontProvider.Font(TCoeFont::LegendFont(), AccumulatedZoom());XCoeTextDrawer textDrawer = TextDrawer();textDrawer->SetAlignment(EHCenterVCenter);textDrawer.DrawText(gc, iText, rect, font);}A key thing to note is that, unlike on previous versions of Symbian OS,on Symbian OS v9, a control can no longer keep a pointer or referenceto a CFont object because the zoom factor of the control may change,for instance, if the screen mode changes.Detailed descriptions of font selection and rendering can be found inthe S60 3rd Edition and UIQ 3 SDK documentation.3.12 Playing Video ClipsFull motion video has been a staple of console and PC games for along time, but is not that common in current mobile games becausestorage space and network costs have been at a premium.
However,some mobile handsets are now being equipped with 8 GB persistentstorage (for example, the Nokia N95 8 GB or the Nokia N81 8 GB)and others are accepting similarly large memory cards. It now becomesfeasible to construct games which contain video introductions and cutaway sequences (‘cut scenes’).The under-the-hood mechanics of video playing on Symbian smartphones is quite complex and may vary across devices. Highly compressedvideo files often require a large amount of processing to display at anacceptable frame rate, and so, silicon vendors have stepped in to incorporate IP blocks and co-processors specifically designed for offloadingrepetitive video codec operations.Video codecs which are decoded on the CPU are sometimes called‘soft codecs’ whereas codecs running on a co-processor may be referredto as ‘hardware accelerated’ codecs (even though these are written inDSP code which is also software).