Archived

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

tenchimusakiryoko

Back Buffer Prob

Recommended Posts

hey, i made a pong game, but the graphics are slow. I tried to use page flipping for the movements but whenever I write to the backbuffer at all, it freezes. What should I do? Heres some of my code that may be the prob. 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); ZeroMemory(&ddscaps,sizeof(ddscaps)); ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER; lpddsprimary->GetAttachedSurface(&ddscaps,&lpddsback); ScaleBitmap(lpddsprimary,"Splash.bmp"); Sleep(2000); ScaleBitmap(lpddsprimary,"stones.bmp"); void DrawBlocks() { lpddsback->Blt(&rc,NULL,NULL,NULL,NULL); lpddsback->Blt(&bl,NULL,NULL,NULL,NULL); } usually i use my bitmap loading function, but that didnt work either. Any help is most appreciated. "Good,evil,I got the gun" Ash- Evil Dead

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The Blt it''s not right.Look in de documentation.

HRESULT Blt(
LPRECT lpDestRect, //destination rect
LPDIRECTDRAWSURFACE7 lpDDSrcSurface, //source surface
LPRECT lpSrcRect, //source rect
DWORD dwFlags, //flags
LPDDBLTFX lpDDBltFx
);

Share this post


Link to post
Share on other sites
hey heres the new source it slows down when there are 4 or more bitmaps.why
//GameFunctions.h//
#include
#define TILE_SIZE 32
#define SCREEN_SIZEX 20
#define SCREEN_SIZEY 20

#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"

LPDIRECTDRAW lpdd=NULL;
LPDIRECTDRAWSURFACE lpddsprimary=NULL;
LPDIRECTDRAWSURFACE lpddsback=NULL;
LPDIRECTDRAWSURFACE lpddsbitmap=NULL;
LPDIRECTDRAWSURFACE lpddscolorkey=NULL;
LPDIRECTDRAWCLIPPER lpddclipper;
LPDIRECTDRAWPALETTE lpddpal=NULL;
PALETTEENTRY palette[256];
PALETTEENTRY save_palette[256];
DDSURFACEDESC ddsd;
DDBLTFX ddbltfx;
DDSCAPS ddscaps;
DDCOLORKEY ddckey;

bool twoplayer;
int MAX_X1 = 0;
int MAX_X2 = 600;
int MAX_Y1 = 0;
int MAX_Y2 = 800;
int BallVecX = 5;
int BallVecY = 5;
float BallSize=4;
int score;
int score2;
RECT rc2;
RECT fb;
RECT fb2;
RECT fb3;
RECT fb4;
RECT rc;
RECT bl;

char buffer[80];
char buffer2[80];
HFONT fnt;
HINSTANCE hinstance;
HDC hdc;
HWND hwnd;




