Terminating Programm ? how to do?
I got a question about my little programm:
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <mmsystem.h>
#include <d3dx9math.h>
#pragma comment (lib,"d3d9")
#pragma comment (lib,"d3dx9")
D3DXMATRIX *WINAPI D3DXMatrixLookAtLH(
D3DXMATRIX *pOut,
CONST D3DXVECTOR3 *pEye,
CONST D3DXVECTOR3 *pAt,
CONST D3DXVECTOR3 *pUp
);
LPDIRECT3D9 D3D9 = NULL;
LPDIRECT3DDEVICE9 D3DDEVICE9 = NULL;
LPDIRECT3DVERTEXBUFFER9 D3D_VB9 = NULL;
LPDIRECT3DINDEXBUFFER9 D3D_IB9 = NULL;
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
struct CUSTOMVERTEX
{
FLOAT x,y,z,rhw;
DWORD color;
};
LRESULT CALLBACK WndProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam);
HRESULT Init_D3D(HWND hWnd);
HRESULT Init_VB_IB();
VOID Render();
VOID SetupMatrices();
INT WINAPI WinMain(HINSTANCE hInstance,HINSTANCE, LPSTR, INT)
{
HWND hWnd;
MSG Msg;
WNDCLASSEX wcex;
wcex.cbClsExtra = 0;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.cbWndExtra = 0;
wcex.hbrBackground = 0;
wcex.hCursor = LoadCursor(0,IDC_HAND);
wcex.hIcon = LoadIcon(0,IDI_APPLICATION);
wcex.hIconSm = LoadIcon(0,IDI_APPLICATION);
wcex.hInstance = hInstance;
wcex.lpfnWndProc = WndProc;
wcex.lpszMenuName = 0;
wcex.style = CS_CLASSDC;
wcex.lpszClassName = "WNDCLASS";
RegisterClassEx(&wcex);
hWnd = CreateWindow("WNDCLASS","RECHTECK",WS_OVERLAPPEDWINDOW,0,0,
500,500,GetDesktopWindow(),0,wcex.hInstance,0);
Init_D3D(hWnd);
Init_VB_IB();
ShowWindow(hWnd,SW_SHOWDEFAULT);
UpdateWindow(hWnd);
while(GetMessage(&Msg, hWnd,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
{
switch(Msg)
{
case WM_DESTROY:
PostQuitMessage(WM_QUIT);
break;
case WM_CLOSE:
PostQuitMessage(WM_QUIT);
break;
case WM_PAINT:
Render();
}
return DefWindowProc(hWnd,Msg,wParam,lParam);
}
HRESULT Init_D3D(HWND hWnd)
{
D3D9 = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS D3DPP;
ZeroMemory(&D3DPP,sizeof(D3DPP));
D3DPP.Windowed = TRUE;
D3DPP.BackBufferFormat = D3DFMT_UNKNOWN;
D3DPP.SwapEffect = D3DSWAPEFFECT_DISCARD;
D3D9->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,&D3DPP,
&D3DDEVICE9);
D3DDEVICE9->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
D3DDEVICE9->SetRenderState(D3DRS_LIGHTING,FALSE);
return S_OK;
}
HRESULT Init_VB_IB()
{
CUSTOMVERTEX Rechteck[]=
{
{100.0f ,100.0f, 1.0f, 1.0f , 0xff0000ff,},
{300.0f ,100.0f, 1.0f, 1.0f , 0xff00ff00,},
{300.0f ,300.0f, 1.0f, 1.0f , 0xff0000ff,},
{100.0f ,300.0f, 1.0f, 1.0f , 0xff00ff00,},
};
WORD Liste[6] = {
0,1,2,
2,3,0
};
int VertexListSize = sizeof(CUSTOMVERTEX) * 4;
D3DDEVICE9->CreateVertexBuffer(VertexListSize,0,D3DFVF_CUSTOMVERTEX,D3DPOOL_MANAGED,
&D3D_VB9,0);
VOID* Vertices;
D3D_VB9->Lock(0,sizeof(Rechteck),(void**)&Vertices,0);
memcpy(Vertices,Rechteck,VertexListSize);
D3D_VB9->Unlock();
int IndexListSize = sizeof(WORD) * 6;
D3DDEVICE9->CreateIndexBuffer(IndexListSize,0,D3DFMT_INDEX16,D3DPOOL_MANAGED,
&D3D_IB9,0);
VOID* Indices;
D3D_IB9->Lock(0,sizeof(Liste),(void**)&Indices,0);
memcpy(Indices,Liste,IndexListSize);
D3D_IB9->Unlock();
return S_OK;
}
VOID Render()
{
D3DDEVICE9->Clear(0,0,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0f,0);
D3DDEVICE9->BeginScene();
SetupMatrices();
D3DDEVICE9->SetStreamSource(0,D3D_VB9,0,sizeof(CUSTOMVERTEX));
D3DDEVICE9->SetIndices(D3D_IB9);
D3DDEVICE9->SetFVF(D3DFVF_CUSTOMVERTEX);
D3DDEVICE9->DrawIndexedPrimitive (D3DPT_TRIANGLELIST,0,0,4,0,2);
D3DDEVICE9->EndScene();
D3DDEVICE9->Present(0,0,0,0);
}
VOID SetupMatrices()
{
D3DXMATRIXA16 MatView;
D3DXVECTOR3 EP(0.0f,3.0f,-5.0f);
D3DXVECTOR3 LAP(0.0f,0.0f,-1.0f);
D3DXVECTOR3 UV(0.0f,1.0f,0.0f);
D3DXMatrixLookAtLH(&MatView,&EP,&LAP,&UV);
D3DDEVICE9->SetTransform(D3DTS_VIEW,&MatView);
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
D3DDEVICE9->SetTransform( D3DTS_PROJECTION, &matProj );
}
The Programm starts and if i click on "CLOSE" the Window closes, but it is still listed in the Taskmanager with almost 100% CPU usage! - so the programm itself doesnt terminate! help plz
And one more question! I am calling the SetupMatrices function, but the view and lookat doesnt work.
One curious thing is, that if i point the mouse over "D3DXMatrixLookAtLH" no tool tips are displayed alhough i copyed the struct for the type to my project (see at the start).
Two things:
1. you need to release your device resources and the device itself before the app exists.
2. You are are sending WM_QUIT in your WM_QUIT handler - infinitive loop.
[edit]
PS: use [ source ][ /source ] tags
[/edit]
1. you need to release your device resources and the device itself before the app exists.
2. You are are sending WM_QUIT in your WM_QUIT handler - infinitive loop.
[edit]
PS: use [ source ][ /source ] tags
[/edit]
You only call you Render() function whenever the window needs to be repainted. And that does not happen until another windows moves over it, you resize it and so on. So you need to call Render() in the loop in WinMain() to see the changes.
Edit: Actually, now that I look at it more closely it seems you're not updating your matrices so you can disregard what I just said. Sorry for any confusion.
Edit: Actually, now that I look at it more closely it seems you're not updating your matrices so you can disregard what I just said. Sorry for any confusion.
What is wrong about this?
LRESULT CALLBACK WndProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
{
switch(Msg)
{
case WM_DESTROY:
CleanUp();
PostQuitMessage(WM_QUIT);
break;
case WM_CLOSE:
CleanUp();
PostQuitMessage(WM_QUIT);
}
return DefWindowProc(hWnd,Msg,wParam,lParam);
}
I relaese the objects and so on in "Cleanup" and then send the WM_QUIT msg but the programm still runs in the f******** background
Help!!
LRESULT CALLBACK WndProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
{
switch(Msg)
{
case WM_DESTROY:
CleanUp();
PostQuitMessage(WM_QUIT);
break;
case WM_CLOSE:
CleanUp();
PostQuitMessage(WM_QUIT);
}
return DefWindowProc(hWnd,Msg,wParam,lParam);
}
I relaese the objects and so on in "Cleanup" and then send the WM_QUIT msg but the programm still runs in the f******** background
Help!!
Yet another comment regarding your message handling.
A better message proc looks like this:
[edit]
What is 'f********'? I only know f****** [wink].
BTW, I tested the above and it runs flawlessly (win2k sp4, dx9c).
[/edit]
A better message proc looks like this:
#define SAFE_RELEASE(x) if (x) { (x)->Release(); x = NULL; }void CleanUp() { SAFE_RELEASE(D3D_VB9); SAFE_RELEASE(D3D_IB9); SAFE_RELEASE(D3DDEVICE9);}LRESULT CALLBACK WndProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam){ switch(Msg) { case WM_DESTROY: CleanUp(); PostQuitMessage(WM_QUIT); break; case WM_CLOSE: CleanUp(); PostQuitMessage(WM_QUIT); break; case WM_PAINT: Render(); break; default: // only call on unhandled messages return DefWindowProc(hWnd,Msg,wParam,lParam); } // return 'message processed' return TRUE;}
[edit]
What is 'f********'? I only know f****** [wink].
BTW, I tested the above and it runs flawlessly (win2k sp4, dx9c).
[/edit]
My second problem was this one->
VOID SetupMatrices()
{
D3DXMATRIXA16 MatView;
D3DXVECTOR3 EP(0.0f,3.0f,-5.0f);
D3DXVECTOR3 LAP(0.0f,0.0f,-1.0f);
D3DXVECTOR3 UV(0.0f,1.0f,0.0f);
D3DXMatrixLookAtLH(&MatView,&EP,&LAP,&UV);
D3DDEVICE9->SetTransform(D3DTS_VIEW,&MatView);
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
D3DDEVICE9->SetTransform( D3DTS_PROJECTION, &matProj );
}
I set everything up, but the view things dont work, i only see my rectangle, but nothing about the vectors i set before
And no TOOLTIPS about D3DXMatrixLookAtLH !!( Is that normal???)
(Bin auch Deutscher)
VOID SetupMatrices()
{
D3DXMATRIXA16 MatView;
D3DXVECTOR3 EP(0.0f,3.0f,-5.0f);
D3DXVECTOR3 LAP(0.0f,0.0f,-1.0f);
D3DXVECTOR3 UV(0.0f,1.0f,0.0f);
D3DXMatrixLookAtLH(&MatView,&EP,&LAP,&UV);
D3DDEVICE9->SetTransform(D3DTS_VIEW,&MatView);
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
D3DDEVICE9->SetTransform( D3DTS_PROJECTION, &matProj );
}
I set everything up, but the view things dont work, i only see my rectangle, but nothing about the vectors i set before
And no TOOLTIPS about D3DXMatrixLookAtLH !!( Is that normal???)
(Bin auch Deutscher)
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE) ^^^^^^^^^^^^^^
D3DFVF_XYZRHW → transformed vector → no 3D transformations applied → view matrix stuff is not used
PS: Das ist ein englisches forum, deshalb antworte ich auch auf englisch. Aber nochmal auf deutsch[smile]: Weil du ein transformiertes vektorformat benutzt, werden alle 3d transformationen ignoriert. Ist also nix kaputt, läuft halt nur mit deinem vertex format nicht.
MfG,
Pat.
There is no need to do PostQuitMessage(WM_QUIT), the parameter is just an exit code. Any non-zero value will do (WinMain is supposed to return this value, and when it returns zero that basically means the app is seriously broken)
Quote:Original post by uavfun
There is no need to do PostQuitMessage(WM_QUIT), the parameter is just an exit code. Any non-zero value will do (WinMain is supposed to return this value, and when it returns zero that basically means the app is seriously broken)
I guess you mean if the return code is something other than zero. However, it's not important as the return code is not processed by the OS, only optionally by a user process that started the app.
There's nothing 'magic' about these codes. They are just some archaic status information intentionally introduced for shell scripts and batch programs to be able to check the operation status of any called program.
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_DIFFUSE)
VOID SetupMatrices()
{
D3DXVECTOR3 vEyePt( 0.0f, 0.0f,5.0f );
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, -3.0f );
D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
D3DDEVICE9->SetTransform( D3DTS_VIEW, &matView );
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
D3DDEVICE9->SetTransform( D3DTS_PROJECTION, &matProj );
}
Now i set this but i cant see my rectangle on the screen only a blue screen.
I really dont know what is wrong about those matrices iam really confused - iam trying to solve this for hours
As i set the LookAt and EyePt i should look at my rectangle from behind, shouldn*t I?
Man iam going to commit suicide if i dont get this problem solved
VOID SetupMatrices()
{
D3DXVECTOR3 vEyePt( 0.0f, 0.0f,5.0f );
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, -3.0f );
D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
D3DDEVICE9->SetTransform( D3DTS_VIEW, &matView );
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
D3DDEVICE9->SetTransform( D3DTS_PROJECTION, &matProj );
}
Now i set this but i cant see my rectangle on the screen only a blue screen.
I really dont know what is wrong about those matrices iam really confused - iam trying to solve this for hours
As i set the LookAt and EyePt i should look at my rectangle from behind, shouldn*t I?
Man iam going to commit suicide if i dont get this problem solved
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement