Archived

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

Which is better?

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

hi... Code A:
#include <d3d8.h>
#include <d3dx8.h>
#include <mmsystem.h>
#include <iostream>
using namespace std;

HRESULT RegisterWinClass(HINSTANCE &, WNDPROC, LPSTR);
HRESULT CreateWinEx(HWND &hWnd, HINSTANCE &, LPSTR, LPSTR);
VOID SetupMatrix();
HRESULT IntilGeometry();
HRESULT IntilDX(HWND &);
VOID Render();
WPARAM MainLoop();
LRESULT WINAPI MainMsg(HWND, UINT, WPARAM, LPARAM);

HWND g_hWnd = NULL;
HDC g_hDc = NULL;
LPDIRECT3D8 g_d3d8 = NULL;
LPDIRECT3DDEVICE8 g_d3ddevice8 = NULL;
LPDIRECT3DVERTEXBUFFER8 g_d3dvb8 = NULL;

struct CUSTOMVERTEX{
	float x, y, z;
	DWORD color;
};

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)


INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, INT)
{
	if(FAILED(RegisterWinClass(hInstance, MainMsg, "DXTest3")))
	{
		return -1;
	}

	if(FAILED(CreateWinEx(g_hWnd, hInstance, "DXTest3", "Direct X Test 3")))
	{
		return -2;
	}
	
	if(FAILED(IntilDX(g_hWnd)))
	{
		return -3;
	}

	if(FAILED(IntilGeometry()))
	{
		return -4;
	}

	ShowWindow(g_hWnd, SW_SHOW);
	UpdateWindow(g_hWnd);

	return (int)MainLoop();
}

HRESULT RegisterWinClass(HINSTANCE &hInstance, WNDPROC wndProc, LPSTR className)
{
	WNDCLASSEX wcx;

	wcx.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wcx.cbSize = sizeof(WNDCLASSEX);
	wcx.lpszClassName = className;
	wcx.hInstance = hInstance;
	wcx.lpfnWndProc = wndProc;
	
	wcx.hbrBackground = NULL;
	wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wcx.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
	
	wcx.cbClsExtra = NULL;
	wcx.cbWndExtra = NULL;
	wcx.lpszMenuName = NULL;

	if(!RegisterClassEx(&wcx))
	{
		return E_FAIL;
	}

	return S_OK;
}

HRESULT CreateWinEx(HWND &hWnd, HINSTANCE &hInstance, LPSTR className, LPSTR winCaption)
{
	hWnd = CreateWindowEx(NULL,
						  className,
						  winCaption,
						  WS_OVERLAPPEDWINDOW,
						  0,
						  0,
						  CW_USEDEFAULT,
						  CW_USEDEFAULT,
						  NULL,
						  NULL,
						  hInstance,
						  NULL);

	if(!hWnd)
	{
		return E_FAIL;
	}

	return S_OK;
}

VOID SetupMatrix()
{
	D3DXMATRIX matWorld;
	D3DXMatrixRotationY(&matWorld, timeGetTime()/150.0f );
	g_d3ddevice8->SetTransform(D3DTS_WORLD, &matWorld);

	D3DXMATRIX matView;
	D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3( 0.0f, 3.0f,-5.0f),
                                 &D3DXVECTOR3( 0.0f, 0.0f, 0.0f),
                                 &D3DXVECTOR3( 0.0f, 1.0f, 0.0f));

	g_d3ddevice8->SetTransform(D3DTS_VIEW, &matView);
	
	D3DXMATRIX matProj;
	D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );

	g_d3ddevice8->SetTransform(D3DTS_PROJECTION, &matProj);
}

