4 Replies - 4803 Views - Last Post: 10 April 2013 - 09:15 AM Rate Topic: -----

#1 assiance  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 23-October 08

Cant get Bitmap to load using directX9

Post icon  Posted 02 January 2009 - 06:14 PM

This is my first time posting something so i hope im doing this right. Well im new to directx and im reading a book on it. For some reason the bitmap wont display when i run the program. It just show a black screen. I even took the code straight of the CD and copied to the program and it wont work. the book is about 2 to 3 years old i think so it might be outdated.

So any help on how to display the bitmap using directX9 will be appreciated

#include <d3d9.h>
#include <d3dx9.h>
#include <time.h>

//application title
#define APPTITLE "Load_Bitmap"

//screen resolution
#define SCREEN_WIDTH 1280
#define SCREEN_HEIGHT 1024

//macros to read the keyboard asynchronously
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code)((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

//forward declarations
LRESULT WINAPI WinProc(HWND, UINT, WPARAM, LPARAM);
ATOM MyRegisterClass(HINSTANCE);
int Game_Init(HWND);
void Game_Run(HWND);
void Game_End(HWND);

//Direct3D objects
LPDIRECT3D9 d3d = NULL;
LPDIRECT3DDEVICE9 d3ddev;

LPDIRECT3DSURFACE9 backbuffer = NULL;
LPDIRECT3DSURFACE9 surface = NULL;

//window event callback function
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
		case WM_DESTROY:
			Game_End(hWnd);
			PostQuitMessage(0);
			return 0;
	}
	return DefWindowProc(hWnd, msg, wParam, lParam);
}

//helper function to set up the window properties
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	//create the window class structure
	WNDCLASSEX wc;
	wc.cbSize = sizeof(WNDCLASSEX);

	//fill the struct with info
	wc.style			= CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc		= (WNDPROC)WinProc;
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hInstance		= hInstance;
	wc.hIcon			= NULL;
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground	= (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName		= NULL;
	wc.lpszClassName	= APPTITLE;
	wc.hIconSm			= NULL;

	//set up the window with the class info
	return RegisterClassEx(&wc);
}

//entry point for a windows program
int WINAPI WinMain(HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPSTR	 lpCmdline,
				   int		 nCmdShow)
{
	//declare variables
	MSG msg;

	//register the class
	MyRegisterClass(hInstance);

	//initialize application
	//note-- got rid of initinstance
	HWND hWnd;
	
	//create a new window
	hWnd = CreateWindow(
		APPTITLE,				//window class
		APPTITLE,				//title bar
		WS_EX_TOPMOST | WS_VISIBLE | WS_POPUP,	//window style
		CW_USEDEFAULT,			//x position of window
		CW_USEDEFAULT,			//y position of window
		SCREEN_WIDTH,					//width of window
		SCREEN_HEIGHT,					//height of window
		NULL,					//parent window
		NULL,					//menu
		hInstance,				//application instance
		NULL);					//window parameters

	//was there an error creating the window?
	if (!hWnd)
		return FALSE;

	//display the window
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	//initialize the game
	if (!Game_Init(hWnd))
		return 0;

	//main message loop
	int done = 0;
	while(!done)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			//look for quit message
			if(msg.message == WM_QUIT)
			{
				MessageBox(hWnd, "Recieved WM_QUIT message", "WinMain", MB_OK);
				done = 1;
			}

			//decode and pass messages on to wndproc
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	else
		//process game loop(else prevents running after window is closed)
		Game_Run(hWnd);
	}
	return msg.wParam;
}

