⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mopoidcontainer.cpp

📁 一款大砖块的游戏
💻 CPP
字号:
/*
* ============================================================================
*  Name     : CMopoidContainer from MopoidContainer.cpp
*  Part of  : Mopoid
*  Created  : 16.01.2004 by Andreas Jakl / Mopius - http://www.mopius.com/
*  Description:
*     Declares container control for application.
*  Version  : 1.02
*  Copyright:
*     'Mopoid' is free software; you can redistribute
*     it and/or modify it under the terms of the GNU General Public License
*     as published by the Free Software Foundation; either version 2 of
*     the License, or (at your option) any later version.
*
*     'Mopoid' is distributed in the hope that it
*     will be useful, but WITHOUT ANY WARRANTY; without even the implied
*     warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*     See the GNU General Public License for more details.
*
*     You should have received a copy of the GNU General Public License
*     along with 'Mopoid'; if not, write to the Free Software
*     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
* ============================================================================
*/

#include <aknutils.h>
#include <Mopoid.rsg>
#include <uikon.hrh>
#include "Mopoidcontainer.h"


CMopoidContainer* CMopoidContainer::NewL(const TRect& aRect)
{
  CMopoidContainer* self = CMopoidContainer::NewLC(aRect);
  CleanupStack::Pop(self);
  return self;
}

CMopoidContainer* CMopoidContainer::NewLC(const TRect& aRect)
{
  CMopoidContainer* self = new (ELeave) CMopoidContainer;
  CleanupStack::PushL(self);
  self->ConstructL(aRect);
  return self;
}

CMopoidContainer::CMopoidContainer()
{
}

CMopoidContainer::~CMopoidContainer()
{
    delete iGameEngine;
  CleanupComponents();
  iCtrlArray.Reset();
}

// -----------------------------------------------------------------------
// Routine that creates and initializes designed components
//
// NOTE: This routine is managed by the C++Builder IDE - DO NOT MODIFY
// -----------------------------------------------------------------------
void CMopoidContainer::InitComponentsL()
{
    /* 22.01.05 18:48 */
    iBackgroundColor = iEikonEnv->Color( EColorControlBackground );
    iEikonEnv->AppUiFactory()->StatusPane()->MakeVisible( EFalse );
    SetRect( TRect( 0, 0, 176, 208 ) );
}

// -----------------------------------------------------------------------
// Routine that cleans up designed components
//
// NOTE: This routine is managed by the C++Builder IDE - DO NOT MODIFY
// -----------------------------------------------------------------------
void CMopoidContainer::CleanupComponents()
{
    /* 22.01.05 18:48 */
}

void CMopoidContainer::ConstructL(const TRect& aRect)
{
  CreateWindowL();
  SetRect(aRect);
  InitComponentsL();
  ActivateL();
  iGameEngine = CMopoidGameEngine::NewL(this);


  // Load game engine


}

void CMopoidContainer::Draw(const TRect& aRect) const
{
    CWindowGc& gc = SystemGc();
    if (iGameEngine)
    {
        gc.BitBlt(TPoint(0,0), iGameEngine->iBackBufferBmp);

        // Game paused?
        if (iGameEngine->iSettings.iGamePaused)
        {
            // Choose a red color
	        gc.SetPenStyle(CGraphicsContext::ESolidPen);
	        gc.SetPenColor(KRgbRed);
            TBuf<30> tempStr;

            // Display the message to press the joystick to end the pause.
            if (iGameEngine->iSettings.iGameStatus != TMopoidSettings::EFinished &&
                iGameEngine->iSettings.iGameStatus != TMopoidSettings::EDied)
            {
	            gc.UseFont(CEikonEnv::Static()->AnnotationFont());
	            CEikonEnv::Static()->ReadResource(tempStr, RS_R_PRESSJOYSTICK);
	            gc.DrawText(tempStr, TRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), 145, CGraphicsContext::ECenter);
            }

            // Display game paused string.
            if (iGameEngine->iSettings.iGameStatus == TMopoidSettings::EPlaying)
            {
	            gc.UseFont(CEikonEnv::Static()->TitleFont());
	            CEikonEnv::Static()->ReadResource(tempStr, RS_R_PAUSE);
	            gc.DrawText(tempStr, TRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), 125, CGraphicsContext::ECenter);
            }
        }
    }
}

// -----------------------------------------------------------------------
// Returns the number of controls in the control group.
// -----------------------------------------------------------------------
TInt CMopoidContainer::CountComponentControls() const
{
  return iCtrlArray.Count();
}

// -----------------------------------------------------------------------
// Returns the component control identified by the given index
// -----------------------------------------------------------------------
CCoeControl* CMopoidContainer::ComponentControl(TInt aIndex) const
{
  return (CCoeControl*)iCtrlArray[aIndex];
}

// -----------------------------------------------------------------------
// Method invoked by control framework to handle key events
// -----------------------------------------------------------------------
TKeyResponse CMopoidContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
{
  if (HandleKeyEvents(aKeyEvent, aType))
    return EKeyWasConsumed;
  else if(iFocusedControl)
    return iFocusedControl->OfferKeyEventL(aKeyEvent, aType);
  else
    return EKeyWasNotConsumed;
}

