Archived

This topic is now archived and is closed to further replies.

Back Buffer Help

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

hey, i need some help with my pong game. I was trying to make the graphics smoother useing a back buffer and page flipping, but i am confused on where to put the flip. i draw the paddle and the ball tyo the back buffer, and so far whenever I flip it, it goes haywire and does what bitmaps do if you dont clear a scene with a black bitmap, it shows lots of balls and paddles. Here is all the source code. #include #include #include #include #include "Graphics.h" #include "ddutil.h" #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) #define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1) #define WINDOW_CLASS_NAME "WINCLASS" int x = 300; int y = 300; int x1 = 200; int y1 = 200; int MAX_X1 = 0; int MAX_X2 = 600; int MAX_Y1 = 0; int MAX_Y2 = 800; int BallX; int BallY; int BallVecX = 10; int BallVecY = 10; int BallSize=4; int score; char buffer[80]; HFONT fnt; HINSTANCE hinstance; HDC hdc; HWND hwnd; RECT rc; RECT bl; void DrawBlocks() { DisplayBitmap(lpddsback,rc.top,rc.bottom,rc.left,rc.right,"main.bmp"); DisplayBitmap(lpddsback,bl.top,bl.bottom,bl.left,bl.right,"third.bmp"); } void TestWalls() { //first 2 sides if(bl.top >800) { BallVecX=-BallVecX + rand()%2; bl.top+=BallVecX; } if(bl.bottom < 0) { BallVecY=-BallVecY+ rand()%2; bl.top+=BallVecY; } //other 2 sides if(bl.top < 0) { BallVecX=-BallVecX+ rand()%2; bl.top+=BallVecX; } if(bl.bottom > 780) { BallVecY=-BallVecY+ rand()%2; BallVecY=rand()%5; } else //penalize player if ball hits bottom of screen// if(bl.bottom > 590) { BallVecY=-BallVecY; bl.bottom+=BallVecY; score-=100; } } void TestPaddle() { ///test to see if the ball hit the paddle/// int x = bl.top+(BallSize/2); int y = bl.bottom+(BallSize/2); if((x >= rc.top && x <= rc.top + rc.left) && (y >= rc.bottom && y<= rc.bottom + rc.right)) { BallVecY=-BallVecY; bl.bottom+=BallVecY; if(KEYDOWN(VK_LEFT)) BallVecX+=(rand()%3); else if(KEYDOWN(VK_RIGHT)) BallVecX-=(rand()%3); else BallVecX+=(-1+rand()%3); } //DrawBlocks(); } void Input() { if(KEYDOWN(VK_RIGHT)) { if(rc.top >= MAX_X2) { rc.top=rc.top + 0; } else rc.top=rc.top + 20; //DrawBlocks(); } if(KEYDOWN(VK_LEFT)) { if(rc.top <= MAX_X1) { rc.top=rc.top - 0; } else rc.top=rc.top - 20; //DrawBlocks(); } } LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { PAINTSTRUCT ps; HDC hdc; switch(msg) { case WM_CREATE: { return(0); }break; case WM_PAINT: { hdc = GetDC(hwnd); ReleaseDC(hwnd,hdc); return(0); }break; case WM_DESTROY: { PostQuitMessage(0); return(0); }break; default:break; } return DefWindowProc(hwnd,msg,wparam,lparam); } int GameMain() { if(KEYUP(VK_ESCAPE)) PostMessage(hwnd,WM_DESTROY,0,0); /////move ball//// bl.top+=BallVecX; bl.bottom+=BallVecY; ////////////////// Input(); TestWalls(); TestPaddle(); DrawBlocks(); /////////////////////Draw Score/////////////////////// sprintf(buffer,"PONG By:Matt Stobber Score:%d ",score); DrawText(buffer,25,25,RGB(100,255,0),lpddsprimary,fnt); return(1); } int GameInit() { rc.top=50,rc.bottom=550,rc.left=200,rc.right=20; bl.top=450,bl.bottom=250,bl.left=20,bl.right=20; DirectDrawCreate(NULL,&lpdd,NULL); lpdd->QueryInterface(IID_IDirectDraw, (LPVOID *)&lpdd); lpdd->SetCooperativeLevel(hwnd,DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT | DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX); lpdd->SetDisplayMode(800,600,24); ZeroMemory(&ddsd,sizeof(ddsd)); ddsd.dwSize=sizeof(ddsd); ddsd.dwFlags=DDSD_CAPS | DDSD_BACKBUFFERCOUNT; ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP | DDSCAPS_VIDEOMEMORY; ddsd.dwBackBufferCount = 1; lpdd->CreateSurface( &ddsd, &lpddsprimary, NULL); ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER; lpddsprimary->GetAttachedSurface(&ddsd.ddsCaps,&lpddsback); ShowCursor(false); ScaleBitmap(lpddsprimary,"Splash.bmp"); Sleep(2000); ScaleBitmap(lpddsprimary,"stones.bmp"); return(1); } int GameShutdown() { if(lpdd) { lpdd->Release(); lpdd=NULL; } return(1); } int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow) { WNDCLASSEX wc; HDC hdc; MSG msg; wc.cbSize=sizeof(WNDCLASSEX); wc.style=CS_DBLCLKS | CS_OWNDC | CS_VREDRAW | CS_HREDRAW; wc.lpfnWndProc=WindowProc; wc.cbWndExtra=0; wc.cbClsExtra=0; wc.hInstance=hinstance; wc.hIcon=LoadIcon(NULL,IDI_APPLICATION); wc.hCursor=LoadCursor(NULL,IDC_ARROW); wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH); wc.lpszMenuName=NULL; wc.lpszClassName=WINDOW_CLASS_NAME; wc.hIconSm=LoadIcon(NULL,IDI_APPLICATION); if(!RegisterClassEx(&wc)) return(0); if(!(hwnd = CreateWindowEx(NULL, WINDOW_CLASS_NAME, "MY WINDOW", WS_POPUP | WS_VISIBLE, 0,0, 640,480, NULL, NULL, hinstance, NULL))) return(0); GameInit(); while(TRUE) { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if(msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } GameMain(); } GameShutdown(); return(msg.wParam); } any help is most appreciated.Thanks

