home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / Epoc / Palmtime / files / Pyramid_src.ZIP / Pyramid.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-15  |  34.3 KB  |  1,119 lines

  1. // Here are the 'public domain' sources
  2. // of Pyramid v1.2
  3. // To get a correct indentation of these sources
  4. // use a Tab char of size 4 !
  5. // The .mmp project is made for the sources to be in
  6. // a \epoc32ex\pyramid directory but you can change that
  7.  
  8. // PYRAMID.CPP
  9. //
  10. // Written rapidly by Frederic Bouvry in 1997
  11. // http://www.palmtime.com
  12.  
  13. // Note: to compile the cards.mbm file, use 'bmconv cards.txt'
  14. // in the cards\bmp subdir and then copy cards.mbm to the Pyramid app dir.
  15. // To compile the pyramid.aif, use 'bmconv pyr.txt' in the cards
  16. // directory then aiftool.
  17.  
  18. #include <e32keys.h>
  19. #include <e32base.h>
  20. #include <e32math.h>
  21. #include <e32svr.h>
  22. #include <e32math.h>
  23. #include <eikconso.h>
  24. #include <e32keys.h>
  25. #include <barsread.h>
  26. #include <coecntrl.h>
  27. #include <eikdef.h>
  28. #include <eikenv.h>
  29. #include <eikappui.h>
  30. #include <eikdialg.h>
  31. #include <eikdialg.hrh>
  32. #include <eikcapc.h>
  33. #include <eikcapca.h>
  34. #include <eikctrls.hrh>
  35. #include <eikcmds.hrh>
  36. #include <eikmfne.h>
  37. #include <eikmfne.hrh>
  38. #include <eikfpne.h>
  39. #include <eikedwin.hrh>
  40. #include <eikcmbut.h>
  41. #include <eiklabel.h>
  42. #include <eikseced.h>
  43. #include <eikrted.h>
  44. #include <txtrich.h>
  45. #include <eiktbar.h>
  46. #include <eikchred.h>
  47. #include <eikchmap.h>
  48. #include <eikrted.h>
  49. #include <eikopbut.h>
  50. #include <eikhopbt.h>
  51. #include <eikchkbx.h>
  52. #include <eikmenub.h>
  53. #include <eikmenup.h>
  54. #include <eikmenu.hrh>
  55. #include <eikfsel.h>
  56. #include <eikapp.h>
  57. #include <eikdoc.h>
  58. #include <basched.h>
  59. #include <coemain.h>
  60. #include <eikfnlab.h>
  61. #include <coeutils.h>
  62. #include <eikcfdlg.h>
  63. #include <eikdoc.h>
  64. #include <eikon.rsg>
  65. #include <eikfutil.h>
  66. #include <eikenv.h>
  67. #include <eikdef.h>
  68. #include <eikcmds.hrh>
  69. #include <eiklabel.h>
  70. #include <fontids.h>
  71. #include <s32file.h>
  72. #include "pyramid.rsg"
  73. #include "pyramid.hrh"
  74. #include "pyramid.h"
  75.  
  76. const TInt TapisRow[28]= {0,1,1,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5,5,5,5,6,6,6,6,6,6,6};
  77. const TInt TapisCol[28]= {0,-1,1,-2,0,2,-3,-1,1,3,-4,-2,0,2,4,-5,-3,-1,1,3,5,-6,-4,-2,0,2,4,6};
  78. const TInt TapisDownTree[28] = {2,4,5,7,8,9,11,12,13,14,16,17,18,19,20,22,23,24,25,26,27,0,0,0,0,0,0,0};
  79. const TInt TapisUpTree[56] = {0,0,0,1,1,0,0,2,2,3,3,0,0,4,4,5,5,6,6,0,0,7,7,8,8,9,9,10,10,0,0,11,11,12,12,13,
  80.                               13,14,14,15,15,0,0,16,16,17,17,18,18,19,19,20,20,21,21,0};
  81.  
  82.  
  83. //
  84. // TPyramidModel
  85. //
  86.  
  87. TPyramidModel::TPyramidModel()
  88.     {
  89.     iLibrary=_L("");
  90.     }
  91.  
  92. // Differs from what ? :-)
  93. TBool TPyramidModel::Differs(const TPyramidModel* aCompare) const
  94.     {
  95.     return((*(TInt32*)this)!=(*(TInt32*)aCompare));
  96.     }
  97.  
  98. void CPyramidControl::Random() // Init a new game and deal the cards
  99. {
  100.     TInt cards[52];
  101.     TInt i,offset, res;
  102.     for(i = 0; i<52; i++)
  103.       cards[i] = 0;
  104.     offset = 0;
  105.     Score = 0;
  106.     state = 0;
  107.     for(i = 0; i<28; i++)
  108.       {
  109.       Math::Rand(seed);
  110.       res = seed.Low() & 0x3F;
  111.       offset = (offset + res) % 52;
  112.       while(cards[offset] != 0)
  113.         offset = (offset + 1) % 52;
  114.       cards[offset] = Tapis[i] = offset+1;
  115.       }
  116.     for(i = 0; i<23; i++)
  117.       {
  118.       Math::Rand(seed);
  119.       res = seed.Low() & 0x3F;
  120.       offset = (offset + res) % 52;
  121.       while(cards[offset] != 0)
  122.         offset = (offset + 1) % 52;
  123.       cards[offset] = Stock[i] = offset+1;
  124.       }
  125.     while(cards[offset] != 0)
  126.       offset = (offset + 1) % 52;
  127.     Tallon = offset+1;
  128.     StockSize = 23;
  129.     WasteSize = 0;
  130.     for(i = 0; i < 28; i++)
  131.       {
  132.       if(i < 21)
  133.           TapisState[i] = 0;
  134.       else
  135.           TapisState[i] = 1;
  136.       }
  137.     DragCard = 0;
  138.     Redeals = 0;
  139. }
  140.  
  141. void CPyramidControl::ConstructL(RWindowTreeNode& parent, const TRect& aRect)
  142.     {
  143.     int i;
  144.     double dbli, dblj;
  145.  
  146.     CWindowGc& gc=SystemGc(); // graphics context we draw to
  147.  
  148.     if(iCoeEnv->ScreenDevice()->SizeInPixels().iWidth < 640)
  149.     {
  150.         ScreenRect = TRect(TPoint(0,0),iCoeEnv->ScreenDevice()->SizeInPixels());
  151.         xoff = ScreenRect.iBr.iX / 640.0;
  152.         yoff = ScreenRect.iBr.iY / 240.0;
  153.         Math::Round(dbli,(73*xoff),0);
  154.         Math::Round(dblj,(97*yoff),0);
  155.         CardSize=TSize(dbli,dblj);
  156.     }
  157.     else
  158.     {
  159.         ScreenRect = TRect(0,0,640,240);
  160.         xoff = yoff = 1;
  161.         CardSize = TSize(73,97);
  162.     }
  163.  
  164.     WasteCol = (TInt)(483 * xoff);
  165.     WasteRow = (TInt)(14 * yoff);
  166.     TallonCol = (TInt)(563 * xoff);
  167.     TallonRow = (TInt)(14 * yoff);
  168.     StockCol = (TInt)(563 * xoff);
  169.     StockRow = (TInt)(128 * yoff); 
  170.  
  171.  
  172.     TFontSpec MyFontSpec(_L("Arial"), 120);
  173.     TFontSpec MyFontSpec2(_L("Arial"), 160);
  174.     TFontStyle MyStyle(EPostureUpright,EStrokeWeightBold,EPrintPosNormal);
  175.     TTime home;
  176.     CreateBackedUpWindowL(parent, EGray4);
  177.     MyFontSpec.iFontStyle = MyStyle; // Font for 'Tallon:', ...
  178.     MyFontSpec2.iFontStyle = MyStyle; // Font for 'Score:'
  179.     iZoomFactor.SetGraphicsDeviceMap(iCoeEnv->ScreenDevice());
  180.     iZoomFactor.SetZoomFactor(TZoomFactor::EZoomOneToOne);
  181.     iDeviceMap = &iZoomFactor;
  182.     SetRectL(aRect);
  183.     iEikonEnv->InfoMsg(_L("Loading..."));
  184.     TBufC<40> multiBitmapFile(_L("\\System\\Apps\\Pyramid\\cards.mbm"));
  185.     iCardsMsBmp = new (ELeave) CFbsBitmap();
  186.     LoadBitmapL(iCardsMsBmp, multiBitmapFile, 0); // Load the bitmap mask for all cards
  187.     iCardsNoBmp = new (ELeave) CFbsBitmap();
  188.     LoadBitmapL(iCardsNoBmp, multiBitmapFile, 1); // Load the empty slot bitmap
  189.     iCardsBkBmp = new (ELeave) CFbsBitmap();
  190.     LoadBitmapL(iCardsBkBmp, multiBitmapFile, 2); // Load the other side of the moon (back of the card)
  191.     for(i = 0; i< 52; i++) // Load the 52 cards
  192.       {
  193.       iCards52Bmp[i] = new (ELeave) CFbsBitmap();
  194.       LoadBitmapL(iCards52Bmp[i], multiBitmapFile, i+3);
  195.       iCards52Bmp[i]->SetSizeInTwips(iDeviceMap);
  196.     }
  197.     for(i = 0; i< 28; i++) // create the bitmaps that will keep the background of a Tapis' card
  198.       {
  199.       iUnderTapis[i] =  new(ELeave) CFbsBitmap;
  200.       iUnderTapis[i]->Create(CardSize, EGray4);
  201.       }
  202.  
  203.     // Set Card size for rescaling
  204.     CardSizeTwips = iCards52Bmp[Tapis[0]]->SizeInTwips();
  205.     CardSizeTwips.iWidth = CardSizeTwips.iWidth * xoff;
  206.     CardSizeTwips.iHeight = CardSizeTwips.iHeight * yoff;
  207.  
  208.     home.HomeTime(); // Base the random numbers generator seed on the current time
  209.     seed = home.DateTime().Second() + home.DateTime().MicroSecond();
  210.     iDeviceMap->GetNearestFontInTwips(MyFont,MyFontSpec);
  211.     iDeviceMap->GetNearestFontInTwips(MyFont2,MyFontSpec2);
  212.     SetObserver(this); // I use an observer for this control but this is not really necessary
  213.     Random(); // Start a new game
  214.     ActivateL();
  215.     UpdateModelL();
  216.     // go for it
  217.  
  218.     }
  219.  
  220. CPyramidControl::~CPyramidControl()
  221.     {
  222.     int i;
  223.     delete iCardsMsBmp;
  224.     delete iCardsBkBmp;
  225.     delete iCardsNoBmp;
  226.     for(i = 0; i< 52; i++)
  227.       delete iCards52Bmp[i];
  228.     for(i = 0; i< 28; i++)
  229.       delete iUnderTapis[i];
  230.     CloseWindow();
  231.     }
  232.  
  233. void CPyramidControl::UpdateModelL()
  234.     {
  235.     }
  236.  
  237.  
  238.  
  239. void CPyramidControl::DrawTapis(CWindowGc& gc) const
  240. {
  241. // if wanting to use sprites, take a look at \eikon\zoom\zoom.cpp
  242.     TInt i;
  243.     TInt posx,posy;
  244.     TRect bmpPieceRect;
  245.     bmpPieceRect.SetRect(TPoint(0,0),CardSize); // full size
  246.     for(i = 0; i < 28; i++)
  247.     {
  248.         if(Tapis[i])
  249.         {
  250.             posx = (TInt)((280 * xoff) + (TapisCol[i] * (40 * xoff)) - (37 * xoff));
  251.             posy = (TInt)((TapisRow[i] * (23 * yoff)) + (3 * yoff));
  252.             iEikonEnv->ScreenDevice()->CopyScreenToBitmap(iUnderTapis[i],
  253.                      TRect(posx,posy,posx + CardSize.iWidth,posy + CardSize.iHeight));
  254. //            gc.BitBlt(TPoint(posx,posy), iCards52Bmp[Tapis[i]-1]);
  255.             iCards52Bmp[Tapis[i]-1]->SetSizeInTwips(CardSizeTwips);    
  256.             gc.DrawBitmap(TPoint(posx,posy), iCards52Bmp[Tapis[i]-1]);
  257.         }
  258.     } 
  259. }
  260.  
  261. void CPyramidControl::DrawLabels(CWindowGc& gc) const
  262. {
  263.     int textwidth;
  264.     TBuf<20> txt;
  265.     TInt fontHeight(MyFont->HeightInPixels());
  266.     TInt fontHeight2(MyFont2->HeightInPixels());
  267.     TRect box;
  268.     TInt offset,margin=0;
  269.     // Draw Score Box and Text
  270.     textwidth = MyFont2->TextWidthInPixels(_L(" Score: 00"));
  271.     box = TRect(10,10,textwidth + 4 + 10,fontHeight2 + 4 + 10);
  272.     offset=(fontHeight2+box.Height())/2 - 1; // 1/2 font ht below halfway down box
  273.     gc.UseFont(MyFont2);
  274.     gc.SetBrushStyle(gc.ESolidBrush);
  275.     gc.SetBrushColor(KRgbBlack);
  276.     gc.SetPenColor(KRgbWhite);
  277.     gc.SetPenStyle(gc.ESolidPen);
  278.     txt.Format(_L(" Score: %d"), Score);
  279.     gc.DrawText(txt,box,offset,CGraphicsContext::ELeft,margin);
  280.  
  281.     // Draw small Waste text and Box
  282.     gc.UseFont(MyFont);
  283.     textwidth = MyFont->TextWidthInPixels(_L(" Waste: 00"));
  284.     box = TRect((WasteCol + (6*xoff)),(WasteRow - (12*yoff)), 
  285.                 (WasteCol+ (6*xoff) + textwidth),(WasteRow - (12*yoff) + fontHeight + 1));
  286. //    box = TRect(WasteCol+6,WasteRow-12,WasteCol+68,WasteRow-1);
  287.     offset=(fontHeight+box.Height())/2; // 1/2 font ht below halfway down box
  288.     txt.Format(_L(" Waste: %d"), WasteSize);
  289.     gc.DrawText(txt,box,offset,CGraphicsContext::ELeft,margin);
  290.  
  291.     //Draw Tallon text and box
  292.     textwidth = MyFont->TextWidthInPixels(_L(" Tallon: 00"));
  293.     box = TRect((TallonCol + (6*xoff)),(TallonRow - (12*yoff)), 
  294.                 (TallonCol + (6*xoff) + textwidth),(TallonRow - (12*yoff) + fontHeight + 1));
  295.     txt.Format(_L(" Tallon: %d"), (Tallon ? 1 : 0) );
  296.     gc.DrawText(txt,box,offset,CGraphicsContext::ELeft,margin);
  297.  
  298.     //Draw StackCol text and box
  299.     textwidth = MyFont->TextWidthInPixels(_L(" Stock: 00"));
  300.     box = TRect(StockCol + (6*xoff),StockRow - (12*yoff), 
  301.                 StockCol + (6*xoff) + textwidth,StockRow - (12*yoff) + fontHeight + 1);
  302.     txt.Format(_L(" Stock: %d"), StockSize);
  303.     gc.DrawText(txt,box,offset,CGraphicsContext::ELeft,margin);
  304. }
  305.  
  306. void CPyramidControl::DrawWaste(CWindowGc& gc) const
  307. {
  308.     TRect bmpPieceRect;
  309.     bmpPieceRect.SetRect(TPoint(0,0),CardSize); // full size
  310.     if(WasteSize > 0)
  311.     {
  312.         iCards52Bmp[Waste[WasteSize-1]-1]->SetSizeInTwips(CardSizeTwips);    
  313.         gc.DrawBitmap(TPoint(WasteCol,WasteRow), iCards52Bmp[Waste[WasteSize-1]-1]);
  314. //        gc.BitBltMasked(TPoint(WasteCol,WasteRow), iCards52Bmp[Waste[WasteSize-1]-1], 
  315. //                      bmpPieceRect, iCardsMsBmp, ETrue);
  316.     }
  317.     else
  318.     {
  319.         iCardsNoBmp->SetSizeInTwips(CardSizeTwips);    
  320.         gc.DrawBitmap(TPoint(WasteCol,WasteRow), iCardsNoBmp);
  321. //        gc.BitBltMasked(TPoint(WasteCol,WasteRow), iCardsNoBmp, bmpPieceRect, iCardsMsBmp, ETrue);
  322.     }
  323. }
  324.  
  325. void CPyramidControl::DrawStock(CWindowGc& gc) const
  326. {
  327.     TRect bmpPieceRect;
  328.     bmpPieceRect.SetRect(TPoint(0,0),CardSize); // full size
  329.     if(StockSize > 0)
  330.     {
  331.         iCardsBkBmp->SetSizeInTwips(CardSizeTwips);    
  332.         gc.DrawBitmap(TPoint(StockCol,StockRow), iCardsBkBmp);
  333. //        gc.BitBltMasked(TPoint(StockCol,StockRow), iCardsBkBmp, bmpPieceRect, iCardsMsBmp, ETrue);
  334.     }
  335.     else
  336.     {
  337.         iCardsNoBmp->SetSizeInTwips(CardSizeTwips);    
  338.         gc.DrawBitmap(TPoint(StockCol,StockRow), iCardsNoBmp);
  339. //        gc.BitBltMasked(TPoint(StockCol,StockRow), iCardsNoBmp, bmpPieceRect, iCardsMsBmp, ETrue);
  340.     }
  341. }
  342. void CPyramidControl::DrawTallon(CWindowGc& gc) const
  343. {
  344.     TRect bmpPieceRect;
  345.     bmpPieceRect.SetRect(TPoint(0,0),CardSize); // full size
  346.     if(Tallon)
  347.     {
  348.         iCards52Bmp[Tallon-1]->SetSizeInTwips(CardSizeTwips);    
  349.         gc.DrawBitmap(TPoint(TallonCol,TallonRow), iCards52Bmp[Tallon-1]);
  350. //        gc.BitBltMasked(TPoint(TallonCol,TallonRow), iCards52Bmp[Tallon-1], bmpPieceRect, iCardsMsBmp, ETrue);
  351.     }
  352.     else
  353.     {
  354.         iCardsNoBmp->SetSizeInTwips(CardSizeTwips);    
  355.         gc.DrawBitmap(TPoint(TallonCol,TallonRow), iCardsNoBmp);
  356. //        gc.BitBltMasked(TPoint(TallonCol,TallonRow), iCardsNoBmp, bmpPieceRect, iCardsMsBmp, ETrue);
  357.     }
  358. }
  359.  
  360. void CPyramidControl::Draw(const TRect&/*aRect*/) const
  361.     {
  362.     CWindowGc& gc=SystemGc(); // graphics context we draw to
  363.     gc.SetPenStyle(gc.ESolidPen);
  364. //    gc.SetBrushStyle(gc.EDiamondCrossHatchBrush);
  365. //    gc.SetBrushColor(KRgbWhite);
  366.     gc.SetBrushStyle(gc.ESolidBrush);
  367.     gc.SetBrushColor(KRgbGray);
  368.     gc.DrawRect(Rect()); // clear the area to be drawn to
  369.     DrawTapis(gc);
  370.     DrawStock(gc);
  371.     DrawTallon(gc);
  372.     DrawWaste(gc);
  373.     DrawLabels(gc);
  374.     gc.SetBrushStyle(gc.ESolidBrush);
  375.        }
  376.  
  377. void CPyramidControl::LoadBitmapL(CFbsBitmap* aBitMap,TDesC& aPathAndFile,TInt aId)
  378.     {
  379.     TParse    mbfn;
  380.     TBufC<2>  c_drive(_L("C:"));
  381.     TBufC<2>  z_drive(_L("D:"));
  382.  
  383.     mbfn.Set(aPathAndFile,&c_drive,NULL);
  384.     if (!aBitMap->Load(mbfn.FullName(),aId,ETrue))
  385.         return;
  386.  
  387.     mbfn.Set(aPathAndFile,&z_drive,NULL);
  388.     User::LeaveIfError(aBitMap->Load(mbfn.FullName(),aId,ETrue));
  389.     return;
  390.     }
  391.  
  392. void CPyramidControl::HandlePointerEventL(const TPointerEvent& aPointerEvent)
  393.     {
  394.     TInt card = 0;
  395.     if (aPointerEvent.iType==TPointerEvent::EButton1Down)
  396.       {
  397.       TInt posx,posy,i;
  398.       if(Score == 52)
  399.         return;
  400.         if(Tallon > 0 &&
  401.            aPointerEvent.iPosition.iX >= TallonCol &&
  402.            aPointerEvent.iPosition.iX < (TallonCol + CardSize.iWidth) &&
  403.            aPointerEvent.iPosition.iY >= TallonRow &&
  404.            aPointerEvent.iPosition.iY < (TallonRow + CardSize.iHeight) )
  405.         card = 29;
  406.       else
  407.         if(  aPointerEvent.iPosition.iX >= StockCol &&
  408.            aPointerEvent.iPosition.iX < (StockCol + CardSize.iWidth) &&
  409.            aPointerEvent.iPosition.iY >= StockRow &&
  410.            aPointerEvent.iPosition.iY < (StockRow + CardSize.iHeight) )
  411.         card = 30;
  412.       else
  413.         if(WasteSize > 0 &&
  414.            aPointerEvent.iPosition.iX >= WasteCol &&
  415.            aPointerEvent.iPosition.iX < (WasteCol + CardSize.iWidth) &&
  416.            aPointerEvent.iPosition.iY >= WasteRow &&
  417.            aPointerEvent.iPosition.iY < (WasteRow + CardSize.iHeight) )
  418.         card = 31;
  419.       else
  420.       for(i = 0; i < 28; i++)
  421.              {
  422.           if(Tapis[i] && TapisState[i])
  423.             {
  424.             posx = (280 * xoff) + (TapisCol[i] * (40 * xoff)) - (37 * xoff);
  425.             posy = (TapisRow[i] * (23 * yoff)) + (3 * yoff);
  426.             if(aPointerEvent.iPosition.iX >= posx &&
  427.                aPointerEvent.iPosition.iX < (posx + CardSize.iWidth) &&
  428.                aPointerEvent.iPosition.iY >= posy &&
  429.                aPointerEvent.iPosition.iY < (posy + CardSize.iHeight) )
  430.                 {
  431.                 card = i+1;
  432.                 break;
  433.                 }
  434.             }
  435.           }
  436.       if(card != 0)
  437.           {
  438.           if(DragCard == 0)
  439.             {
  440.                DragCardHit = card;
  441.             switch(card)
  442.               {
  443.               case 29:{ // Tallon
  444.                       DragCardValue = (Tallon-1)%13 + 1;
  445.                       break;
  446.                       }
  447.               case 30:break; // Stock
  448.               case 31:{ // Waste
  449.                       DragCardValue = (Waste[WasteSize-1]-1)%13 + 1;
  450.                       break;
  451.                       }
  452.               default:{
  453.                       DragCardValue = (Tapis[card-1]-1)%13 + 1;
  454.                       break;
  455.                       }
  456.                  }
  457.             if((card != 30) && (DragCardValue == 13)) // a king
  458.                 {
  459.                 Score++;
  460.                 DropCardHit = 0;
  461.                 state = 3;
  462.                 }
  463.             else
  464.                 {
  465.                 state = 1;
  466.                 if(card != 30)
  467.                   DragCard = card;
  468.                 }
  469.             }
  470.           else
  471.               {
  472.               if(card == DragCard)
  473.                 {
  474.                 state = 2; // cancel drag card
  475.                 DragCardHit = card;
  476.                 DragCard = 0;
  477.                 }
  478.               else
  479.                 {
  480.                 TInt Bingo = 0;
  481.                 switch(card)
  482.                   {
  483.                   case 29:{ // Tallon
  484.                           if((Tallon-1)%13 + DragCardValue == 12)
  485.                               Bingo = 1;
  486.                             break;
  487.                           }
  488.                   case 30:break; // Stock
  489.                   case 31:{ // Waste
  490.                           if((Waste[WasteSize-1]-1)%13 + DragCardValue == 12)
  491.                               Bingo = 1;
  492.                             break;
  493.                           }
  494.                   default:{ // Tapis
  495.                           if((Tapis[card-1]-1)%13 + DragCardValue == 12)
  496.                               Bingo = 1;
  497.                             break;
  498.                           }
  499.                   }
  500.                 if(Bingo)
  501.                      {
  502.                      Score += 2;
  503.                      DragCardHit = DragCard;
  504.                      DropCardHit = card;
  505.                      DragCard = 0;
  506.                      state = 3;
  507.                      }
  508.                 }
  509.               }
  510.           if(state)
  511.             ReportEventL(EEventStateChanged);
  512.           }
  513.       }
  514.     }
  515.  
  516. void CPyramidControl::Restart()
  517. {
  518.     Random();
  519.     ActivateGc();
  520.     Draw(ScreenRect);
  521.     DeactivateGc();
  522. }
  523.  
  524. void CPyramidControl::HandleControlEventL(CCoeControl* aControl,TCoeEvent aEventType)
  525. {
  526.     int px, py, crd;
  527.     if(aEventType != EEventStateChanged)
  528.       return;
  529.     int oldstate = state;
  530.     state = 0;
  531.     ActivateGc();
  532.     CWindowGc& gc=SystemGc(); // graphics context we draw to
  533.     TRect bmpPieceRect;
  534.     bmpPieceRect.SetRect(TPoint(0,0),CardSize); // full size
  535.     gc.SetPenSize(TSize(4,4));
  536.     gc.SetPenColor(KRgbDarkGray);
  537.     switch(oldstate)
  538.       {
  539.       case 1:{
  540.              switch(DragCardHit)
  541.                {
  542.                case 29:{
  543.                         iCards52Bmp[Tallon-1]->SetSizeInTwips(CardSizeTwips);    
  544.                         gc.SetDrawMode(CGraphicsContext::EDrawModeNOTPEN);Tallon-1;
  545.                         gc.DrawBitmap(TPoint(TallonCol, TallonRow), iCards52Bmp[Tallon-1]);
  546.                         gc.SetDrawMode(CGraphicsContext::EDrawModePEN);
  547.                        break;
  548.                        }
  549.                case 30:{
  550.                        if(StockSize == 0)
  551.                          {
  552.                            if(Redeals >= MaxRedeals)
  553.                            {
  554.                            iEikonEnv->InfoMsg(_L("No more redeals allowed !"));
  555.                            break;
  556.                            }
  557.                          Redeals++;
  558.                          if(Tallon != 0)
  559.                            Stock[StockSize++] = Tallon;
  560.                          while(WasteSize > 0)
  561.                            Stock[StockSize++] = Waste[--WasteSize];
  562.                          if(StockSize > 0)
  563.                              {
  564.                              Tallon = Stock[--StockSize];
  565.                             iCardsBkBmp->SetSizeInTwips(CardSizeTwips);    
  566.                             gc.DrawBitmap(TPoint(StockCol,StockRow), iCardsBkBmp);
  567.                             // gc.BitBltMasked(TPoint(StockCol,StockRow), iCardsBkBmp, bmpPieceRect, iCardsMsBmp, ETrue);
  568.                              }
  569.                          else
  570.                             iCardsNoBmp->SetSizeInTwips(CardSizeTwips);    
  571.                             gc.DrawBitmap(TPoint(StockCol,StockRow), iCardsNoBmp);
  572.                             gc.DrawBitmap(TPoint(WasteCol,WasteRow), iCardsNoBmp);
  573.                             // gc.BitBltMasked(TPoint(StockCol,StockRow), iCardsNoBmp, bmpPieceRect, iCardsMsBmp, ETrue);
  574.                             //gc.BitBltMasked(TPoint(WasteCol,WasteRow), iCardsNoBmp, bmpPieceRect, iCardsMsBmp, ETrue);
  575.                          if(Tallon > 0)
  576.                          {
  577.                              iCards52Bmp[Tallon-1]->SetSizeInTwips(CardSizeTwips);    
  578.                             gc.DrawBitmap(TPoint(TallonCol,TallonRow), iCards52Bmp[Tallon-1]);
  579.                            //  gc.BitBltMasked(TPoint(TallonCol,TallonRow), iCards52Bmp[Tallon-1], bmpPieceRect, iCardsMsBmp, ETrue);
  580.                          } 
  581.                          else 
  582.                          {
  583.                             iCardsNoBmp->SetSizeInTwips(CardSizeTwips);    
  584.                             gc.DrawBitmap(TPoint(TallonCol,TallonRow), iCardsNoBmp);
  585.                            //  gc.BitBltMasked(TPoint(TallonCol,TallonRow), iCardsNoBmp, bmpPieceRect, iCardsMsBmp, ETrue);
  586.                          }
  587.                          DrawLabels(gc);
  588.                          break;
  589.                          }
  590.                        if(Tallon > 0)
  591.                          {
  592.                          Waste[WasteSize++] = Tallon;
  593.                              iCards52Bmp[Waste[WasteSize-1]-1]->SetSizeInTwips(CardSizeTwips);    
  594.                             gc.DrawBitmap(TPoint(WasteCol,WasteRow), iCards52Bmp[Waste[WasteSize-1]-1]);
  595.                          //gc.BitBltMasked(TPoint(WasteCol,WasteRow), iCards52Bmp[Waste[WasteSize-1]-1], bmpPieceRect, iCardsMsBmp, ETrue);
  596.                          }
  597.                        if(StockSize > 0)
  598.                          {
  599.                          Tallon = Stock[--StockSize];
  600.                              iCards52Bmp[Tallon-1]->SetSizeInTwips(CardSizeTwips);    
  601.                             gc.DrawBitmap(TPoint(TallonCol,TallonRow), iCards52Bmp[Tallon-1]);
  602.                          //gc.BitBltMasked(TPoint(TallonCol,TallonRow), iCards52Bmp[Tallon-1], bmpPieceRect, iCardsMsBmp, ETrue);
  603.                          }
  604.                        else
  605.                        {
  606.                            iCardsNoBmp->SetSizeInTwips(CardSizeTwips);    
  607.                             gc.DrawBitmap(TPoint(StockCol,StockRow), iCardsNoBmp);
  608.                          //gc.BitBltMasked(TPoint(StockCol,StockRow), iCardsNoBmp, bmpPieceRect, iCardsMsBmp, ETrue);
  609.                        }
  610.                         DrawLabels(gc);
  611.                        break;
  612.                        }
  613.                case 31:{
  614.                             iCards52Bmp[Waste[WasteSize-1]-1]->SetSizeInTwips(CardSizeTwips);    
  615.                             gc.SetDrawMode(CGraphicsContext::EDrawModeNOTPEN);
  616.                             gc.DrawBitmap(TPoint(WasteCol,WasteRow), iCards52Bmp[Waste[WasteSize-1]-1]);
  617.                             gc.SetDrawMode(CGraphicsContext::EDrawModePEN);
  618.  
  619.  
  620.                        break;
  621.                        }
  622.                default:{
  623.                        TInt posx,posy;
  624.                         posx = (TInt)((280 * xoff) + (TapisCol[DragCardHit-1] * (40 * xoff)) - (37 * xoff));
  625.                         posy = (TInt)((TapisRow[DragCardHit-1] * (23 * yoff)) + (3 * yoff));
  626.                         gc.SetDrawMode(CGraphicsContext::EDrawModeNOTPEN);
  627.                         iCards52Bmp[Tapis[DragCardHit-1]-1]->SetSizeInTwips(CardSizeTwips);    
  628.                         gc.DrawBitmap(TPoint(posx,posy), iCards52Bmp[Tapis[DragCardHit-1]-1]);
  629.                         gc.SetDrawMode(CGraphicsContext::EDrawModePEN);
  630.                        break;
  631.                        }
  632.                }
  633.              break;
  634.              }
  635.         case 2:{
  636.             switch(DragCardHit)
  637.                {
  638.                case 29:{
  639.                        iCards52Bmp[Tallon-1]->SetSizeInTwips(CardSizeTwips);    
  640.                         gc.DrawBitmap(TPoint(TallonCol,TallonRow), iCards52Bmp[Tallon-1]);
  641.                        //gc.BitBltMasked(TPoint(TallonCol,TallonRow), iCards52Bmp[Tallon-1], bmpPieceRect, iCardsMsBmp, ETrue);
  642.                        break;
  643.                        }
  644.                case 31:{
  645.                        iCards52Bmp[Waste[WasteSize-1]-1]->SetSizeInTwips(CardSizeTwips);    
  646.                         gc.DrawBitmap(TPoint(WasteCol,WasteRow), iCards52Bmp[Waste[WasteSize-1]-1]);
  647.                        //gc.BitBltMasked(TPoint(WasteCol,WasteRow), iCards52Bmp[Waste[WasteSize-1]-1], bmpPieceRect, iCardsMsBmp, ETrue);
  648.                        break;;
  649.                        }
  650.                 default:
  651.                 {
  652.                     TInt posx,posy;
  653.                     posx = (TInt)((280 * xoff) + (TapisCol[DragCardHit-1] * (40 * xoff)) - (37 * xoff));
  654.                     posy = (TInt)((TapisRow[DragCardHit-1] * (23 * yoff)) + (3 * yoff));
  655.                     iCards52Bmp[Tapis[DragCardHit-1]-1]->SetSizeInTwips(CardSizeTwips);    
  656.                     gc.DrawBitmap(TPoint(posx,posy), iCards52Bmp[Tapis[DragCardHit-1]-1]);
  657.                     //gc.BitBltMasked(TPoint(posx,posy), iCards52Bmp[Tapis[DragCardHit-1]-1], bmpPieceRect, iCardsMsBmp, ETrue);
  658.                     break;
  659.                 }
  660.                }
  661.              break;
  662.              }
  663.       case 3:{
  664.              TInt card;
  665.              int i;
  666.              for(i = 0; i < 2; i++)
  667.              {
  668.              if(i == 0)
  669.                card = DragCardHit;
  670.              else
  671.                card = DropCardHit;
  672.              if(card == 0)
  673.                continue;
  674.              switch(card)
  675.                {
  676.                case 29:{
  677.                        Tallon = 0;
  678.                        iCardsNoBmp->SetSizeInTwips(CardSizeTwips);    
  679.                         gc.DrawBitmap(TPoint(TallonCol,TallonRow), iCardsNoBmp);
  680.                        //gc.BitBltMasked(TPoint(TallonCol,TallonRow), iCardsNoBmp, bmpPieceRect, iCardsMsBmp, ETrue);
  681.                        break;
  682.                        }
  683.                case 31:{
  684.                        --WasteSize;
  685.                        if(WasteSize > 0)
  686.                        {
  687.                           iCards52Bmp[Waste[WasteSize-1]-1]->SetSizeInTwips(CardSizeTwips);    
  688.                         gc.DrawBitmap(TPoint(WasteCol,WasteRow), iCards52Bmp[Waste[WasteSize-1]-1]);
  689.                          //  gc.BitBltMasked(TPoint(WasteCol,WasteRow), iCards52Bmp[Waste[WasteSize-1]-1], bmpPieceRect, iCardsMsBmp, ETrue);
  690.                        }
  691.                         else
  692.                         {
  693.                         iCardsNoBmp->SetSizeInTwips(CardSizeTwips);    
  694.                         gc.DrawBitmap(TPoint(WasteCol,WasteRow), iCardsNoBmp);
  695.                          //gc.BitBltMasked(TPoint(WasteCol,WasteRow), iCardsNoBmp, bmpPieceRect, iCardsMsBmp, ETrue);
  696.                         }
  697.                          break;;
  698.                        }
  699.                default:{
  700.                        TInt posx,posy,cardup,carddown;
  701.                        Tapis[card-1] = TapisState[card-1] = 0;
  702.                        cardup = TapisUpTree[(card-1)*2];
  703.                        if(cardup)
  704.                            {
  705.                            carddown = TapisDownTree[cardup-1];
  706.                            if((Tapis[carddown-1] == 0) && (Tapis[carddown] == 0))
  707.                              TapisState[cardup-1] = 1;
  708.                            }
  709.                        cardup = TapisUpTree[(card-1)*2 + 1];
  710.                        if(cardup)
  711.                            {
  712.                            carddown = TapisDownTree[cardup-1];
  713.                            if((Tapis[carddown-1] == 0) && (Tapis[carddown] == 0))
  714.                              TapisState[cardup-1] = 1;
  715.                            }
  716.                         posx = (TInt)((280 * xoff) + (TapisCol[card-1] * (40 * xoff)) - (37 * xoff));
  717.                         posy = (TInt)((TapisRow[card-1] * (23 * yoff)) + (3 * yoff));
  718.                         iUnderTapis[card-1]->SetSizeInTwips(CardSizeTwips);    
  719.                         gc.DrawBitmap(TPoint(posx,posy), iUnderTapis[card-1]);
  720.                         //gc.BitBlt(TPoint(posx,posy), iUnderTapis[card-1], TRect(0,0,CardSize.iWidth, CardSize.iHeight));
  721.                        break;
  722.                        }
  723.                }
  724.                }
  725.              DrawLabels(gc);
  726.              break;
  727.              }
  728.       }
  729.     if(Score == 52) // He won !
  730.       {
  731.       gc.SetPenStyle(gc.ESolidPen);
  732.       gc.SetBrushStyle(gc.ESolidBrush);
  733.       gc.SetBrushColor(KRgbWhite);
  734.       gc.DrawRect(Rect()); // clear the area to be drawn to
  735.       crd = 0;
  736.       for(py = 0; py < 2 ; py++)
  737.       for(px = 0; px < 8 ; px++)
  738.         {
  739.             iCards52Bmp[crd]->SetSizeInTwips(CardSizeTwips);    
  740.             gc.DrawBitmap(TPoint((px*78+8)*xoff,(py*120+10)*yoff), iCards52Bmp[crd]);
  741. // fix this
  742.             //gc.BitBltMasked(TPoint(px*78+8,py*120+10), iCards52Bmp[crd], bmpPieceRect, iCardsMsBmp, ETrue);
  743.         crd = (crd + 13) % 52;
  744.         }
  745.       }
  746.     DeactivateGc();
  747.     if(Score == 52)
  748.       {
  749.       CEikDialog* dialog = new (ELeave) CExampleAboutDialog();
  750.       GamesPlayed[Redeals]++;
  751.       GamesWon[Redeals]++;
  752.       MaxScore[Redeals] = 52;
  753.       TotalScore[Redeals] += 52;
  754.       dialog->ExecuteLD(R_EXAMPLE_WIN_DIALOG);
  755.       Random();
  756.       ActivateGc();
  757.       Draw(ScreenRect);
  758.       DeactivateGc();
  759.       }
  760. }
  761.  
  762. TKeyResponse CPyramidControl::OfferKeyEventL(
  763.             const TKeyEvent& aKeyEvent,TEventCode aType
  764.             )
  765.     {
  766.     return EKeyWasNotConsumed;
  767.     }
  768.  
  769. //
  770. // class CPyramidContainer
  771. //
  772.  
  773. void CPyramidContainer::ConstructL(const TRect& aRect, TPyramidModel* aModel)
  774.     {
  775.     iModel=aModel;
  776.     CreateWindowL();
  777.     iContext=this;
  778.        iBrushStyle=CGraphicsContext::ESolidBrush;
  779.     iBrushColor=KRgbWhite;
  780.     SetRectL(aRect);
  781.     iPyramidControl = new(ELeave)CPyramidControl;
  782.     iPyramidControl->ConstructL(Window(), iPyramidControl->ScreenRect);
  783.     ActivateL();
  784.     iPyramidControl->MyActivateGc(); // little hack for saving correctly the background
  785.     iPyramidControl->Draw(iPyramidControl->ScreenRect); // of the cards while using a BackedUpWindow
  786.     iPyramidControl->MyDeactivateGc();
  787.     }
  788.  
  789. CPyramidContainer::~CPyramidContainer()
  790.     {
  791.     delete iPyramidControl;
  792.     }
  793.     
  794. TInt CPyramidContainer::CountComponentControls() const
  795.     {
  796.     return 1;
  797.     }
  798.  
  799. CCoeControl* CPyramidContainer::ComponentControl(TInt aIndex) const
  800.     {
  801.     switch (aIndex)
  802.       {
  803.       case 0: return iPyramidControl;
  804.       default: return 0;
  805.       };
  806.     }
  807.  
  808.  
  809. TKeyResponse CPyramidContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
  810.     {
  811.     if    (iPyramidControl)
  812.            return iPyramidControl->OfferKeyEventL(aKeyEvent,aType);
  813.     else
  814.         return EKeyWasNotConsumed;
  815.     }
  816.  
  817. void CPyramidContainer::Draw(const TRect& /*aRect*/) const
  818.     {
  819.     CWindowGc& gc = SystemGc();
  820.     gc.SetPenStyle(CGraphicsContext::ENullPen);
  821.     gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
  822.     gc.DrawRect(Rect());
  823.     }
  824.  
  825. //
  826. // CPyramidAppUi
  827. //
  828.  
  829. void CPyramidAppUi::ConstructL()
  830.     {
  831.     BaseConstructL();
  832.     iModel=((CPyramidDocument*)iDocument)->Model();
  833.     iContainer=new(ELeave) CPyramidContainer;
  834.     iContainer->ConstructL(ClientRect(),iModel);
  835.     ReadProfile();
  836.     // add container to stack; enables key event handling.
  837.     AddToStackL(iContainer);
  838.     }
  839.  
  840. void CPyramidAppUi::HandleCommandL(TInt aCommand)
  841.     {
  842.     switch (aCommand)
  843.         {
  844.  
  845.     case EEikCmdEditCut:            // <---Pre-defined command id
  846.     case EEikCmdEditCopy:           // <---Pre-defined command id
  847.     case EEikCmdEditPaste:          // <---Pre-defined command id
  848.     case EEikCmdEditSelectAll:      // <---Pre-defined command id
  849.         iEikonEnv->InfoMsg(_L("Not used by Pyramid"));
  850.         return;
  851.         // Infrared command
  852.     case EEikCmdIrdaSend:
  853.         iEikonEnv->InfoMsg(_L("Sending cards to Jupiter"));
  854.         break;
  855.     case EEikCmdIrdaReceive:
  856.         iEikonEnv->InfoMsg(_L("Receiving cards from Pluton"));
  857.         break;
  858.  
  859.     case EEikCmdPreferences:
  860.         {
  861.         CEikDialog* dialog = new (ELeave) CPrefDialog(&(iContainer->iPyramidControl->MaxRedeals));
  862.         dialog->ExecuteLD(R_EXAMPLE_PREFERENCES_DIALOG);
  863.         break;
  864.         }
  865.  
  866.     case EEikCmdExit:
  867.         {
  868.         if((iContainer->iPyramidControl->Score > 0) && (iContainer->iPyramidControl->Score < 52))
  869.           {
  870.           CEikDialog* dialog = new (ELeave) CExampleAboutDialog();
  871.           if(dialog->ExecuteLD(R_EXAMPLE_ADDTOSTATISTICS_DIALOG))
  872.             {
  873.             iContainer->iPyramidControl->GamesPlayed[iContainer->iPyramidControl->Redeals]++;
  874.             iContainer->iPyramidControl->TotalScore[iContainer->iPyramidControl->Redeals] += iContainer->iPyramidControl->Score;
  875.             if(iContainer->iPyramidControl->Score > iContainer->iPyramidControl->MaxScore[iContainer->iPyramidControl->Redeals])
  876.               iContainer->iPyramidControl->MaxScore[iContainer->iPyramidControl->Redeals] = iContainer->iPyramidControl->Score;
  877.             }
  878.           }
  879.         UpdateProfile();
  880.         CBaActiveScheduler::Exit();
  881.         }
  882.     case EEikCmdAbout:
  883.         {
  884.         CEikDialog* dialog = new (ELeave) CExampleAboutDialog();
  885.         dialog->ExecuteLD(R_EXAMPLE_ABOUT_DIALOG);
  886.         break;
  887.         }
  888.     case EEikCmdNewGame:
  889.         {
  890.         if((iContainer->iPyramidControl->Score > 0) && (iContainer->iPyramidControl->Score < 52))
  891.           {
  892.           CEikDialog* dialog = new (ELeave) CExampleAboutDialog();
  893.           if(dialog->ExecuteLD(R_EXAMPLE_ADDTOSTATISTICS_DIALOG))
  894.             {
  895.             iContainer->iPyramidControl->GamesPlayed[iContainer->iPyramidControl->Redeals]++;
  896.             iContainer->iPyramidControl->TotalScore[iContainer->iPyramidControl->Redeals] += iContainer->iPyramidControl->Score;
  897.             if(iContainer->iPyramidControl->Score > iContainer->iPyramidControl->MaxScore[iContainer->iPyramidControl->Redeals])
  898.               iContainer->iPyramidControl->MaxScore[iContainer->iPyramidControl->Redeals] = iContainer->iPyramidControl->Score;
  899.             }
  900.           }
  901.         iContainer->iPyramidControl->Restart();
  902.         break;
  903.         }
  904.     case EEikCmdStatistics:
  905.         {
  906.         CEikDialog* dialog = new (ELeave) CStatDialog(&iContainer->iPyramidControl->GamesPlayed[0],
  907.         &iContainer->iPyramidControl->GamesWon[0], &iContainer->iPyramidControl->MaxScore[0],
  908.         &iContainer->iPyramidControl->TotalScore[0]);
  909.         dialog->ExecuteLD(R_EXAMPLE_STATISTICS_DIALOG);
  910.         break;
  911.         }
  912.  
  913.     case EEikCmdCStatistics:
  914.         {
  915.         CEikDialog* dialog = new (ELeave) CExampleAboutDialog();
  916.         if(dialog->ExecuteLD(R_EXAMPLE_CSTATISTICS_DIALOG))
  917.           {
  918.           int i;
  919.           for(i = 0; i < 4; i++)
  920.             {
  921.             iContainer->iPyramidControl->GamesPlayed[i] = 0;
  922.             iContainer->iPyramidControl->GamesWon[i] = 0;
  923.             iContainer->iPyramidControl->MaxScore[i] = 0;
  924.             iContainer->iPyramidControl->TotalScore[i] = 0;
  925.              }
  926.           }
  927.         break;
  928.         }
  929.  
  930.         }
  931.     }
  932.  
  933. CPyramidAppUi::~CPyramidAppUi()
  934.     {
  935.     delete iContainer;
  936.     }
  937.  
  938. //
  939. // CPyramidDocument
  940. //
  941.  
  942. CEikAppUi* CPyramidDocument::CreateAppUiL()
  943.     {
  944.     return(new(ELeave) CPyramidAppUi);
  945.     }
  946.  
  947. //
  948. // CPyramidApplication
  949. //
  950.  
  951. TUid CPyramidApplication::AppDllUid() const
  952.     {
  953.     return KUidPyramidApp;
  954.     }
  955.  
  956. CApaDocument* CPyramidApplication::CreateDocumentL()
  957.     {
  958.     return new(ELeave) CPyramidDocument(*this);
  959.     }
  960.  
  961. //
  962. // EXPORTed functions
  963. //
  964.  
  965. EXPORT_C CApaApplication* NewApplication()
  966.     {
  967.     return new CPyramidApplication;
  968.     }
  969.  
  970. GLDEF_C TInt E32Dll(TDllReason)
  971.     {
  972.     return KErrNone;
  973.     }
  974.  
  975. void CPrefDialog::PreLayoutDynInitL()
  976. {
  977.     TInt dlgid;
  978.     switch(*dlgpref)
  979.       {
  980.       case 1:dlgid = EExampleControlIdHorOptButton2; break;
  981.       case 2:dlgid = EExampleControlIdHorOptButton3; break;
  982.       case 3:dlgid = EExampleControlIdHorOptButton4; break;
  983.       default: dlgid = EExampleControlIdHorOptButton1; break;
  984.       }
  985.     SetHorOptionButListCurrentSet(EExampleControlIdHorOptButtonList,dlgid);
  986. }
  987.  
  988. TBool CPrefDialog::OkToExitL(TInt)
  989.     {
  990.     switch(HorOptionButListCurrentSet(EExampleControlIdHorOptButtonList))
  991.       {
  992.       case EExampleControlIdHorOptButton2: *dlgpref = 1; break;
  993.       case EExampleControlIdHorOptButton3: *dlgpref = 2; break;
  994.       case EExampleControlIdHorOptButton4: *dlgpref = 3; break;
  995.       default: *dlgpref = 0; break;
  996.       }
  997.     return ETrue;
  998.     }
  999.  
  1000. void CStatDialog::PreLayoutDynInitL()
  1001. {
  1002. TBuf<60> txt;
  1003. TReal r;
  1004. TRealFormat rf(6,2);
  1005. int i;
  1006. txt.Format(_L("Games Played: %d / %d / %d / %d"), gamesp[0], gamesp[1], gamesp[2], gamesp[3]);
  1007. SetLabelL(EExampleControlIdStat2, txt);
  1008. txt.Format(_L("Games Won:    %d / %d / %d / %d"), gamesw[0], gamesw[1], gamesw[2], gamesw[3]);
  1009. SetLabelL(EExampleControlIdStat3, txt);
  1010. txt = _L("Win Percent: ");
  1011. for(i = 0;; i++)
  1012.   {
  1013.   if(gamesp[i] == 0)
  1014.     r = 0;
  1015.   else
  1016.     r = ((TReal)gamesw[i]/(TReal)gamesp[i]) * 100;
  1017.   txt.AppendNum(r, rf);
  1018.   txt.Append('%');
  1019.   if(i == 3)
  1020.     break;
  1021.   txt.Append(_L(" / "));
  1022.   }
  1023. SetLabelL(EExampleControlIdStat4, txt);
  1024. txt = _L("Ave Score: ");
  1025. for(i = 0;; i++)
  1026.   {
  1027.   if(gamesp[i] == 0)
  1028.     r = 0;
  1029.   else
  1030.     r = (TReal)tscore[i]/(TReal)gamesp[i];
  1031.   txt.AppendNum(r, rf);
  1032.   if(i == 3)
  1033.     break;
  1034.   txt.Append(_L(" / "));
  1035.   }
  1036. SetLabelL(EExampleControlIdStat5, txt);
  1037. txt.Format(_L("Best Score: %d / %d / %d / %d"), mscore[0], mscore[1], mscore[2], mscore[3]);
  1038. SetLabelL(EExampleControlIdStat6, txt);
  1039. }
  1040.  
  1041. const TUid PROFILE_UID={0x1000055E};
  1042.  
  1043. void CPyramidAppUi::UpdateProfile()
  1044.     {
  1045.     int i;
  1046.       // Open the "ini" file. Application architecture creates the "ini" file
  1047.       // if it does not exist
  1048.     CDictionaryStore* iniFile = Application()->OpenIniFileLC(iCoeEnv->FsSession());
  1049.  
  1050.       // The stream containing the background preference will be associated
  1051.       // with a Uid. AssignL() creates the stream if it does not exist
  1052.     RDictionaryWriteStream writeStream;
  1053.     writeStream.AssignLC(*iniFile,PROFILE_UID);
  1054.     writeStream.WriteInt32L(iContainer->iPyramidControl->MaxRedeals);
  1055.     for(i = 0; i < 4; i++)
  1056.         {
  1057.         writeStream.WriteInt32L(iContainer->iPyramidControl->GamesPlayed[i]);
  1058.         writeStream.WriteInt32L(iContainer->iPyramidControl->GamesWon[i]);
  1059.         writeStream.WriteInt32L(iContainer->iPyramidControl->MaxScore[i]);
  1060.         writeStream.WriteInt32L(iContainer->iPyramidControl->TotalScore[i]);
  1061.         }
  1062.     writeStream.CommitL();
  1063.     CleanupStack::PopAndDestroy();
  1064.      
  1065.     // Commit changes to the store
  1066.     if (iniFile->Commit()!=KErrNone)
  1067.         iniFile->RevertL(); 
  1068.     // Destroys the "ini" file store object and closes
  1069.     // the "ini" file
  1070.     CleanupStack::PopAndDestroy(); // iniFile
  1071.     }
  1072.  
  1073. void CPyramidAppUi::ReadProfile()
  1074.     {
  1075.     TInt v, i;
  1076.       // Open the "ini" file. Application architecture creates the "ini" file
  1077.       // if it does not exist
  1078.     CDictionaryStore* iniFile = Application()->OpenIniFileLC(iCoeEnv->FsSession());
  1079.     
  1080.       // The stream containing the background preference will be associated
  1081.       // with a Uid.
  1082.       // Open the stream associted with the Uid; it will be created
  1083.       // if it does not exist.
  1084.     RDictionaryReadStream readStream;
  1085.     readStream.OpenL(*iniFile,PROFILE_UID);
  1086.  
  1087.     iContainer->iPyramidControl->MaxRedeals = 2;
  1088.     for(i = 0; i < 4; i++)
  1089.       {
  1090.       iContainer->iPyramidControl->GamesPlayed[i] = 0;
  1091.       iContainer->iPyramidControl->GamesWon[i] = 0;
  1092.       iContainer->iPyramidControl->MaxScore[i] = 0;
  1093.       iContainer->iPyramidControl->TotalScore[i] = 0;
  1094.       }
  1095.       // If the stream has just been created (because the "ini" file has just
  1096.       // been created), it will be "empty" and attempting to read from 
  1097.       // the stream will leave with a KErrEof.
  1098.     TRAPD(err, v = readStream.ReadInt32L());
  1099.     if (!err)
  1100.         {
  1101.         iContainer->iPyramidControl->MaxRedeals = v;
  1102.         for(i = 0; i < 4; i++)
  1103.           {
  1104.           v = readStream.ReadInt32L();
  1105.           iContainer->iPyramidControl->GamesPlayed[i] = v;
  1106.           v = readStream.ReadInt32L();
  1107.           iContainer->iPyramidControl->GamesWon[i] = v;
  1108.           v = readStream.ReadInt32L();
  1109.           iContainer->iPyramidControl->MaxScore[i] = v;
  1110.           v = readStream.ReadInt32L();
  1111.           iContainer->iPyramidControl->TotalScore[i] = v;
  1112.           }
  1113.         }
  1114.     readStream.Close();
  1115.  
  1116.     CleanupStack::PopAndDestroy();
  1117.  
  1118.     }
  1119.