Sign in to follow this  

Can anyone see what is wrong with the following code for my tile system

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

#include "windows.h"
// include directx9
#include <d3d9.h>
#include <d3dx9tex.h>
#include <dxerr9.h>

// Constants.
const COLUMNSINTILEFILE = 2;
const TILESIZE = 32;
const MAPCOLUMNCOUNT = 6;
const MAPROWCOUNT = 6;
const XOFFSET = 220;
const YOFFSET = 120;

HINSTANCE hInst; //Global Handle to hold the application instance
HWND wndHandle;  //Global variable to hold the window handle

LPDIRECT3D9             pD3D;				// the Direct3D Object
LPDIRECT3DDEVICE9       pd3dDevice;			// the Direct3D Device

//forward declarations
bool initWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM);

// DirectX functions
bool initDirect3D();	
void render(void);
void CleanUpDirect3D();

void PaintBackground();
int ColumnRow2Sector(int c, int r, int numCols);
HRESULT PlaceTile(IDirect3DSurface9* pBackBuffer,
				  IDirect3DSurface9* pTileSurface, int TileNumber,
				  int DstCol, int DstRow, int numTileCols, int numMapCols,
				  int tileSize, int xOffset, int yOffset);
int TileNumber2SourceX(int TileNumber, int numCols, int tileSize);
int TileNumber2SourceY(int TileNumber, int numCols, int tileSize);
int Column2X(int col, int tileSize, int numCols);
int Row2Y(int r, int tileSize);

// Global variables.
//HWND g_hWnd;
IDirect3D9* g_pDirect3D = NULL;
IDirect3DDevice9* g_pDirect3DDevice = NULL;
IDirect3DSurface9* g_pBitmapSurface = NULL;
HRESULT g_hResult = D3D_OK;
char g_szErrorMsg[256];
IDirect3DSurface9* g_pBackBuffer = NULL;
char g_Sectors[] =
{0,0,0,0,0,0,
0,2,2,2,2,0,
0,2,1,1,2,0,
0,2,1,1,2,0,
0,2,0,2,2,0,
0,0,0,0,0,3};

/*********************************************************************
* WinMain the main entry point for windows applications
*********************************************************************/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	//initialize the window
	if (!initWindow(hInstance))
	{
		MessageBox(NULL, "Unable to create window", "ERROR", MB_OK);
		return false;
	}

	if (!initDirect3D())
	{
		MessageBox(NULL, "Unable to init Direct3D", "ERROR", MB_OK);
		return false;
	}

	// Main message loop:
	MSG msg; 
	ZeroMemory( &msg, sizeof(msg) );
	while( msg.message!=WM_QUIT )
	{
		if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
		{
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}		
		else
		{
			render();
		} 
	}
	// release the device and the direct3D object
	if( pd3dDevice != NULL) 
		pd3dDevice->Release();

	if( pD3D != NULL)
		pD3D->Release();
	//CleanUpDirect3D();
	return(int)msg.wParam;
}

bool initWindow(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	//fill in the WNDCLASS structure. this describes how the window will look to the system
	wcex.cbSize = sizeof(WNDCLASSEX); // the size of the structure
	wcex.style = CS_HREDRAW | CS_VREDRAW; // the class style
	wcex.lpfnWndProc = (WNDPROC) WndProc; // The window procedure callback
	wcex.cbClsExtra = 0; //extra bytes to allocate for this class
	wcex.cbWndExtra = 0; //extra bytes to allocate for this instance
	wcex.hInstance = hInstance; //Handle the application to the instance
	wcex.hIcon = 0; //icon to associate with the application
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW); // the default cursor
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // the background color
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = "PacMan2D"; //The Class name being created
	wcex.hIconSm = 0; //the handle to the small icon
	RegisterClassEx(&wcex);

	//Create the window
	wndHandle = CreateWindow("PacMan2D",		//The window class to use
		"PacMan2D",		//The TitleBar text
		WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE,	//The Window style
		CW_USEDEFAULT,	//The starting xCoordinate
		CW_USEDEFAULT,	//The starting yCoordinate
		640,			//The pixel width of the window
		480,			//The pixel height of the window
		NULL,			//The parent window. Null for desktop
		NULL,			//The menu from the pop up application
		hInstance,		//The handle to the application instance
		NULL);			//No values passed to the window

	//make sure that the window handle that is created is valid
	if(!wndHandle)
		return false;

	//Display the window on the screen
	ShowWindow(wndHandle, SW_SHOW);
	UpdateWindow(wndHandle);
	return true;
}

/*********************************************************************
* WndProc
*********************************************************************/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{	
	//Check any available message from the queue
	switch (message) 
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	
	case WM_PAINT:
		ValidateRect(hWnd, NULL);
		return 0;

	case WM_KEYDOWN:
		if( wParam == VK_ESCAPE ) 
		{
			PostQuitMessage(0);
			break;

		}	
	}
	//Always return the message to the default window
	return DefWindowProc(hWnd, message, wParam, lParam);
}

