This repository was archived by the owner on Jan 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestoreGameResolution.cpp
executable file
·134 lines (115 loc) · 3.97 KB
/
RestoreGameResolution.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
/*!\file RestoreGameResolution.cpp
* \brief Restores game screen resolution when reentering from menuing system.
* \author Dean N. Butcher
* \version 2.1
* \date November 2004
*/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "ScreenResolutionManagement.h"
bool WriteResFile(int width, int height)
{
const char RESOLUTION[][10] = {"640 480", "800 600", "1024 768", "1280 1024"};
FILE *resFile = NULL;
if((resFile = fopen("csr.a2g", "w")) == NULL)
{
MessageBox(NULL, "Can't create resolution file! Defaulting to 800 x 600.", "Resolution Write...", MB_ICONEXCLAMATION | MB_OK);
ChangeScreenResolution(800, 600);
return false;
}
// Determine resolution required.
if(width == 640 && height == 480)
fwrite(RESOLUTION[0], sizeof(char), sizeof(RESOLUTION[0]), resFile);
else
if(width == 800 && height == 600)
fwrite(RESOLUTION[1], sizeof(char), sizeof(RESOLUTION[1]), resFile);
else
if(width == 1024 && height == 768)
fwrite(RESOLUTION[2], sizeof(char), sizeof(RESOLUTION[2]), resFile);
else
if(width == 1280 && height == 1024)
fwrite(RESOLUTION[3], sizeof(char), sizeof(RESOLUTION[3]), resFile);
else
{
MessageBox(NULL, "Resolution not supported!", "Resolution Change...", MB_ICONEXCLAMATION | MB_OK);
fclose(resFile);
return false;
}
fclose(resFile);
return true;
}
void FetchAndConvertStringToInt(char resolution[], size_t sizeBytes)
{
const char SPACE = ' ';
char textCoordinates[2][5];
int resWidth, resHeight;
if(resolution[3] == SPACE) // resolution is of the form xxx yyy
{
textCoordinates[0][0] = resolution[0];
textCoordinates[0][1] = resolution[1];
textCoordinates[0][2] = resolution[2];
textCoordinates[0][3] = SPACE;
textCoordinates[0][4] = SPACE;
textCoordinates[1][0] = resolution[4];
textCoordinates[1][1] = resolution[5];
textCoordinates[1][2] = resolution[6]; // Full resolution copied over at this point!
textCoordinates[1][3] = SPACE;
textCoordinates[1][4] = SPACE;
}
else
if(resolution[3] != SPACE && resolution[4] == SPACE && !(resolution[8] >= '0' && resolution[8] <= '9')) // resolution is of the form xxxx yyy
{
textCoordinates[0][0] = resolution[0];
textCoordinates[0][1] = resolution[1];
textCoordinates[0][2] = resolution[2];
textCoordinates[0][3] = resolution[3];
textCoordinates[0][4] = SPACE;
textCoordinates[1][0] = resolution[5];
textCoordinates[1][1] = resolution[6];
textCoordinates[1][2] = resolution[7];
textCoordinates[1][3] = SPACE;
textCoordinates[1][4] = SPACE;
}
else // resolution is of the form xxxx yyyy
{
textCoordinates[0][0] = resolution[0];
textCoordinates[0][1] = resolution[1];
textCoordinates[0][2] = resolution[2];
textCoordinates[0][3] = resolution[3];
textCoordinates[0][4] = SPACE;
textCoordinates[1][0] = resolution[5];
textCoordinates[1][1] = resolution[6];
textCoordinates[1][2] = resolution[7];
textCoordinates[1][3] = resolution[8];
textCoordinates[1][4] = SPACE;
}
// Convert to ints
resWidth = atoi(textCoordinates[0]);
resHeight = atoi(textCoordinates[1]);
if(!WriteResFile(resWidth, resHeight))
MessageBox(NULL, "Unable to write resolution to file! Continue to play.", "Resolution file update...", MB_ICONEXCLAMATION | MB_OK);
if(!CheckCurrentResolution(resWidth, resHeight))
ChangeScreenResolution(resWidth, resHeight);
}
bool FetchResolution()
{
char buffer[11];
size_t stringLength = 0;
FILE *resFile = NULL;
if((resFile = fopen("csr.a2g", "r")) == NULL)
return false;
while(!feof(resFile))
{
fread(buffer, sizeof(char), sizeof(buffer), resFile);
if(ferror(resFile))
{
MessageBox(NULL, "Can't read resolution file! Refreshing.", "Resolution Read...", MB_ICONEXCLAMATION | MB_OK);
return false;
}
}
fclose(resFile);
stringLength = sizeof(buffer);
FetchAndConvertStringToInt(buffer, stringLength);
return true;
}