//Displays a bitmap at any given coordinates
int DisplayBitmap(LPDIRECTDRAWSURFACE lpdd,int x1,int y1,int x,int y,LPSTR szImage)
{

HBITMAP hbm;
HDC hdcImage=NULL;
HDC hdcSurf=NULL;
BOOL bReturn=FALSE;
DDSURFACEDESC ddsd;

ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize=sizeof(ddsd);


lpdd->GetSurfaceDesc (&ddsd);

hbm = (HBITMAP) LoadImage(NULL,szImage,
IMAGE_BITMAP, x,
y, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
hdcImage=CreateCompatibleDC(NULL);
SelectObject(hdcImage,hbm);
lpdd->GetDC(&hdcSurf);

if(BitBlt(hdcSurf,x1,y1,x,y,
hdcImage,0,0,SRCCOPY) == FALSE)
{
goto exit;
}
bReturn=TRUE;

exit:
if(hdcSurf)
lpdd->ReleaseDC(hdcSurf);
if(hdcImage)
DeleteDC(hdcImage);
if(hbm)
DeleteObject(hbm);

return bReturn;
}

//scales a bitmap across the screen(stretches)
int ScaleBitmap(LPDIRECTDRAWSURFACE lpdd, LPSTR szImage)
{

HBITMAP hbm;
HDC hdcImage=NULL;
HDC hdcSurf=NULL;
BOOL bReturn=FALSE;
DDSURFACEDESC ddsd;

ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize=sizeof(ddsd);

lpdd->GetSurfaceDesc (&ddsd);

hbm = (HBITMAP) LoadImage(NULL,szImage,
IMAGE_BITMAP, ddsd.dwWidth,
ddsd.dwHeight, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
hdcImage=CreateCompatibleDC(NULL);
SelectObject(hdcImage,hbm);
lpdd->GetDC(&hdcSurf);

if(BitBlt(hdcSurf,0,0,ddsd.dwWidth,ddsd.dwHeight,
hdcImage,0,0,SRCCOPY) == FALSE)
{
goto exit;
}
bReturn=TRUE;

exit:
if(hdcSurf)
lpdd->ReleaseDC(hdcSurf);
if(hdcImage)
DeleteDC(hdcImage);
if(hbm)
DeleteObject(hbm);

return bReturn;
}

int LoadBitmap(LPDIRECTDRAWSURFACE lpdd, LPSTR szImage)
{

HBITMAP hbm;
HDC hdcImage=NULL;
HDC hdcSurf=NULL;
BOOL bReturn=FALSE;
DDSURFACEDESC ddsd;

ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize=sizeof(ddsd);

lpdd->GetSurfaceDesc (&ddsd);

hbm = (HBITMAP) LoadImage(NULL,szImage,
IMAGE_BITMAP, ddsd.dwWidth,
ddsd.dwHeight, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
hdcImage=CreateCompatibleDC(NULL);
SelectObject(hdcImage,hbm);
lpdd->GetDC(&hdcSurf);



exit:
if(hdcSurf)
lpdd->ReleaseDC(hdcSurf);
if(hdcImage)
DeleteDC(hdcImage);
if(hbm)
DeleteObject(hbm);

return bReturn;
}

//My tile function,it takes a pointer to a surface,and passes the parameter to it
//very useful
int DrawTiles(LPDIRECTDRAWSURFACE lpdds)
{
int tile;
int x;
int y;


RECT tile_src;

char map[20][20] = {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};


for(y =0; y < SCREEN_SIZEY; y++)
{
for(x = 0;x < SCREEN_SIZEX; x++)
{

tile=map[y][x];
tile_src.left = (tile - 1) * TILE_SIZE;
tile_src.top = 0;
tile_src.right = tile * TILE_SIZE;
tile_src.bottom = TILE_SIZE;

lpddsprimary->BltFast(x * TILE_SIZE, y * TILE_SIZE, lpdds,&tile_src,NULL);

}
}
return(0);
}


/////////////////////////FONT ENGINE///////////////////////////////

void DrawText(char *text, int x,int y,COLORREF color,LPDIRECTDRAWSURFACE lpdds,HFONT fnt)
{



HDC hdc;
lpdds->GetDC(&hdc);
SetTextColor(hdc,color);
SetBkMode(hdc,TRANSPARENT);
SelectObject(hdc,fnt);
TextOut(hdc,x,y,text,strlen(text));
lpdds->ReleaseDC(hdc);
}
/////////////////////////////////////
int GameMain();


void ClearScreen()
{
ddbltfx.dwSize = sizeof( ddbltfx );
ddbltfx.dwFillColor = 0;
if(lpddsback->Blt( NULL, NULL, NULL,
DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx));
}

void TwoPlayerDrawBlocks()
{

ClearScreen();
DisplayBitmap(lpddsback,fb.top,fb.bottom,fb.left,fb.right,"stones.bmp");
DisplayBitmap(lpddsback,fb2.top,fb2.bottom,fb2.left,fb2.right,"stones.bmp");
DisplayBitmap(lpddsback,fb3.top,fb3.bottom,fb3.left,fb3.right,"stones.bmp");
DisplayBitmap(lpddsback,fb4.top,fb4.bottom,fb4.left,fb4.right,"stones.bmp");
DisplayBitmap(lpddsback,bl.top,bl.bottom,bl.left,bl.right,"Ball.bmp");
DisplayBitmap(lpddsback,rc2.top,rc2.bottom,rc2.left,rc2.right,"main2.bmp");
DisplayBitmap(lpddsback,rc.top,rc.bottom,rc.left,rc.right,"main.bmp");
lpddsprimary->Flip(NULL,DDFLIP_WAIT);

}

void DrawBlocks()
{

ClearScreen();


DisplayBitmap(lpddsback,bl.top,bl.bottom,bl.left,bl.right,"Ball.bmp");
DisplayBitmap(lpddsback,rc.top,rc.bottom,rc.left,rc.right,"main.bmp");

lpddsprimary->Flip(NULL,DDFLIP_WAIT);
}
void Intro()
{

bool selected1;
bool selected2;
bool selected3;
bool start;
start = false;
selected1 = true;
selected2 = false;
selected3 = false;
while(start == false)
{

DisplayBitmap(lpddsback,275,100,200,200,"Start.bmp");
DisplayBitmap(lpddsback,225,275,300,300,"Exit.bmp");
DisplayBitmap(lpddsback,275,350,350,350,"2Player.bmp");
if(selected1 == true)
{
DisplayBitmap(lpddsback,275,100,200,200,"Start2.bmp");

}

if(selected2 == true)
{
DisplayBitmap(lpddsback,275,275,275,275,"Exit2.bmp");

}
if(selected3 == true)
{
DisplayBitmap(lpddsback,275,275,350,350,"2Player2.bmp");

}

if(KEYDOWN(VK_DOWN))
{

if(selected1==true)
{
selected1=false;
ScaleBitmap(lpddsprimary,"clear.bmp");
selected2=true;

}

if(selected2=true)
{
selected2=false;
ScaleBitmap(lpddsprimary,"clear.bmp");
selected3=true;

}

}

if(KEYDOWN(VK_UP))
{
if(selected2==true)
{
selected2=false;
ScaleBitmap(lpddsprimary,"clear.bmp");
selected1=true;

}
if(selected3==true)
{
selected3=false;
ScaleBitmap(lpddsprimary,"clear.bmp");
selected2=true;
}

}

if(KEYDOWN(VK_SPACE))
{
if(selected1==true)
{
break;
}
if(selected2==true)
{
exit(0);
}
if(selected3==true)
{
twoplayer=true;
break;
}

}

lpddsprimary->Flip(NULL,DDFLIP_WAIT);
ClearScreen();
}
}

void TestWalls()
{

//first 2 sides
if(bl.top >780)
{
BallVecX=-BallVecX + rand()%2;
bl.top+=BallVecX;
}
if(bl.bottom < 20)
{
BallVecY=-BallVecY+ rand()%2;
bl.top+=BallVecY;
}
//other 2 sides
if(bl.top < 0)
{
BallVecX=-BallVecX+ rand()%2;
bl.top+=BallVecX;
}
if(bl.bottom > 670)
{
BallVecY=-BallVecY+ rand()%2;
BallVecY=rand()%5;
}
else

//penalize player if ball hits bottom of screen//
if(bl.bottom > 580)
{
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;

}

}


void TwoPlayerTestWalls()
{

//first 2 sides
if(bl.top >780)
{
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 > 670)
{
BallVecY=-BallVecY+ rand()%2;
BallVecY=rand()%5;
}
else

//penalize player if ball hits bottom of screen//
if(bl.bottom > 580)
{
BallVecY=-BallVecY;
bl.bottom+=BallVecY;
score-=100;
}
//penalize player2 if ball hits top of screen//
if(bl.bottom < 10)
{
BallVecY=-BallVecY;
bl.bottom+=BallVecY;
score2-=100;
}


}


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

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

if(KEYDOWN(VK_NUMPAD3))
{
if(rc2.top >= MAX_X2)
{
rc2.top=rc2.top + 0;
}
else
rc2.top=rc2.top + 20;
}

if(KEYDOWN(VK_NUMPAD1))
{
if(rc2.top <= MAX_X1)
{
rc2.top=rc2.top - 0;
}
else
rc2.top=rc2.top - 20;
}


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

}

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

}
}


