-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoomtool.c
More file actions
186 lines (147 loc) · 3.35 KB
/
doomtool.c
File metadata and controls
186 lines (147 loc) · 3.35 KB
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
/*****************************************
DoomTool
doomtool.c FPS computation for Doom
(C) 2023 M. Feliks
*****************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "gitver.h"
void get_token(char* row, char* strtics)
{
char* p = strtok(row, " ");
int i = 0;
while(p != NULL) {
//printf("%s\n", p);
p = strtok(NULL, " ");
i += 1;
if (i == 4) {
strcpy(strtics, p);
}
}
}
int get_realtics(char* resultfile)
{
FILE* fp;
char row[80];
char strtics[16];
int realtics;
int is_found = 0;
fp = fopen(resultfile, "r");
if (!fp) {
return 0;
}
while(fgets(row, 80, fp)) {
if (strncmp(row, "timed ", sizeof ("timed ") - 1) == 0) {
get_token(row, strtics);
is_found = 1;
}
}
fclose(fp);
if (!is_found) {
return 0;
}
realtics = atoi(strtics);
if (realtics == 0) {
// probably error
}
return realtics;
}
int get_realtics_wrapper(char* resultfile)
{
int errorcode;
errorcode = get_realtics(resultfile);
if (errorcode == 0) {
printf("I have trouble finding realtics.\n");
}
return errorcode;
}
int launch_doom(int argc, char* argv[], char* tempfile)
{
int i, exitcode;
char row[80];
if (argc < 2) {
return 1;
}
strcpy(row, argv[1]);
for (i = 2; i < argc; i++) {
if (strcmp(argv[i], ">") == 0) {
break;
}
strcat(row, " ");
strcat(row, argv[i]);
}
strcat(row, " > ");
strcat(row, tempfile);
exitcode = system(row);
return exitcode;
}
int launch_doom_wrapper(int argc, char* argv[], char* tempfile)
{
int errorcode;
errorcode = launch_doom(argc, argv, tempfile);
if (errorcode != 0) {
printf("I cannot run Doom, sorry.\n");
}
return errorcode;
}
int create_tempfile(char* tempfile)
{
unsigned int left, right;
char buff[13];
FILE* fp;
srand((unsigned int)time(NULL));
left = rand() % 9999;
right = rand() % 9999;
sprintf(buff, "%04d", left);
strcpy(tempfile, buff);
sprintf(buff, "%04d", right);
strcat(tempfile, buff);
strcat(tempfile, ".txt");
fp = fopen(tempfile, "w");
if (!fp) {
printf("I cannot create a temporary file.\n");
return 1;
}
fclose(fp);
return 0;
}
void destroy_tempfile(char* tempfile)
{
FILE* fp = fopen(tempfile, "r");
// check if file exists
if (fp) {
fclose(fp);
remove(tempfile);
}
}
void compute_fps(int realtics)
{
double fps;
char strfps[16], row[80];
fps = 74690. / (double)realtics;
sprintf(strfps, "%f", fps);
strcpy(row, "Your FPS is: ");
strcat(row, strfps);
strcat(row, "\n");
printf(row);
}
int main(int argc, char* argv[])
{
int exitcode, realtics;
char tempfile[13];
exitcode = create_tempfile(tempfile);
if (exitcode == 0) {
exitcode = launch_doom_wrapper(argc, argv, tempfile);
if (exitcode == 0) {
realtics = get_realtics_wrapper(tempfile);
if (realtics > 0) {
compute_fps(realtics);
}
}
}
destroy_tempfile(tempfile);
printf("Thanks for using doomtool %s\n", GIT_VERSION);
return exitcode;
}