Sign in to follow this  
technoknight

Can't load the bitmap.

Recommended Posts

Have a look at my code. Create a bmp file with name Sample.bmp and put it in ur project directory. I cannot make the program to load my bitmap. Anyhelp suggested???
#include "stdafx.h"
#include "example1.h"
#define INITGUID
#include <ddraw.h>

//Image loading and intialization functions//

BOOL bInit=FALSE;

//Global Interface Pointers//

LPDIRECTDRAW7	lpDD=NULL;  //Directdraw object
LPDIRECTDRAWSURFACE7	lpDDSPrimary=NULL;  //Directdraw primary surface
LPDIRECTDRAWCLIPPER	lpClip=NULL;  //Directdraw Clipper
LPDIRECTDRAWSURFACE7	lpBmp=NULL;  //Bitmap Surface

//Windows Class Information//
static char szClass[]="Example1";
static char szCaption[]="First Attempt";

//Error returning String//

const char *ErrStr=NULL;

//Error Messages//

const char Err_Reg_Class[]	="Error Registering Windows Class";
const char Err_Create_Win[]	="Error Creating Window";
const char Err_DirectDrawCreate[]	="DirectDrawCreate Error";
const char Err_Query[]	="QueryInterface Error";
const char Err_Coop[]	="SetCooperativeLevel Failed";
const char Err_CreateClip[]	="CreateClip Failed";
const char Err_CreateSurf[]		="CreateSurf Failed";
const char Err_LoadBMP[]	="Error Loading Image";

//Cleanup Functions to releasr objects

#define SafeRelease(x) if(x){x->Release();x=NULL;}

void Cleanup(void)
{

	//release the interfaces

	SafeRelease(lpBmp);
	SafeRelease(lpDDSPrimary);
	SafeRelease(lpClip);
	SafeRelease(lpDD);

	//dispaly error if one thrown

	if(ErrStr)
		MessageBox(NULL,ErrStr,szCaption,MB_OK);
}



void DrawImage()
{
	//return if not ready to draw at this time
	if(!lpBmp||!bInit)
		return;

		//draw the image

		lpDDSPrimary->Blt(NULL,lpBmp,NULL,DDBLT_WAIT,NULL);
	
}




LRESULT CALLBACK WindowProc(HWND hWnd,unsigned uMsg,WPARAM wParam,LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_PAINT:
			PAINTSTRUCT ps;
			BeginPaint(hWnd,&ps);
			DrawImage();
			EndPaint(hWnd,&ps);
			break;

	case WM_DESTROY:
		Cleanup();
		PostQuitMessage(0);
		break;

	default:
		return DefWindowProc(hWnd,uMsg,wParam,lParam);
	}
	return 0L;
}






LPDIRECTDRAWSURFACE7 bitmap_surface(LPCTSTR file_name)
{
	HDC hdc;
	HBITMAP bit;
	LPDIRECTDRAWSURFACE7 surf;

	//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 the 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 bitmap and return failure to the 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,0,0,surf_width,surf_height,bit_dc,0,0,SRCCOPY);

		//release the DCs

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

	//clear bitmap


	DeleteObject(bit);

	//return pointer to caller

	return surf;
}


static  BOOL Init(HINSTANCE hInstance, int nCmdShow)
{
	WNDCLASS	wc;
	HRESULT	hRet;
	DDSURFACEDESC2	ddsd;

	//Setup and register window class

	wc.style=CS_HREDRAW|CS_VREDRAW;
	wc.lpfnWndProc=(WNDPROC) WindowProc;
	wc.cbClsExtra=0;
	wc.cbWndExtra=sizeof(DWORD);
	wc.hInstance=hInstance;
	wc.hIcon=NULL;
	wc.hCursor=LoadCursor(NULL,IDC_ARROW);
	wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
	wc.lpszMenuName=NULL;
	wc.lpszClassName=szClass;
	if(!RegisterClass(&wc))
	{
		ErrStr=Err_Reg_Class;
		return FALSE;
	}


	//Get Dimensions of Display
	int ScreenWidth=GetSystemMetrics(SM_CXSCREEN);
	int ScreenHeight=GetSystemMetrics(SM_CYSCREEN);

	//Create a Window and Display

	HWND hWnd;


	hWnd=CreateWindow(szClass,szCaption,WS_VISIBLE|WS_POPUP,0,0,ScreenWidth,ScreenHeight,NULL,NULL,hInstance,NULL);
	if(!hWnd)
	{
		ErrStr=Err_Create_Win;
		return FALSE;
	}

	ShowWindow(hWnd,nCmdShow);
	UpdateWindow(hWnd);

	

	//Create the main directdraw object

	hRet=DirectDrawCreateEx(NULL, (LPVOID*)&lpDD, IID_IDirectDraw7, NULL);
	if(hRet!=DD_OK){
		ErrStr=Err_DirectDrawCreate;
		return FALSE;
	}


	//Set our Cooperative level

	hRet=lpDD->SetCooperativeLevel(hWnd,DDSCL_NORMAL);    //SAVED
	if(hRet!=DD_OK)
	{
		ErrStr=Err_Coop;
		return FALSE;
	}

	//Create Clippe

	hRet=lpDD->CreateClipper(NULL,&lpClip,NULL);
	if(hRet!=DD_OK)
	{
		ErrStr=Err_CreateClip;
		return FALSE;
	}
	lpClip->SetHWnd(0,hWnd);


	//Create the Primary Surface

	ZeroMemory(&ddsd,sizeof(ddsd));
	ddsd.dwSize=sizeof(ddsd);
	ddsd.dwFlags=DDSD_CAPS;
	ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE;
	hRet=lpDD->CreateSurface(&ddsd,&lpDDSPrimary,NULL);
	if(hRet!=DD_OK){
		ErrStr=Err_CreateSurf;
		return FALSE;
	}

	//Set the clipper for the Primary surface

	lpDDSPrimary->SetClipper(lpClip);

	//Flag Initialization as completed

	bInit=TRUE;


	//load the bitmap image

	lpBmp=bitmap_surface("Sample.bmp");

	//display the Image

	DrawImage();

	return TRUE;
}

//Message Loop







int PASCAL WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
	MSG msg;   //Windows Messaging structure

	//Initialize the application, exit on failure
	if(!Init(hInstance,nCmdShow)){
		Cleanup();
		return FALSE;
	}

	//handle the messagte loop till we exit

	while (GetMessage(&msg,NULL,NULL,NULL)){
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	//exit returning final message

	return(msg.wParam);
}

The bitmap_surface() seems to have problem at
if(ddrval!=DD_OK){
		//no, release bitmap and return failure to the caller

		DeleteObject(bit);
		return NULL;


This code terminates the loading for the bmp file. ANY HELP??? Edited by Coder: Added source tags [Edited by - Coder on December 4, 2004 5:59:02 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by technoknight
This code terminates the loading for the bmp file. ANY HELP???


Where, _exactly_, does the problem occur? It might help if the program reported errors (e.g. using MessageBox) instead of trying to ignore them.

Share this post


Link to post
Share on other sites
So..let me get this straight.

You are trying to load a BMP into a Off Screen surface, and then blt that, but it crashes as soon as you attempt to load the bmp right?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this