examples/Graphics/WS/Direct/CDirectDisplayLife.cpp

00001 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 //
00015 
00016 #include <coemain.h>
00017 
00018 #include "CDirectDisplayLife.h"
00019 
00020 // Dimension of each cell
00021 const TInt CDirectDisplayLife::KBlockSize = 20;
00022 // Delay between generations (microseconds)
00023 // Actual minimum is 1/10s on WINS
00024 const TInt CDirectDisplayLife::KGenerationInterval = 70000;
00025 // X & Y origins of upper left of cell display
00026 const TInt CDirectDisplayLife::iXOrigin = 50;
00027 const TInt CDirectDisplayLife::iYOrigin = 30;
00028 
00029 CDirectDisplayLife::CDirectDisplayLife(RWsSession& aClient, 
00030                                                                            RWindow& aWindow,
00031                                                                            CLifeEngine& aLifeEngine)
00032 : CTimer(CActive::EPriorityStandard),
00033         iClient(aClient),
00034         iWindow(aWindow),
00035         iLifeEngine(aLifeEngine)
00036         {
00037         }
00038 
00039 CDirectDisplayLife::~CDirectDisplayLife()
00040         {
00041         Cancel();
00042         delete iDirectScreenAccess;
00043         }
00044 
00045 void CDirectDisplayLife::ConstructL()
00046         {
00047         CTimer::ConstructL();
00048         // Create the DSA object
00049         iDirectScreenAccess = CDirectScreenAccess::NewL(
00050                 iClient,                                // WS session
00051                 *(CCoeEnv::Static()->ScreenDevice()),           // CWsScreenDevice
00052                 iWindow,                                // RWindowBase
00053                 *this                                   // MDirectScreenAccess
00054                 );
00055 
00056         CActiveScheduler::Add(this);
00057         }
00058 
00059 // Start game display
00060 void CDirectDisplayLife::StartL()
00061         {
00062         // Initialise DSA
00063         iDirectScreenAccess -> StartL();
00064         // Get graphics context for it
00065         iGc = iDirectScreenAccess -> Gc();
00066         iGc -> SetBrushStyle(CGraphicsContext::ESolidBrush);
00067         // Get region that DSA can draw in
00068         iRegion = iDirectScreenAccess -> DrawingRegion();
00069         // Set the display to clip to this region
00070         iGc -> SetClippingRegion(iRegion); 
00071 
00072         After(TTimeIntervalMicroSeconds32(KGenerationInterval));
00073         }
00074         
00075 // Implement MDirectScreenAccess
00076 void CDirectDisplayLife::Restart(RDirectScreenAccess::TTerminationReasons /*aReason*/)
00077         {
00078         // Restart display
00079         // Note that this will result in the clipping region being updated
00080         // so that menus, overlaying dialogs, etc. will not be drawn over
00081         TRAPD(err,StartL());
00082         if(err != KErrNone)
00083                 {
00084                 User::Panic(_L("Failed to Restart display"),err);
00085                 }
00086         }
00087 
00088 void CDirectDisplayLife::AbortNow(RDirectScreenAccess::TTerminationReasons /*aReason*/)
00089         {
00090         // Cancel timer and display
00091         Cancel();
00092         }
00093 
00094 // Draw cells using DSA
00095 void CDirectDisplayLife::RunL()
00096         {
00097         // Update engine
00098         iLifeEngine.AddGeneration();
00099         const TCellArray& iCells = iLifeEngine.GetCellArray();
00100 
00101         // Loop through cells drawing each
00102         TRect drawBlock(iXOrigin, iYOrigin, iXOrigin+KBlockSize,iYOrigin+KBlockSize);
00103         for (int y=0; y<DIM_Y_ARRAY; y++)
00104                 {
00105                 for (int x=0; x<DIM_X_ARRAY; x++)
00106                         {
00107                         if (iCells[x][y])
00108                                 iGc -> SetBrushColor(KRgbBlue);
00109                         else
00110                                 iGc -> SetBrushColor(KRgbYellow);
00111                         iGc -> DrawRect(drawBlock);
00112                         drawBlock.Move(KBlockSize,0);
00113 
00114                         // Redraw the changed areas
00115                         TPoint modified;
00116                         modified.iX = iXOrigin+KBlockSize;
00117                         modified.iY = iYOrigin+KBlockSize;
00118 
00119                         // Update the screen for the areas that have been redrawn
00120                         iDirectScreenAccess->ScreenDevice()->Update();
00121                         }
00122                 drawBlock.iTl.iX = iXOrigin;
00123                 drawBlock.iBr.iX = iXOrigin+KBlockSize;
00124                 drawBlock.Move(0,KBlockSize);
00125                 }
00126         iClient.Flush();
00127         // Renew request
00128         After(TTimeIntervalMicroSeconds32(KGenerationInterval));
00129         }
00130 
00131 void CDirectDisplayLife::DoCancel()
00132         {
00133         // Cancel timer
00134         CTimer::DoCancel();
00135         // Cancel DSA
00136         iDirectScreenAccess -> Cancel();
00137         }
00138 

Generated by  doxygen 1.6.2