window creation using windows api

Started by
22 comments, last by guitarguy 15 years, 10 months ago
Quote:Original post by Chris27
That doesn't sound like a good idea to me. They should at least explain what each funtion used in creating a Window does and how Windows messages/events works. I honestly think creating a Window using Win32 API is pretty confusing and if you don't understand it how can you make effective use of DirectX with it.


Thats what i thought too, thats why im tryng to get it nailed now, ive read all about how making a window works, but im curious about whether or not i need to remember every line, for future use, or will i be able to just copy and paste this over every time i need to use it?

Also, ive managed a compiled window now :D but i did it by opening Visual studio with the configuration settings of the directx sdk example, and everything seemed to work fine. Are there any important configuration changes settings that i need to know about?
Advertisement
Here's a minimal snippet that creates a window and renders to it via D3D9.
#include <windows.h>#include <d3d9.h>HWND hWindow;IDirect3D9* D3D;IDirect3DDevice9* Device;struct Vertex{	float x, y, z, rhw;	D3DCOLOR Color;};#define FVF_VERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)LRESULT WINAPI WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ){	switch( msg )	{	case WM_CLOSE:		PostQuitMessage( 0 );		return 0;	case WM_PAINT:		ValidateRect( hWnd, NULL );		return 0;	}	return DefWindowProc( hWnd, msg, wParam, lParam );}int main(){	HINSTANCE hInstance = GetModuleHandle( NULL );	//create a window	WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0, 0, hInstance, NULL, NULL, NULL, NULL, "MiniD3D", NULL };	RegisterClassEx( &wc );	hWindow = CreateWindowEx( WS_EX_APPWINDOW | WS_EX_WINDOWEDGE, "MiniD3D", "MiniD3D", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 		300, 300, NULL, NULL, hInstance, NULL );	ShowWindow( hWindow, SW_SHOW );	UpdateWindow( hWindow );	//Set up d3d	D3D = Direct3DCreate9( D3D_SDK_VERSION );	D3DPRESENT_PARAMETERS d3dpp = { 0 };	d3dpp.Windowed = TRUE;	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;	d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;	D3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWindow, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &Device );	//now create our triangle	Vertex Triangle[3] = 	{		{ 150.0f, 50.0f, 0.5f, 1.0f, D3DCOLOR_XRGB( 255, 0, 0 ) },		{ 250.0f, 250.0f, 0.5f, 1.0f, D3DCOLOR_XRGB( 0, 255, 0 ) },		{ 50.0f, 250.0f, 0.5f, 1.0f, D3DCOLOR_XRGB( 0, 0, 255 ) }	};	MSG msg;	bool RunApp = true;	while( RunApp )	{		if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )		{			if( msg.message == WM_QUIT )				RunApp = false;			TranslateMessage( &msg );			DispatchMessage( &msg );		}		//render stuff		Device->Clear( 0, 0, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 0 ), 1.0f, 0 );		Device->BeginScene();		Device->SetFVF( FVF_VERTEX );		Device->DrawPrimitiveUP( D3DPT_TRIANGLELIST, 1, Triangle, sizeof(Vertex) );		Device->EndScene();		Device->Present( 0, 0, 0, 0 );	}	Device->Release();	D3D->Release();	DestroyWindow( hWindow );	return 0;}

That should definitely compile, as well as being relatively straightforward.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
DirectX
http://www.directxtutorial.com/

Win32
http://www.functionx.com/win32/index.htm

I think these are decent tutorials considering they are not made by proffesionals.
yeah i compared the examples and your code, and figured out the multi-byte/unicode settings, turns out i was missing some additional dependencies too. Ive managed to compile! so thanks for all your help.

one last thing: ive figured out what each section of the code does, commented it and such, but i dont remember what every single type definition means, such as the LPSTR (long pointer to string) etc. my question is do i need to remember this line-for-line so i can write my own without referencing, or is it still good practise to take it easy and re-use the same project (for the window creation/initialisation?

This topic is closed to new replies.

Advertisement