-
Notifications
You must be signed in to change notification settings - Fork 2
/
DisplaySettingsDlg.cpp
293 lines (234 loc) · 8.09 KB
/
DisplaySettingsDlg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/****************************************
* Nehe MFC Display Settings *
* by Yvo van Dillen *
* 2001/2004 *
* *
*****************************************/
#include "stdafx.h"
#include "nehemfc.h"
#include "DisplaySettingsDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
typedef struct //our struct for the available modes
{ //width/height/bits per pixel
UINT dwWidth,
dwHeight,
dwBpp;
}TDisplayMode;
/////////////////////////////////////////////////////////////////////////////
// CDisplaySettingsDlg dialog
CDisplaySettingsDlg::CDisplaySettingsDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDisplaySettingsDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDisplaySettingsDlg)
m_bDontAskAgain = FALSE;
m_bFullscreen = FALSE;
//}}AFX_DATA_INIT
}
CDisplaySettingsDlg::~CDisplaySettingsDlg()
{
TDisplayMode *mode;
for(int i=0;i<m_DisplayModes.GetSize();i++ ) //destroy our array of modes
{
mode = (TDisplayMode*)m_DisplayModes[i];
delete mode;
}
m_DisplayModes.RemoveAll();
}
void CDisplaySettingsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDisplaySettingsDlg)
DDX_Control(pDX, IDC_RESOLUTIONS, m_cboResolutions);
DDX_Check(pDX, IDC_DONTASKAGAIN, m_bDontAskAgain);
DDX_Check(pDX, IDC_FULLSCREEN, m_bFullscreen);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDisplaySettingsDlg, CDialog)
//{{AFX_MSG_MAP(CDisplaySettingsDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDisplaySettingsDlg message handlers
// Function name : CDisplaySettingsDlg::OnOK
// Description : the OK button function
// Return type : void
void CDisplaySettingsDlg::OnOK()
{
TDisplayMode *mode = NULL;
UpdateData(); // retreive vars
SaveSettings(); // save vars (ok is pressed)
int sel = m_cboResolutions.GetCurSel(); // get the current selection from the combobox
if( sel >= 0 )
{
mode = (TDisplayMode *)m_cboResolutions.GetItemData(sel); // get the mode stored in the itemdata (cast)
}
if( mode != NULL ) // not null ? (correct selection)
{
theApp.m_Width = mode->dwWidth; //pass vars to main app
theApp.m_Height = mode->dwHeight;
theApp.m_bitsPerPixel = mode->dwBpp ;
theApp.m_isFullScreen = m_bFullscreen;
CDialog::OnOK();
}
}
// Function name : CDisplaySettingsDlg::OnInitDialog
// Description : initialization of the dialog
// Return type : BOOL
BOOL CDisplaySettingsDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_strIniFile = __argv[0]; // get application name
if( m_strIniFile.ReverseFind('.') >= 0 ) // strip the extenstion
{
m_strIniFile = m_strIniFile.Left( m_strIniFile.ReverseFind('.') );
}
m_strIniFile += ".ini"; // and paste '.ini'
EnumDisplays(); // enumerate the display modes
FillCombo(); // fill the combo with the fetched modes
LoadSettings(); // load the settings
UpdateData(FALSE); // update vars with the loaded settings
if( m_bDontAskAgain ) // did the user pressed 'don't ask again' the last time ?
{
OnOK(); // yup.. bail
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
// Function name : CDisplaySettingsDlg::ModeExists
// Description : checks if the mode already exists in our array
//
// this is function is added because some nVidia-cards (GF2)
// give back 8 of the same resolutions (yup... it's strange)
//
// Return type : BOOL
// Argument : DEVMODE *dm
BOOL CDisplaySettingsDlg::ModeExists(DEVMODE *dm)
{
TDisplayMode *mode;
for(int i=0;i<m_DisplayModes.GetSize();i++ )
{
mode = (TDisplayMode*)m_DisplayModes[i];
if( mode->dwBpp == dm->dmBitsPerPel &&
mode->dwWidth == dm->dmPelsWidth &&
mode->dwHeight == dm->dmPelsHeight )
return TRUE;
}
return FALSE;
}
// Function name : CDisplaySettingsDlg::EnumDisplays
// Description : loop all displaymodes
// Return type : void
void CDisplaySettingsDlg::EnumDisplays()
{
DWORD dwMode=0;
DEVMODE dm;
TDisplayMode *mode;
while( ::EnumDisplaySettings( NULL , dwMode , &dm ) ) //loop modes
{
if( !ModeExists( &dm ) ) // are we missing this mode ?
{
if( dm.dmBitsPerPel == 16 || // 16 or 32 bits ?
dm.dmBitsPerPel == 32 )
{
if( dm.dmPelsWidth >= 640 ) // resolution >= 640 x 480 ?
{
mode = new TDisplayMode; // add it...
if( mode )
{
mode->dwWidth = dm.dmPelsWidth;
mode->dwHeight = dm.dmPelsHeight;
mode->dwBpp = dm.dmBitsPerPel;
m_DisplayModes.Add( mode );
}
}
}
}
dwMode++;
}
}
// Function name : CDisplaySettingsDlg::FillCombo
// Description : Fills the combobox with the previously fetched displaymodes
// Return type : void
void CDisplaySettingsDlg::FillCombo()
{
int item;
CString str;
TDisplayMode *mode;
for(int i=0;i<m_DisplayModes.GetSize();i++ ) // loop modes
{
mode = (TDisplayMode*)m_DisplayModes[i];
str.Format("%d x %d x %d",mode->dwWidth,mode->dwHeight,mode->dwBpp); //format modes
item = m_cboResolutions.AddString( str ); //add it to the combo
m_cboResolutions.SetItemData( item , (DWORD)mode ); //cast it to the itemdata
}
if( m_cboResolutions.GetCount() <= 0 ) //do we have <=0 entries ?
{
item = m_cboResolutions.AddString("Not supported."); //your videocard can't do anything ?!
m_cboResolutions.EnableWindow( FALSE );
}
m_cboResolutions.SetCurSel( 0 );
}
// Function name : CDisplaySettingsDlg::LoadSettings
// Description : Loads the settings from the current inifile
// Return type : void
void CDisplaySettingsDlg::LoadSettings()
{
UINT width,height,bpp;
width = GetPrivateProfileInt("Display Settings" , "Width" , 0 , m_strIniFile ); //load the settings from the inifile
height = GetPrivateProfileInt("Display Settings" , "Height" , 0 , m_strIniFile );
bpp = GetPrivateProfileInt("Display Settings" , "BPP" , 0 , m_strIniFile );
m_bFullscreen = GetPrivateProfileInt("Display Settings" , "Fullscreen" , 1 , m_strIniFile );
m_bDontAskAgain = GetPrivateProfileInt("Display Settings" , "DontAskAgain" , 0 , m_strIniFile );
int i;
BOOL bFound = FALSE;
if( m_cboResolutions.GetCount() > 0 ) //now set the current selection matching to the loaded settings
{
m_cboResolutions.SetCurSel( 0 );
for(i=0;i<m_cboResolutions.GetCount();i++)
{
TDisplayMode *mode = (TDisplayMode *)m_cboResolutions.GetItemData(i);
if( width == mode->dwWidth &&
height == mode->dwHeight &&
bpp == mode->dwBpp )
{
m_cboResolutions.SetCurSel( i );
bFound = TRUE;
break;
}
}
}
if( !bFound )
m_bDontAskAgain = 0; //make sure the 'don't ask again'-check is flagged out (if not found)
}
// Function name : CDisplaySettingsDlg::SaveSettings
// Description : Saves the current settings to the ini files
// Return type : void
void CDisplaySettingsDlg::SaveSettings( )
{
TDisplayMode *mode = NULL;
int sel = m_cboResolutions.GetCurSel();
if( sel >= 0 )
{
mode = (TDisplayMode *)m_cboResolutions.GetItemData(sel);
}
if( mode == NULL )return;
WritePrivateProfileString("Display Settings" , "Width" , WordToString( mode->dwWidth ) , m_strIniFile );
WritePrivateProfileString("Display Settings" , "Height" , WordToString( mode->dwHeight ) , m_strIniFile );
WritePrivateProfileString("Display Settings" , "BPP" , WordToString( mode->dwBpp ) , m_strIniFile );
WritePrivateProfileString("Display Settings" , "Fullscreen" , WordToString( m_bFullscreen ) , m_strIniFile );
WritePrivateProfileString("Display Settings" , "DontAskAgain" , WordToString( m_bDontAskAgain ) , m_strIniFile );
}
// Function name : CDisplaySettingsDlg::WordToString
// Description : Quick conversion from a unsigned integer to a string
// Return type : CString
// Argument : UINT uValue
CString CDisplaySettingsDlg::WordToString(UINT uValue)
{
CString str;
str.Format("%u",uValue );
return str;
}