Sign in to follow this  

Double Buffer Issue w/ Pong Ball

This topic is 4732 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

Everything gets sent to the double buffer before it gets painted onto the game_dc, yet the ball moves on the screen as if it's two. There is no flickering, but the eyes make it seem like there's two balls. If I pause the screen, I only see one ball. I think it's just eye tricks, but it's not smooth. It sorta strains the eyes to see it. A possible reason is the speed that the ball is going. SPEED is at 22 as default. As you can see here, each frame increments the ball's X position by 22. That could explain its jumping (if that's what it is), but Andre' Lamothe says with double buffering, it should be smooth without doubt.
if (ball_moveRight == true)
ball_x += SPEED;
Am I doing something wrong in this function?
inline void PaintScreen()
{
 			// Paint Background Image onto Back Buffer
			SelectObject(obj_dc, bmpBkgnd);
			BitBlt(back_dc, 0, 0, 800, 600, obj_dc, 0, 0, SRCCOPY);

			// Paint Current LPad Position onto Back Buffer
			SelectObject(obj_dc, bmpLPad); 
			BitBlt(back_dc, LPad_x, LPad_y, LPad_cx, LPad_cy, obj_dc, 0, 0, SRCCOPY);	
			
			// Paint Current RPad Position onto Back Buffer
			SelectObject(obj_dc, bmpRPad);
			BitBlt(back_dc, 776, RPad_y, RPad_cy, RPad_cy, obj_dc, 0, 0, SRCCOPY);

			// Mask Ball onto Back Buffer for Transparency
			SelectObject(obj_dc, bmpBallMask);
			BitBlt(back_dc, ball_x, ball_y, ball_cx, ball_cy, obj_dc, 0, 0, SRCAND);

			// Paint Ball onto Back Buffer
			SelectObject(obj_dc, bmpBall);
			BitBlt(back_dc, ball_x, ball_y, ball_cx, ball_cy, obj_dc, 0, 0, SRCPAINT);

			// If the player has the difficulty setting to 'bring it on!', render the second 
			// ball onto the screen.
			if (ShowBall2 == true)
				{
				// Mask Ball2 onto Back Buffer for Transparency
				SelectObject(obj_dc, bmpBall2Mask);
				BitBlt(back_dc, ball2_x, ball2_y, ball2_cx, ball2_cy, obj_dc, 0, 0, SRCAND);

				// Paint Ball2 onto Back Buffer
				SelectObject(obj_dc, bmpBall);
				BitBlt(back_dc, ball2_x, ball2_y, ball2_cx, ball2_cy, obj_dc, 0, 0, SRCPAINT);
			}

			// Send back_dc to game_dc
			BitBlt(game_dc, 0, 0, WIDTH, HEIGHT, back_dc, 0, 0, SRCCOPY);

			// Erase Back Buffer
			BitBlt(back_dc, 0, 0, WIDTH, HEIGHT, back_dc, 0, 0, BLACKNESS);
}





Or the game loop?
	// Game Loop
	while (!done)
	{	   				
		DWORD start_time = GetTickCount();
			
		if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
		{
			// Did the Player Decide to Quit? If so, Exit the Game Loop.
			if (Msg.message == WM_QUIT)
			{
				done = TRUE;

			}
			else
			{
				TranslateMessage(&Msg);		// Translate any Accelerator Keys
				DispatchMessage(&Msg);		// Send to WndProc
			}
		}
		else
		{ 
			if (Active == true)				// Only Get Player's Input/Physics/Collision/AI/Painting 
			{								// when Active Is set to true.	
				
					// Did someone reach the MaxWins?
					if ((PlayerScore >= MaxWins) || (ComputerScore >= MaxWins))
					{
						WinLostMessage();					  // Get a Random Winning/Loosing Message for the Player
						ResetGame();						  // Reset Game Data
						ShowWindow(hDlgStart, SW_SHOWNORMAL); // Show Start Menu
					}

				if (  KEYDOWN('W') || KEYDOWN('S') || KEYDOWN(VK_ESCAPE) || KEYDOWN(VK_UP) || KEYDOWN(VK_DOWN) )
				{
					// Input
					GetPlayerInput();
				}	
				
				// Physics & Collision
				GetPhysics();
				
				// AI
				GetAI();
			}

			// Paint the Bitmaps & Current X/Y Coords onto the Screen
			PaintScreen();

			// Set Score on the Window Title
			SetScore();  
 
		    // lock framerate to 33 milliseconds (30fps)
			// Sleep 1 millisecond to reduce full 100% usage of the computer, especially for laptops when hot.
		    while((GetTickCount() - start_time) < 33) Sleep(1);		
			
			
		}
	}

