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

📄 ski.cpp

📁 series60 应用程序开发的源代码 series60 应用程序开发的源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
* @return the key handler
*/
TKeyHandler& CSkiEngine::KeyHandler()
{
    return iKeyHandler;
}

/**
* Key handler accessor
* @return the key handler
*/
const TKeyHandler& CSkiEngine::KeyHandler() const
{
    return iKeyHandler;
}

/**
* Scroll driver accessor
* @return the scrll driver
*/
TScrollDriver& CSkiEngine::ScrollDriver()
{
    return iScrollDriver;
}

/**
* Scroll driver accessor
* @return the scrll driver
*/
const TScrollDriver& CSkiEngine::ScrollDriver() const
{
    return iScrollDriver;
}

//
// SkiBitmapLoader::
//

/**
* Loads a bitmap from an id
* @param aName The filename of the mbm
* @param aId the id of the image wanted for the created bitmap
* @return the newly created bitmap, on ther cleanup stack
*/
CFbsBitmap* SkiBitmapLoader::CreateBitmapLC(const TDesC& aName,TInt aId)
{
    CFbsBitmap* bitmap = new(ELeave) CFbsBitmap();
    CleanupStack::PushL(bitmap);
    User::LeaveIfError(bitmap->Load(aName, aId));
    return bitmap;
}

/**
* Adds the skier sprites to a gallery
* @param aGallery the gallery to add the skier sprites to
*/
void SkiBitmapLoader::AddSkierSpritesToGalleryL(CGameGallery& aGallery)
{
    TFileName filename = KMultiBitmapFilename();
    CompleteWithAppPath(filename);  // don't need to handle error here - the path will be unchanged

    CFbsBitmap* bitmap = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesS7);
    CFbsBitmap* mask = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesSm7);

    aGallery.AddBitmapL(bitmap, mask, TSkierAttribs::ESkier0);
    CleanupStack::Pop(2, bitmap);

    bitmap = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesS6);
    mask = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesSm6);

    aGallery.AddBitmapL(bitmap, mask, TSkierAttribs::ESkier30);
    CleanupStack::Pop(2, bitmap);

    bitmap = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesS5);
    mask = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesSm5);

    aGallery.AddBitmapL(bitmap, mask, TSkierAttribs::ESkier60);
    CleanupStack::Pop(2, bitmap);

    bitmap = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesS4);
    mask = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesSm4);

    aGallery.AddBitmapL(bitmap, mask, TSkierAttribs::ESkier90);
    CleanupStack::Pop(2, bitmap);

    bitmap = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesS3);
    mask = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesSm3);

    aGallery.AddBitmapL(bitmap, mask, TSkierAttribs::ESkier120);
    CleanupStack::Pop(2, bitmap);

    bitmap = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesS2);
    mask = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesSm2);

    aGallery.AddBitmapL(bitmap, mask, TSkierAttribs::ESkier150);
    CleanupStack::Pop(2, bitmap);

    bitmap = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesS1);
    mask = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesSm1);

    aGallery.AddBitmapL(bitmap, mask, TSkierAttribs::ESkier180);
    CleanupStack::Pop(2, bitmap);

    bitmap = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesSkiercrash);
    mask = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesSkiercrashm);

    aGallery.AddBitmapL(bitmap, mask, TSkierAttribs::ESkierFall);
    CleanupStack::Pop(2, bitmap);
}

