Jump to content
  • Advertisement
Sign in to follow this  
Gamesmaster3

My Text Keeps flashing

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been experimenting with getting text to change on the screen when you press a button.In this case it changes if I press the left mouse button.The text shows up but the problem is it flashes.At first I thought it had something to do with the twinkling program that was a part of this project before but I've since got rid of that portion of the code but still the text flashes.Can anyone tell me why and how to fix.Oh yeah if you have a better way of displaying text altogether I'm open to that too.Just give me some ideas here. Here's the code.
 
//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include "changemsg.h"

//-----------------------------------------------------------------
// Game Engine Functions
//-----------------------------------------------------------------
BOOL GameInitialize(HINSTANCE hInstance)
{
  // Create the game engine
  _pGame = new GameEngine(hInstance, TEXT("Change Message Demo"),
    TEXT("Change Message Dem"), IDI_MD, IDI_MD_SM, 600, 450);
   // Set the frame rate
  _pGame->SetFrameRate(30);

  // Store the instance handle
  _hInstance = hInstance;

  return TRUE;
}

void GameStart(HWND hWindow)
{
  // Seed the random number generator
  srand(GetTickCount());

  // Create the offscreen device context and bitmap
  _hOffscreenDC = CreateCompatibleDC(GetDC(hWindow));
  _hOffscreenBitmap = CreateCompatibleBitmap(GetDC(hWindow),
    _pGame->GetWidth(), _pGame->GetHeight());
  SelectObject(_hOffscreenDC, _hOffscreenBitmap);

  // Create and load the text
  HDC hDC = GetDC(hWindow);

 

  // Create the starry background
  _pBackground = new Bitmap(hDC,600, 450,RGB(0,0,0));

  // Play the background music
  _pGame->PlayMIDISong(TEXT("battle_2.mid"));


int _iMessage = 1;
  // Start the game
  NewGame();
}

void GameEnd()
{
  // Close the MIDI player for the background music
  _pGame->CloseMIDIPlayer();

  // Cleanup the offscreen device context and bitmap
  DeleteObject(_hOffscreenBitmap);
  DeleteDC(_hOffscreenDC);  

  
  // Cleanup the background
  delete _pBackground;

  // Cleanup the sprites
  _pGame->CleanupSprites();

  // Cleanup the game engine
  delete _pGame;
}

void GameActivate(HWND hWindow)
{
  // Resume the background music
  _pGame->PlayMIDISong(TEXT(""), FALSE);
}

void GameDeactivate(HWND hWindow)
{
  // Pause the background music
  _pGame->PauseMIDISong();
}