HRESULT IntilGeometry()
{
	CUSTOMVERTEX vertex[] =	{
								{-1.0f, -1.0f, 0.0f, D3DXCOLOR( 255.0f,   0.0f,   0.0f, 0.0f)},
								{ 1.0f, -1.0f, 0.0f, D3DXCOLOR(   0.0f, 255.0f,   0.0f, 0.0f)},
								{ 0.0f,  1.0f, 0.0f, D3DXCOLOR(   0.0f,   0.0f, 255.0f, 0.0f)}							
							};
	
	if(FAILED(g_d3ddevice8->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),
											   0,
											   D3DFVF_CUSTOMVERTEX,
											   D3DPOOL_DEFAULT,
											   &g_d3dvb8)))
	{
		return E_FAIL;
	}
	
	VOID *pVertex;
	if(FAILED(g_d3dvb8->Lock(0, sizeof(vertex), (BYTE**)&pVertex, 0)))
	{
		return E_FAIL;
	}
	memcpy(pVertex, vertex, sizeof(vertex));
	g_d3dvb8->Unlock();

	return S_OK;
}

HRESULT IntilDX(HWND &hWnd)
{
	if((g_d3d8 = Direct3DCreate8(D3D_SDK_VERSION)) == NULL)
	{
		return E_FAIL;
	}
	
	D3DDISPLAYMODE d3ddm;
	
	if(FAILED(g_d3d8->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
	{
		return E_FAIL;
	}
	
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));
	d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = d3ddm.Format;

	if(FAILED(g_d3d8->CreateDevice(D3DADAPTER_DEFAULT,
								   D3DDEVTYPE_HAL,
								   hWnd,
								   D3DCREATE_SOFTWARE_VERTEXPROCESSING,
								   &d3dpp,
								   &g_d3ddevice8)))
	{
		return E_FAIL;
	}

	g_d3ddevice8->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	g_d3ddevice8->SetRenderState(D3DRS_LIGHTING, FALSE);
	
	return S_OK;
}

VOID Render()
{
	g_d3ddevice8->Clear(0, 
						NULL, 
						D3DCLEAR_TARGET, 
						D3DCOLOR_XRGB(0, 0, 0),
						1.0f,
						0);

	g_d3ddevice8->BeginScene();

	SetupMatrix();

	g_d3ddevice8->SetStreamSource(0, g_d3dvb8, sizeof(CUSTOMVERTEX));
	g_d3ddevice8->SetVertexShader(D3DFVF_CUSTOMVERTEX);
	g_d3ddevice8->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 1);

	g_d3ddevice8->EndScene();

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

WPARAM MainLoop()
{
	MSG msg;
	ZeroMemory(&msg, sizeof(msg));
	bool done = false;

	while(!done)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
			{
				done = true;
				break;
			}
            
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			Render();
		}
	}

	return msg.wParam;
}

LRESULT WINAPI MainMsg(HWND hWnd, UINT uiMsg, WPARAM wParam, 
					   LPARAM lParam)
{
	switch(uiMsg)
	{
		case WM_PAINT:
			{
				break;
			}
		case WM_CLOSE:
			{
				PostQuitMessage(0);
				break;
			}
		case WM_DESTROY:
			{
				PostQuitMessage(0);
				break;
			}
		default:
			{
				break;
			}
	}

	return DefWindowProc(hWnd, uiMsg, wParam, lParam);
}
And here is.... Code B:
#include <d3d8.h>
#include <d3dx8.h>
#include <mmsystem.h>
#include <iostream>
using namespace std;

HRESULT RegisterWinClass(HINSTANCE &, WNDPROC, LPSTR);
HRESULT CreateWinEx(HWND &hWnd, HINSTANCE &, LPSTR, LPSTR);
VOID SetupMatrix();
HRESULT IntilGeometry();
HRESULT IntilDX(HWND &);
VOID Render();
WPARAM MainLoop();
LRESULT WINAPI MainMsg(HWND, UINT, WPARAM, LPARAM);

HWND g_hWnd = NULL;
HDC g_hDc = NULL;
LPDIRECT3D8 g_d3d8 = NULL;
LPDIRECT3DDEVICE8 g_d3ddevice8 = NULL;
LPDIRECT3DVERTEXBUFFER8 g_d3dvb8 = NULL;

struct CUSTOMVERTEX{
	float x, y, z;
	DWORD color;
};

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)

