0

I'm doing a game using DirectX 9. My game has top-down view. I loaded two texture files(*.png image). One is the background and over that is the character. I've given up, down, left, right movements for my character.
The program runs fine, showing the char and its movement over the background texture.
I'm running the same program on my laptop(I was working on other system earlier) and the texture of character is not visible. The texture of background is visible, but not the character.
This is the code....

#include<windows.h>
#include<d3dx9.h>
#include<iostream>
using namespace std;
HWND hwnd;
IDirect3D9 *d3dptr;

D3DCAPS9 caps;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
void d3dInit();
void d3ddraw();
void d3d_cam();
void g_texture();
void vert_Update();
void d3d_show();

float charY=0;
float charX=0;
float x1= 375.0f;
float x2= 375.0f;


IDirect3DDevice9 *device;
HRESULT res;
IDirect3DIndexBuffer9 *ib;
IDirect3DVertexBuffer9 *bgVertexBuffer;
IDirect3DVertexBuffer9 *playerVertexBuffer;
IDirect3DTexture9 *bg=0;
//IDirect3DTexture9 *AI=0;
IDirect3DTexture9 *player=0;



struct vertex
{
	float x;
	float y;
	float z;
	float u;
	float v;

	vertex(float x1,float y1,float z1,float u1,float v1)
	{
		x=x1;
		y=y1;
		z=z1;
		u=u1;
		v=v1;
	}
};

bool InitWndApp (HINSTANCE hInstance, int show)
{
	WNDCLASS w;
	w.style = CS_HREDRAW | CS_VREDRAW;
	w.lpfnWndProc = WndProc;
	w.cbClsExtra = 0;
	w.cbWndExtra = 0;
	w.hInstance = hInstance;
	w.hIcon = LoadIcon(0,IDI_APPLICATION);
	w.hCursor = LoadCursor(0,IDC_ARROW);
	w.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	w.lpszMenuName = 0;
	w.lpszClassName =TEXT("My Class");

	if (!RegisterClass(&w))
		{
			MessageBox(0,TEXT("Registration Failed"),TEXT("My Msg"), MB_OK);
			return false;
		}
	hwnd = CreateWindow(TEXT("My Class"),TEXT("Window"), WS_OVERLAPPEDWINDOW, 0, 0, 800, 600, 0, 0, hInstance, 0);
	if (!hwnd)
		{
			MessageBox(0,TEXT("Registration Failed"),TEXT("Error CX18905"), MB_OK);
			return false;
		}

	ShowWindow(hwnd, show);
	UpdateWindow(hwnd);
	return true;
}

