Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

loading and displaying bitmaps [noobie]

This topic is 5579 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

Some of you recently helped me get a window in DirectX to work. I followed a tutorial that I found on the net, and actually understand what is going on with this one. So now, I want to start loading and displaying bitmaps. My end goal is to create a tile map, just using 2 or 3 tile types(i.e. grass.bmp, water.bmp, and sand.bmp). I understand the idea behind tiles in that I need to create an array for the map, and index the tiles, and display them to the screen. What I need help with now, is where to put this in my code, and how exactly to go about displaying the bitmaps. I have "Tricks of the Windows Game Programming Gurus," but I get discouraged at times because I feel there isn't enough comments. So can someone help me with this? Assume that I have the 3 tiles I mentioned above, each being 32x32. Here is my code so far:
      #include< windows.h >	// Windows header file

#include< d3d8.h >		// Direct3D header file


const int WINDOW_WIDTH = 640;	// window width

const int WINDOW_HEIGHT = 480;	// window height

const int WINDOW_X = 0;
const int WINDOW_Y = 0;
const char *WINDOW_TITLE="Silent";
const char *WINDOW_CLASS_NAME="Silent Test";

HINSTANCE g_hInst;	// handle to the instance of program

HWND g_hWnd;		// handle to the window


bool g_bFullscreen;	// boolean to hold whether or not it's in full screen


IDirect3D8 *g_pDirect3D;	// pointer to Direct3D

IDirect3DDevice8 *g_pDevice;// pointer to the Direct3D device



HRESULT g_hr;	// variable to hold function call results


bool Direct3DInit()	// function to initialize Direct3D

{
	g_pDirect3D=Direct3DCreate8(D3D_SDK_VERSION);	// create Direct3D

	if (g_pDirect3D==NULL)	// check if it succeeded, if it didn't, return failure

		return false;

	D3DDISPLAYMODE displayMode;	// structure to hold display mode info


	if (g_bFullscreen==false)	// if it's not in fullscreen, fill structure with display mode

    {
		// get the current display mode on the default display adapter

		g_hr=g_pDirect3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &displayMode);
		if (FAILED(g_hr))	// if above call failed, return failure

			return false;
	}

	else
	{
		displayMode.Width=WINDOW_WIDTH;	// set the screen width to the window width

		displayMode.Height=WINDOW_HEIGHT;	// set the screen height to the window height

		displayMode.RefreshRate=0;		// set the refresh rate to default

		displayMode.Format=D3DFMT_R5G6B5;	// set the color format to 565 16 bit

	}

	D3DPRESENT_PARAMETERS presentParameters;	// used to explain to Direct3D how it will present things on the screen

	memset(&presentParameters, 0, sizeof(D3DPRESENT_PARAMETERS));	// need to fill it with 0's first


	if (g_bFullscreen==false)	// if user answered "no"

	{
		presentParameters.Windowed = TRUE;	// then set windowed to true

	}
	else	// otherwise

	{
		presentParameters.Windowed = FALSE;	// set windowed to false

	}

	// tells Direct3D to present the graphics to the screen the quickest way possible

	presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
	presentParameters.BackBufferFormat = displayMode.Format;	// the color format

	presentParameters.BackBufferWidth = displayMode.Width;		// the back buffer width

	presentParameters.BackBufferHeight = displayMode.Height;	// the back buffer height


	// create the device

	g_hr=g_pDirect3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd,
								D3DCREATE_SOFTWARE_VERTEXPROCESSING,
								&presentParameters, &g_pDevice );
	if (FAILED(g_hr)) // if failed, return failure

		return false;

	return true;
}

// call this function everytime to draw scene

void DrawScene()
{
	// clear the screen

	g_pDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

	// make it so we can draw stuff

	g_pDevice->BeginScene();



	// TEST AREA //




	// make it so we cant draw stuff

	g_pDevice->EndScene();

	// put the back buffer onto the screen

	g_pDevice->Present(NULL, NULL, NULL, NULL);
}