CUSTOMVERTEX g_vertex[]  =	{
								{-1.0f, -1.0f, 0.0f, D3DXCOLOR( 255.0f,   0.0f,   0.0f, 0.0f)},
								{ 1.0f, -1.0f, 0.0f, D3DXCOLOR(   0.0f, 255.0f,   0.0f, 0.0f)},
								{ 0.0f,  1.0f, 0.0f, D3DXCOLOR(   0.0f,   0.0f, 255.0f, 0.0f)}							
							};

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, INT)
{
	if(FAILED(RegisterWinClass(hInstance, MainMsg, "DXTest3")))
	{
		return -1;
	}

	if(FAILED(CreateWinEx(g_hWnd, hInstance, "DXTest3", "Direct X Test 3")))
	{
		return -2;
	}
	
	if(FAILED(IntilDX(g_hWnd)))
	{
		return -3;
	}

	if(FAILED(IntilGeometry()))
	{
		return -4;
	}

	ShowWindow(g_hWnd, SW_SHOW);
	UpdateWindow(g_hWnd);

	return (int)MainLoop();
}

HRESULT RegisterWinClass(HINSTANCE &hInstance, WNDPROC wndProc, LPSTR className)
{
	WNDCLASSEX wcx;

	wcx.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wcx.cbSize = sizeof(WNDCLASSEX);
	wcx.lpszClassName = className;
	wcx.hInstance = hInstance;
	wcx.lpfnWndProc = wndProc;
	
	wcx.hbrBackground = NULL;
	wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wcx.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
	
	wcx.cbClsExtra = NULL;
	wcx.cbWndExtra = NULL;
	wcx.lpszMenuName = NULL;

	if(!RegisterClassEx(&wcx))
	{
		return E_FAIL;
	}

	return S_OK;
}

HRESULT CreateWinEx(HWND &hWnd, HINSTANCE &hInstance, LPSTR className, LPSTR winCaption)
{
	hWnd = CreateWindowEx(NULL,
						  className,
						  winCaption,
						  WS_OVERLAPPEDWINDOW,
						  0,
						  0,
						  CW_USEDEFAULT,
						  CW_USEDEFAULT,
						  NULL,
						  NULL,
						  hInstance,
						  NULL);

	if(!hWnd)
	{
		return E_FAIL;
	}

	return S_OK;
}

VOID SetupMatrix()
{
	D3DXMATRIX matWorld;
	D3DXMatrixRotationY(&matWorld, timeGetTime()/150.0f );
	g_d3ddevice8->SetTransform(D3DTS_WORLD, &matWorld);

	D3DXMATRIX matView;
	D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3( 0.0f, 3.0f,-5.0f),
                                 &D3DXVECTOR3( 0.0f, 0.0f, 0.0f),
                                 &D3DXVECTOR3( 0.0f, 1.0f, 0.0f));

	g_d3ddevice8->SetTransform(D3DTS_VIEW, &matView);
	
	D3DXMATRIX matProj;
	D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );

	g_d3ddevice8->SetTransform(D3DTS_PROJECTION, &matProj);
}

HRESULT IntilGeometry()
{
	if(FAILED(g_d3ddevice8->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),
											   0,
											   D3DFVF_CUSTOMVERTEX,
											   D3DPOOL_DEFAULT,
											   &g_d3dvb8)))
	{
		return E_FAIL;
	}
	
	/*VOID *pVertex;
	if(FAILED(g_d3dvb8->Lock(0, sizeof(vertex), (BYTE**)&pVertex, 0)))
	{
		return E_FAIL;
	}
	memcpy(pVertex, vertex, sizeof(vertex));
	g_d3dvb8->Unlock();*/

	return S_OK;
}

HRESULT IntilDX(HWND &hWnd)
{
	if((g_d3d8 = Direct3DCreate8(D3D_SDK_VERSION)) == NULL)
	{
		return E_FAIL;
	}
	
	D3DDISPLAYMODE d3ddm;
	
	if(FAILED(g_d3d8->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
	{
		return E_FAIL;
	}
	
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));
	d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = d3ddm.Format;

	if(FAILED(g_d3d8->CreateDevice(D3DADAPTER_DEFAULT,
								   D3DDEVTYPE_HAL,
								   hWnd,
								   D3DCREATE_SOFTWARE_VERTEXPROCESSING,
								   &d3dpp,
								   &g_d3ddevice8)))
	{
		return E_FAIL;
	}

	g_d3ddevice8->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	g_d3ddevice8->SetRenderState(D3DRS_LIGHTING, FALSE);
	
	return S_OK;
}