/*********************************************************************
* initDirect3D
* initializes direct3D
*********************************************************************/
bool initDirect3D()
{
	pD3D = NULL;
	pd3dDevice = NULL;

	// create the directX object
	if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
	{
		return false;
	}

	// fill the presentation parameters structure
	D3DPRESENT_PARAMETERS d3dpp; 
	ZeroMemory( &d3dpp, sizeof(d3dpp) );
	d3dpp.Windowed = FALSE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferCount  = 1;
	d3dpp.BackBufferHeight = 480;
	d3dpp.BackBufferWidth  = 640;
	d3dpp.hDeviceWindow    = wndHandle;

	// create a default directx device
	if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, wndHandle,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&d3dpp, &pd3dDevice ) ) )
	{
		return false;
	}

	g_hResult = g_pDirect3DDevice->CreateOffscreenPlainSurface(64, 64,
		D3DFMT_X8R8G8B8,D3DPOOL_SYSTEMMEM,&g_pBitmapSurface,NULL);
	if (FAILED(g_hResult))
	{
		strcpy(g_szErrorMsg, "Error creating bitmap surface.");
		PostQuitMessage(WM_QUIT);
	}
	g_hResult = D3DXLoadSurfaceFromFile(g_pBitmapSurface, NULL, NULL,
		"image.bmp", NULL, D3DX_DEFAULT, 0, NULL);
	if (FAILED(g_hResult))
	{
		strcpy(g_szErrorMsg, "Couldn’t load bitmap file.");
		PostQuitMessage(WM_QUIT);
	}

	g_hResult = g_pDirect3DDevice->GetBackBuffer(0,0,
		D3DBACKBUFFER_TYPE_MONO, &g_pBackBuffer);
	if (FAILED(g_hResult))
	{
		strcpy(g_szErrorMsg, "Couldn’t get back buffer.");
		PostQuitMessage(WM_QUIT);
	}
	return true;
}
//////////////////////////////////////////////////////
// CleanUpDirect3D()
//////////////////////////////////////////////////////
void CleanUpDirect3D()
{
	if (g_pBackBuffer)
		g_pBackBuffer->Release();
	if (g_pBitmapSurface)
		g_pBitmapSurface->Release();
	if (g_pDirect3DDevice)
		g_pDirect3DDevice->Release();
	if (g_pDirect3D)
		g_pDirect3D->Release();
}
/*********************************************************************
* render
*********************************************************************/
void render(void)
{
	// check to make sure we have a valid Direct3D Device
	if( NULL == pd3dDevice )
		return;

	// Clear the backbuffer to a blue color
	pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
	PaintBackground();
	// Present the backbuffer contents to the display
	pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

//////////////////////////////////////////////////////
// PaintBackground()
//////////////////////////////////////////////////////
void PaintBackground()
{
	g_pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
		D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
	for (int Row = 0; Row < MAPROWCOUNT; ++Row)
	{
		for (int Col = 0; Col < MAPCOLUMNCOUNT; ++Col)
		{
			int iSector = ColumnRow2Sector(Col, Row, MAPCOLUMNCOUNT);
			int TileNumber = g_Sectors[iSector];
			g_hResult = PlaceTile(g_pBackBuffer, g_pBitmapSurface,
				TileNumber, Col, Row, COLUMNSINTILEFILE, MAPCOLUMNCOUNT,
				TILESIZE, XOFFSET, YOFFSET);
			if (FAILED(g_hResult))
			{
				strcpy(g_szErrorMsg, "Error drawing tiles.");
				PostQuitMessage(WM_QUIT);
			}
		}
	}
}

//////////////////////////////////////////////////////
// PlaceTile()
//////////////////////////////////////////////////////
HRESULT PlaceTile(IDirect3DSurface9* pBackBuffer,
				  IDirect3DSurface9* pTileSurface, int TileNumber,
				  int DstCol, int DstRow, int numTileCols, int numMapCols,
				  int tileSize, int xOffset, int yOffset)
{
	RECT SrcTileRect;
	POINT DstPoint;
	SrcTileRect.left = TileNumber2SourceX(TileNumber,
		numTileCols, tileSize);
	SrcTileRect.right = SrcTileRect.left + tileSize;
	SrcTileRect.top = TileNumber2SourceY(TileNumber,
		numTileCols, tileSize);
	SrcTileRect.bottom = SrcTileRect.top + tileSize;
	DstPoint.x = Column2X(DstCol, tileSize, numMapCols) + xOffset;
	DstPoint.y = Row2Y(DstRow, tileSize) + yOffset;
	HRESULT hResult = g_pDirect3DDevice->UpdateSurface(g_pBitmapSurface, &SrcTileRect, pBackBuffer, &DstPoint);
	return hResult;
}

//////////////////////////////////////////////////////
// ColumnRow2Sector()
//////////////////////////////////////////////////////
int ColumnRow2Sector(int c, int r, int numCols)
{
	return r * numCols + c;
}

//////////////////////////////////////////////////////
// TileNumber2SourceX()
//////////////////////////////////////////////////////
int TileNumber2SourceX(int TileNumber, int numCols, int tileSize)
{
	return (TileNumber - ((int)(TileNumber / numCols))
		* numCols) * tileSize;
}

//////////////////////////////////////////////////////
// TileNumber2SourceY()
//////////////////////////////////////////////////////
int TileNumber2SourceY(int TileNumber, int numCols, int tileSize)
{
	return ((int)(TileNumber / numCols)) * tileSize;
}

//////////////////////////////////////////////////////
// Column2X()
//////////////////////////////////////////////////////
int Column2X(int col, int tileSize, int numCols)
{
	return (col - ((int)((col / numCols)) *
		numCols)) * tileSize;
}

//////////////////////////////////////////////////////
// Row2Y()
//////////////////////////////////////////////////////
int Row2Y(int r, int tileSize)
{
	return r * tileSize;
}

Share this post


Link to post
Share on other sites
I was very tempted to post "No", but I resisted.

What's wrong with it? Any debug output? Does it compile? Does it link? Any crashes? Are you using the debug DX runtimes? What SDK version? What compiler? Does it seem to work and you're asking is if we see any problems with it?

Share this post


Link to post
Share on other sites
thank you for encapsulating the code, i am using visual 6, and it builds fine no errors what so ever, when i try to lrun the program i just get a black screen that hangs there, not the tile images that i want?

Share this post


Link to post
Share on other sites

This topic is 4205 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.

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