File: gt2aMain.c

package info (click to toggle)
openmohaa 0.82.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 34,192 kB
  • sloc: cpp: 315,720; ansic: 275,789; sh: 312; xml: 246; asm: 141; makefile: 7
file content (276 lines) | stat: -rw-r--r-- 4,704 bytes parent folder | download | duplicates (2)
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
/*
GameSpy GT2 SDK
GT2Action - sample app
Dan "Mr. Pants" Schoenblum
dan@gamespy.com

Copyright 2000 GameSpy Industries, Inc

*/

#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include "gt2aMain.h"
#include "gt2aDisplay.h"
#include "gt2aClient.h"
#include "gt2aServer.h"
#include "gt2aInput.h"
#include "gt2aSound.h"
#include "../../ghttp/ghttp.h"

const V3b Red    = { 255, 0, 0 };
const V3b Green  = { 0, 255, 0 };
const V3b Blue   = { 0, 0, 255 };
const V3b Yellow = { 255, 255, 0 };
const V3b Orange = { 255, 128, 0 };
const V3b Purple = { 255, 0, 255 };
const V3b Black  = { 0, 0, 0 };
const V3b White  = { 255, 255, 255 };
const V3b Grey   = { 128, 128, 128 };

static GT2Bool host = GT2True;
static GT2Bool dedicated;

void Log
(
	const char * format,
	...
)
{
#if 1
	FILE * fp;
	va_list args;

	va_start(args, format);

	fp = fopen("gt2a.log", "at");
	if(fp)
	{
		fprintf(fp, "%08d: ", (current_time() % 100000000));
		vfprintf(fp, format, args);
		fclose(fp);
	}

	va_end(args);
#endif
}

static void Idle
(
	void
)
{
	msleep(1);
	glutPostRedisplay();
}

static void Timer
(
	int value
)
{
	unsigned long now;

	// Reset the timer.
	///////////////////
	glutTimerFunc(10, Timer, 0);

	// Get the current time.
	////////////////////////
	now = current_time();

	// Let the subsystems think.
	////////////////////////////
	if(host)
		ServerThink(now);
	if(!dedicated)
		ClientThink(now);
	DisplayThink(now);
}

static GHTTPBool NaminatorCallback
(
	GHTTPRequest request,
	GHTTPResult result,
	char * buffer,
	GHTTPByteCount bufferLen,
	void * param
)
{
	if(result == GHTTPSuccess)
	{
		char * str;

		// Set the nick.
		////////////////
		strncpy(localNick, buffer, MAX_NICK);
		localNick[MAX_NICK - 1] = '\0';

		// Cap off the newline.
		///////////////////////
		str = strchr(localNick, '\n');
		if(str)
			*str = '\0';

		// Strip out stuff that'll mess up our
		// key\value message passing.
		//////////////////////////////////////
		while(str = strchr(localNick, '\\'))
			*str = '/';

		printf("%s\n", localNick);
	}
	else
	{
		printf("Error\n");
	}

	return GHTTPTrue;
}

static void ParseArgs
(
	int argc,
	char ** argv
)
{
	GT2Bool gotNick = GT2False;
	int i;

	for(i = 1 ; i < argc ; i++)
	{
		// Connect.
		///////////
		if(strncasecmp(argv[i], "-c", 2) == 0)
		{
			if(++i < argc)
			{
				// Get the address to connect to.
				/////////////////////////////////
				strcpy(serverAddress, argv[i]);
				if(!strchr(serverAddress, ':'))
					strcat(serverAddress, PORT_STRING);
				host = GT2False;
			}
		}
		// Fullscreen.
		//////////////
		else if(strcasecmp(argv[i], "-full") == 0)
		{
			fullScreen = GT2True;
		}
		// Nick.
		////////
		else if((strcasecmp(argv[i], "-nick") == 0) ||
			(strcasecmp(argv[i], "-name") == 0))
		{
			if(++i < argc)
			{
				char * str;

				// Set the nick.
				////////////////
				strncpy(localNick, argv[i], MAX_NICK);
				localNick[MAX_NICK - 1] = '\0';

				// Strip out stuff that'll mess up our
				// key\value message passing.
				//////////////////////////////////////
				while(str = strchr(localNick, '\\'))
					*str = '/';

				// We got a nick.
				/////////////////
				gotNick = GT2True;
			}
		}
		// Dedicated server.
		////////////////////
		else if(strcasecmp(argv[i], "-dedicated") == 0)
		{
			dedicated = GT2True;
		}
		// Width.
		/////////
		else if(strcasecmp(argv[i], "-width") == 0)
		{
			if(++i < argc)
				screenWidth = atoi(argv[i]);
		}
		// Height.
		//////////
		else if(strcasecmp(argv[i], "-height") == 0)
		{
			if(++i < argc)
				screenHeight = atoi(argv[i]);
		}
	}

	// If we didn't get a nick, get a naminator nick.
	/////////////////////////////////////////////////
	if(!gotNick)
	{
		printf("Getting naminator name...");
		ghttpGetFile("http://www.planetquake.com/excessive/name.asp", GHTTPTrue, NaminatorCallback, NULL);
	}
}

static GT2Bool Initialize
(
	void
)
{
	// Init the display.
	////////////////////
	InitializeDisplay();

	// Init input handling.
	///////////////////////
	InitializeInput();

	// Init sound.
	//////////////
	InitializeSound();

	// Start the server if we're hosting.
	/////////////////////////////////////
	if(host)
	{
		if(!InitializeServer())
		{
			printf("Failed to initialize server!\n");
			return GT2False;
		}
	}

	// Start the client if not dedicated.
	/////////////////////////////////////
	if(!dedicated)
	{
		if(!InitializeClient())
		{
			printf("Failed to initialize client!\n");
			return GT2False;
		}
	}

	return GT2True;
}

int main
(
	int argc,
	char ** argv
)
{
	srand(time(NULL));
	glutInit(&argc, argv);
	ParseArgs(argc, argv);
	glutTimerFunc(10, Timer, 0);
	glutIdleFunc(Idle);
	if(!Initialize())
		return 1;
	glutMainLoop();
	return 0;
}