VOID Render()
{
	g_d3ddevice8->Clear(0, 
						NULL, 
						D3DCLEAR_TARGET, 
						D3DCOLOR_XRGB(0, 0, 0),
						1.0f,
						0);

	g_d3ddevice8->BeginScene();

	SetupMatrix();

	VOID *pVertex;
	g_d3dvb8->Lock(0, sizeof(g_vertex), (BYTE**)&pVertex, 0);
	memcpy(pVertex, g_vertex, sizeof(g_vertex));
	g_d3dvb8->Unlock();

	g_d3ddevice8->SetStreamSource(0, g_d3dvb8, sizeof(CUSTOMVERTEX));
	g_d3ddevice8->SetVertexShader(D3DFVF_CUSTOMVERTEX);
	g_d3ddevice8->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 1);

	g_d3ddevice8->EndScene();

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

WPARAM MainLoop()
{
	MSG msg;
	ZeroMemory(&msg, sizeof(msg));
	bool done = false;

	while(!done)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
			{
				done = true;
				break;
			}
            
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			Render();
		}
	}

	return msg.wParam;
}

LRESULT WINAPI MainMsg(HWND hWnd, UINT uiMsg, WPARAM wParam, 
					   LPARAM lParam)
{
	switch(uiMsg)
	{
		case WM_PAINT:
			{
				break;
			}
		case WM_CLOSE:
			{
				PostQuitMessage(0);
				break;
			}
		case WM_DESTROY:
			{
				PostQuitMessage(0);
				break;
			}
		default:
			{
				break;
			}
	}

	return DefWindowProc(hWnd, uiMsg, wParam, lParam);
}
Which one is better A or B .... in term of Lock() and Unlock() procedure.... Thanks

Share this post


Link to post
Share on other sites
I would tend to agree if there weren''t a few nice and helpful APs going about.

Anyway, about the original post. That''s a lot of code to go through. Would you mind outlining the difference for those who don''t want to read through both of them and compare?


Why you shouldn''t use iostream.h - ever! | A Good free online C++ book

Share this post


Link to post
Share on other sites
To johnc82: You posted too much code, noone has the time to go through all this to compare the 2 locking schemes

Please be more specific, in this question and in future questions (also, please read this forum''s FAQ, and the general FAQ)

To the Anonymous Poster: Please behave yourself. If you can''t help people, then please don''t litter the forum with impolite posts.
The guy asked the question the wrong way obviously, you could''ve just showed him how to do things right.

Muhammad Haggag

Share this post


Link to post
Share on other sites
hi......

ok...

Well, is simple should I put the:

VOID *pVertex;
if(FAILED(g_d3dvb8->Lock(0, sizeof(vertex), (BYTE**)&pVertex, 0)))
{
return E_FAIL;
}
memcpy(pVertex, vertex, sizeof(vertex));
g_d3dvb8->Unlock();


in the rendering fuction.... or in the initial function?

Thanks

[edited by - johnc82 on December 15, 2003 2:19:48 AM]

Share this post


Link to post
Share on other sites
It depends:
- If your data is dynamic (changes often), you''d use a dynamic vertex buffer (created with the D3DUSAGE_DYNAMIC flag), and lock it, copy the data to it, unlock it every frame.

- If your data is static, you''d use static vertex buffers that are created and initialized with the data (i.e. you only lock when initializing the data)

I suggest you also take a look at microsoft''s DX faq. It has some stuff on dynamic buffers, I think.

Muhammad Haggag

Share this post


Link to post
Share on other sites
Keep in mind johnc82,

That with the newer hardware (with hardware T&L) it likes all the vertex buffers static. It should be uploaded to the card at initialization time. Dynamic is ok, but slower than static data since the vertex need to be transferred over the AGP bus every frame. With static it gets uploaded to the graphics card once, then it stores it in its own memory and renders from there.

Share this post


Link to post
Share on other sites