int MsgLoop()
{
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
while (GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg1, WPARAM wParam, LPARAM lParam)
{
	//int i=1;			Want to do something with move once if the key is pressed, instead of moving continuously..
	switch(msg1)
	{
		case WM_LBUTTONDOWN:
		PostQuitMessage(0);
		return 0;
		case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
		case WM_MOUSEMOVE:
		d3d_show();
		return 0;
		case WM_KEYDOWN:
		switch(wParam){
		case VK_RIGHT:
			charX+= 100.0f;
			vert_Update();
			device->SetTexture(0,0);
			d3d_show();
			//device->SetTexture(0,AI);			this will be dealt later..
			break;
		case VK_LEFT:
			charX-= 100.0f;
			vert_Update();
			device->SetTexture(0,0);
			d3d_show();
			//device->SetTexture(0,AI);
			break;
		case VK_UP:			
			charY+= 100.0f;
			vert_Update();
			device->SetTexture(0,0);
			d3d_show();
			//device->SetTexture(0,AI);		this will be dealt later..
			break;
		case VK_DOWN:
			charY-= 100.0f;
			vert_Update();
			device->SetTexture(0,0);
			d3d_show();
			//device->SetTexture(0,AI);		this will be dealt later..
			break;
		}
	}
	return DefWindowProc(hwnd ,msg1 ,wParam ,lParam);
}

void d3dInit(void)
{
	d3dptr= Direct3DCreate9((D3D_SDK_VERSION));
	d3dptr-> GetDeviceCaps(D3DADAPTER_DEFAULT,
	D3DDEVTYPE_HAL,
	&caps);
	int vp=0;
	if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
	vp= D3DCREATE_HARDWARE_VERTEXPROCESSING;
	else
	vp= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
	D3DPRESENT_PARAMETERS d3dpp;

	d3dpp.BackBufferWidth= 800;
	d3dpp.BackBufferHeight= 600;
	d3dpp.BackBufferFormat= D3DFMT_X8R8G8B8;

	d3dpp.BackBufferCount= 1;
	d3dpp.MultiSampleType= D3DMULTISAMPLE_NONE;
	d3dpp.MultiSampleQuality= 0;
	d3dpp.SwapEffect= D3DSWAPEFFECT_DISCARD;
	d3dpp.Windowed= true;
	d3dpp.hDeviceWindow= hwnd;
	d3dpp.EnableAutoDepthStencil= true;
	d3dpp.AutoDepthStencilFormat=D3DFMT_D24S8;
	d3dpp.Flags= 0;
	d3dpp.FullScreen_RefreshRateInHz= D3DPRESENT_RATE_DEFAULT;
	d3dpp.PresentationInterval= D3DPRESENT_INTERVAL_IMMEDIATE;

	res= d3dptr-> CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,vp,&d3dpp,&device);
	if(FAILED(res))
		{
			MessageBox(0,"FAILED",0,MB_OK);
		}
	d3ddraw();
	d3d_cam();
	d3d_show();
}

void d3ddraw()
{

	device-> CreateVertexBuffer(sizeof(vertex)*12,0,D3DFVF_XYZ,D3DPOOL_MANAGED,&bgVertexBuffer,0);// background vertex buffer
	vertex *vert;
	bgVertexBuffer-> Lock(0,0,(void**)&vert,0);

		vert[0] = vertex(400.0f, 400.0f, 0.0f, 1.0f, 0.0f);
		vert[1] = vertex(400.0f, -400.0f, 0.0f,  1.0f, 1.0f);
		vert[2] = vertex(-400.0f, -400.0f, 0.0f,  0.0f, 1.0f);
		vert[3] = vertex(-400.0f, 400.0f, 0.0f,  0.0f, 0.0f);
		
		vert[4] = vertex(-305.0f, -308.0f, -1.0f, 1.0f, 0.0f);
		vert[5] = vertex(-305.0f, -398.0f, -1.0f,  1.0f, 1.0f);
		vert[6] = vertex(-391.0f, -398.0f, -1.0f,  0.0f, 1.0f);
		vert[7] = vertex(-391.0f, -308.0f, -1.0f,  0.0f, 0.0f);

		/*vert[8] = vertex(x1, -250.0f, -1.0f, 1.0f, 0.0f);				//My would be AI.. There will be lots of it.
		vert[9] = vertex(x2, -340.0f, -1.0f,  1.0f, 1.0f);
		vert[10] = vertex(300.0f, -340.0f, -1.0f,  0.0f, 1.0f);
		vert[11] = vertex(300.0f, -250.0f, -1.0f,  0.0f, 0.0f);*/


	bgVertexBuffer-> Unlock();	
	WORD *ind;
	device->CreateIndexBuffer(sizeof(WORD)*18,0,D3DFMT_INDEX16,D3DPOOL_MANAGED,&ib,0);
	ib->Lock(0,0,(void**)&ind,0);
	//Rect Index for Background. 
	ind[0]=0;
	ind[1]=1;
	ind[2]=2;
	ind[3]=0;
	ind[4]=2;
	ind[5]=3;
	
	ind[6]=4;
	ind[7]=5;
	ind[8]=6;
	ind[9]=4;
	ind[10]=6;
	ind[11]=7;

	ind[12]=8;
	ind[13]=9;
	ind[14]=10;
	ind[15]=8;
	ind[16]=10;
	ind[17]=11;
	
	ib->Unlock();
}
void d3d_cam(){		
		D3DXVECTOR3 pos(0.0f,0.0f,-400.0f);
		D3DXVECTOR3 tgt(0.0f,0.0f,0.0f);
		D3DXVECTOR3 up(0.0f,1.0f,0.0f);
		D3DXMATRIX view;
		D3DXMatrixLookAtLH(&view,&pos,&tgt,&up);
		device->SetTransform(D3DTS_VIEW,&view);
		D3DXMATRIX proj;
		D3DXMatrixPerspectiveFovLH(&proj,D3DX_PI *0.5f,800/600,1.0f,1000.0f);
		device->SetStreamSource(0,bgVertexBuffer,0,sizeof(vertex));
		device->SetTransform(D3DTS_PROJECTION, &proj);
		device->SetFVF(D3DFVF_XYZ|D3DFVF_TEX1);							
		device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
		device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
		device->SetRenderState(D3DRS_LIGHTING,false);
		device->SetIndices(ib);
		//device->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);	//tutorial stuff..not to be used when dealing with textures
		g_texture();
		device->Clear(0,0,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0xffffffff,1.0f,0);
}

