Sign in to follow this  
raptorstrike

flickering problem

Recommended Posts

ok im trying to post the socre on the screen but its flickering so baddly i cant see it maybe i did my double buffering wrong or something because i dont thinks it should be this bad any way heres what i got BTW im using windows dev-c++
#include "breakout.h"
    

BOOL GameInitialize(HINSTANCE hInstance)
{
    _pGame = new GameEngine(hInstance , TEXT("breakout"), TEXT("breakout"), IDI_UFO, IDI_UFO_SM,500, 650);
        if (_pGame == NULL)
        {
            return FALSE;
        }; 
    _pGame->SetFrameRate(40);
    
        _hInstance =hInstance;
        return TRUE;
}
void GameStart(HWND hWindow)
{
    Offscreenbitmap = CreateCompatibleBitmap(GetDC(hWindow),_pGame->GetWidth(), _pGame->GetHeight());
    OffscreenDC = CreateCompatibleDC(GetDC(hWindow));
    RECT BlockBounds = { 0,0,_pGame->GetWidth(), _pGame->GetHeight()};
    for(int j = 0; j<30; j++)
    {
        blockbits[j] = new Bitmap(hDC, IDB_BLOCK , _hInstance);
    };    
    for(int i = 0; i<30; i++)
    {
        blocks[i] = new Sprite(blockbits[i] , BlockBounds , SA_NONE);
        _pGame->AddSprite(blocks[i]);    
    };
    for (int l = 0; l <30; l++)
    { 
        blocks[l]->SetPosition(rand()%500, rand()%450);
        for(int s=0; s < 30; s++)
          if( blocks[l]->TestCollision(blocks[s]) == TRUE)
          {
              if (blocks[s] != blocks[l])
               blocks[l]->SetPosition(rand()%500, rand()%450);
          };    
                      
    };    
        
    RECT BallBounds =  { 0 , 0 , _pGame->GetWidth() , _pGame->GetHeight()};
    RECT PaddleBounds = { 0 , 0 , _pGame->GetWidth() , _pGame->GetHeight()};
    srand(GetTickCount());
    HDC hDC = GetDC(hWindow);
    BackGround = new Bitmap(hDC, IDB_BACKGROUND , _hInstance);
    ballbit = new Bitmap(hDC , IDB_BALL , _hInstance);
    paddlebit = new Bitmap(hDC , IDB_PADDLE , _hInstance);
    ball = new Sprite( ballbit , BallBounds , BA_BOUNCE);
    paddle = new Sprite (paddlebit , PaddleBounds , BA_WRAP);
    ball->SetVelocity(0,5);
    paddle->SetVelocity(PaddleSpeed,0);
    paddle->SetPosition(250,600);
    _pGame->AddSprite(ball);
    _pGame->AddSprite(paddle);
    
}  
void GameEnd()
{
    _pGame->CleanupSprites();
    //_pGame->CloseMIDIPlayer();
    delete _pGame;
}
void GameActivate (HWND hWindow)
{
    
}   
void GameDeactivate  (HWND hWindow)
{
    
}  
void GamePaint( HDC hDC)
{
    TCHAR szText[64];
    RECT rect = { 237, 360, 301 };
    wsprintf(szText, "%d", score);
    DrawText(hDC, szText, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    BackGround->Draw(hDC, 0 , 0 , TRUE);
    _pGame->DrawSprites(hDC);
}
void GameCycle ()
{
     DrawText(hDC, szText, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    _pGame->UpdateSprites();
    //ball->Update();
    //paddle->Update();
    InvalidateRect(_pGame->GetWindow(), NULL, FALSE);
    HWND hWIndow =_pGame->GetWindow();
    HDC hDC = GetDC(hWindow);
    GamePaint(OffscreenDC);
    BitBlt(hDC, 0, 0, _pGame->GetWidth(), _pGame-> GetHeight(), OffscreenDC, 0, 0, SRCCOPY);
    ReleaseDC(hWindow, hDC);
    if(PaddleSpeed != 0 && PaddleSpeed < 0)
    PaddleSpeed++;
    if(PaddleSpeed != 0 && PaddleSpeed > 0)
    PaddleSpeed--;
    paddle->SetVelocity(PaddleSpeed,0);
}
void HandleKeys()
{
    if(GetAsyncKeyState(VK_LEFT) < 0)
    {
        PaddleSpeed = -7;
        paddle->SetVelocity(PaddleSpeed, 0);
    };  
    if(GetAsyncKeyState(VK_RIGHT) < 0)
    {
        PaddleSpeed = 7;
        paddle->SetVelocity(PaddleSpeed, 0);
    };      
    
}
void MouseButtonDown(int x, int y, BOOL bLeft)
{}
void MouseButtonUp(int x, int y, BOOL bLeft)
{}
void MouseMove(int x, int y)
{} 
BOOL SpriteCollision(Sprite* pSpriteHitter, Sprite* pSpriteHittee)
{
  if(pSpriteHitter->GetBitmap() == ballbit && pSpriteHittee->GetBitmap() == paddlebit)
  {
      if(PaddleSpeed != 0)
      ball->SetVelocity(-PaddleSpeed,-5);
      else ball->SetVelocity(0,-5); 
  }
  for(int i = 0; i < 30; i++)
  {
      if(pSpriteHitter->GetBitmap() == ballbit && pSpriteHittee->GetBitmap() == blockbits[i] && pSpriteHittee->IsHidden() != TRUE)
      {
      
           ball->SetVelocity(0,5);
           pSpriteHittee->SetHidden(TRUE);
      };    
  }; 
  return false;

}              


Share this post


Link to post
Share on other sites
i'm using that same engine as you (can't remember the name of the book it came with, though it was blue book). anyways i get that flickering problem too, i think its because of the windows api, though i really have no idea

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this