void MoveBall()
{

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



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);
}


#include
#include
#include
#include
#include "GameFunctions.h"
#include "ddutil.h"

MSG msg;

void FlyBy()
{

if(fb.top <=900)
{
fb.top = fb.top + 1;
}
if(fb.top >= 900)
{
fb.top=-300;
}
////////////////////////
if(fb2.top <=900)
{
fb2.top = fb2.top + 2;
}
if(fb2.top >= 900)
{
fb2.top=-430;
}
////////////////////////////
if(fb3.top <=900)
{
fb3.top = fb3.top + 4;
}
if(fb3.top >= 900)
{
fb3.top=-200;
}
///////////////////////////////
if(fb4.top <=900)
{
fb4.top = fb4.top + 4;
}
if(fb4.top >= 900)
{
fb4.top=-350;
}
////////end//////
}

void TwoPlayerTestPaddle()
{

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



if((x >= fb.top && x <= fb.top + fb.left) &&
(y >= fb.bottom && y<= fb.bottom + fb.right))
{
BallVecY=-BallVecY;
bl.bottom+=BallVecY;

}

if((x >= fb2.top && x <= fb2.top + fb2.left) &&
(y >= fb2.bottom && y<= fb2.bottom + fb2.right))
{
BallVecY=-BallVecY;
bl.bottom+=BallVecY;

}

if((x >= fb3.top && x <= fb3.top + fb3.left) &&
(y >= fb3.bottom && y<= fb3.bottom + fb3.right))
{
BallVecY=-BallVecY;
bl.bottom+=BallVecY;

}

if((x >= fb4.top && x <= fb4.top + fb4.left) &&
(y >= fb4.bottom && y<= fb4.bottom + fb4.right))
{
BallVecY=-BallVecY;
bl.bottom+=BallVecY;

}

if((x >= rc.top && x <= rc.top + rc.left) &&
(y >= rc.bottom && y<= rc.bottom + rc.right))
{
BallVecY=-BallVecY;
bl.bottom+=BallVecY;
}

if((x >= rc2.top && x <= rc2.top + rc2.left) &&
(y >= rc2.bottom && y<= rc2.bottom + rc2.right))
{
BallVecY=-BallVecY;
bl.bottom+=BallVecY;

}



}