Thanks for the future help. [Edited by - philvaira on January 1, 2005 12:34:31 PM]

Share this post


Link to post
Share on other sites
i dont know if the speed is what is causeing the problem but 22 is an auful lot of speed take a 640X480 resalution

640
___ ~=(approx) 29.90

22

if your FPS is 30 frames a second or more your giving the
the player less then a second to get the paddle were it
needs to be. Maybe i inturpreted your speed wrong but it
seems like alot (note if you've played the game a little
and there is no problem then just ignore this)
-------------------------------------------------------------------------------
in regards to time based movement all you really have to do is get the time it took in-beetween frames like so

Counter = SDL_GetTicks();
srand(Counter);
td2=SDL_GetTicks();
dt=((float)(td2-td))*0.1;
td=td2;
this is with SDL and so just sub in the get tick method for your API then every place you say

Object.x /*(or y)*/ += Velocity

change it to

Object.x /*(or y)*/ += Velocity * dt;

this might give you an error because the RECT stuct may not take floats in that case just cast it back to an int

Object.x /*(or y)*/ += (int)(Velocity * dt);

if your velocity is one for any object just do this

Object.x /*(or y)*/ += (int)(dt);

cause 1*dt = dt

it takes a lot to try and explain it but its actully very easy to implement

Share this post


Link to post
Share on other sites
this is how it works
#include <mmsystme.h>
#pragma comment(lib,"winmm.lib")


unsigned long ltime = timeGetTime();
while(!done)
{
// message loop

unsigned long ctime = timeGetTime();
float time = (float)(ctime-ltime)/1000.0f;
ltime = ctime;

// update game objects
// multiply objects speed by time
}

Share this post


Link to post
Share on other sites
Thanks for the advice. However, nothing worked hehe. I set SPEED = 1, and the ball is smooth. If it is any faster, it seems hard for the eyes to tell which is the ball. With SPEED at 22, it takes 2 seconds for it to get from the left side to the right side of the screen. Is this a double buffer issue rather than a timing issue?

Share this post


Link to post
Share on other sites
Quote:
Original post by mike25025
this is how it works
*** Source Snippet Removed ***


Thanks. I tried what you provided, yet the ball is extremely fast on the screen, causing about 20 visible balls to appear before they get deleted.

Share this post


Link to post
Share on other sites
i would say yes and that your frame rate isnt fast enough but when you went with the time based movement and it was way too fast that means you have a decent frame rate so im not quite sure also is erase buffer clearing the buffer or totally deleting it?

EDIT you might want to try and update the screen twice a frame i dont really know if that will fix it though

Share this post


Link to post
Share on other sites
Quote:
Original post by philvaira
Quote:
Original post by mike25025
this is how it works
*** Source Snippet Removed ***


Thanks. I tried what you provided, yet the ball is extremely fast on the screen, causing about 20 visible balls to appear before they get deleted.


did you multiply the speed be time?

float x = 0;
float sx = 20;

unsinged long ltime = timeGetTime();
while(!done)
{
// message loop

unsinged long ctime = timeGetTime();
float time = (float)(ctime-ltime)/1000.0f;
ltime = ctime;

// update game objects
// multiply objects speed by time
x += sx*time; // LIKE THIS?
}

Share this post


Link to post
Share on other sites
According to this...

// Erase Back Buffer
BitBlt(back_dc, 0, 0, WIDTH, HEIGHT, back_dc, 0, 0, BLACKNESS);

It's erasing everything in the buffer, and starting up in the next frame again. Commenting this line out results in the same output.

Mike, yes.

Should I post the whole source code?

Share this post


Link to post
Share on other sites

This topic is 4732 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.

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