Jump to content
  • Advertisement
Sign in to follow this  

I can't find the problem.

This topic is 5047 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 just got started on a small 2D project with DirectDraw. My problem has to due with just displaying my background image. I have displayed it before in different ways, but for some reason I can't get the following to display. I just get a black screen. Here's what I got so far. //system includes #include <windows.h> #include <windowsx.h> #include <ddraw.h> #include <stdio.h> //system defines #define WIN32_LEAN_AND_MEAN //custom includes #include "defines.h" //global definitions #include "bmp.h" //bitmap file reader //globals BOOL ActiveApp; //is this application active? LPDIRECTDRAW lpDirectDrawObject=NULL; //direct draw object LPDIRECTDRAWSURFACE lpPrimary=NULL; //primary surface LPDIRECTDRAWPALETTE lpPrimaryPalette; //its palette LPDIRECTDRAWSURFACE lpSecondary=NULL; //back buffer LPDIRECTDRAWPALETTE lpSecondaryPalette; //its palette LPDIRECTDRAWSURFACE lpBackground=NULL; //background image CBmpFileReader background; //background image //helper functions LPDIRECTDRAWPALETTE CreatePalette(LPDIRECTDRAWSURFACE surface); BOOL InitDirectDraw(HWND hwnd); HWND CreateDefaultWindow(char* name,HINSTANCE hInstance); BOOL LoadImages(){ //load graphics from files to surfaces //get the background image if(!background.load("bckgnd.bmp"))return FALSE; //read from file background.draw(lpBackground); //draw to background surface //set palettes in all surfaces if(!background.setpalette(lpPrimaryPalette))return FALSE; if(!background.setpalette(lpSecondaryPalette))return FALSE; return TRUE; } //LoadImages BOOL ComposeFrame(){ //compose a frame of animation lpSecondary->BltFast(0,0,lpBackground,NULL,DDBLTFAST_NOCOLORKEY|DDBLTFAST_WAIT); return TRUE; } //ComposeFrame BOOL RestoreSurfaces(){ //restore all surfaces BOOL result=TRUE; //primary and secondary surfaces if(FAILED(lpPrimary->Restore()))return FALSE; if(FAILED(lpSecondary->Restore()))return FALSE; //surfaces containing images if(SUCCEEDED(lpBackground->Restore())) //if background restored result=result&&background.draw(lpBackground); //redraw image else return FALSE; return result; } //RestoreSurfaces BOOL PageFlip(){ //return TRUE if page flip succeeds if(lpPrimary->Flip(NULL,DDFLIP_WAIT)==DDERR_SURFACELOST) return RestoreSurfaces(); return TRUE; } //PageFlip BOOL ProcessFrame(){ //process a frame of animation ComposeFrame(); //compose a frame in back surface return PageFlip(); //flip video memory surfaces } //ProcessFrame //message handler (window procedure) long CALLBACK WindowProc(HWND hwnd,UINT message, WPARAM wParam,LPARAM lParam){ switch(message){ case WM_ACTIVATEAPP: ActiveApp=wParam; break; case WM_CREATE: break; case WM_KEYDOWN: //keyboard hit { //get virtual key code and data bits int virtual_code = (int)wParam; int key_state = (int)lParam; //switch on the virtual_key code to be clean switch(virtual_code) { case VK_ESCAPE://if escape key is pressed { DestroyWindow(hwnd);//exit game break; }//end case default: break; }//end switch return(0); }break; case WM_LBUTTONDOWN: //left mouse button down //InputManager.LMouseDown(lParam); //handle it break; case WM_LBUTTONUP: //left mouse button up //if(InputManager.LMouseUp(lParam)) // DestroyWindow(hwnd); break; case WM_MOUSEMOVE: //mouse move //InputManager.MouseMove(lParam); break; case WM_DESTROY: //end of game //ObjectManager.reset(); //clear objects if(lpDirectDrawObject!=NULL){ //if DD object exists if(lpSecondary!=NULL) //if secondary surface exists lpSecondary->Release(); //release secondary surface if(lpPrimary!=NULL) //if primary surface exists lpPrimary->Release(); //release primary surface if(lpBackground!=NULL) //if background exists lpBackground->Release(); //release background lpDirectDrawObject->Release(); //release DD object } ShowCursor(TRUE); //show the mouse cursor PostQuitMessage(0); //and exit break; default: //default window procedure return DefWindowProc(hwnd,message,wParam,lParam); } //switch(message) return 0L; } //WindowProc int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow){ MSG msg; //current message HWND hwnd; //handle to fullscreen window hwnd=CreateDefaultWindow("DungeonQuest",hInstance); if(!hwnd)return FALSE; //set up window ShowWindow(hwnd,nCmdShow); UpdateWindow(hwnd); SetFocus(hwnd); //allow input from keyboard ShowCursor(FALSE); //hide the cursor //init graphics //for(int i=0; i<NUM_SPRITES; i++) //null out sprites // g_pSprite=NULL; BOOL OK=InitDirectDraw(hwnd);//initialize DirectDraw if(OK)OK=LoadImages(); //load images from disk if(!OK){ //bail out if initialization failed DestroyWindow(hwnd); return FALSE; } //message loop while(TRUE) if(PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)){ if(!GetMessage(&msg,NULL,0,0))return msg.wParam; TranslateMessage(&msg); DispatchMessage(&msg); } else if(ActiveApp)ProcessFrame(); else WaitMessage(); } //WinMain I know this is a bit much, but I've troubleshot my problem to the ComposeFrame function. Instead of Blitting to the Secondary surface, I have blitted to the primary, and it worked. I also used the background.draw(lpSecondary) function that I used in loading the image, and that worked. I am wondering what could possibly be the problem with my blitting to the secondary surface? I would appreciate any ideas, thanks.

Share this post

Link to post
Share on other sites

bool LoadSecondary()
{ // You could easily use this function to model a generic function that would load any bitmap you like
// into a directdraw surface.
// www.msdn.com has docs for LoadImage() which are good.

HBITMAP hbmp = (HBITMAP)LoadImage(GetModuleHandle(NULL), "yourbmphere.bmp", IMAGE_BITMAP, BLOCKS_BMP_WIDTH,

if (!hbmp)
return false;

GetObject(hbmp, sizeof(bm), &bm); // gets me a DC or something like that


ddsd.dwSize = sizeof(ddsd);
ddsd.dwWidth = bm.bmWidth;
ddsd.dwHeight = bm.bmHeight;
if (g_App.GetDirectDrawObject()->CreateSurface(&ddsd, &m_BuildingBlocksBMP, NULL) != DD_OK)
return false;
DDCopyBitmap(m_BuildingBlocksBMP, hbmp, 0, 0, 0, 0);

return true;


Make sure you clear the ddsd you use AND make absolutely sure you set ddsd.dwSize of, as this can cause a LOT of weird errors.

Share this post

Link to post
Share on other sites
I never found out what the problem was, but I fixed it by using the Blt function instead of BltFast. I don't know. Crazy.
Thanks for the reply, I'm using something similar right now to load my bitmap files.

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!