Share this post


Link to post
Share on other sites
hey, i just fixed some of it, now the ball and paddle blink no stop. What should i do now? Heres the updated source code.
#include
#include
#include
#include
#include "Graphics.h"
#include "ddutil.h"


#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
#define WINDOW_CLASS_NAME "WINCLASS"
int x = 300;
int y = 300;
int x1 = 200;
int y1 = 200;
int MAX_X1 = 0;
int MAX_X2 = 600;
int MAX_Y1 = 0;
int MAX_Y2 = 800;
int BallX;
int BallY;
int BallVecX = 10;
int BallVecY = 10;
int BallSize=4;
int score;
char buffer[80];
HFONT fnt;
HINSTANCE hinstance;
HDC hdc;
HWND hwnd;

RECT rc;
RECT bl;
void DrawBlocks()
{

ScaleBitmap(lpddsprimary,"stones.bmp");
DisplayBitmap(lpddsback,rc.top,rc.bottom,rc.left,rc.right,"main.bmp");
DisplayBitmap(lpddsback,bl.top,bl.bottom,bl.left,bl.right,"third.bmp");

lpddsprimary->Flip(NULL,DDFLIP_WAIT);
}

void TestWalls()
{

//first 2 sides
if(bl.top >800)
{
BallVecX=-BallVecX + rand()%2;
bl.top+=BallVecX;
}
if(bl.bottom < 0)
{
BallVecY=-BallVecY+ rand()%2;
bl.top+=BallVecY;
}
//other 2 sides
if(bl.top < 0)
{
BallVecX=-BallVecX+ rand()%2;
bl.top+=BallVecX;
}
if(bl.bottom > 780)
{
BallVecY=-BallVecY+ rand()%2;
BallVecY=rand()%5;
}
else
//penalize player if ball hits bottom of screen//
if(bl.bottom > 590)
{
BallVecY=-BallVecY;
bl.bottom+=BallVecY;
score-=100;
}

}


