Skip to main content
GameDev.net gamedev.net
🔒 Locked

direct3d problem

Started by kevlur Jan 31, 2004 at 8:47 AM 10 replies 650+ views
Original Post
kevlur
kevlur
Hi, i have read some tutorials about direct3d, and im trying to write an idiot direct3d app (with directX9), with only direct3d initialization without neither vertexbuffers nor drawprimitive, i wish only to be able to clear the screen with a d3dDevice->Clear() call and the program have to stay alive in the Messagepeek loop, this is all, the only problem is that the program dont clear the screen, but it dont give any error from any functions, it all seems good, and it arrive and stay alive in the messagepeek loop, there are some MessageBox in some places to try to understand where is the problem , but i dont... Could someone tell me why this idiot app dont clear the screen with green? A very big part of the code is from a tutorial... ( i hope i dont get kick from the forum for the lenght of the paste #define INITGUID #define WIN32_LEAN_AND_MEAN #include <d3d9.h> #include <d3dx9.h> #include <D3DX9Math.h> #pragma comment(lib, "d3d9.lib") #pragma comment(lib, "d3dx9.lib") LPDIRECT3D9 g_pD3D = NULL; LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; float scrWidth = 1024; float scrHeight = 768; const int scrDepth = 16; HINSTANCE g_hInst; HWND g_hWnd; HRESULT g_hr; bool g_minimized = FALSE; HRESULT InitD3D( HWND hWnd ) { if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) { return E_FAIL; } D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { return E_FAIL; } return S_OK; } VOID Cleanup() { if( g_pd3dDevice != NULL) g_pd3dDevice->Release(); if( g_pD3D != NULL) g_pD3D->Release(); } HRESULT Render() { if( g_pd3dDevice == NULL) return E_FAIL; g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,255,0), 1.0f, 0 ); if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { MessageBox(g_hWnd,"dentro Render()","boh",MB_OK); g_pd3dDevice->EndScene(); } g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); } void SetupScreen(float WindowWidth, float WindowHeight) { D3DXMATRIX matOrtho; D3DXMATRIX matIdentity; D3DXMatrixOrthoLH(&matOrtho, WindowWidth, WindowHeight, 0.0f, 1.0f); D3DXMatrixIdentity(&matIdentity); g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matOrtho); g_pd3dDevice->SetTransform(D3DTS_WORLD, &matIdentity); g_pd3dDevice->SetTransform(D3DTS_VIEW, &matIdentity); g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE); } LRESULT CALLBACK WndProc(HWND wpHWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_SYSCOMMAND: { switch (wParam) { case SC_SCREENSAVE: case SC_MONITORPOWER: return 0; } break; } return 0; case WM_SIZE: switch (wParam) { case SIZE_MINIMIZED: Cleanup(); ShowCursor ( TRUE); g_minimized = TRUE; return 0; case SIZE_RESTORED: //InitD3D(g_hWnd); ShowCursor (FALSE); g_minimized = FALSE; return 0; } break; case WM_DESTROY: { PostQuitMessage(0); return 0; } break; default:break; } return DefWindowProc(wpHWnd, msg, wParam, lParam); } int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX winClass; MSG msg; g_hInst=hInstance; winClass.cbSize = sizeof(WNDCLASSEX); winClass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW; winClass.lpfnWndProc = WndProc; winClass.cbClsExtra = 0; winClass.cbWndExtra = 0; winClass.hInstance = g_hInst; winClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); winClass.hCursor = LoadCursor(NULL, IDC_ARROW); winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); winClass.lpszMenuName = NULL; winClass.lpszClassName = "DXclass"; winClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if (!RegisterClassEx(&winClass)) { MessageBox(g_hWnd, "Unable to create window class", "Initialisation Error...", MB_OK); return 0; } g_hWnd = CreateWindowEx(NULL, "DXclass", "DX Tutorial", WS_POPUP, 0, 0, scrWidth, scrHeight, NULL, NULL, g_hInst, NULL); if (!g_hWnd) { MessageBox(g_hWnd, "Unable to create window", "Initialisation Error...", MB_OK); return 0; } // InitScene(); if(InitD3D(g_hWnd) == E_FAIL) { MessageBox(g_hWnd,"dentro initd3d, errore","boh",MB_OK); Cleanup(); return 0; } ShowCursor (FALSE); MessageBox(g_hWnd,"dopo initd3d","boh",MB_OK); SetupScreen(scrWidth, scrHeight); while(1) { while (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); if (msg.message == WM_QUIT) break; } if (msg.message==WM_QUIT) break; if ( g_minimized == FALSE) { // if ( KeyDown(DIK_ESCAPE) ) // PostQuitMessage(0); // Update_Fps(); // UpdateKeys(); // DrawScene(); if(Render() == E_FAIL) { MessageBox(g_hWnd,"errore di render","boh",MB_OK); return 0; } MessageBox(g_hWnd,"nel loop del peek message","boh",MB_OK); Cleanup(); return 0; } } Cleanup(); return 0; }
Deranged
Deranged
[ source ] PUT SOURCE HERE BUT WITHOUT THE SPACES IN THE TAGS [ /source ]

[Mercury Software] [Google!] [ Look I DONT Follow Trends ]
kevlur
kevlur
Sorry, but i dont understand, what do you mean with "without spaces in the tags"?

Ilici
Ilici

omg i''m using source tags


now press the "quote" button to see how it''s done.
kevlur
kevlur
thanks
kevlur
kevlur
Someone can tell me why it doesn''t cler the screen with green?


#define INITGUID

#define WIN32_LEAN_AND_MEAN

#include <d3d9.h>
#include <d3dx9.h>
#include <D3DX9Math.h>


#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")



LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;


float scrWidth = 1024;
float scrHeight = 768;
const int scrDepth = 16;


HINSTANCE g_hInst;
HWND g_hWnd;
HRESULT g_hr;
bool g_minimized = FALSE;



HRESULT InitD3D( HWND hWnd )
{
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
{
return E_FAIL;
}

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

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

return S_OK;
}


VOID Cleanup()
{
if( g_pd3dDevice != NULL)
g_pd3dDevice->Release();

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


HRESULT Render()
{
if( g_pd3dDevice == NULL) return E_FAIL;

g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,255,0), 1.0f, 0 );

if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{

MessageBox(g_hWnd,"dentro Render()","boh",MB_OK);

g_pd3dDevice->EndScene();
}

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

void SetupScreen(float WindowWidth, float WindowHeight)
{
D3DXMATRIX matOrtho;
D3DXMATRIX matIdentity;

D3DXMatrixOrthoLH(&matOrtho, WindowWidth, WindowHeight, 0.0f, 1.0f);
D3DXMatrixIdentity(&matIdentity);

g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matOrtho);
g_pd3dDevice->SetTransform(D3DTS_WORLD, &matIdentity);
g_pd3dDevice->SetTransform(D3DTS_VIEW, &matIdentity);


g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
}