int Game_Init(HWND hwnd)
{
	HRESULT result;

	//display init message
	MessageBox(hwnd, "Program is about to start", "Game_Init", MB_OK);

	//initialize Direct3D
	d3d = Direct3DCreate9(D3D_SDK_VERSION);
	if(d3d == NULL)
	{
		MessageBox(hwnd, "Error initializing Direct3D", "Error", MB_OK);
		return 0;
	}

	//set Direct 3D presentation parameters
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = FALSE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferCount = 1;
	d3dpp.BackBufferWidth = SCREEN_WIDTH;
	d3dpp.BackBufferHeight = SCREEN_HEIGHT;
	d3dpp.hDeviceWindow = hwnd;

	//create Direct3D device
	d3d->CreateDevice(
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_HAL,
		hwnd,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&d3dpp,
		&d3ddev);

	if(d3ddev == NULL)
	{
		MessageBox(hwnd, "Error creating Direct3D device", "Error", MB_OK);
		return 0;
	}

	//set random number seed
	srand(time(NULL));

	//clear the backbuffer to black
	d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

	//create pointer to the back buffer
	d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);

	//create surface
	result = d3ddev->CreateOffscreenPlainSurface(
		640,				//width of the surface
		480,				//height of the surface
		D3DFMT_X8R8G8B8,		//surface format
		D3DPOOL_DEFAULT,	//memory pool to use
		&surface,			//pointer to the surface
		NULL);				//reserved (always NULL)

	if(result != D3D_OK)
		return 1;

	//load surface from file
	result = D3DXLoadSurfaceFromFile(
		surface,			//destination surface
		NULL,				//destination palette
		NULL,				//destination rectangle
		"legotron.bmp",			//source filename
		NULL,				//source rectangle
		D3DX_DEFAULT,		//controls how image is filtered
		99,					//for transparency (0 for none)
		NULL);

	//make sure file loaded okay
	if (result != D3D_OK)
		return 1;

	//draw surface to the backbuffer
	d3ddev->StretchRect(surface, NULL, backbuffer, NULL, D3DTEXF_NONE);

	//return okay
	return 1;
}

void Game_Run(HWND hwnd)
{
	

	//make sure the Direct3D device is valid
	if(d3ddev == NULL)
		return;


	//start rendering
	if (d3ddev->BeginScene())
	{
		//create pointer to backbuffer
		d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);

		//draw surface to the backbuffer
		d3ddev->StretchRect(surface, NULL, backbuffer, NULL, D3DTEXF_NONE);
		

		//stop rendering
		d3ddev->EndScene();
	}

	//display the back buffer on the screen
	d3ddev->Present(NULL, NULL, NULL, NULL);

	//check for escape key(exit program)
	if (KEY_DOWN(VK_ESCAPE))
		PostMessage(hwnd, WM_DESTROY, 0, 0);
}

void Game_End(HWND hwnd)
{
	//display close message
	MessageBox(hwnd, "Program is about to end", "Game End", MB_OK);

	//free the surface
	surface->Release();

	//release the Direct3D device
	if (d3ddev != NULL)
		d3ddev->Release();

	//release the Direct3D object
	if (d3d != NULL)
		d3d->Release();
}



the bitmap is at the bottom

legotron.bmp

Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Cant get Bitmap to load using directX9

#2 assiance  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 23-October 08

Re: Cant get Bitmap to load using directX9

Posted 02 January 2009 - 06:19 PM

IM A BIG IDIOT

I FIGURED IT OUT THANX ANYWAYS
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Cant get Bitmap to load using directX9

Posted 02 January 2009 - 06:20 PM

Why not share your solution for others. Someday someone might have the same issue, then we have a solution available for them :)
Was This Post Helpful? 0
  • +
  • -

#4 ninthjarl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-January 09

Re: Cant get Bitmap to load using directX9

Posted 10 January 2009 - 01:18 AM

Please share the solution, i'm building a gui for my game and I'm using IDirect3DSurface facing similar problems cos I am used to direct draw.

This post has been edited by ninthjarl: 10 January 2009 - 08:28 AM

Was This Post Helpful? 0
  • +
  • -

#5 n_joi_2000  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 10-April 13

Re: Cant get Bitmap to load using directX9

Posted 10 April 2013 - 09:15 AM

I think the one who posted this original message didnt post the fix. I think the solution is to change the present parameters for
d3dpp.Windowed = TRUE;
instead of FALSE.
Compile that and it should work. replace the BMP with your BMP of course, becuase I dont think you can get the legotron.bmp anymore. maybe the link is dead.

I just hope this helps anyone out there that happens to come across this issue.

Thanks,
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1