void TestPaddle()
{

///test to see if the ball hit the paddle///
int x = bl.top+(BallSize/2);
int y = bl.bottom+(BallSize/2);

if((x >= rc.top && x <= rc.top + rc.left) &&
(y >= rc.bottom && y<= rc.bottom + rc.right))
{
BallVecY=-BallVecY;
bl.bottom+=BallVecY;
if(KEYDOWN(VK_LEFT))
BallVecX+=(rand()%3);
else
if(KEYDOWN(VK_RIGHT))
BallVecX-=(rand()%3);
else
BallVecX+=(-1+rand()%3);
}

}

void Input()
{
if(KEYDOWN(VK_RIGHT))
{
if(rc.top >= MAX_X2)
{
rc.top=rc.top + 0;
}
else
rc.top=rc.top + 20;
DrawBlocks();
}

if(KEYDOWN(VK_LEFT))
{
if(rc.top <= MAX_X1)
{
rc.top=rc.top - 0;
}
else
rc.top=rc.top - 20;
DrawBlocks();
}
}

void MoveBall()
{

/////move ball////
bl.top+=BallVecX;
bl.bottom+=BallVecY;
//////////////////
}


LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
PAINTSTRUCT ps;
HDC hdc;

switch(msg)
{
case WM_CREATE:
{
return(0);
}break;
case WM_PAINT:
{
hdc = GetDC(hwnd);
ReleaseDC(hwnd,hdc);
return(0);
}break;


case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
}break;

default:break;
}
return DefWindowProc(hwnd,msg,wparam,lparam);
}


int GameMain()
{
if(KEYUP(VK_ESCAPE))
PostMessage(hwnd,WM_DESTROY,0,0);

MoveBall();
Input();
TestWalls();
TestPaddle();
DrawBlocks();



/////////////////////Draw Score///////////////////////
sprintf(buffer,"PONG By:Matt Stobber Score:%d ",score);
DrawText(buffer,25,25,RGB(100,255,0),lpddsprimary,fnt);

return(1);
}

int GameInit()
{

rc.top=50,rc.bottom=550,rc.left=200,rc.right=20;
bl.top=450,bl.bottom=250,bl.left=20,bl.right=20;
DirectDrawCreate(NULL,&lpdd,NULL);

lpdd->QueryInterface(IID_IDirectDraw,
(LPVOID *)&lpdd);


lpdd->SetCooperativeLevel(hwnd,DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT | DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX);

lpdd->SetDisplayMode(800,600,24);

ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize=sizeof(ddsd);
ddsd.dwFlags=DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP | DDSCAPS_VIDEOMEMORY;
ddsd.dwBackBufferCount = 1;

lpdd->CreateSurface( &ddsd, &lpddsprimary, NULL);


ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
lpddsprimary->GetAttachedSurface(&ddsd.ddsCaps,&lpddsback);


ShowCursor(false);
ScaleBitmap(lpddsprimary,"Splash.bmp");
Sleep(2000);
ScaleBitmap(lpddsprimary,"stones.bmp");

return(1);
}




int GameShutdown()
{

if(lpdd)
{
lpdd->Release();
lpdd=NULL;
}

return(1);
}

int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
WNDCLASSEX wc;
HDC hdc;
MSG msg;

wc.cbSize=sizeof(WNDCLASSEX);
wc.style=CS_DBLCLKS | CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
wc.lpfnWndProc=WindowProc;
wc.cbWndExtra=0;
wc.cbClsExtra=0;
wc.hInstance=hinstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName=NULL;
wc.lpszClassName=WINDOW_CLASS_NAME;
wc.hIconSm=LoadIcon(NULL,IDI_APPLICATION);

if(!RegisterClassEx(&wc))
return(0);

if(!(hwnd = CreateWindowEx(NULL,
WINDOW_CLASS_NAME,
"MY WINDOW",
WS_POPUP | WS_VISIBLE,
0,0,
640,480,
NULL,
NULL,
hinstance,
NULL)))
return(0);



GameInit();
while(TRUE)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{

if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}

GameMain();
}
GameShutdown();
return(msg.wParam);
}


Share this post


Link to post
Share on other sites