Jump to content
  • Advertisement
Sign in to follow this  
23yrold3yrold

(Win32) Correct window so it doesn't run so slow

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

Okay, I'm trying to make a little program which goes fullscreen on the second monitor and has a dialog box for controlling it on the main desktop. This works fine, but the way I'm creating the window makes it run bloody slow. I assume it must be how I'm creating the window, because if I make a straight-up Allegro program and drag the window to the second monitor, it runs fine. And I know the way I'm making it fullscreen is a cheap hack (just a second-monitor-sized pop-up) since I couldn't find a "normal" way to do it. Is there a normal way that would work better or is it just something about Win32 I can't get around or do I need to finally pick up some DirectX or what? Hacked up code example of how I'm doing it:
const int buffersize = 10240;

HDC		hDC = NULL;
HGLRC		hRC = NULL;
HWND		hWnd = NULL, dialog = NULL;
HINSTANCE	hInstance;

BITMAP  *buffer;

BOOL	done = FALSE;   // Bool Variable To Exit Loop

LRESULT CALLBACK WndProc(HWND	hWnd, UINT	uMsg, WPARAM	wParam, LPARAM	lParam)
{
	HDC         hDc;
	PAINTSTRUCT ps;
	
	switch (uMsg)
	{
		case WM_CREATE:
		return 0;
		
		case WM_PAINT:
		{
			hDc = BeginPaint(hWnd, &ps);
			
			clear_to_color(buffer, makecol(200, 200, 200));
			
			draw_to_hdc(hDc, buffer, 0, 0);
			EndPaint(hWnd, &ps);
			return 0;
		}

		case WM_CLOSE:
		{
			PostQuitMessage(0);
			return 0;
		}
	}

	return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

BOOL CALLBACK ControlDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	// static HWND checks[5], texts[5], buttons[3];
	static HWND	groupbox;
	dialog = hDlg;
	
	
	switch (message)
	{
		case WM_INITDIALOG:
		{
			HINSTANCE hInstance = (HINSTANCE) GetWindowLong(hDlg, GWL_HINSTANCE);	
			groupbox = CreateWindow(TEXT("button"), TEXT("this is my text lawls!"), 
			WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 10, 10, 250, 180, hDlg, (HMENU) 9, hInstance, NULL);
		}
		return TRUE;
          
		case WM_COMMAND:
			switch (LOWORD (wParam))
			{
				case IDAPPLY:
				{
					char textbuffer[255];
					int numcount = 0;
					
					for(int i = 0 ; i < 4 ; ++i)
					{
						GetWindowText(GetDlgItem(hDlg, IDC_EDITNAME1 + i), textbuffer,  255);
						GetWindowText(GetDlgItem(hDlg, IDC_EDITSCORE1 + i), textbuffer,  255);
						
						if((bool) SendMessage(GetDlgItem(hDlg, IDC_CHECK1 + i), BM_GETCHECK, 0, 0))
						{
							++numcount;
						}
					}
					
					InvalidateRect(hWnd, NULL, true);
					UpdateWindow(hWnd);
				}
				return false;
				
				case ID_HELP_DOCUMENTATION:
				{
					// DestroyWindow(groupbox);
				}
				return false;
				
				case IDEXIT:
					EndDialog(hDlg, 0);
					done = true;
				return true;
			}
		break;
		
		case WM_DESTROY:
		case WM_CLOSE:
			EndDialog(hDlg, 0);
			done = true;
		return 0;
	}

	return FALSE;
}

int WINAPI WinMain(	HINSTANCE	hInstance,
					HINSTANCE	hPrevInstance,
					LPSTR		lpCmdLine,
					int			nCmdShow)
{
	WNDCLASS	wc;
	
	install_allegro(SYSTEM_NONE, &errno, atexit);
	set_color_depth(32);
	
	buffer  = create_bitmap(800, 600);
	
	wc.style		= CS_OWNDC;
	wc.lpfnWndProc		= (WNDPROC) WndProc;
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hInstance		= hInstance;
	wc.hIcon		= LoadIcon(NULL, IDI_WINLOGO);
	wc.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground	= NULL;
	wc.lpszMenuName		= NULL;
	wc.lpszClassName	= "Main Window Class";

	if (!RegisterClass(&wc))
	{
		MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;
	}

	MSG		msg;
	
	hWnd = CreateWindowEx(WS_EX_APPWINDOW,
				"Main Window Class"
				"",
				WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
				1152, 0,
				800,
				600,
				NULL,
				NULL,
				hInstance,
				NULL);

	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);
	
	DialogBox(hInstance, TEXT ("ControlBox"), hWnd, ControlDlgProc);

	while(!done)
	{
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if (msg.message == WM_QUIT)Message?
			{
				done=TRUE;
			}
			else
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
	}
	
	destroy_bitmap(buffer);
	
	// Shutdown
	return (msg.wParam);
}




What am I doing wrong/not so right?

Share this post


Link to post
Share on other sites
Advertisement
If you create a window on the second monitor and then call ShowWindow() with SW_MAXIMIZE it should maximize your window on that monitor. That said, it's very strange that simply making a large window would cause a performance decrease.

Share this post


Link to post
Share on other sites
You have a 'busy loop' in your WinMain function:

while(!done)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// code removed
}
}

The above loop will be constantly calling PeekMessage, ramping the CPU usage up to 100% (or 50% on dual-core, 25% on quad, etc). Using the GetMessage function will suspend the program until there is a message to be processed.

You probably copied the above code from some game code. Usually games have a loop like:

while (!done)
{
while (PeekMessage (...))
{
Process message
}
Update and render game state
}

which has an update and render of the game each time through the loop.

Skizz

Share this post


Link to post
Share on other sites
Well the program is very game-like, so it might be appropriate. I'll see if that's the problem next time I take a look at the code though; thanks for the idea.

Share this post


Link to post
Share on other sites
If you make a non-maximised window, and it's one pixel too wide for the monitor, or one pixel off from the top left, it'll extend onto the second monitor slightly, which really upsets the graphics card for some reason. That might also be causing issues.

Share this post


Link to post
Share on other sites
I'm setting it to exactly the dimensions of the second monitor, so I'll check that too but I think it unlikely. I also think that first idea was unlikely in hindsight, partly because 100% CPU usage shouldn't slow it down to the degree I'm seeing (and because of animations I need it running anyway) but mostly because the Allegro program runs at 100% CPU as well and it's fine.

If anyone's wondering why I don't just use the pure Allegro program, a) it doesn't have a spiffy dialog box control, and b) it has a huge ugly blue window frame.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!