examples/Graphics/WS/Direct/CDirectDisplayLife.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 */
00029 
00030 
00031 #include <coemain.h>
00032 
00033 #include "CDirectDisplayLife.h"
00034 
00035 // Dimension of each cell
00036 const TInt CDirectDisplayLife::KBlockSize = 20;
00037 // Delay between generations (microseconds)
00038 // Actual minimum is 1/10s on WINS
00039 const TInt CDirectDisplayLife::KGenerationInterval = 70000;
00040 // X & Y origins of upper left of cell display
00041 const TInt CDirectDisplayLife::iXOrigin = 50;
00042 const TInt CDirectDisplayLife::iYOrigin = 30;
00043 
00044 CDirectDisplayLife::CDirectDisplayLife(RWsSession& aClient, 
00045                                                                            RWindow& aWindow,
00046                                                                            CLifeEngine& aLifeEngine)
00047 : CTimer(CActive::EPriorityStandard),
00048         iClient(aClient),
00049         iWindow(aWindow),
00050         iLifeEngine(aLifeEngine)
00051         {
00052         }
00053 
00054 CDirectDisplayLife::~CDirectDisplayLife()
00055         {
00056         Cancel();
00057         delete iDirectScreenAccess;
00058         }
00059 
00060 void CDirectDisplayLife::ConstructL()
00061         {
00062         CTimer::ConstructL();
00063         // Create the DSA object
00064         iDirectScreenAccess = CDirectScreenAccess::NewL(
00065                 iClient,                                // WS session
00066                 *(CCoeEnv::Static()->ScreenDevice()),           // CWsScreenDevice
00067                 iWindow,                                // RWindowBase
00068                 *this                                   // MDirectScreenAccess
00069                 );
00070 
00071         CActiveScheduler::Add(this);
00072         }
00073 
00074 // Start game display
00075 void CDirectDisplayLife::StartL()
00076         {
00077         // Initialise DSA
00078         iDirectScreenAccess -> StartL();
00079         // Get graphics context for it
00080         iGc = iDirectScreenAccess -> Gc();
00081         iGc -> SetBrushStyle(CGraphicsContext::ESolidBrush);
00082         // Get region that DSA can draw in
00083         iRegion = iDirectScreenAccess -> DrawingRegion();
00084         // Set the display to clip to this region
00085         iGc -> SetClippingRegion(iRegion); 
00086 
00087         After(TTimeIntervalMicroSeconds32(KGenerationInterval));
00088         }
00089         
00090 // Implement MDirectScreenAccess
00091 void CDirectDisplayLife::Restart(RDirectScreenAccess::TTerminationReasons /*aReason*/)
00092         {
00093         // Restart display
00094         // Note that this will result in the clipping region being updated
00095         // so that menus, overlaying dialogs, etc. will not be drawn over
00096         TRAPD(err,StartL());
00097         if(err != KErrNone)
00098                 {
00099                 User::Panic(_L("Failed to Restart display"),err);
00100                 }
00101         }
00102 
00103 void CDirectDisplayLife::AbortNow(RDirectScreenAccess::TTerminationReasons /*aReason*/)
00104         {
00105         // Cancel timer and display
00106         Cancel();
00107         }
00108 
00109 // Draw cells using DSA
00110 void CDirectDisplayLife::RunL()
00111         {
00112         // Update engine
00113         iLifeEngine.AddGeneration();
00114         const TCellArray& iCells = iLifeEngine.GetCellArray();
00115 
00116         // Loop through cells drawing each
00117         TRect drawBlock(iXOrigin, iYOrigin, iXOrigin+KBlockSize,iYOrigin+KBlockSize);
00118         for (int y=0; y<DIM_Y_ARRAY; y++)
00119                 {
00120                 for (int x=0; x<DIM_X_ARRAY; x++)
00121                         {
00122                         if (iCells[x][y])
00123                                 iGc -> SetBrushColor(KRgbBlue);
00124                         else
00125                                 iGc -> SetBrushColor(KRgbYellow);
00126                         iGc -> DrawRect(drawBlock);
00127                         drawBlock.Move(KBlockSize,0);
00128 
00129                         // Redraw the changed areas
00130                         TPoint modified;
00131                         modified.iX = iXOrigin+KBlockSize;
00132                         modified.iY = iYOrigin+KBlockSize;
00133 
00134                         // Update the screen for the areas that have been redrawn
00135                         iDirectScreenAccess->ScreenDevice()->Update();
00136                         }
00137                 drawBlock.iTl.iX = iXOrigin;
00138                 drawBlock.iBr.iX = iXOrigin+KBlockSize;
00139                 drawBlock.Move(0,KBlockSize);
00140                 }
00141         iClient.Flush();
00142         // Renew request
00143         After(TTimeIntervalMicroSeconds32(KGenerationInterval));
00144         }
00145 
00146 void CDirectDisplayLife::DoCancel()
00147         {
00148         // Cancel timer
00149         CTimer::DoCancel();
00150         // Cancel DSA
00151         iDirectScreenAccess -> Cancel();
00152         }
00153 

Generated by  doxygen 1.6.2