examples/Graphics/WS/BitmapSprite/BitmapSprite.cpp

00001 /*
00002 Copyright (c) 2005-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description: 
00028 This file contains implementations of derived class 
00029 functions for client and windows.
00030 PROJECT: BitmapSprite
00031 CREATE A SPRITE USING BITMAPS LOADED FROM FILE
00032 PROJECT OVERVIEW
00033 Creates a sprite using bitmaps loaded from file.
00034 A hatched background has been drawn on the window to illustrate
00035 the fact that the sprite does not overlay a rectangular area, like 
00036 window, but only the area specified by the bitmap mask.
00037 The animation of the sprite does not cause a redraw event for the 
00038 window beneath it.
00039 The sprite can be moved around the screen using the arrow keys.
00040 Functions introduced in this project include:
00041 RWsSprite::Construct()
00042 RWsSpriteBase::AppendMember()
00043 RWsSpriteBase::Activate()
00044 RWsSpriteBase::Close()
00045 STRUCTURE
00046 To create a sprite, an application must set up sprite members to
00047 contain bitmaps, and add the sprite members to the sprite. The 
00048 bitmaps can either be read in from files, or created using 
00049 off-screen bitmaps which can be drawn to via a graphics context
00050 in a similar way to a window. This example project uses the former
00051 method. 
00052 */
00053 
00054 
00055 #include <eikenv.h>
00056 #include <w32std.h>
00057 #include "Base.h"
00058 #include "BitmapSprite.h"
00059 
00060 #include <bitmapsprite.mbg>
00061 
00062 CSprite::CSprite(CWsClient *aClient) : iClient(aClient), iSpritePos(80, 75)
00063         {
00064         }
00065 
00066 CSprite::~CSprite()
00067         {
00068         for (TInt i=0; i<8; i++)
00069                 {
00070                 delete iSpriteMember[i].iBitmap;
00071                 delete iSpriteMember[i].iMaskBitmap;
00072                 }
00073         iSprite.Close();
00074         }
00075 
00076 void CSprite::UpdatePos(TPoint aAdjust)
00077         {
00078         iSpritePos += aAdjust;
00079         iSprite.SetPosition(iSpritePos);
00080         }
00081 
00082 void CSprite::ConstructL(CWindow* aWindow)
00083         {
00084         CEikonEnv* eikenv=CEikonEnv::Static();
00085         iSprite = RWsSprite(iClient->iWs);
00086         User::LeaveIfError(iSprite.Construct(aWindow->Window(), iSpritePos, 0));
00087         // Initialize sprite members
00088         _LIT(KStar,"*"); // "*" means "the <app-name>.mbm file in the app directory"
00089         for (TInt i=0; i<8; i++)
00090                 {
00091                 iSpriteMember[i].iInvertMask = EFalse;
00092                 iSpriteMember[i].iOffset = TPoint (0,0);
00093                 iSpriteMember[i].iInterval = TTimeIntervalMicroSeconds32 (200000);
00094                 iSpriteMember[i].iBitmap = eikenv->CreateBitmapL(KStar, i);
00095                 if (i%2 == 0)
00096                         {
00097                         iSpriteMember[i].iMaskBitmap=eikenv->CreateBitmapL(KStar, EMbmBitmapspriteMil1mask);
00098                         }
00099                 else
00100                         {
00101                         iSpriteMember[i].iMaskBitmap=eikenv->CreateBitmapL(KStar, EMbmBitmapspriteMil2mask);
00102                         }
00103                 User::LeaveIfError(iSprite.AppendMember(iSpriteMember[i]));
00104                 }
00105 
00106         // Activate the sprite
00107         User::LeaveIfError(iSprite.Activate());
00108         }
00109 
00110 
00112 //                                       CMainWindow implementation
00114 
00115 
00116 /****************************************************************************\
00117 |       Function:       Constructor/Destructor for CMainWindow
00118 |       Input:          aClient         Client application that owns the window
00119 \****************************************************************************/
00120 CMainWindow::CMainWindow (CWsClient* aClient)
00121 : CWindow (aClient)
00122         {
00123         }
00124 
00125 
00126 CMainWindow::~CMainWindow ()
00127         {
00128         iWindow.Close();
00129         }
00130 
00131 /****************************************************************************\
00132 |       Function:       CMainWindow::Draw
00133 |       Purpose:        Redraws the contents of CMainWindow within a given
00134 |                               rectangle.
00135 |       Input:          aRect   Rectangle that needs redrawing
00136 |       Output:         None
00137 \****************************************************************************/
00138 
00139 void CMainWindow::Draw(const TRect& aRect)
00140         {
00141         // Draw a hatched pattern to illustrate that sprite is drawn only 
00142         // where its bitmap mask is 0
00143         CWindowGc* gc=SystemGc(); // get a gc
00144         gc->SetClippingRect(aRect); // clip outside this rect
00145         gc->Clear(aRect); // clear
00146         gc->SetPenStyle(CGraphicsContext::ESolidPen);
00147         gc->SetPenColor(TRgb::Gray4(2));
00148         TSize size = Window().Size();
00149         TInt width = size.iWidth;
00150         TInt height = size.iHeight;
00151         TInt numHoriz=height/5;
00152         TInt numVert=width/10;
00153         for (TInt i=numHoriz; i>0; i--)
00154                 {
00155                 gc->DrawLine (TPoint(0,height/numHoriz*i), TPoint(width, height/numHoriz*i));
00156                 }
00157         for (TInt j=numVert; j>0; j--)
00158                 {
00159                 gc->DrawLine (TPoint(width/numVert*j, 0), TPoint(width/numVert*j, height));
00160                 }
00161         }
00162 
00163 
00164 /****************************************************************************\
00165 |       Function:       CMainWindow::HandlePointerEvent
00166 |       Purpose:        Handles pointer events for CMainWindow.
00167 |       Input:          aPointerEvent   The pointer event!
00168 |       Output:         None
00169 \****************************************************************************/
00170 
00171 void CMainWindow::HandlePointerEvent (TPointerEvent& /*aPointerEvent*/)
00172         {       
00173         }
00174 
00175 
00177 //                                       CExampleWsClient implementation
00179 
00180 CExampleWsClient* CExampleWsClient::NewL(const TRect& aRect)
00181         {
00182         // make new client
00183         CExampleWsClient* client=new (ELeave) CExampleWsClient(aRect); 
00184         CleanupStack::PushL(client); // push, just in case
00185         client->ConstructL(); // construct and run
00186         CleanupStack::Pop();
00187         return client;
00188         }
00189 
00190 /****************************************************************************\
00191 |       Function:       Constructor/Destructor for CExampleWsClient
00192 |                               Destructor deletes everything that was allocated by
00193 |                               ConstructMainWindowL()
00194 \****************************************************************************/
00195 
00196 CExampleWsClient::CExampleWsClient(const TRect& aRect)
00197 :iRect(aRect)
00198         {
00199         }
00200 
00201 CExampleWsClient::~CExampleWsClient ()
00202         {
00203         delete iMainWindow;
00204         delete iSprite;
00205         }
00206 
00207 
00208 /****************************************************************************\
00209 |       Function:       CExampleWsClient::ConstructMainWindowL()
00210 |                               Called by base class's ConstructL
00211 |       Purpose:        Allocates and creates all the windows owned by this client
00212 |                               (See list of windows in CExampleWsCLient declaration).
00213 \****************************************************************************/
00214 
00215 void CExampleWsClient::ConstructMainWindowL()
00216         {
00217         iMainWindow=new (ELeave) CMainWindow(this);
00218         iMainWindow->ConstructL(iRect, TRgb (255,255,255));
00219         iSprite=new (ELeave) CSprite (this);
00220         iSprite->ConstructL(iMainWindow);
00221         }
00222 
00223 
00224 /****************************************************************************\
00225 |       Function:       CExampleWsClient::RunL()
00226 |                               Called by active scheduler when an even occurs
00227 |       Purpose:        Processes events according to their type
00228 |                               For key events: calls HandleKeyEventL() (global to client)
00229 |                               For pointer event: calls HandlePointerEvent() for window
00230 |                                  event occurred in.
00231 \****************************************************************************/
00232 void CExampleWsClient::RunL()
00233         {
00234         // get the event
00235         iWs.GetEvent(iWsEvent);
00236         TInt eventType=iWsEvent.Type();
00237         // take action on it
00238         switch (eventType)
00239                 {
00240         // events global within window group
00241         case EEventNull:
00242                 break;
00243         case EEventKey:
00244                 {
00245                 TKeyEvent& keyEvent=*iWsEvent.Key(); // get key event
00246                 HandleKeyEventL (keyEvent);
00247                 break;
00248                 }
00249         case EEventModifiersChanged:
00250                 break;
00251         case EEventKeyUp:
00252         case EEventKeyDown:
00253         case EEventFocusLost:
00254         case EEventFocusGained:
00255         case EEventSwitchOn:
00256         case EEventPassword:
00257         case EEventWindowGroupsChanged:
00258         case EEventErrorMessage:
00259                 break;
00260         // events local to specific windows
00261         case EEventPointer:
00262                 {
00263                 CWindow* window=(CWindow*)(iWsEvent.Handle()); // get window
00264                 TPointerEvent& pointerEvent=*iWsEvent.Pointer();
00265                 window->HandlePointerEvent (pointerEvent);
00266                 break;
00267                 }
00268         case EEventPointerExit:
00269         case EEventPointerEnter:
00270                 break;
00271         case EEventPointerBufferReady:
00272                 {
00273                 break;
00274                 }
00275         case EEventDragDrop:
00276                 break;
00277         default:
00278                 break;
00279                 }
00280         IssueRequest(); // maintain outstanding request
00281         }
00282 
00283 /****************************************************************************\
00284 |       Function:       CExampleWsClient::HandleKeyEventL()
00285 |       Purpose:        Processes key events for CExampleWsClient
00286 |                               Gets the key code from the key event.  Exits on 'Escape'
00287 \****************************************************************************/
00288 void CExampleWsClient::HandleKeyEventL (TKeyEvent& aKeyEvent)
00289         {
00290         TUint   code = aKeyEvent.iCode;
00291         switch (code)
00292                 {
00293                 case EKeyLeftArrow:
00294                         iSprite->UpdatePos(TPoint(-2,0));
00295                         break;
00296                 case EKeyRightArrow:
00297                         iSprite->UpdatePos(TPoint(2,0));
00298                         break;
00299                 case EKeyUpArrow:
00300                         iSprite->UpdatePos(TPoint(0,-2));
00301                         break;
00302                 case EKeyDownArrow:
00303                         iSprite->UpdatePos(TPoint(0,2));
00304                         break;
00305                 }
00306         }
00307 

Generated by  doxygen 1.6.2