Use configured resolution for login/outgame/ingame
[ryzomcore.git] / ryzom / tools / client / client_config / display_dlg.cpp
blob3e9cb21a4c9fab20edb59d70efecd3d001544231
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2010 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 // ***************************************************************************
21 // display_dlg.cpp : implementation file
22 // ***************************************************************************
24 #include "stdafx.h"
25 #include "client_config.h"
26 #include "display_dlg.h"
27 #include "cfg_file.h"
29 using namespace std;
30 using namespace NLMISC;
31 using namespace NL3D;
33 // ***************************************************************************
35 std::vector<string> GLExtensions;
36 std::string GLRenderer;
37 std::string GLVendor;
38 std::string GLVersion;
39 std::string D3DDescription;
40 std::string D3DDeviceName;
41 std::string D3DDriver;
42 std::string D3DDriverVersion;
43 std::string D3DVendor;
45 uint VideoMemory;
46 uint HardwareSoundBuffer;
47 uint64 SystemMemory;
48 uint CPUFrequency;
50 bool GetGLInformation ()
52 // *** INIT VARIABLES
54 GLExtensions.clear ();
55 GLRenderer = "";
56 GLVendor = "";
57 GLVersion = "";
59 // *** INIT OPENGL
61 // Register a window class
62 WNDCLASS wc;
63 memset(&wc,0,sizeof(wc));
64 wc.style = CS_HREDRAW | CS_VREDRAW ;//| CS_DBLCLKS;
65 wc.lpfnWndProc = DefWindowProc;
66 wc.cbClsExtra = 0;
67 wc.cbWndExtra = 0;
68 wc.hInstance = GetModuleHandle(NULL);
69 wc.hIcon = (HICON)NULL;
70 wc.hCursor = LoadCursor(NULL,IDC_ARROW);
71 wc.hbrBackground = WHITE_BRUSH;
72 wc.lpszClassName = "RyzomGetGlInformation";
73 wc.lpszMenuName = NULL;
74 if ( !RegisterClass(&wc) )
75 return false;
77 // Create a window
78 ULONG WndFlags = WS_OVERLAPPEDWINDOW+WS_CLIPCHILDREN+WS_CLIPSIBLINGS;
79 RECT WndRect;
80 WndRect.left=0;
81 WndRect.top=0;
82 WndRect.right=100;
83 WndRect.bottom=100;
84 HWND hWnd = CreateWindow ( "RyzomGetGlInformation",
85 "",
86 WndFlags,
87 CW_USEDEFAULT,CW_USEDEFAULT,
88 WndRect.right,WndRect.bottom,
89 NULL,
90 NULL,
91 GetModuleHandle(NULL),
92 NULL);
93 if (!hWnd)
94 return false;
96 HDC hDC = GetDC(hWnd);
98 // Remove current gl context
99 wglMakeCurrent (hDC, NULL);
101 // Select pixel format
102 int depth = GetDeviceCaps (hDC, BITSPIXEL);
103 PIXELFORMATDESCRIPTOR pfd;
104 memset(&pfd,0,sizeof(pfd));
105 pfd.nSize = sizeof(pfd);
106 pfd.nVersion = 1;
107 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
108 pfd.iPixelType = PFD_TYPE_RGBA;
109 pfd.cColorBits = (char)depth;
110 if(depth<=16)
111 pfd.cDepthBits = 16;
112 else
114 pfd.cDepthBits = 24;
115 pfd.cAlphaBits = 8;
117 pfd.iLayerType = PFD_MAIN_PLANE;
118 int pf=ChoosePixelFormat(hDC, &pfd);
119 if (!pf)
120 return false;
121 if ( !SetPixelFormat(hDC, pf, &pfd) )
122 return false;
124 // Create final context
125 HGLRC hRC = wglCreateContext (hDC);
126 wglMakeCurrent(hDC, hRC);
128 // *** GET INFORMATION
130 GLVendor = (const char *) glGetString (GL_VENDOR);
131 GLRenderer = (const char *) glGetString (GL_RENDERER);
132 GLVersion = (const char *) glGetString (GL_VERSION);
134 // Get extension string
135 string glext = (char*)glGetString(GL_EXTENSIONS);
136 const char *token = strtok ( (char*)glext.c_str (), " ");
137 while( token != NULL )
139 GLExtensions.push_back (token);
140 token = strtok( NULL, " ");
143 // Get proc adress
144 typedef const char* (APIENTRY * PFNWGFGETEXTENSIONSSTRINGARB) (HDC);
145 PFNWGFGETEXTENSIONSSTRINGARB wglGetExtensionsStringARB;
146 if ((wglGetExtensionsStringARB=(PFNWGFGETEXTENSIONSSTRINGARB)wglGetProcAddress("wglGetExtensionsStringARB")))
148 // Get extension string
149 glext = wglGetExtensionsStringARB (hDC);
150 token = strtok ( (char*)glext.c_str (), " ");
151 while( token != NULL )
153 GLExtensions.push_back (token);
154 token = strtok( NULL, " ");
158 // *** RELEASE OPENGL
160 if (hRC)
161 wglDeleteContext(hRC);
162 if (hWnd&&hDC)
164 ReleaseDC (hWnd,hDC);
165 DestroyWindow (hWnd);
168 // Done
169 return true;
172 // ***************************************************************************
174 bool GetD3DInformation (NL3D::IDriver *d3dDriver)
176 IDriver::CAdapter desc;
177 if (d3dDriver->getAdapter (0xffffffff, desc))
179 D3DDescription = desc.Description;
180 D3DDeviceName = desc.DeviceName;
181 D3DDriver = desc.Driver;
182 D3DDriverVersion = toString ((uint16)(desc.DriverVersion>>48))+"."+
183 toString ((uint16)(desc.DriverVersion>>32))+"."+
184 toString ((uint16)(desc.DriverVersion>>16))+"."+
185 toString ((uint16)(desc.DriverVersion&0xffff));
186 D3DVendor = desc.Vendor;
187 return true;
189 return false;
192 // ***************************************************************************
194 bool GetVideoMemory ()
196 VideoMemory = 0;
197 bool ret = false;
199 // Initialise Direct Draw
200 IDirectDraw *dd;
201 HRESULT result = DirectDrawCreate(NULL, &dd, NULL);
202 if (result == DD_OK)
204 DDCAPS caps;
205 memset (&caps, 0, sizeof(DDCAPS));
206 caps.dwSize = sizeof(DDCAPS);
207 dd->GetCaps (&caps, NULL);
208 VideoMemory = caps.dwVidMemTotal;
209 ret = true;
210 dd->Release ();
213 // Can't get it
214 return ret;
217 // ***************************************************************************
219 bool GetHardwareSoundBuffer ()
221 bool ret = false;
222 HardwareSoundBuffer = 0;
224 // The DirectSound object
225 #if (DIRECTSOUND_VERSION >= 0x0800)
226 LPDIRECTSOUND8 _DirectSound;
227 #else
228 LPDIRECTSOUND _DirectSound;
229 #endif
231 #if (DIRECTSOUND_VERSION >= 0x0800)
232 if (DirectSoundCreate8(NULL, &_DirectSound, NULL) == DS_OK)
233 #else
234 if (DirectSoundCreate(NULL, &_DirectSound, NULL) == DS_OK)
235 #endif
237 DSCAPS caps;
238 memset (&caps, 0, sizeof (DSCAPS));
239 caps.dwSize = sizeof (DSCAPS);
241 HRESULT result = _DirectSound->GetCaps (&caps);
242 if (result == DS_OK)
244 HardwareSoundBuffer = caps.dwFreeHw3DStaticBuffers;
245 ret = true;
247 _DirectSound->Release ();
249 return ret;
252 // ***************************************************************************
254 bool GetSystemInformation (IDriver *d3dDriver)
256 bool result = GetGLInformation ();
257 result |= GetD3DInformation (d3dDriver);
258 result |= GetVideoMemory ();
259 result |= GetHardwareSoundBuffer ();
260 SystemMemory = CSystemInfo::totalPhysicalMemory ();
261 CPUFrequency = (uint)(CSystemInfo::getProcessorFrequency () / (uint64)1000000);
262 return result;
264 // ***************************************************************************
266 std::vector<CVideoMode> VideoModes[2];
268 // ***************************************************************************
270 void RegisterVideoModes (uint driverId, IDriver *driver)
272 vector<GfxMode> modes;
273 driver->getModes (modes);
275 uint i;
276 VideoModes[driverId].clear();
277 for (i=0; i<modes.size(); i++)
279 // Keep only 32 bits
280 if ((modes[i].Depth == 32) && (modes[i].Width >= 800) && (modes[i].Width >= 600))
282 // Add this mode
283 CVideoMode mode;
284 mode.Width = modes[i].Width;
285 mode.Height = modes[i].Height;
286 mode.ColorDepth = modes[i].Depth;
287 mode.Frequency = modes[i].Frequency;
288 VideoModes[driverId].push_back (mode);
293 // ***************************************************************************
294 // CDisplayDlg dialog
295 // ***************************************************************************
297 CDisplayDlg::CDisplayDlg(CWnd* pParent /*=NULL*/)
298 : CBaseDialog(CDisplayDlg::IDD, pParent)
300 //{{AFX_DATA_INIT(CDisplayDlg)
301 DriverChoiceMode = DrvChooseUnknwown;
302 Windowed = -1;
303 Width = 0;
304 Height = 0;
305 Mode = -1;
306 PositionX = 0;
307 PositionY = 0;
308 //}}AFX_DATA_INIT
311 // ***************************************************************************
313 void CDisplayDlg::DoDataExchange(CDataExchange* pDX)
315 CDialog::DoDataExchange(pDX);
316 //{{AFX_DATA_MAP(CDisplayDlg)
317 DDX_Control(pDX, IDC_DISPLAY_FS_TEXT0, TextFS0);
318 DDX_Control(pDX, IDC_DISPLAY_WND_TEXT0, TextWnd0);
319 DDX_Control(pDX, IDC_DISPLAY_WND_TEXT1, TextWnd1);
320 DDX_Control(pDX, IDC_DISPLAY_WND_TEXT2, TextWnd2);
321 DDX_Control(pDX, IDC_DISPLAY_WND_TEXT3, TextWnd3);
322 DDX_Control(pDX, IDC_POSITION_Y, PositionYCtrl);
323 DDX_Control(pDX, IDC_POSITION_X, PositionXCtrl);
324 DDX_Control(pDX, IDC_HEIGHT, HeightCtrl);
325 DDX_Control(pDX, IDC_WIDTH, WidthCtrl);
326 DDX_Control(pDX, IDC_MODE, ModeCtrl);
327 DDX_Radio(pDX, IDC_DRV3D_AUTO, DriverChoiceMode);
328 DDX_Radio(pDX, IDC_FULLSCREEN, Windowed);
329 DDX_Text(pDX, IDC_WIDTH, Width);
330 DDX_Text(pDX, IDC_HEIGHT, Height);
331 DDX_CBIndex(pDX, IDC_MODE, Mode);
332 DDX_Text(pDX, IDC_POSITION_X, PositionX);
333 DDX_Text(pDX, IDC_POSITION_Y, PositionY);
334 //}}AFX_DATA_MAP
337 // ***************************************************************************
339 BEGIN_MESSAGE_MAP(CDisplayDlg, CDialog)
340 //{{AFX_MSG_MAP(CDisplayDlg)
341 ON_BN_CLICKED(IDC_FULLSCREEN, OnFullscreen)
342 ON_BN_CLICKED(IDC_WINDOW, OnWindow)
343 ON_BN_CLICKED(IDC_DIRECT3D, OnDirect3d)
344 ON_BN_CLICKED(IDC_DRV3D_AUTO, OnDrv3DAuto)
345 ON_EN_CHANGE(IDC_HEIGHT, OnChangeHeight)
346 ON_BN_CLICKED(IDC_OPENGL, OnOpengl)
347 ON_EN_CHANGE(IDC_POSITION_X, OnChangePositionX)
348 ON_EN_CHANGE(IDC_WIDTH, OnChangeWidth)
349 ON_EN_CHANGE(IDC_POSITION_Y, OnChangePositionY)
350 ON_CBN_SELCHANGE(IDC_MODE, OnSelchangeMode)
351 //}}AFX_MSG_MAP
352 END_MESSAGE_MAP()
354 // ***************************************************************************
355 // CDisplayDlg message handlers
356 // ***************************************************************************
358 void CDisplayDlg::OnFullscreen()
360 UpdateData (TRUE);
361 updateState ();
362 UpdateData (FALSE);
363 InvalidateConfig ();
366 // ***************************************************************************
368 void CDisplayDlg::OnWindow()
370 UpdateData (TRUE);
371 updateState ();
372 UpdateData (FALSE);
373 InvalidateConfig ();
376 // ***************************************************************************
378 void CDisplayDlg::updateState ()
380 ModeCtrl.EnableWindow (Windowed == 0);
381 TextFS0.EnableWindow (Windowed == 0);
382 WidthCtrl.EnableWindow (Windowed == 1);
383 HeightCtrl.EnableWindow (Windowed == 1);
384 PositionXCtrl.EnableWindow (Windowed == 1);
385 PositionYCtrl.EnableWindow (Windowed == 1);
386 TextWnd0.EnableWindow (Windowed == 1);
387 TextWnd1.EnableWindow (Windowed == 1);
388 TextWnd2.EnableWindow (Windowed == 1);
389 TextWnd3.EnableWindow (Windowed == 1);
391 // Fill the combobox values
392 ModeCtrl.ResetContent ();
393 uint i;
394 if (DriverChoiceMode != DrvChooseUnknwown)
396 TDriver actualDriver = getActualDriver();
397 for (i=0; i<VideoModes[actualDriver].size (); i++)
399 ucstring videoMode = toString (VideoModes[actualDriver][i].Width) + "x" + toString (VideoModes[actualDriver][i].Height) +
400 " " + toString (VideoModes[actualDriver][i].ColorDepth) + " " + CI18N::get ("uiConfigBits") + " " +
401 toString (VideoModes[actualDriver][i].Frequency) + " " + CI18N::get ("uiConfigHz");
403 // hulud : Don't know how to set a wide string in a combo box...
404 ModeCtrl.SendMessage (CB_INSERTSTRING, -1, (LPARAM)(videoMode.toString().c_str()));
408 // Select the string
409 ModeCtrl.SetCurSel (Mode);
412 // ***************************************************************************
414 BOOL CDisplayDlg::OnInitDialog()
416 CDialog::OnInitDialog();
418 updateState ();
420 return TRUE; // return TRUE unless you set the focus to a control
421 // EXCEPTION: OCX Property Pages should return FALSE
424 // ***************************************************************************
426 void CDisplayDlg::OnDirect3d()
428 UpdateData (TRUE);
429 updateState ();
430 UpdateData (FALSE);
431 InvalidateConfig ();
434 // ***************************************************************************
435 void CDisplayDlg::OnDrv3DAuto()
437 UpdateData (TRUE);
438 updateState ();
439 UpdateData (FALSE);
440 InvalidateConfig ();
443 // ***************************************************************************
445 void CDisplayDlg::OnChangeHeight()
447 InvalidateConfig ();
450 // ***************************************************************************
452 void CDisplayDlg::OnOpengl()
454 UpdateData (TRUE);
455 updateState ();
456 UpdateData (FALSE);
457 InvalidateConfig ();
460 // ***************************************************************************
462 void CDisplayDlg::OnChangePositionX()
464 InvalidateConfig ();
467 // ***************************************************************************
469 void CDisplayDlg::OnChangeWidth()
471 InvalidateConfig ();
474 // ***************************************************************************
476 void CDisplayDlg::OnChangePositionY()
478 InvalidateConfig ();
481 // ***************************************************************************
483 void CDisplayDlg::OnSelchangeMode()
485 InvalidateConfig ();
488 // ***************************************************************************
489 CDisplayDlg::TDriver CDisplayDlg::getActualDriver() const
491 switch(DriverChoiceMode)
493 case DrvChooseOpenGL: return OpenGL;
494 case DrvChooseDirect3D: return Direct3D;
495 default:
497 // D3D better on ATI Radeon cards
498 std::string deviceName;
499 uint64 drvVersion;
500 CSystemInfo::getVideoInfo(deviceName, drvVersion);
501 return strstr(toLower(deviceName).c_str(), "radeon") != NULL ? Direct3D : OpenGL;
504 return OpenGL;