LRESULT CALLBACK WndProc(HWND wpHWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SYSCOMMAND:
{
switch (wParam)
{
case SC_SCREENSAVE:
case SC_MONITORPOWER:
return 0;
}
break;
}
return 0;

case WM_SIZE:
switch (wParam)
{
case SIZE_MINIMIZED:
Cleanup();
ShowCursor ( TRUE);
g_minimized = TRUE;
return 0;

case SIZE_RESTORED:
//InitD3D(g_hWnd);

ShowCursor (FALSE);
g_minimized = FALSE;
return 0;
}
break;

case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;


default:break;
}


return DefWindowProc(wpHWnd, msg, wParam, lParam);

}


int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{

WNDCLASSEX winClass;
MSG msg;

g_hInst=hInstance;


winClass.cbSize = sizeof(WNDCLASSEX);
winClass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winClass.lpfnWndProc = WndProc;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
winClass.hInstance = g_hInst;
winClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.lpszClassName = "DXclass";
winClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if (!RegisterClassEx(&winClass))
{
MessageBox(g_hWnd, "Unable to create window class", "Initialisation Error...", MB_OK);
return 0;
}


g_hWnd = CreateWindowEx(NULL,
"DXclass",
"DX Tutorial",
WS_POPUP,
0, 0,
scrWidth, scrHeight,
NULL,
NULL,
g_hInst,
NULL);

if (!g_hWnd)
{
MessageBox(g_hWnd, "Unable to create window", "Initialisation Error...", MB_OK);
return 0;
}


// InitScene();




if(InitD3D(g_hWnd) == E_FAIL)
{
MessageBox(g_hWnd,"dentro initd3d, errore","boh",MB_OK);
Cleanup();
return 0;
}
ShowCursor (FALSE);

MessageBox(g_hWnd,"dopo initd3d","boh",MB_OK);

SetupScreen(scrWidth, scrHeight);

while(1)
{

while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);

if (msg.message == WM_QUIT)
break;
}

if (msg.message==WM_QUIT)
break;




if ( g_minimized == FALSE)
{
// if ( KeyDown(DIK_ESCAPE) )

// PostQuitMessage(0);


// Update_Fps();

// UpdateKeys();

// DrawScene();


if(Render() == E_FAIL)
{
MessageBox(g_hWnd,"errore di render","boh",MB_OK);
return 0;
}
MessageBox(g_hWnd,"nel loop del peek message","boh",MB_OK);
Cleanup();
return 0;

}
}


Cleanup();

return 0;
}


Thanks
psykr
psykr
It seems that you''re calling CleanUp() once during every iteration of your message loop.
kevlur
kevlur
Yes, its weird, but i put it there because without Cleanup() and return() the program runs infinite and for infinite print to screen MessageBox and its hard to close it, but it doesn''t works either without Cleanup() and return in the message loop, the problem it isn''t this...

Thank you anyhow
kevlur
kevlur
I dont know very well direct3d, maybe im missing some thing somewhere but i dont know, im going mad, please help me
bzroom
bzroom
I think the message box is from one tutorial where you only render it once, the very first tutorials. Then you added loop code. If the messagebox is the only thing wrong with your program (from the 2 sends i glanced at it, itappears u do a message box everyframe.) then simply comment it out.


Sorry if im wrong, i havn''t used dirextx in about 2 years and forget everything.
bzroom
bzroom
Yea, every frame your not only displaying a messagebox, but also cleaning up your directx objects, anddd, returning from your main function which closes your program. Definately get rid of the Cleanup and return lines in your message loop, the problem, is elsewhere.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.