// -----------------------------------------------------------------------
// Method invoked to handle an event from an observed control
// -----------------------------------------------------------------------
void CMopoidContainer::HandleControlEventL(CCoeControl * aControl, TCoeEvent aEventType)
{
  DispatchControlEvents(aControl, aEventType);
}

// -----------------------------------------------------------------------
// This method attemps to dispatch commands to individual handlers
//
// NOTE: This routine is managed by the C++Builder IDE - DO NOT MODIFY
// -----------------------------------------------------------------------
bool CMopoidContainer::DispatchViewCommandEvents(TInt aCommand)
{
    switch ( aCommand )
    {
        case 1001:
            OniMenuItemNewGameViewCommand( aCommand );
        break;
        case 1002:
            OniMenuItemAboutViewCommandL( aCommand );
        break;
        default:
            return false;
    }
    return true;
}

// -----------------------------------------------------------------------
// Routine that attempts to dispatch Control Events
//
// NOTE: This routine is managed by the C++Builder IDE - DO NOT MODIFY
// -----------------------------------------------------------------------
void CMopoidContainer::DispatchControlEvents(CCoeControl * aControl, TCoeEvent aEventType)
{
}

// -----------------------------------------------------------------------
// Routine where you can handle key events
// -----------------------------------------------------------------------
TBool CMopoidContainer::HandleKeyEvents(const TKeyEvent& aKeyEvent, TEventCode aType)
{
    TBool handled = EFalse;
    if(aType == EEventKeyDown)
    {
        // Player pressed a key. Store this so that the panel moves in the
        // corresponding direction until the player releases the key.
		switch ( aKeyEvent.iScanCode )
		{
			case EStdKeyLeftArrow:
				{
					iGameEngine->iKeyHandler.LeftPressed();
					handled = ETrue;
					break;
				}
			case EStdKeyRightArrow:
				{
					iGameEngine->iKeyHandler.RightPressed();
					handled = ETrue;
					break;
				}
            case EStdKeyDevice0:
            case EStdKeyDevice1:
                {
                    // Left or right softkey
                    iGameEngine->PauseGame();
					handled = ETrue;
					break;
                }
            case '5':
            case EStdKeyDevice3:
                {
                    // Joystick pressed. If the menu was active, resume the game.
                    if (iGameEngine->iHaveFocus && iGameEngine->iSettings.iGamePaused)
                    {
                        TRAPD(err, iGameEngine->ResumeGameL());
                    }
					break;
                }
        }
    }
    else if (aType == EEventKeyUp)
    {
        // Player released a key.
		switch ( aKeyEvent.iScanCode )
		{
			case EStdKeyLeftArrow:
            case EStdKeyRightArrow:
            case '4':
            case '6':
				{
                    // Not moving in any direction anymore.
					iGameEngine->iKeyHandler.NoDirection();
					handled = ETrue;
					break;
				}
        }

    }
    return handled;
}

// -----------------------------------------------------------------------
// Helper routine to manage Control Focus
// -----------------------------------------------------------------------
void CMopoidContainer::ChangeFocus(CCoeControl * aNewControl)
{
  if(iFocusedControl)
    iFocusedControl->SetFocus(EFalse);
  iFocusedControl = aNewControl;
  if(iFocusedControl)
    iFocusedControl->SetFocus(ETrue);
}

TInt CMopoidContainer::ExecuteiAknAboutNoteL()
{
    /* 22.01.05 18:48 */
    TBuf < 256 > TBuf_2561;

    iCoeEnv->ReadResource( TBuf_2561, RS_R_ABOUTMESSAGE );
    CAknInformationNote * iAknAboutNote = new( ELeave )CAknInformationNote;
    iAknAboutNote->SetTimeout( CAknNoteDialog::ENoTimeout );
    iAknAboutNote->SetTone( CAknNoteDialog::ENoTone );
    iAknAboutNote->SetTextWrapping( ETrue );
    return iAknAboutNote->ExecuteLD( TBuf_2561 );
}

void CMopoidContainer::OniMenuItemAboutViewCommandL( TInt aCommand )
{
    // Menu command: Display about note
    TRAPD(err, ExecuteiAknAboutNoteL());
}

void CMopoidContainer::OniMenuItemNewGameViewCommand( TInt aCommand )
{
    // Menu command: Start a new game
    iGameEngine->StartNewGame();
}

void CMopoidContainer::OniMenuItemSoundOnViewCommandL( TInt aCommand )
{
    // Menu command: Deactivate sound (clicked on sound on -> switch to sound off)
    iGameEngine->SetSoundLevelL(EFalse);
}

void CMopoidContainer::OniMenuItemSoundOffViewCommandL( TInt aCommand )
{
    // Menu command: Activate sound (clicked on sound off -> switch to sound on)
    iGameEngine->SetSoundLevelL(ETrue);
}









⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -