Skip to main content
GameDev.net gamedev.net
🔒 Locked

Extracting frames from a BMP

Started by drdarkon Jul 31, 2002 at 5:00 PM 0 replies 250+ views
Original Post
drdarkon
drdarkon
Hey everyone, I am working on a 2d game engine in DirectX. Everything that moves and animates in my game is based off of objects. The part I am having trouble with at this point is the loading of individual frames. I am using a function that has no problem loading in the file. My problem is that I am having a hard time extracting the frame that I want. The function will open the BMP to load from, create a surface, and copy the frame in. The problem is that it creates a surface the size of the BMP and copies the frame to the location it was in the BMP. When I tried to set the surface width/height to the obj->width/height, the frame didn''t even display on the screen...What is the best way to do this/ What am I doing wrong? Thanks a million
  

//-----------------------------------------------------------------//

//-----------------------------------------------------------------//

//------ bitmap_surface()-function loads a BMP into a DD surface --//

//-----------------------------------------------------------------//

//-----------------------------------------------------------------//


//this function loads a frame from a bitmap into a LPDIRECTDRAWSURFACE4


LPDIRECTDRAWSURFACE4 bitmap_surface(OBJECT_PTR obj, LPCTSTR file_name, LPDIRECTDRAW4 lpDD,
									LPDIRECTDRAWSURFACE4 lpDDSPrimary, 
									int cx, int cy)//the cell coordinates (based off of the obj->width/height)

									
{
	HDC hdc;
	HBITMAP bit;
	LPDIRECTDRAWSURFACE4 surf;

	ShowCursor(FALSE); // Hide the cursor



	// load the interface bitmap


	bit=(HBITMAP) LoadImage(NULL,file_name,IMAGE_BITMAP,0,0,
								LR_DEFAULTSIZE|LR_LOADFROMFILE);
	if (!bit) 

		// failed to load, return failure to caller


		return NULL;

	// get bitmap dimensions


	BITMAP bitmap;
    GetObject( bit, sizeof(BITMAP), &bitmap );
	int surf_width=bitmap.bmWidth;
	int surf_height=bitmap.bmHeight;

	// create surface


	HRESULT ddrval;
	DDSURFACEDESC2 ddsd;
	ZeroMemory(&ddsd,sizeof(ddsd));
	ddsd.dwSize = sizeof(DDSURFACEDESC2);
	ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT ;
	ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN|DDSCAPS_SYSTEMMEMORY; 
	ddsd.dwWidth = surf_width;
	ddsd.dwHeight = surf_height; 

	// attempt to create surface


	ddrval=lpDD->CreateSurface(&ddsd,&surf,NULL);

	// created ok?


	if (ddrval!=DD_OK) {

		// no, release the bitmap and return failure to caller


		DeleteObject(bit);
		return NULL;

	} else {

		// yes, get a DC for the surface


		surf->GetDC(&hdc);

		// generate a compatible DC


		HDC bit_dc=CreateCompatibleDC(hdc);

		// blit the interface to the surface


		SelectObject(bit_dc,bit);
		BitBlt(hdc,cx*(obj->width+1)+1,
			cy*(obj->height+1)+1,
			cx*(obj->width+1)+1+obj->width,
			cy*(obj->height+1)+1+obj->width,
			bit_dc,0,0,SRCCOPY);

		// release the DCs


		surf->ReleaseDC(hdc);
		DeleteDC(bit_dc);
	}

	// clear bitmap 


	DeleteObject(bit);

	//set color key as pure green


	DDPIXELFORMAT ddpf;
	ddpf.dwSize=sizeof(ddpf);
	lpDDSPrimary->GetPixelFormat(&ddpf);
	DWORD KeyColor = ddpf.dwGBitMask;
	DDCOLORKEY key;
	key.dwColorSpaceLowValue= KeyColor;
	key.dwColorSpaceHighValue = KeyColor;

    // now set the color key pure green for source blitting

    surf->SetColorKey(DDCKEY_SRCBLT, &key);
    
	// return pointer to caller


	return surf;
}

  
- Jesse
Thanks
Carrot
Carrot
Instead of loading the bitmap and trying to extract each frame from the HBITMAP object, why not just load the whole bimap into the same size surface (which kinda looks like you are doing anyway), and then at blitting time just pull the correct frame out of the surface using a RECT type structure?


Topic Locked

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

Sign in to reply to this topic.