/**
* Adds the map sprites to a gallery
* @param aGallery the gallery to add the map sprites to
*/
void SkiBitmapLoader::AddMapSpritesToGalleryL(CGameGallery& aGallery)
{
    TFileName filename = KMultiBitmapFilename();
    CompleteWithAppPath(filename);  // don't need to handle error here - the path will be unchanged
    //
    //blue flag

    CFbsBitmap* bitmap = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesFlagarrowleft);
    CFbsBitmap* mask = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesFlagm);

    aGallery.AddBitmapL(bitmap, mask, TMapPrimitive::ELeftFlag);
    CleanupStack::Pop(2, bitmap);

    //
    //red flag

    bitmap = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesFlagarrowright);
    mask = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesFlagm);

    aGallery.AddBitmapL(bitmap, mask, TMapPrimitive::ERightFlag);
    CleanupStack::Pop(2, bitmap);

    //
    //log

    bitmap = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesLog);
    mask = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesLogmask);

    aGallery.AddBitmapL(bitmap, mask, TMapPrimitive::ELog);
    CleanupStack::Pop(2, bitmap);

    //
    //tree

    bitmap = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesTree);
    mask = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesTreemask);

    aGallery.AddBitmapL(bitmap, mask, TMapPrimitive::ETree);
    CleanupStack::Pop(2, bitmap);

    //
    //bump

    bitmap = SkiBitmapLoader::CreateBitmapLC(filename,EMbmImagesSnowbump);
    aGallery.AddBitmapL(bitmap, NULL, TMapPrimitive::EBump);
    CleanupStack::Pop(bitmap);
}

//
// CSkiFrameRenderer::
//

/**
* Standard Symbian 2 phase constructor
* @param aSize The size of the screen
* @param aEngineAttribs the engine attributes
*/
CSkiFrameRenderer* CSkiFrameRenderer::NewL(TSize aSize, CSkiEngine& aEngineAttribs)
{
    CSkiFrameRenderer* self = NewLC(aSize, aEngineAttribs);
    CleanupStack::Pop(self);
    return self;
}

/**
* Standard Symbian 2 phase constructor.  The object is left on the cleanup stack
* @param aSize The size of the screen
* @param aEngineAttribs the engine attributes
*/
CSkiFrameRenderer* CSkiFrameRenderer::NewLC(TSize aSize, CSkiEngine& aEngineAttribs)
{
    CSkiFrameRenderer* self = new (ELeave) CSkiFrameRenderer(aEngineAttribs);
    CleanupStack::PushL(self);
    self->ConstructL(aSize);
    return self;
}

/**
* Destructor
*/
CSkiFrameRenderer::~CSkiFrameRenderer()
{
    delete iDBArea;
}

/**
* Renders a game frame
* @return A bitmap containing a game frame
*/
const CFbsBitmap* CSkiFrameRenderer::RenderedFrameL() const
{
    iDBArea->ClearBufferedArea();

    CSkiMap* map = iEngineAttribs.Map().IntersectsLC(iScrAttribs.Rect(), iEngineAttribs.MapGallery());

    CMapPrimitiveArray* bumps = map->TypeLC(TMapPrimitive::EBump);
    const TInt bumpCount = bumps->Count();

    for (TInt ii = 0; ii < bumpCount; ii++)
    {
        DrawPrimitiveL(bumps->At(ii));
    }

    CleanupStack::PopAndDestroy(bumps);

    // Get scenery and find position of skier.  When we get passed the y position of the
    // skier we can draw the skier
    //

    TSkiVector posVect = iEngineAttribs.Skier().Attribs().Posn();
    TPoint skierPosn(static_cast<TInt>(posVect.iX), static_cast<TInt>(posVect.iY));
    TBool skierDrawn = EFalse;

    TInt type = TMapPrimitive::ETree | TMapPrimitive::ELeftFlag | TMapPrimitive::ERightFlag | TMapPrimitive::ELog;
    CMapPrimitiveArray* scenery = map->TypeLC(type);
    const TInt count = scenery->Count();

    for (TInt iii = 0; iii < count; iii++)
    {
        TMapPrimitive primitive = scenery->At(iii);

        // So this doesn't mess up if we have a negative offset..
        //
        TInt primitiveYPos = primitive.Posn().iY;
        TInt skierYPos = skierPosn.iY;

        TBool primitiveInfront = EFalse;

        if ((primitiveYPos < 0 && skierYPos < 0) || (primitiveYPos >= 0 && skierYPos >= 0))
        {
            primitiveInfront = primitiveYPos > skierYPos;
        }

        else if (primitiveYPos < 0) // && skierYPos >= 0
        {
            primitiveInfront = EFalse;
        }

        else // if skierYPos <0 && primitiveYPos >= 0
        {
            primitiveInfront = ETrue;
        }

        if (!skierDrawn &&  primitiveInfront)
        {
            DisplaySkierL();
            skierDrawn = ETrue;
        }

        DrawPrimitiveL(primitive);
    }

    if (!skierDrawn)
    {
        DisplaySkierL();
    }

    CleanupStack::PopAndDestroy(2, map);

    return &(iDBArea->GetDoubleBufferedAreaBitmap());
}