void Direct3DRelease()
{
	// release the device

	if (g_pDevice)
		g_pDevice->Release();
	g_pDevice=NULL;

	// release the Direct3D object

	if (g_pDirect3D)
		g_pDirect3D->Release();
	g_pDirect3D=NULL;
}

// windows message processing function

LRESULT CALLBACK WndProc(HWND wpHWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
		case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
		} break;

		default:break;
	}

	return DefWindowProc(wpHWnd, msg, wParam, lParam);
}

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX winClass;
	MSG msg;

	// set our global HINSTANCE to the one we get passed

	g_hInst=hInstance;

	// setup and register the window class

	winClass.cbSize			= sizeof(WNDCLASSEX);
	winClass.style			= CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
	winClass.lpfnWndProc	= WndProc;
	winClass.cbClsExtra		= 0;
	winClass.cbWndExtra		= 0;
	winClass.hInstance		= g_hInst;
	winClass.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	winClass.hCursor		= LoadCursor(NULL, IDC_ARROW);
	winClass.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
	winClass.lpszClassName	= WINDOW_CLASS_NAME;
	winClass.hIconSm		= LoadIcon(NULL, IDI_APPLICATION);
	winClass.lpszMenuName	= NULL;

	if (!RegisterClassEx(&winClass))
		return 0;

	// ask user wheter or not they want to run in fullscreen

	if (MessageBox(NULL, "Would you like to run in fullscreen?", "Fullscreen?", MB_YESNO)==IDYES)
		g_bFullscreen=true;
	else
		g_bFullscreen=false;

	if (g_bFullscreen==false)
	{
		// create a normal window with a border, a caption, and an X button

		g_hWnd = CreateWindowEx(WS_EX_CLIENTEDGE,
											WINDOW_CLASS_NAME,
											WINDOW_TITLE,
											WS_SYSMENU | WS_BORDER | WS_CAPTION | WS_VISIBLE,
											WINDOW_X, WINDOW_Y,
											WINDOW_WIDTH, WINDOW_HEIGHT,
											NULL,
											NULL,
											g_hInst,
											NULL);
	}
	else
	{
		// create a fullscreen window, one that doesn't have anything in it

		g_hWnd = CreateWindowEx(NULL,
								WINDOW_CLASS_NAME,
								WINDOW_TITLE,
								WS_POPUP | WS_VISIBLE,
								WINDOW_X, WINDOW_Y,
								WINDOW_WIDTH, WINDOW_HEIGHT,
								NULL,
								NULL,
								g_hInst,
								NULL);
	}

	// make sure the window was created properly, if it wasn't, quit

	if (!g_hWnd)
		return 0;

	// try to initialize the Direct3D parts of the program. if it fails, makes sure everything they might have created gets released

	if (Direct3DInit()==false)
	{
		Direct3DRelease();
		return 0;
	}

	while(1)
	{
		// windows message stuff

		while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);

			if (msg.message == WM_QUIT)
				break;
		}

		if (msg.message == WM_QUIT)
			break;

		// quit if the user presses escape

		if (GetAsyncKeyState(VK_ESCAPE))
			PostQuitMessage(0);

		// Draw the scene each fram

		DrawScene();
	}

	// release Direct3D

	Direct3DRelease();

	return 0;
}
      
My main concern at this point is just putting things on the screen. I've been able to create triangles on the screen, but I want to load a bitmap. Thanks for the help guys. [edited by - -silent- on February 10, 2003 5:49:54 PM]

Share this post


Link to post
Share on other sites
Advertisement
Well, the drawing part goes between the BeginScene/ EndScene. However, there are some tricky things which you need to take care of, such as transforming the vertices etc. I suggest you take a look at an article called "Dissecting sprites in Direct3d" (or close to that here at GameDev, it has helped me a lot in understanding these concepts

There are 10 kinds of people: those who know binary and those who don''t

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!