Skip to main content
gamedev.net
🔒 Locked

DirectDraw Problem

Started by Ephimus 2 replies 700+ views
Original Post
Ephimus
Ephimus
Hello. I am trying to learn to use direct draw. I am having a problem getting a bitmap to load. Here's the relevant code.

//Globals
LPDIRECTDRAW lpdd;
LPDIRECTDRAWSURFACE lpddsprimary;
LPDIRECTDRAWSURFACE lpddsback;
LPDIRECTDRAWSURFACE lpddsbitmap;
LPDIRECTDRAWPALETTE lpddpal;
LPDIRECTDRAWCLIPPER lpddclipper;
PALETTEENTRY color_palette[256];
DDSURFACEDESC       ddsd;
DDSCAPS             ddscaps;
HRESULT             ddrval;
Direct Draw initialization

if (DirectDrawCreate(NULL, &lpdd, NULL) !=DD_OK)
        return(0);
    
    if((lpdd->SetCooperativeLevel(main_window_handle,DDSCL_ALLOWMODEX | DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT))!=DD_OK)
        return(0);
    
    if((lpdd->SetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP))!=DD_OK)
        return(0);
    
    memset(&ddsd,0,sizeof(ddsd));
    ddsd.dwSize         = sizeof(ddsd);
    ddsd.dwFlags        = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
    ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
    ddsd.dwBackBufferCount = 1;
    
    if (lpdd->CreateSurface(&ddsd,&lpddsprimary,NULL)!=DD_OK)
        return(0);
     
    ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
    
    lpddsprimary->GetAttachedSurface(&ddscaps,&lpddsback);
    
    memset(&ddsd,0,sizeof(ddsd));
    ddsd.dwSize         = sizeof(ddsd);
    ddsd.dwFlags        = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
    ddsd.dwWidth        = 34;
    ddsd.dwHeight       = 34;
    ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
    
    if (lpdd->CreateSurface(&ddsd,&lpddsbitmap,NULL)!=DD_OK)
        return(0);
     
    if ( ( lpddsbitmap = DDLoadBitmap( lpdd, "test.bmp", 0, 0) ) == NULL )
    return FALSE;   

Main loop


RECT source_rect,dest_rect;
    
    source_rect.left    = 0;
    source_rect.top     = 0;
    source_rect.right   = 34;
    source_rect.bottom  = 34;
    
    dest_rect.left      = 50;
    dest_rect.top       = 50;
    dest_rect.right     = 84; 
    dest_rect.bottom    = 84;
    
    
  
    if (KEY_DOWN(VK_ESCAPE) || KEY_DOWN(VK_SPACE))
    PostMessage(main_window_handle, WM_DESTROY,0,0);
    
    memset(&ddsd,0,sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    
    lpddsback->Lock(NULL,&ddsd,DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL);
    
    if ( FAILED( lpddsback->BltFast( 0, 0, lpddsbitmap, &source_rect, DDBLTFAST_WAIT ) ) )
    return FALSE;
    
    lpddsback->Unlock(ddsd.lpSurface);
    
    while(lpddsprimary->Flip(NULL,DDFLIP_WAIT)!=DD_OK);

The code compiles fine. I just get nothing on the screen intead of the bitmap. I am using the ddutil.cpp functions from MS for my bitmap loading. Any help would be appreciated. Thanks,
Dave Hunt
Dave Hunt
The backbuffer must not be locked during the Blt/BltFast.
Ephimus
Ephimus
Dave,

That did the trick. I was thinking that you had to lock a surface before you wrote anything to it. Guess not.

Thanks again for the assistance. I spent hours trying to figure this out.

Ephimus
Dave Hunt
Dave Hunt
Glad I could help.

You do have to lock the buffer when YOU want to do anything to it (i.e. modify the bits directly). But you can't lock the buffer when you want DirectDraw to do something to it (i.e. Blt(), BltFast(), etc.).

Good luck with the rest of your project.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.