Jump to content
  • Advertisement
Sign in to follow this  
Samsonite

Making DX window stay for a while.

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

Hello! I need some help on making a window stay up for a while. Here is my code, you can see that i tried using 'system("PAUSE");' for the job.
 #include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <d3d9.h>

using namespace std;

LPDIRECT3D9 pD3D;
LPDIRECT3DDEVICE9 pd3dDevice;
HINSTANCE hInst;
HWND wndHandle;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

bool initWindow( HINSTANCE hInstance )
{
	WNDCLASSEX wcex;

	wcex.cbSize			=sizeof( WNDCLASSEX );
	wcex.style			=CS_HREDRAW| CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra     = 0;
	wcex.cbWndExtra     = 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= 0;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW);
	wcex.lpszMenuName   = NULL;
	wcex.lpszClassName  = "DirectXExample";
	wcex.hIconSm		= 0;
	RegisterClassEx(&wcex);

	wndHandle = CreateWindow("DirectXExample", "DirectXExample",WS_EX_TOPMOST|WS_POPUP|WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL);
	if(!wndHandle)
		return false;

	ShowWindow(wndHandle, SW_SHOW);
	UpdateWindow(wndHandle);
	return true;
}

bool initDirect3D(void)
{
	pD3D = NULL;
	pd3dDevice = NULL;

	if(NULL == ( pD3D = Direct3DCreate9(D3D_SDK_VERSION) ) )
	{
		return false;
	}

	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory( &d3dpp, sizeof (d3dpp));
	d3dpp.Windowed = FALSE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferCount = 1;
	d3dpp.BackBufferHeight = 480;
	d3dpp.BackBufferWidth = 640;
	d3dpp.hDeviceWindow = wndHandle;

	if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, wndHandle, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice)))
	{
		return false;
	}
	return true;
}

void render(void)
{
	if(NULL==pd3dDevice )
		return;
	pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0, 255), 1.0f, 0);

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

void cleanUp(void)
{
	if(pd3dDevice != NULL)
		pd3dDevice->Release( );

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



LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	if (!initWindow(hInstance) )
		return false;
	if(!initDirect3D( ) )
		return false;


	MSG msg;

	ZeroMemory( &msg, sizeof(msg ) );
	while( msg.message!=WM_QUIT)
	{
		if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE ) )
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			render();
			system("PAUSE");
		}
	}
	cleanUp();
	return (int) msg.wParam;
}




I think it's lazy of the authors of tutorials not to include such a trivial question as to how you can acctually see your great new DX window

Share this post


Link to post
Share on other sites
Advertisement
You could put a Sleep(50); in place of your System("PAUSE"); command if you want to throttle the performance...

But from looking at the code you posted I don't really see what your problem is [oh] - it's got a fairly standard message pump that looks like it'll only terminate when you tell it to (e.g. closing the window)... other than that it'll just spin round in a loop as fast as it can [grin]

Is your window starting up and then closing (that is, the application also ends) straight away? if so, that might be an error in the code...

hth
Jack

Share this post


Link to post
Share on other sites
Yeah, the window terminates so fast i can't even take a screenshot. And yes, my apllication is also terminated

EDIT: Sleep(50); didn't do it, the same thing happens!

Share this post


Link to post
Share on other sites
if (!initWindow(hInstance) )
return false;
if(!initDirect3D( ) )
return false;
maybe one of those is returning false?

also you shouldnt use
if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

as that would only dispatch one message per frame, where you could recieve many more than that. you should use something more like this:

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

Share this post


Link to post
Share on other sites
Quote:
Original post by Samsonite
EDIT: Sleep(50); didn't do it, the same thing happens!


that is 50 milliseconds, so not strange, try 60000 instead and you should have 1minute to see your window.

But really, that one should not exit, you have your message pump, and I as far as I can see there is nothing telling the window to close... so something else must be wrong.

EDIT: but take a look at the previous post

Share this post


Link to post
Share on other sites
Quote:
Original post by Samsonite
Why wouldn't D3D initialize? I think it's the problem, because the Sleep(); function didn't work.

From everything I've seen of your code in this thread, I'd say it's an error causing your app to die - rather than just a simple logic/code error that makes it close too quickly.

You could do some verbose return-code checking to see which function (and hopefully why) is failing...

Quote:
Original post by Samsonite
EDIT: Arg! It might be because my SDK is shitty and won't install the latest version...

This would probably be a logical place to start from [smile] - get your SDK sorted out (seems that Dustin posted a (possible) solution in that thread) and try it again.

hth
Jack

Share this post


Link to post
Share on other sites
One thing that I noticed is that you're supplying an EX window style to the regular window style. To do this you need to call CreateWindowEx():

wndHandle = CreateWindow(WS_EX_TOPMOST, "DirectXExample", "DirectXExample", WS_POPUP | WS_VISIBLE, 0, 0, 640, 480, 0, 0, GetModuleHandle(0), 0);





Edit: Also, can I ask why you're creating a REF device instead of HAL? REF will be extremely slow.

Share this post


Link to post
Share on other sites
Ah, i know that REF is software rendering, and therefore much slower. But in the book i'm currently reading it uses REL. I have stayed away from changing it in fear for some problems later.

But thanks for the help!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!