Correct refresh rate under Windows code

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

Post Reply
JohnRus
Engineer
Engineer
Posts: 14
Joined: 06 Dec 2004 23:04
Location: Sankt-Peterburg, Russia
Contact:

Correct refresh rate under Windows code

Post by JohnRus »

This will work really better.

Code: Select all

static void MakeWindow(bool full_screen)
{
	DEVMODE displayMode;
	DWORD dwModeNumber = 0;
	DWORD dwRefreshRate = 0;

	_fullscreen = full_screen;

	_wnd.double_size = _double_size && !full_screen;

	// recreate window?
	if ((full_screen|_wnd.fullscreen) && _wnd.main_wnd) {
		DestroyWindow(_wnd.main_wnd);
		_wnd.main_wnd = 0;
	}

	ZeroMemory(&displayMode, sizeof(DEVMODE));
	while(EnumDisplaySettings(NULL, dwModeNumber++, &displayMode))
	{
		if((displayMode.dmPelsWidth == _wnd.width_org) & (displayMode.dmPelsHeight == _wnd.height_org))
		{
			if(displayMode.dmDisplayFrequency > dwRefreshRate) dwRefreshRate = displayMode.dmDisplayFrequency;
		}
	}

	if(full_screen)
	{
		displayMode.dmSize = sizeof(DEVMODE);
		displayMode.dmPelsWidth = _wnd.width_org;
		displayMode.dmPelsHeight = _wnd.height_org;
		displayMode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
		if(_fullscreen_bpp)
		{
			displayMode.dmBitsPerPel = _fullscreen_bpp;
			displayMode.dmFields |= DM_BITSPERPEL;
		}
		displayMode.dmDisplayFrequency = dwRefreshRate;
		displayMode.dmFields |= DM_DISPLAYFREQUENCY;

		if(ChangeDisplaySettings(&displayMode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
		{
			if(MessageBox(NULL,
				"Failed to create fullscreen window. Ignore this?", "ERROR", MB_ICONERROR|MB_YESNO)!=IDYES)
			{
				MakeWindow(false);
				return;
			}
		}
	} 
	else if(_wnd.fullscreen)
	{
		// restore display?
		ChangeDisplaySettings(NULL, 0);
	}

	{
		RECT r;
		uint style;
		int x, y, w, h;

		if ((_wnd.fullscreen=full_screen) != false) {
			style = WS_POPUP | WS_VISIBLE;
			SetRect(&r, 0, 0, _wnd.width_org, _wnd.height_org);
		} else {
			style = WS_OVERLAPPEDWINDOW | WS_VISIBLE;
			SetRect(&r, 0, 0, _wnd.width, _wnd.height);
		}

		AdjustWindowRect(&r, style, FALSE);
		w = r.right - r.left;
		h = r.bottom - r.top;
		x = (GetSystemMetrics(SM_CXSCREEN)-w)>>1;
		y = (GetSystemMetrics(SM_CYSCREEN)-h)>>1;

		if (_wnd.main_wnd) {
			SetWindowPos(_wnd.main_wnd, 0, x, y, w, h, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER);
		} else {
			char Windowtitle[50] = "OpenTTD ";
#ifdef WITH_REV_HACK
			// also show revision number/release in window title
			extern const char _openttd_revision[];
			strncat(Windowtitle, _openttd_revision, sizeof(Windowtitle)-(strlen(Windowtitle) + 1));
#endif
			_wnd.main_wnd = CreateWindow("TTD", Windowtitle, style, x, y, w, h, 0, 0, _inst, 0);
			if (_wnd.main_wnd == NULL)
				error("CreateWindow failed");
		}
	}
	GameSizeChanged(); // invalidate all windows, force redraw
}

PS. Hello all. This is my first post in open ttd. I make many changes to code, especially for better performance, and for better work under win32 platform. And, for sample, i make c++ port of openttd, this is better actually, i think.
I realy want to join open ttd developes. How can i do this?)
Thank.
Bjarni
Tycoon
Tycoon
Posts: 2088
Joined: 08 Mar 2004 13:10

Post by Bjarni »

join the IRC channel on freenode.net called #openttd
Usually we get diff files and they are easier to work with, since they can be applied with a single command, while your code needs to be typed in manually :wink:
User avatar
Darkvater
Tycoon
Tycoon
Posts: 3053
Joined: 24 Feb 2003 18:45
Location: Hong Kong

Post by Darkvater »

I can only second Bjarni here.

If you are unfamiliar to IRC, you can look at http://www.mirc.com/irc.html to see how it works. Download mIRC and join our irc channel:
1. server: irc.freenode.net
2. channel: #openttd

(you basically type in the first window "/server irc.freenode.net" then after you have connected you doe "/join #openttd"; we can help you from there).

Altough I am the windows developer it seems my windows code knowledge far surpresses yours because I don't know what the code you wrote here does :) .
Just as bjarni said, diff files would be much better. You can download any SVN client for windows (I suggest TortoiseSVN), with which you can always get the latest source-code (if you are unfamiliar with it, we'll help you on IRC). Then you just change the code there; select 'create patch' and TortoiseSVN will create a diff file (that is a file with only the lines changed) which we can very easily apply.

Looking forward to seeing you around! :D
TrueLight: "Did you bother to read any of the replies, or you just pressed 'Reply' and started typing?"
<@[R-Dk]FoRbiDDeN> "HELP, this litte arrow thing keeps following my mouse, and I can't make it go away."
User avatar
Darkvater
Tycoon
Tycoon
Posts: 3053
Joined: 24 Feb 2003 18:45
Location: Hong Kong

Post by Darkvater »

Johnrus; if you are reading the thread. Can you tell me what has changed internally and why it is better/faster?
TrueLight: "Did you bother to read any of the replies, or you just pressed 'Reply' and started typing?"
<@[R-Dk]FoRbiDDeN> "HELP, this litte arrow thing keeps following my mouse, and I can't make it go away."
JohnRus
Engineer
Engineer
Posts: 14
Joined: 06 Dec 2004 23:04
Location: Sankt-Peterburg, Russia
Contact:

Post by JohnRus »

Hello Darkvater.

Thx for answers for my question. I will try using subversion and irc.

Now about code. There is one of my changes.
This is not a "fastest" code. This is one of nessesary changes. Code enumerating acceptable video refresh rates and then try set maximum available of monitors supported refresh rate.

When this is need: Allready. Else lowest monitor refresh rate selected.

When this work fine: allready when monitor driver is correctly configured.

When this work bad (may raise crush): when monitor driver isn't configured correctly. (For sample oldest 14'' monitors which not support info signal from videocard.)

But i think finally we must add refresh rate selection in gui menu.
JohnRus
Engineer
Engineer
Posts: 14
Joined: 06 Dec 2004 23:04
Location: Sankt-Peterburg, Russia
Contact:

Post by JohnRus »

ps.
For sample, we should add this handler into WndProcGdi function:


Code: Select all

static LRESULT CALLBACK WndProcGdi(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg){

....................
....................


	case WM_ACTIVATEAPP:
		_wnd.has_focus = (bool)wParam;
		break;

	case WM_ACTIVATE:
		{
			bool bApplicationActive = LOWORD(wParam);

			if(!bApplicationActive)
			{
				ChangeDisplaySettings(NULL, 0);
			}

			break;
		}

........................
.......................

this will fix the bug, when screen not corectly redrawing when openttd minimazing
User avatar
Darkvater
Tycoon
Tycoon
Posts: 3053
Joined: 24 Feb 2003 18:45
Location: Hong Kong

Post by Darkvater »

I've tried merging your patch; I think I did it ok. Look in the attachment below...

There is one issue why I cannot accept it yet and that is if I am in fullscreen then ALT+TAB out, and back in, the game doesn't switch back resolution; but shows me the "fullscreen" window in a window.

Look below or look in http://darkvater.no-ip.com/ottd/patches/win32.patch
Attachments
win32.patch
(3.12 KiB) Downloaded 474 times
TrueLight: "Did you bother to read any of the replies, or you just pressed 'Reply' and started typing?"
<@[R-Dk]FoRbiDDeN> "HELP, this litte arrow thing keeps following my mouse, and I can't make it go away."
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 7 guests