///////////////////////////////////////////////////////////////////////
int TwoPlayerMain()
{

if(KEYUP(VK_ESCAPE))
PostMessage(hwnd,WM_DESTROY,0,0);
MoveBall();
FlyBy();
TwoPlayerInput();
TwoPlayerTestWalls();
TwoPlayerTestPaddle();
TwoPlayerDrawBlocks();


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

sprintf(buffer2,"PONG Player2 Score:%d ",score2);
DrawText(buffer2,25,50,RGB(100,255,0),lpddsprimary,fnt);

return(0);
}


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 GameInit()
{
fb.top=-400,fb.bottom=200,fb.left=200,fb.right=20;
fb2.top=-430,fb2.bottom=350,fb2.left=250,fb2.right=24;
fb3.top=-200,fb3.bottom=450,fb3.left=350,fb3.right=18;
fb4.top=-350,fb4.bottom=530,fb4.left=175,fb4.right=26;
rc.top=50,rc.bottom=550,rc.left=200,rc.right=20;
bl.top=150,bl.bottom=250,bl.left=30,bl.right=20;
rc2.top=50,rc2.bottom=50,rc2.left=200,rc2.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);



lpdd->CreateClipper(0,&lpddclipper,NULL);
lpddsprimary->SetClipper(&lpddclipper);



ShowCursor(false);
ScaleBitmap(lpddsprimary,"Splash.bmp");
Sleep(2000);
ScaleBitmap(lpddsprimary,"clear.bmp");
Intro();
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);
}
if(twoplayer==true)
{
TwoPlayerMain();
}
else
GameMain();
}
GameShutdown();
return(msg.wParam);
}



Share this post


Link to post
Share on other sites
It likes that you are reloading the bitmaps every frame in your DisplayBitmap() function, just load it into a offscreen surface and blit it on the screen

Share this post


Link to post
Share on other sites