/**
* C++ constructor
* @param aEngineAtrbs the engine attributes
*/
CSkiFrameRenderer::CSkiFrameRenderer(CSkiEngine& aEngineAttribs) :
    iEngineAttribs(aEngineAttribs),
    iScrAttribs(aEngineAttribs.ScreenAttribs())
{
}


/**
* Standard SymbianOS 2nd phase constructor
* @param aSize The sze of the screen
*/
void CSkiFrameRenderer::ConstructL(TSize aSize)
{
    iDBArea = CDoubleBufferedArea::NewL(aSize, EColor4K);
}

/**
* Draw a map primitive to the back buffer
* @param aPrimitive the primitive to draw
*/
void CSkiFrameRenderer::DrawPrimitiveL(const TMapPrimitive& aPrimitive) const
{
    CMapGallery& gallery = iEngineAttribs.MapGallery();
    CSkiSprite& sprite = gallery.SpriteL(aPrimitive.Type());

    // this obtains the sprite rectangle with absolute (map) coordinates
    TRect absoluteBounds = gallery.RectangleL(aPrimitive);
    // this converts to a screen coordinate
    TPoint scrPosn = absoluteBounds.iTl - iScrAttribs.Offset();

    DrawSpriteL(scrPosn, sprite);
}

/**
* Draw the skier to the back buffer
*/
void CSkiFrameRenderer::DisplaySkierL() const
{
    CSkier& skier = iEngineAttribs.Skier();

    TSkiVector posVect = skier.Attribs().Posn();
    TPoint skierPosn(static_cast<TInt>(posVect.iX), static_cast<TInt>(posVect.iY));

    CFbsBitmap& bitmap = skier.SpriteL().Bitmap();

    // GET SKIER SPRITE ON SCREEN
    //
    //
    TSize skierSize = bitmap.SizeInPixels();
    TRect skiRect(skierPosn - iScrAttribs.Offset(), skierSize);
    // map position is bottom centre of object
    skiRect.Move(-skierSize.iWidth / 2, -skierSize.iHeight - skier.Attribs().Elevation());

    TPoint posn = skiRect.iTl;
    CFbsBitGc& gc = iDBArea->GetDoubleBufferedAreaContext();

    if (skier.Jumping())
    {
        //Draw shadow if the skier is jumping
        TPoint shadowPosn = skierPosn - iScrAttribs.Offset();
        shadowPosn.iY -= skierSize.iHeight;
        TRect shadow(shadowPosn, TSize(8,8));

        gc.SetBrushColor(KRgbBlack);
        gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
        gc.DrawEllipse(shadow);
        gc.SetBrushColor(KRgbWhite);
        gc.SetBrushStyle(CGraphicsContext::ENullBrush);

        // put the skier in the "air"
        posn.iY -= skier.Attribs().Elevation();
    }

    gc.BitBltMasked(posn, &(bitmap), bitmap.SizeInPixels(), skier.SpriteL().Mask(), ETrue);

}


/**
* Draw a sprite to the back buffer
* @param aPosn the screen position to draw to
* @param aSprite the sprite to draw
*/
void CSkiFrameRenderer::DrawSpriteL(TPoint aPosn, const CSkiSprite& aSprite) const
{
    const CFbsBitmap* mask = aSprite.Mask();
    const CFbsBitmap& bitmap = aSprite.Bitmap();
    CFbsBitGc& gc = iDBArea->GetDoubleBufferedAreaContext();
    gc.Reset();

    if (mask)
    {
        gc.BitBltMasked(aPosn, &bitmap, bitmap.SizeInPixels(), mask, ETrue);
    }
    else
    {
        CFbsBitmap* copy = new (ELeave) CFbsBitmap;
        CleanupStack::PushL(copy);
        User::LeaveIfError(copy->Duplicate(bitmap.Handle()));
        gc.BitBlt(aPosn, copy);
        CleanupStack::PopAndDestroy(copy);
    }
}