void vert_Update()
{
	vertex *vert;
	bgVertexBuffer-> Lock(0,0,(void**)&vert,0);
	float movx,movy,movz;
	for(int i=4;i<8;i++)
	{
		movx=vert[i].x;
		movy=vert[i].y;
		movz=vert[i].z;
		vert[i].x=movx+charX;
		vert[i].y=movy+charY;

		vert[i].z=movz;
	}
	charX=0;movz=0;charY=0;

	bgVertexBuffer-> Unlock();
}


void g_texture()
{
		D3DXCreateTextureFromFile(device,"abc.png",&bg);										//loading the textures..
		D3DXCreateTextureFromFile(device,"charac.png",&player);
		//D3DXCreateTextureFromFile(device,"initial.png",&AI);									//will be used acc. to the 2nd layer blocks

		//device->SetFVF(D3DFVF_XYZ|D3DFVF_TEX1);
		
		device->SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);
		device->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);
		device->SetSamplerState(0, D3DSAMP_MIPFILTER,D3DTEXF_LINEAR);
}

void npc()
{
			x1-=3.0f;
			x2-=3.0f;
			d3d_show();

}

void d3d_show(){
	
	device->Clear(0,0,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0x00000000,1.0f,0);
	device->BeginScene();
			
		device->SetTexture(0,bg);
		device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
		device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,4,0,2);
		device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
		//device->SetStreamSource(0,playerVertexBuffer,0,sizeof(vertex));
		//device->SetFVF(D3DFVF_XYZ|D3DFVF_TEX1);
		device->SetTexture(0,player);														
		device->SetTextureStageState(0,D3DTSS_ALPHAOP, D3DTOP_ADD);
		device->SetTextureStageState(0,D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
		device->SetTextureStageState(0,D3DTSS_ALPHAARG2, D3DTA_TEXTURE);
		device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
		device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,4,6,2);							//separate call for each rect
		device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);

		//device->SetTexture(0,AI);														
		//device->SetTextureStageState(0,D3DTSS_ALPHAOP, D3DTOP_ADD);
		//device->SetTextureStageState(0,D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
		//device->SetTextureStageState(0,D3DTSS_ALPHAARG2, D3DTA_TEXTURE);
		//device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
		//device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,4,12,2);							//separate call for each rect
		//device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);

	
	device->EndScene();
	device->Present(0,0,0,0);
}



int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR CmdLine, int CmdShow)
{
if(!InitWndApp(hInstance,CmdShow))
{
	MessageBox(0,TEXT("Failed"),0,0);
	return 0;
}
d3dInit();


return MsgLoop();
}

I doubt that there is a mistake in the code, but still...Someone please check the code and tell me where m wrong.
Thanks in advance.

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by sL@Y3R_WiLLRocK
0

Some one please help me.. Is the problem due to the compatibility issues of different systems? My code works on every other system. If its the compatibility issue, then someone help me in resolving it..
Thanks in advance.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.