void GamePaint(HDC hDC)
{
  // Draw the background
  _pBackground->Draw(hDC,0,0);


	 
 

  // Draw the sprites
  _pGame->DrawSprites(hDC);

  // Draw the message depending on the value of iMessage

  
 HWND hWindow = _pGame ->GetWindow(); 
  switch( _iMessage)
  {

  case 1:
HDC hDC;
RECT rect;

	  
GetClientRect(hWindow, &rect);
hDC = GetDC(hWindow); 
  
  SetBkMode(hDC, TRANSPARENT);
  SetTextColor(hDC, RGB(255, 255, 255));
  DrawText(hDC, TEXT("This is Message1"), -1, &rect,
	  DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  ReleaseDC(hWindow,hDC);
  return ;
  
  case 2:

	  
GetClientRect(hWindow, &rect);
hDC = GetDC(hWindow); 
  
  SetBkMode(hDC, TRANSPARENT);
  SetTextColor(hDC, RGB(255, 255, 255));
  DrawText(hDC, TEXT("and this is message 2"), -1, &rect,
	  DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  ReleaseDC(hWindow,hDC);
  return ;
  }

  
}

void GameCycle()
{
  if (!_bGameOver)
  {
   
    
  

    // Update the sprites
    //_pGame->UpdateSprites();

    // Obtain a device context for repainting the game
    HWND  hWindow = _pGame->GetWindow();
    HDC   hDC = GetDC(hWindow);

    // Paint the game to the offscreen device context
    GamePaint(_hOffscreenDC);

    // Blit the offscreen bitmap to the game screen
    BitBlt(hDC, 0, 0, _pGame->GetWidth(), _pGame->GetHeight(),
      _hOffscreenDC, 0, 0, SRCCOPY);

    // Cleanup
    ReleaseDC(hWindow, hDC);
  }
}

void HandleKeys()
{
}

void MouseButtonDown(int x, int y, BOOL bLeft)
{
  if (!_bGameOver && bLeft)
  {
	  ++_iMessage;
	 // InvalidateRect(_pGame->GetWindow(),NULL,FALSE);
	  
   
  }
  else if (_bGameOver && !bLeft)
    // Start a new game
    NewGame();
}

void MouseButtonUp(int x, int y, BOOL bLeft)
{
}

void MouseMove(int x, int y)
{
 
}

void HandleJoystick(JOYSTATE jsJoystickState)
{
}

BOOL SpriteCollision(Sprite* pSpriteHitter, Sprite* pSpriteHittee)
{
  

  return FALSE;
}

void SpriteDying(Sprite* pSprite)
{
  
}

//-----------------------------------------------------------------
// Functions
//-----------------------------------------------------------------
void NewGame()
{
  // Clear the sprites
  _pGame->CleanupSprites();

  int iMessage = 0;
  _bGameOver = FALSE;

  

  // Play the background music
  _pGame->PlayMIDISong();
}


Share this post


Link to post
Share on other sites
Advertisement
Not read all that - looks like a lot of windows specific stuff, however, and it looks like you first clear the window, then draw text on it. This will result in your window first being cleared, and then having text drawn on it. Some people would call that 'flicker' :)

If that is the cause, then the key word you need to look up is "double buffering".

Share this post


Link to post
Share on other sites
I think it might because you are creating a brand new dc to draw the text rather then working with the one you already have. Try drawing the text with the same dc you pass to the GamePaint function instead...

Share this post


Link to post
Share on other sites
Quote:
Original post by Squirm
Not read all that - looks like a lot of windows specific stuff, however, and it looks like you first clear the window, then draw text on it. This will result in your window first being cleared, and then having text drawn on it. Some people would call that 'flicker' :)

If that is the cause, then the key word you need to look up is "double buffering".


First off the only reason I included so much of the code is because
1)Often the problem is in a total different area than you would think it's
in
2)To show everyone that I did do the work (granted it's not much here)and I'm
not just another scavenger.
But to get to your comment I use to have the flicker problem when I first started but I thought that getting a Offscreen Device Context and bitmap solved the flicker problem.At least it had for graphics.Does it not work for text too??

Quote:
xllx_reliant_xllx
Where is your GamePaint() function being called from? under BeginPaint()? more source code would be helpful.


The actual code for GamePaint and other gameengine functions were put into the GameEngine.cpp file.Because it already worked on previous game projects I didn't see a need to put it here.

Quote:
MeatBeef
I think it might because you are creating a brand new dc to draw the text rather then working with the one you already have. Try drawing the text with the same dc you pass to the GamePaint function instead...



You couldn't show me what you mean MeatBeef??The variable hDC is a local variable.So I have to declare it again for any function that uses it correct?

Share this post


Link to post
Share on other sites
I made this change and it doesn't flicker; Using the same book.


void GamePaint(HDC hDC)
{
RECT rcBounds = { 0, 0, 600, 400 };
// Draw the background galaxy
g_pMybackground->Draw(hDC, 0, 0);
g_pGame->DrawSprites(hDC);
switch( _iMessage)
{

case 1:







SetBkMode(hDC, TRANSPARENT);
SetTextColor(hDC, RGB(255, 255, 255));
DrawText(hDC, TEXT("This is Message1"), -1, &rcBounds,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);

return ;

case 2:





SetBkMode(hDC, TRANSPARENT);
SetTextColor(hDC, RGB(255, 255, 255));
DrawText(hDC, TEXT("and this is message 2"), -1, &rcBounds,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);

return ;
}


}

Share this post


Link to post
Share on other sites
Thanks alot Zaris.It sure dosen't flicker anymore.But what is it about your code that's different from what I did.Is there something about setting the actual size of the rectangle that did it or was it something else?

Share this post


Link to post
Share on other sites
You were reaquiring the window; causing it to redraw; all I did was take out HWND hWindow = _pGame ->GetWindow();
and the additional hDC settings.



you don't need to redefine hDC it is define when you call gamepaint(hDC)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!