//
// CSkiGameLoop::
//

/**
* Standard SymbianOS 2-phase constructor
* @param aEngine the game engine
* @param aRenderer the frame rederer
* @aam aDSA direct screen access
*/
CSkiGameLoop* CSkiGameLoop::NewL(CSkiEngine& aEngine, CSkiFrameRenderer& aRenderer, CDSAWrapper& aDSA)
{
    CSkiGameLoop* self = NewLC(aEngine, aRenderer, aDSA);
    CleanupStack::Pop(self);
    return self;
}

/**
* Standard SymbianOS 2-phase constructor.  The object is left on the cleanup stack
* @param aEngine the game engine
* @param aRenderer the frame rederer
* @aam aDSA direct screen access
*/
CSkiGameLoop* CSkiGameLoop::NewLC(CSkiEngine& aEngine, CSkiFrameRenderer& aRenderer, CDSAWrapper& aDSA)
{
    CSkiGameLoop* self = new (ELeave) CSkiGameLoop(aEngine, aRenderer, aDSA);
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
}

/**
* Desructor
*/
CSkiGameLoop::~CSkiGameLoop()
{
}

/**
* Kicks off the timer
* @param anInterval the time in miliseconds between timer events
*/
void CSkiGameLoop::Start(TTimeIntervalMicroSeconds32 anInterval)
{
    Cancel();
    iInterval = anInterval;
    After(iInterval);
}

/**
* C++ constructor
* @param aEngine the game engine
* @param aRenderer the frame rederer
* @aam aDSA direct screen access
*/
CSkiGameLoop::CSkiGameLoop(CSkiEngine& aEngine, CSkiFrameRenderer& aRenderer, CDSAWrapper& aDSA) :
    CTimer(EPriorityHigh),
    iEngine(aEngine),
    iRenderer(aRenderer),
    iDSA(aDSA),
    iPaused(EFalse)
{
}

/**
* From CActive.  Updates the game engine and screen
*/
void CSkiGameLoop::RunL()
{
    if (!iPaused)
    {
        // instantiates DSA if not already present
        if (!iDSA.DSAAvailable())
        {
            iDSA.InstantiateDSAL(*this);
        }

        iDSA.StartDSAL();
        iEngine.UpdateEngineL();
        iDSA.UpdateScreenDevice();

        // Blit the offscreen image onto the screen at the top left position
        CFbsBitGc* gc = iDSA.DSAGc();

        if (gc)
        {
            gc->BitBlt(TPoint(0,0), iRenderer.RenderedFrameL());
        }
    }

    After(iInterval);
}

/**
* Standard SymbianOS 2nd phase construction
*/
void CSkiGameLoop::ConstructL()
{
    CTimer::ConstructL();
    CActiveScheduler::Add(this);
}

/**
* From CActive.  Called if RunL leaves
*/
TInt CSkiGameLoop::RunError(TInt /*aError*/)
{
    return KErrNone;
}

/**
* From CActive.
*/
void CSkiGameLoop::DoCancel()
{
    iDSA.CancelDSA();
    iDSA.DeleteDSA();
    CTimer::DoCancel();
}

/**
* Sets the paused flag.  If this is set, the game engine and screen aren't updated.
* @param aFlag the required value for the pause flag
*/
void CSkiGameLoop::SetPaused(TBool aFlag)
{
    iPaused = aFlag;
}

/**
* From MDirectScreenAccess
*/
void CSkiGameLoop::Restart(RDirectScreenAccess::TTerminationReasons /*aReason*/)
{
}

/**
* From MDirectScreenAccess
*/
void CSkiGameLoop::AbortNow(RDirectScreenAccess::TTerminationReasons /*aReason*/)
{
    iDSA.CancelDSA();
    iPaused = ETrue;
}

//
// CDSAWrapper::
//

/**
* Standard Symbian OS 2-phase construction
*/
CD

⌨️ 快捷键说明

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