Sign in to follow this  

Hi ! help me with 2d tile project!

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

code is below; but when it debug also a blue window. no tile was draw;
#include <d3d9.h>
#include <d3dx9.h>
LPDIRECT3D9             g_pD3D       = NULL; // 用来创建D3D设备
LPDIRECT3DDEVICE9       g_pd3dDevice = NULL; // D3D设备
ID3DXSprite *pSprite = NULL;
LPDIRECT3DTEXTURE9 g_pTexture = NULL; 
RECT SrcRect;

// 初始化D3D
HRESULT InitD3D( HWND hWnd )
{   

	// 创建D3D对象
	if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
		return E_FAIL;

	// 获取当前的显示模式
	D3DDISPLAYMODE d3ddm;
	if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
		return E_FAIL;

	D3DPRESENT_PARAMETERS d3dpp; 
	ZeroMemory( &d3dpp, sizeof(d3dpp) );
	d3dpp.Windowed = TRUE;				// 窗口模式
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;	// 设置交换模式
	d3dpp.BackBufferFormat = d3ddm.Format;		// 设置背景缓冲区格式为当前左面格式

	// 创建D3D设备
	// 第一个参数:使用默认的显卡适配器
	// 第二个参数:请求使用硬件抽象层(HAL)
	// 第三个参数:窗口句柄
	// 第四个参数:使用软件处理顶点
	// 第五个参数:创建的参数
	// 第六个参数:创建的D3D设备指针
	if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, 
					  D3DDEVTYPE_HAL, 
					  hWnd,
					  D3DCREATE_SOFTWARE_VERTEXPROCESSING,
					  &d3dpp, 
					  &g_pd3dDevice ) ) )
	{
		return E_FAIL;
	}
    if(FAILED(D3DXCreateSprite(g_pd3dDevice, &pSprite)))
	{
	   return E_FAIL;
	}
	D3DXCreateTextureFromFile( g_pd3dDevice, "haha.bmp", &g_pTexture );

    return S_OK;
}
// 释放D3D
VOID Cleanup()
{
	if( g_pd3dDevice != NULL) 
		g_pd3dDevice->Release();

	if( g_pD3D != NULL)
		g_pD3D->Release();
}

// 渲染
VOID Render()
{
	if( NULL == g_pd3dDevice )
		return;

	// 清除背景为兰色
	g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );

	// 开始绘制场景
	g_pd3dDevice->BeginScene();

	// 这里绘制你需要的东西
    SrcRect.left =6; // left coordinate of tile
    SrcRect.top =6; // top coordinate of tile
    SrcRect.right = SrcRect.left + 32; // add tile width
    SrcRect.bottom = SrcRect.top + 32;
	g_pd3dDevice->SetTexture( 0, g_pTexture );
    pSprite->Draw(g_pTexture,&SrcRect,NULL,NULL,0X99999999);
	// 结束场景绘制
	g_pd3dDevice->EndScene();

	// 显示到屏幕上
	g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}


// 消息处理
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	switch( msg )
	{
		case WM_DESTROY:	// 退出消息
			PostQuitMessage( 0 );
			return 0;

		case WM_PAINT:		// 绘制
			Render();
			ValidateRect( hWnd, NULL );
			return 0;
	}

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

// WinMain程序入口
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
	// 注册窗口类
	WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, 
			  GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
			  "D3D Tutorial", NULL };
	RegisterClassEx( &wc );

	// 创建窗口
	HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 01: CreateDevice", 
				  WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
				  GetDesktopWindow(), NULL, wc.hInstance, NULL );

	// 初始化D3D
	if( SUCCEEDED( InitD3D( hWnd ) ) )
	{ 
		// 显示窗口
		ShowWindow( hWnd, SW_SHOWDEFAULT );
		UpdateWindow( hWnd );

		// 消息循环
		MSG msg; 
		while( GetMessage( &msg, NULL, 0, 0 ) )
		{
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}
	}

	// 结束处理
	Cleanup();
	UnregisterClass( "D3D Tutorial", wc.hInstance );
	return 0;
}
help me thank u !!!!!!!!!!!!!!!!!!! [Edited by - jollyjeffers on December 6, 2008 5:00:50 PM]

Share this post


Link to post
Share on other sites
Help us to help you.

I would like to see you post back with:

1. Any error messages, including debug output from D3D
2. An analysis of all return codes and your thoughts on any suspect ones - especially if calls are failing.
3. Your step-through analysis and some reasoning on where the error might be
4. A comparison with similar tile rendering tutorials or sample applications. Do you have a reference to work with? If so, how is it different from yours? Why does it work and yours not?

Finally: you've shown no initiative in solving this problem for yourself - why should we invest our free time to do your work for you?

To write code is much less than half the job - being able to debug it, test it and maintain it are equally big if not greater challenges. Code-dumping to a forum whenever you get stuck will not get you far.

Best regards,
Jack

Share this post


Link to post
Share on other sites

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