File: gt2aDisplay.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 (1655 lines) | stat: -rw-r--r-- 33,318 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
/*
GameSpy GT2 SDK
GT2Action - sample app
Dan "Mr. Pants" Schoenblum
dan@gamespy.com

Copyright 2000 GameSpy Industries, Inc

*/

#include "gt2aMain.h"
#include "gt2aDisplay.h"
#include "gt2aClient.h"
#include "gt2aMath.h"
#include "gt2aInput.h"
#include "gt2aLogic.h"
#include "TGAFile.h"

#define TITLE                  "GameSpy GT2Action by Dan 'Mr. Pants' Schoenblum"
#define FONT                   GLUT_STROKE_ROMAN
#define TEXT_SCALE             10
#define CHAT_LINES             3
#define CHAT_SCROLL_TIME       (3.5 * 1000)
#define NUM_STARS              1000
#define FRAME_HISTORY_LEN      250
#define NUM_ROCKET_TEXTURES    4
#define NUM_EXPLOSION_TEXTURES 2
#define NUM_SHIP_TEXTURES      2
#define NUM_ASTEROID_TEXTURES  3
#define NUM_MINE_TEXTURES      3
#define NUM_SPINNER_TEXTURES   3
#define WINDOW_MIN             0
#define WINDOW_MAX             10000
#define POSITION_HISTORY_LEN   5
#define MAX_FLICKER            50
#define STARS_SCALE            4
#define RADAR_SIZE             1500
#define FULL_VIEW              (20000.0 * Zoom)
#define HALF_VIEW              (FULL_VIEW / 2.0)
#define ZOOM_IN_SPEED          0.4
#define ZOOM_OUT_SPEED         0.4
#define ZOOM_MIN               1.0
#define ZOOM_MAX               1.5

typedef struct Star
{
	V2f position;
	V3b color;
	byte alpha;
	byte flicker;
} Star;

GT2Bool fullScreen;

GT2Bool DrawGraphsOption;
GT2Bool DrawUpdateTimeOption = GT2True;
GT2Bool DrawUpdateLengthOption = GT2True;
GT2Bool DrawFrameTimeOption = GT2True;
GT2Bool DrawMarksOption = GT2True;
GT2Bool DrawFPSOption;
GT2Bool DrawWithPredictionOption = GT2True;
GT2Bool DrawBackgroundOption = GT2True;
GT2Bool DrawStarsOption = GT2True;
GT2Bool DrawBoundingCirclesOption = GT2False;
GT2Bool DrawRadarOption = GT2True;
float RadarScaleOption = 1;
float RadarPointScaleOption = 2;
GT2Bool ViewClippingOption = GT2True;

int screenWidth = 500;
int screenHeight = 500;

static char chatLines[CHAT_LINES][CHAT_MAX];
static unsigned long lastChatScroll;

static unsigned long Now;
static unsigned long Diff;

static GLuint backgroundTexture;
static GLuint shipTextures[NUM_SHIP_TEXTURES];
static GLuint explosionTextures[NUM_EXPLOSION_TEXTURES];
static GLuint rocketTextures[NUM_ROCKET_TEXTURES];
static GLuint asteroidTextures[NUM_ASTEROID_TEXTURES];
static GLuint mineTextures[NUM_MINE_TEXTURES];
static GLuint spinnerTextures[NUM_SPINNER_TEXTURES];

static Star stars[NUM_STARS];

static int FrameHistory[FRAME_HISTORY_LEN];
static int FrameHistoryStart;

static V2f localPosition;

static float Zoom = 1.0;

static void ScrollChat
(
	void
)
{
	int i;

	// First find where to start the copying.
	/////////////////////////////////////////
	for(i = 0 ; i < (CHAT_LINES - 1) ; i++)
		if(!chatLines[i][0])
			break;

	// Move all lines 1 step towards the end of the array.
	//////////////////////////////////////////////////////
	for( ; i > 0 ; i--)
		strcpy(chatLines[i], chatLines[i - 1]);

	// Clear the first line.
	////////////////////////
	chatLines[0][0] = '\0';
}

static void RemoveOldestChat
(
	void
)
{
	int i;

	// No lines?
	////////////
	if(!chatLines[0][0])
		return;

	// Find the oldest line.
	////////////////////////
	for(i = (CHAT_LINES - 1) ; !chatLines[i][0] && (i > 0) ; i--);

	// Remove it.
	/////////////
	chatLines[i][0] = '\0';
}

static void AddChatLine
(
	const char * message
)
{
	// Do scrolling.
	////////////////
	ScrollChat();

	// Copy in the new line.
	////////////////////////
	strncpy(chatLines[0], message, CHAT_MAX);
	chatLines[0][CHAT_MAX - 1] = '\0';

	// Update the scroll time.
	//////////////////////////
	lastChatScroll = current_time();
}

static int GetStringWidth
(
	const char * string
)
{
	int width = 0;
	
	if(!string)
		return 0;

	while(*string)
		width += glutStrokeWidth(FONT, *string++);

	return (width * TEXT_SCALE);
}

static void DrawString
(
	const char * string,
	int x,
	int y,
	const V3b color,
	float scale
)
{
	scale *= TEXT_SCALE;
	glColor3ubv(color);
	glPushMatrix();
		glTranslatef(x, y, 0);
		glScalef(scale, scale, 1);
		while(*string)
			glutStrokeCharacter(FONT, *string++);
	glPopMatrix();
}

static int GetCharacterWidth
(
	char ch
)
{
	int width;

	width = glutStrokeWidth(FONT, ch);
	width *= TEXT_SCALE;

	return width;
}

static void DrawCharacter
(
	char ch,
	int x,
	int y,
	const V3b color
)
{
	glColor3ubv(color);
	glPushMatrix();
		glTranslatef(x, y, 0);
		glScalef(TEXT_SCALE, TEXT_SCALE, 1);
		glutStrokeCharacter(FONT, ch);
	glPopMatrix();
}

void DisplayChat
(
	const char * message
)
{
	// Add this to the chat buffer.
	///////////////////////////////
	AddChatLine(message);
}

static void DrawChat
(
	void
)
{
	const char * str;
	int x;
	int y;
	int i;
	float scale;

	// Settings.
	////////////
	x = 100;
	y = 200;
	scale = 0.3;

	// Draw all the chat messages.
	//////////////////////////////
	for(i = 0 ; (i < CHAT_LINES) && chatLines[i][0] ; i++)
	{
		DrawString(chatLines[i], x, y, White, scale);
		y += 500;
	}

	// Get the current chat buffer.
	///////////////////////////////
	str = GetChatBuffer();
	if(str)
		DrawString(str, x, WINDOW_MAX - 500, White, scale);
}

static void DrawBoundingCircle
(
	float radius
)
{
	// Check the bounding circles option.
	/////////////////////////////////////
	if(DrawBoundingCirclesOption)
	{
		int i;
		V2f point;

		// Draw in white.
		/////////////////
		glColor3f(1, 1, 1);

		// Draw the circle (circle, sphere, whatever).
		//////////////////////////////////////////////
#if 1
		glBegin(GL_LINE_LOOP);
			for(i = 0 ; i < 360 ; i += 30)
			{
				RotationToVector(point, i);
				ScaleV2f(point, point, radius);
				glVertex2fv(point);
			}
		glEnd();
#else
		glutWireSphere(radius, 16, 16);
#endif

		// Draw the forward line.
		/////////////////////////
		glBegin(GL_LINES);
			glVertex2f(0, 0);
			glVertex2f(0, radius * 2);
		glEnd();
	}
}

static void DrawPlayer
(
	Player * player,
	const V2f position,
	float rotation
)
{
	int width;
	static const float textScale = 0.25;
	V2f textPosition;
	float diff;
	static char text[MAX_NICK + 32];

	// Get the text to show.
	////////////////////////
	sprintf(text, "%s: %d", player->nick, player->score);

	glMatrixMode(GL_MODELVIEW);

	glPushMatrix();
		glTranslatef(position[0], position[1], 0);

		width = GetStringWidth(text);
		width *= textScale;
		textPosition[0] = -(width / 2);
#if 0
		textPosition[1] = 700;
#else
		if((position[0] + textPosition[0]) < 20)
			textPosition[0] = (20 - position[0]);
		diff = (MAP_MAX - (position[0] + textPosition[0] + width));
		if(diff < 0)
			textPosition[0] += diff;
		if(position[1] < (MAP_MAX - 1000))
			textPosition[1] = 700;
		else
			textPosition[1] = -1000;
#endif
		DrawString(text, textPosition[0], textPosition[1], White, textScale);

		if(!player->dead)
		{
			glRotatef(rotation, 0, 0, -1);

			glPushMatrix();
				if(player->roll)
				{
					glScalef(1, 1, .005);
					glRotatef(35 * player->roll, 0, 1, 0);
				}

				glEnable(GL_TEXTURE_2D);
				glEnable(GL_BLEND);

				if(player->motion)
					glBindTexture(GL_TEXTURE_2D, shipTextures[1]);
				else
					glBindTexture(GL_TEXTURE_2D, shipTextures[0]);

				glColor4f(1, 1, 1, 1);

				glBegin(GL_QUADS);
					glTexCoord2f(0, 0);
					glVertex2f(-500, -300);
					glTexCoord2f(1, 0);
					glVertex2f(500, -300);
					glTexCoord2f(1, 1);
					glVertex2f(500, 700);
					glTexCoord2f(0, 1);
					glVertex2f(-500, 700);
				glEnd();

				glDisable(GL_BLEND);
				glDisable(GL_TEXTURE_2D);
			glPopMatrix();
		}

		DrawBoundingCircle(PLAYER_RADIUS);
	glPopMatrix();
}

static void Predict
(
	V2f newPosition,
	float * rotation,
	Player * player
)
{
	GT2Bool local;

	assert(newPosition);
	assert(rotation);
	assert(player);

	// Is this the local player?
	////////////////////////////
	local = (player == &players[localIndex]);

	// Should we predict?
	/////////////////////
	if(DrawWithPredictionOption && !player->dead && player->motion)
	{
		// Predict new position.
		////////////////////////
		ComputeNewPosition(
			newPosition,
			player->position,
			player->motion,
			player->rotation,
			Diff,
			PLAYER_SPEED,
			GT2True);

		// Predict rotation if not local.
		/////////////////////////////////
		if(!local)
			*rotation = ComputeNewRotation(player->rotation, player->turning, Diff, PLAYER_TURN_SPEED);
	}
	else
	{
		// Just use the actual position.
		////////////////////////////////
		CopyV2f(newPosition, player->position);

		// Use the real rotation if not local.
		//////////////////////////////////////
		if(!local)
			*rotation = player->rotation;
	}

	// If local, use our rotation.
	//////////////////////////////
	if(local)
		*rotation = localRotation;
}

static void DrawPlayers
(
	void
)
{
	int i;
	Player * player;
	V2f position;
	float rotation;
	float distance;

	// First draw the other players.
	////////////////////////////////
	for(i = 0 ; i < MAX_PLAYERS ; i++)
	{
		player = &players[i];

		// Check if we need to draw the player.
		///////////////////////////////////////
		if(player->used && (i != localIndex))
		{
			// Predict the new position.
			////////////////////////////
			Predict(position, &rotation, player);

			// Check if the players are close enough.
			/////////////////////////////////////////
			if(ViewClippingOption)
			{
				distance = DistanceV2f(localPosition, position);
				if(distance >= FULL_VIEW)
					continue;
			}

			// Do the actual drawing.
			/////////////////////////
			DrawPlayer(player, position, rotation);
		}
	}

	// Draw the local player.
	/////////////////////////
	assert((localIndex >= 0) && (localIndex < MAX_PLAYERS));
	player = &players[localIndex];

	// Do the actual drawing.
	// The position was already set in SetMapView.
	//////////////////////////////////////////////
	DrawPlayer(player, localPosition, localRotation);
}

static void DrawRocket
(
	CObject * rocket
)
{
	V2f position;

	// Are we using prediction?
	///////////////////////////
	if(DrawWithPredictionOption)
	{
		// Predict new position.
		////////////////////////
		ComputeNewPosition(
			position,
			rocket->position,
			FORWARD,
			rocket->rotation,
			Diff,
			ROCKET_SPEED,
			GT2False);
	}
	else
	{
		// Just use the actual position.
		////////////////////////////////
		CopyV2f(position, rocket->position);
	}

	glMatrixMode(GL_MODELVIEW);

	glPushMatrix();
		glTranslatef(position[0], position[1], 0);

		glRotatef(rocket->rotation, 0, 0, -1);

		glEnable(GL_TEXTURE_2D);
		glEnable(GL_BLEND);

		glBindTexture(GL_TEXTURE_2D, rocketTextures[RandomInt(0, NUM_ROCKET_TEXTURES - 1)]);

		glColor4f(1, 1, 1, 1);

		glBegin(GL_QUADS);
			glTexCoord2f(0, 0);
			glVertex2f(-350, -350);
			glTexCoord2f(1, 0);
			glVertex2f(350, -350);
			glTexCoord2f(1, 1);
			glVertex2f(350, 350);
			glTexCoord2f(0, 1);
			glVertex2f(-350, 350);
		glEnd();

		glDisable(GL_BLEND);
		glDisable(GL_TEXTURE_2D);

		DrawBoundingCircle(ROCKET_RADIUS);
	glPopMatrix();
}

static void DrawMine
(
	CObject * mine
)
{
	glMatrixMode(GL_MODELVIEW);

	glPushMatrix();
		glTranslatef(mine->position[0], mine->position[1], 0);

		glRotatef(mine->rotation, 0, 0, -1);

		glEnable(GL_TEXTURE_2D);
		glEnable(GL_BLEND);

		glBindTexture(GL_TEXTURE_2D, mineTextures[(int)mine->position[0] % NUM_MINE_TEXTURES]);

		glColor4f(1, 1, 1, 1);

		glBegin(GL_QUADS);
			glTexCoord2f(0, 0);
			glVertex2f(-800, -800);
			glTexCoord2f(1, 0);
			glVertex2f(800, -800);
			glTexCoord2f(1, 1);
			glVertex2f(800, 800);
			glTexCoord2f(0, 1);
			glVertex2f(-800, 800);
		glEnd();
		glDisable(GL_BLEND);
		glDisable(GL_TEXTURE_2D);

		DrawBoundingCircle(MINE_RADIUS);
	glPopMatrix();
}

static void DrawAsteroid
(
	CObject * asteroid
)
{
	glMatrixMode(GL_MODELVIEW);

	glPushMatrix();
		glTranslatef(asteroid->position[0], asteroid->position[1], 0);

		glRotatef(asteroid->rotation, 0, 0, -1);

		glEnable(GL_TEXTURE_2D);
		glEnable(GL_BLEND);

		//glBindTexture(GL_TEXTURE_2D, asteroidTextures[(int)asteroid->position[0] % NUM_ASTEROID_TEXTURES]);
		glBindTexture(GL_TEXTURE_2D, asteroidTextures[1]);

		glColor4f(1, 1, 1, 1);

		glBegin(GL_QUADS);
			glTexCoord2f(0, 0);
			glVertex2f(-800, -800);
			glTexCoord2f(1, 0);
			glVertex2f(800, -800);
			glTexCoord2f(1, 1);
			glVertex2f(800, 800);
			glTexCoord2f(0, 1);
			glVertex2f(-800, 800);
		glEnd();
		glDisable(GL_BLEND);
		glDisable(GL_TEXTURE_2D);

		DrawBoundingCircle(ASTEROID_RADIUS);
	glPopMatrix();
}

static void DrawExplosion
(
	CObject * explosion
)
{
	unsigned long totalTime;

	totalTime = (explosion->totalTime + Diff);

	glMatrixMode(GL_MODELVIEW);

	glPushMatrix();
		glTranslatef(explosion->position[0], explosion->position[1], 0);

		glPushMatrix();
			glScalef(totalTime / 100.0, totalTime / 100.0, 1);

			glEnable(GL_TEXTURE_2D);
			glEnable(GL_BLEND);

			glColor4f(1, 1, 1, 1);

			glBindTexture(GL_TEXTURE_2D, explosionTextures[1]);

			glBegin(GL_QUADS);
				glTexCoord2f(0, 0);
				glVertex2f(-700, -700);
				glTexCoord2f(1, 0);
				glVertex2f(700, -700);
				glTexCoord2f(1, 1);
				glVertex2f(700, 700);
				glTexCoord2f(0, 1);
				glVertex2f(-700, 700);
			glEnd();

			glRotatef(totalTime, 0, 0, -1);

			glBindTexture(GL_TEXTURE_2D, explosionTextures[0]);

			glBegin(GL_QUADS);
				glTexCoord2f(0, 0);
				glVertex2f(-700, -700);
				glTexCoord2f(1, 0);
				glVertex2f(700, -700);
				glTexCoord2f(1, 1);
				glVertex2f(700, 700);
				glTexCoord2f(0, 1);
				glVertex2f(-700, 700);
			glEnd();

			glDisable(GL_BLEND);
			glDisable(GL_TEXTURE_2D);
		glPopMatrix();

		DrawBoundingCircle(EXPLOSION_RADIUS);
	glPopMatrix();
}

static void DrawObjects
(
	void
)
{
	int i;
	CObject * object;
	float distance;

	// Draw all the objects.
	////////////////////////
	for(i = 0 ; i < MAX_OBJECTS ; i++)
	{
		object = &cObjects[i];

		// Is the object in use?
		////////////////////////
		if(object->used)
		{
			if(ViewClippingOption)
			{
				// Is the player close enough to see it?
				////////////////////////////////////////
				distance = DistanceV2f(localPosition, object->position);
				if(distance >= FULL_VIEW)
					continue;
			}

			// Draw it based on type.
			/////////////////////////
			if(object->type == ObjectRocket)
				DrawRocket(object);
			else if(object->type == ObjectMine)
				DrawMine(object);
			else if(object->type == ObjectAsteroid)
				DrawAsteroid(object);
			else if(object->type == ObjectExplosion)
				DrawExplosion(object);
		}
	}
}

static void DrawConnecting
(
	void
)
{
	static const char * text = "Connecting";
	static int width = -1;
	static const V3b textColor = { 0, 204, 0};
	static unsigned long lastSpin;
	static int spinnerIndex = -1;
	static const char * spinnerText = "|/-\\";
	static const V3b spinnerColor = { 0, 204, 0 };
	char spinnerChar;
	int spinnerX;

	// Get the width of the string if we don't already have it.
	///////////////////////////////////////////////////////////
	if(width == -1)
		width = GetStringWidth(text);

	// Draw the string.
	///////////////////
	DrawString(text, (WINDOW_MAX - width) / 2, (WINDOW_MAX / 2), textColor, 1);

	// Init if its the first time.
	//////////////////////////////
	if(spinnerIndex == -1)
	{
		spinnerIndex = 0;
		lastSpin = Now;
	}
	// Check if its time to spin the spinner.
	/////////////////////////////////////////
	else if((Now - lastSpin) >= 250)
	{
		spinnerIndex++;
		spinnerIndex %= 4;
		lastSpin = Now;
	}

	// Get the char to show.
	////////////////////////
	spinnerChar = spinnerText[spinnerIndex];

	// Get the x position of the char.
	//////////////////////////////////
	spinnerX = ((WINDOW_MAX - GetCharacterWidth(spinnerChar)) / 2);

	// Draw the spinner.
	////////////////////
	DrawCharacter(spinnerChar, spinnerX, 3000, spinnerColor);
}

static int ReadLittleInt
(
	FILE * file
)
{
	byte bytes[4];
	int i;

	if(fread(bytes, 1, 4, file) != 4)
		return -1;

	i = bytes[0];
	i |= (bytes[1] << 8);
	i |= (bytes[2] << 16);
	i |= (bytes[3] << 24);

	return i;
}

static GT2Bool LoadTexture
(
	const char * filename,
	GLuint * texture,
	GT2Bool useAlpha
)
{
	int width;
	int height;
	GLubyte * bytes;
	int len;
	int i;
	GLubyte temp;
	int format;

	// Load the file.
	/////////////////
	bytes = LoadTGAFile(filename, &width, &height);
	if(!bytes)
		return GT2False;

	// Convert from BGRA to RGBA.
	/////////////////////////////
	len = (width * height * 4);
	for(i = 0 ; i < len ; i += 4)
	{
		temp = bytes[i];
		bytes[i] = bytes[i + 2];
		bytes[i + 2] = temp;
	}

	// Generate and bind the texture.
	/////////////////////////////////
	glGenTextures(1, texture);
	glBindTexture(GL_TEXTURE_2D, *texture);

	// Load it from memory.
	///////////////////////
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	if(useAlpha)
		format = 4;
	else
		format = 3;
	glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bytes);

	// Set to decal mode.
	/////////////////////
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	// Setup the min/mag filters.
	/////////////////////////////
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	free(bytes);

	return GT2True;
}

static void DrawBackground
(
	void
)
{
	if(!DrawBackgroundOption)
		return;

	glEnable(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, backgroundTexture);

	glColor4f(1, 1, 1, 1);

	glBegin(GL_QUADS);
#if 0
		glTexCoord2f(0, 0);
		glVertex2f(MAP_MIN, MAP_MIN);
		glTexCoord2f(1, 0);
		glVertex2f(MAP_MAX, MAP_MIN);
		glTexCoord2f(1, 1);
		glVertex2f(MAP_MAX, MAP_MAX);
		glTexCoord2f(0, 1);
		glVertex2f(MAP_MIN, MAP_MAX);
#else
		glTexCoord2f(0.01, 0.01);
		glVertex2f(MAP_MIN, MAP_MIN);
		glTexCoord2f(0.99, 0.01);
		glVertex2f(MAP_MAX, MAP_MIN);
		glTexCoord2f(0.99, 0.99);
		glVertex2f(MAP_MAX, MAP_MAX);
		glTexCoord2f(0.01, 0.99);
		glVertex2f(MAP_MIN, MAP_MAX);
#endif
	glEnd();

	glDisable(GL_TEXTURE_2D);
}

static void InitStar
(
	unsigned long now,
	Star * star
)
{
	int color;

	star->position[0] = RandomFloat(MAP_MIN, MAP_MAX, GT2True);
	star->position[1] = RandomFloat(MAP_MIN, MAP_MAX, GT2True);

	color = RandomInt(0, 19);
	if(color < 15)
		CopyV3b(star->color, White);
	else if(color < 17)
		CopyV3b(star->color, Red);
	else if(color < 19)
		CopyV3b(star->color, Yellow);
	else
		CopyV3b(star->color, Blue);
	star->alpha = RandomInt(0, 255);
	star->flicker = RandomInt(1, MAX_FLICKER);
}

static void DrawStars
(
	void
)
{
	int i;
	int alpha;
	V2f position;

	if(!DrawStarsOption)
		return;

	// Figure out the eye position.
	///////////////////////////////
	CopyV2f(position, localPosition);
	if(position[0] < HALF_VIEW)
		position[0] = HALF_VIEW;
	else if(position[0] > (MAP_MAX - HALF_VIEW))
		position[0] = (MAP_MAX - HALF_VIEW);
	if(position[1] < HALF_VIEW)
		position[1] = HALF_VIEW;
	else if(position[1] > (MAP_MAX - HALF_VIEW))
		position[1] = (MAP_MAX - HALF_VIEW);

	// Draw the stars.
	//////////////////
	glEnable(GL_BLEND);
	glPushMatrix();
		glTranslatef(position[0] / STARS_SCALE, position[1] / STARS_SCALE, 0);

		glBegin(GL_POINTS);
			for(i = 0 ; i < NUM_STARS ; i++)
			{
				alpha = stars[i].alpha;
				alpha += RandomInt(-stars[i].flicker, stars[i].flicker);
				alpha = ClampInt(alpha, 0, 255);
				glColor4ub(stars[i].color[0], stars[i].color[1], stars[i].color[2], (byte)alpha);
				glVertex2fv(stars[i].position);
			}
		glEnd();
	glPopMatrix();
	glDisable(GL_BLEND);
}

static void DrawSpinner
(
	void
)
{
	float rotation;
	float scale;

	scale = (Now % 1001);
	scale -= 50;
	scale /= 50;

	glMatrixMode(GL_MODELVIEW);

	glPushMatrix();
		glTranslatef(MAP_HALF, MAP_HALF, 0);

		glEnable(GL_TEXTURE_2D);
		glEnable(GL_BLEND);

#if 1
		glPushMatrix();
			rotation = (Now % 360);
			glRotatef(rotation, 0, 0, 1);
			glBindTexture(GL_TEXTURE_2D, spinnerTextures[0]);
			glColor4f(1, 1, 1, 1);
			glBegin(GL_QUADS);
				glTexCoord2f(0, 0);
				glVertex2f(-SPINNER_COORD, -SPINNER_COORD);
				glTexCoord2f(1, 0);
				glVertex2f(SPINNER_COORD, -SPINNER_COORD);
				glTexCoord2f(1, 1);
				glVertex2f(SPINNER_COORD, SPINNER_COORD);
				glTexCoord2f(0, 1);
				glVertex2f(-SPINNER_COORD, SPINNER_COORD);
			glEnd();
		glPopMatrix();
#endif

		glPushMatrix();
			rotation = (Now % (360 * 8));
			rotation /= 8;
			glRotatef(rotation, 0, 0, 1);
			glScalef(scale, -scale, 1);
			glBindTexture(GL_TEXTURE_2D, spinnerTextures[2]);
			glColor4f(1, 1, 1, 0.8);
			glBegin(GL_QUADS);
				glTexCoord2f(0, 0);
				glVertex2f(-SPINNER_COORD, -SPINNER_COORD);
				glTexCoord2f(1, 0);
				glVertex2f(SPINNER_COORD, -SPINNER_COORD);
				glTexCoord2f(1, 1);
				glVertex2f(SPINNER_COORD, SPINNER_COORD);
				glTexCoord2f(0, 1);
				glVertex2f(-SPINNER_COORD, SPINNER_COORD);
			glEnd();
		glPopMatrix();

		glPushMatrix();
			rotation = (Now % (360 * 7));
			rotation /= 7;
			glRotatef(rotation, 0, 0, 1);
			glScalef(scale, -scale, 1);
			glBindTexture(GL_TEXTURE_2D, spinnerTextures[1]);
			glColor4f(1, 1, 1, 0.8);
			glBegin(GL_QUADS);
				glTexCoord2f(0, 0);
				glVertex2f(-SPINNER_COORD, -SPINNER_COORD);
				glTexCoord2f(1, 0);
				glVertex2f(SPINNER_COORD, -SPINNER_COORD);
				glTexCoord2f(1, 1);
				glVertex2f(SPINNER_COORD, SPINNER_COORD);
				glTexCoord2f(0, 1);
				glVertex2f(-SPINNER_COORD, SPINNER_COORD);
			glEnd();
		glPopMatrix();

		glDisable(GL_BLEND);
		glDisable(GL_TEXTURE_2D);

		DrawBoundingCircle(SPINNER_RADIUS);
	glPopMatrix();
}

static void DrawGraphs
(
	void
)
{
	int i;
	int diff;
	int len;
	int maxDiff = 0;
	int maxLen = 0;

	if(updateHistoryStart == -1)
		return;

	if(!DrawGraphsOption)
		return;

	glPushMatrix();
		glScalef(20, 20, 1);
		if(DrawUpdateTimeOption)
		{
			glColor3ubv(Green);
			glBegin(GL_LINE_STRIP);
				for(i = 0 ; i < UPDATE_HISTORY_LEN ; i++)
				{
					diff = updateHistory[(updateHistoryStart + i) % UPDATE_HISTORY_LEN].diff;

					if(diff > maxDiff)
						maxDiff = diff;

					if(diff == -1)
					{
						glColor3ubv(Red);
						glVertex2f(i * 2, 50);
						glColor3ubv(Green);
					}
					else
					{
						if(diff == -1)
							diff = 0;
						glVertex2f(i * 2, diff / 2);
					}
				}
			glEnd();
		}

		if(DrawUpdateLengthOption)
		{
			glColor3ubv(Purple);
			glBegin(GL_LINE_STRIP);
				for(i = 0 ; i < UPDATE_HISTORY_LEN ; i++)
				{
					len = updateHistory[(updateHistoryStart + i) % UPDATE_HISTORY_LEN].len;

					if(len > maxLen)
						maxLen = len;

					glVertex2f(i * 2, len / 8);
				}
			glEnd();
		}

		if(DrawFrameTimeOption)
		{
			glColor3ubv(Yellow);
			glBegin(GL_LINE_STRIP);
				for(i = 0 ; i < FRAME_HISTORY_LEN ; i++)
				{
					diff = FrameHistory[(FrameHistoryStart + i) % FRAME_HISTORY_LEN];
					glVertex2f(i * 2, diff / 2);
				}
			glEnd();
		}

		if(DrawMarksOption)
		{
			glColor3ubv(White);
			glBegin(GL_LINES);
				for(i = 0 ; i < screenHeight; i += 25)
				{
					glVertex2f(screenWidth - 10, i);
					glVertex2f(screenWidth, i);
				}
			glEnd();
		}

	glPopMatrix();
}

static void DrawFPS
(
	unsigned long diff
)
{
	if(DrawFPSOption && diff)
	{
		static int counter;
		static char buf[64];
		if(++counter == 10)
		{
			counter = 0;
			sprintf(buf, "FPS: %d", 1000 / diff);
		}
		DrawString(buf, WINDOW_MAX - 1500, 600, Grey, 0.2);
	}

	{
	char buf[64];
	sprintf(buf, "%d asteroid%s", ClientNumAsteroids, 
		(ClientNumAsteroids==1) ? "" : "s");
	DrawString(buf, WINDOW_MAX - 3500, 600, Grey, 0.2);
	}
}

static void DrawRadar
(
	void
)
{
	int i;
	Player * player;
	CObject * object;
	GLdouble plane0[4] = { 0, 1, 1, 0 };
	GLdouble plane1[4] = { 1, 0, 1, 0 };

	if(!DrawRadarOption)
		return;

	glPushMatrix();
		// Move to the upper-right corner.
		//////////////////////////////////
		glTranslatef(
			WINDOW_MAX - (RADAR_SIZE * RadarScaleOption),
			WINDOW_MAX - (RADAR_SIZE * RadarScaleOption),
			0);

		// Scale so we can draw in map space.
		/////////////////////////////////////
		glScalef(
			RADAR_SIZE / MAP_MAX * RadarScaleOption,
			RADAR_SIZE / MAP_MAX * RadarScaleOption,
			1);

#if 1
		// Enable textures.
		///////////////////
		glEnable(GL_TEXTURE_2D);
		glEnable(GL_BLEND);
		glColor4f(1, 1, 1, 0.5);

		// Draw a background.
		/////////////////////
		glBindTexture(GL_TEXTURE_2D, backgroundTexture);
		glBegin(GL_QUADS);
			glTexCoord2f(0, 0);
			glVertex2f(0, 0);
			glTexCoord2f(1, 0);
			glVertex2f(MAP_MAX, 0);
			glTexCoord2f(1, 1);
			glVertex2f(MAP_MAX, MAP_MAX);
			glTexCoord2f(0, 1);
			glVertex2f(0, MAP_MAX);
		glEnd();

		// Draw the spinner.
		////////////////////
		glPushMatrix();
			glTranslatef(MAP_HALF, MAP_HALF, 0);
			glRotatef(Now % 3600, 0, 0, 1);

			glBindTexture(GL_TEXTURE_2D, spinnerTextures[0]);
			glBegin(GL_QUADS);
				glTexCoord2f(0, 0);
				glVertex2f(-SPINNER_COORD, -SPINNER_COORD);
				glTexCoord2f(1, 0);
				glVertex2f(SPINNER_COORD, -SPINNER_COORD);
				glTexCoord2f(1, 1);
				glVertex2f(SPINNER_COORD, SPINNER_COORD);
				glTexCoord2f(0, 1);
				glVertex2f(-SPINNER_COORD, SPINNER_COORD);
			glEnd();
		glPopMatrix();

		// Disable textures.
		////////////////////
		glDisable(GL_BLEND);
		glDisable(GL_TEXTURE_2D);
#endif

		// Draw a box around the radar.
		///////////////////////////////
		glColor3ubv(Yellow);
		glBegin(GL_LINE_STRIP);
			glVertex2f(0, MAP_MAX);
			glVertex2f(0, 0);
			glVertex2f(MAP_MAX, 0);
		glEnd();

		// Setup the clipping planes.
		/////////////////////////////
		glEnable(GL_CLIP_PLANE0);
		glEnable(GL_CLIP_PLANE1);
		glClipPlane(GL_CLIP_PLANE0, plane0);
		glClipPlane(GL_CLIP_PLANE1, plane1);

		// Scale points.
		////////////////
		if(RadarPointScaleOption != 1)
			glPointSize(RadarPointScaleOption);

		glBegin(GL_POINTS);
			for(i = 0 ; i < MAX_OBJECTS ; i++)
			{
				object = &cObjects[i];

				if(!object->used)
					continue;

				if(object->type == ObjectRocket)
					glColor3ubv(Red);
				else if(object->type == ObjectMine)
					glColor3ubv(Yellow);
				else if(object->type == ObjectAsteroid)
					glColor3ubv(White);
				else
					continue;

				glVertex2fv(object->position);
			}

			glColor3ubv(Orange);
			for(i = 0 ; i < MAX_PLAYERS ; i++)
			{
				if(i == localIndex)
					continue;

				player = &players[i];

				if(!player->used)
					continue;

				glVertex2fv(player->position);
			}

			glColor3ubv(Green);
			glVertex2fv(players[localIndex].position);
		glEnd();

		// Unscale points.
		//////////////////
		if(RadarPointScaleOption != 1)
			glPointSize(1);
	glPopMatrix();

	glDisable(GL_CLIP_PLANE0);
	glDisable(GL_CLIP_PLANE1);
}

static void SetMapView
(
	void
)
{
	V2f corner;
	Player * player;
	float rotation;
	static V2f positionHistory[POSITION_HISTORY_LEN];
	static int positionHistoryStart = -1;
	int i;

	// Get the local player.
	////////////////////////
	player = &players[localIndex];

	// Predict the local player's position.
	///////////////////////////////////////
	Predict(localPosition, &rotation, player);

	// Put this position in the history.
	////////////////////////////////////
	if(positionHistoryStart == -1)
	{
		positionHistoryStart = 0;
		for(i = 0 ; i < POSITION_HISTORY_LEN ; i++)
			CopyV2f(positionHistory[i], localPosition);
	}
	else
	{
		CopyV2f(positionHistory[positionHistoryStart++], localPosition);
		positionHistoryStart %= POSITION_HISTORY_LEN;
	}

	// Average out the position history to get the display position.
	////////////////////////////////////////////////////////////////
	SetV2f(localPosition, 0, 0);
	for(i = 0 ; i < POSITION_HISTORY_LEN ; i++)
		AddV2f(localPosition, localPosition, positionHistory[i]);
	ScaleV2f(localPosition, localPosition, (1.0 / POSITION_HISTORY_LEN));

	// Set the left and bottom of the view.
	///////////////////////////////////////
	SubScalarV2f(corner, localPosition, HALF_VIEW);

	// Don't show anything outside the map.
	///////////////////////////////////////
	ClampV2f(corner, corner, 0, MAP_MAX - FULL_VIEW);

	glLoadIdentity();
	glViewport(0, 0, screenWidth, screenHeight);
	gluOrtho2D(corner[0], corner[0] + FULL_VIEW, corner[1], corner[1] + FULL_VIEW);
}

static void SetWindowView
(
	void
)
{
	glLoadIdentity();
	glViewport(0, 0, screenWidth, screenHeight);
	gluOrtho2D(0, WINDOW_MAX, 0, WINDOW_MAX);
}

static void Display
(
	void
)
{
	static unsigned long lastDisplay;
	unsigned long now;
	unsigned long diff;

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

	// Get the time difference.
	///////////////////////////
	diff = (now - lastDisplay);

	// Update the history.
	//////////////////////
	if(FrameHistoryStart == -1)
		FrameHistoryStart = 0;
	else
	{
		FrameHistory[FrameHistoryStart++] = diff;
		FrameHistoryStart %= FRAME_HISTORY_LEN;
	}

	// New last display.
	////////////////////
	lastDisplay = now;

	// Set the zoom.
	////////////////
	if(localMotion == STILL)
	{
		if(Zoom > ZOOM_MIN)
		{
			Zoom -= (ZOOM_IN_SPEED * diff / 1000);
			if(Zoom < ZOOM_MIN)
				Zoom = ZOOM_MIN;
		}
	}
	else
	{
		if(Zoom < ZOOM_MAX)
		{
			Zoom += (ZOOM_OUT_SPEED * diff / 1000);
			if(Zoom > ZOOM_MAX)
				Zoom = ZOOM_MAX;
		}
	}

	// Clear the color buffer.
	//////////////////////////
	glClear(GL_COLOR_BUFFER_BIT);
	
	// We want to know how long since the last server update.
	/////////////////////////////////////////////////////////
	Now = now;
	Diff = (now - lastServerUpdate);

	// Are we connected to the server?
	//////////////////////////////////
	if(connected)
	{
		// Set the view for drawing in map space.
		/////////////////////////////////////////
		SetMapView();

		// Draw the ground.
		///////////////////
		DrawBackground();

		// Draw some stars.
		///////////////////
		DrawStars();

		// Draw the spinner.
		////////////////////
		DrawSpinner();

		// Draw all the objects.
		////////////////////////
		DrawObjects();

		// Draw all the players.
		////////////////////////
		DrawPlayers();

		// Set the view for drawing in window space.
		////////////////////////////////////////////
		SetWindowView();

		// Draw the radar.
		//////////////////
		DrawRadar();

		// Draw the chat.
		/////////////////
		DrawChat();

		// Draw graphs.
		///////////////
		DrawGraphs();

		// Show the framerate.
		//////////////////////
		DrawFPS(diff);
	}
	else
	{
		// Set the view for drawing in window space.
		////////////////////////////////////////////
		SetWindowView();

		// Draw the connecting screen.
		//////////////////////////////
		DrawConnecting();
	}

	// Saw the front and back buffers.
	//////////////////////////////////
	glutSwapBuffers();
}

static void Reshape
(
	int width,
	int height
)
{
	screenWidth = width;
	screenHeight = height;

	// Setup a simple 2D orthographic view.
	///////////////////////////////////////
	glLoadIdentity();
	glViewport(0, 0, width, height);
	gluOrtho2D(0, MAP_MAX, 0, MAP_MAX);
}

void InitializeDisplay
(
	void
)
{
	int i;
	unsigned long now;
	char buffer[32];

	// Init glut window.
	////////////////////

	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_ALPHA);

	if(fullScreen)
	{
		glutGameModeString("640x480:32");
		glutEnterGameMode();
		//glutFullScreen();
		glutSetCursor(GLUT_CURSOR_NONE);
	}
	else
	{
		glutInitWindowSize(screenWidth, screenHeight);
		glutInitWindowPosition(200, 200);
		glutCreateWindow(TITLE);
	}
	// Set display callbacks.
	/////////////////////////
	glutDisplayFunc(Display);
	glutReshapeFunc(Reshape);

	// Set the background color.
	////////////////////////////
	glClearColor(0, 0, 0, 0);

	// Smooth points.
	/////////////////
	glEnable(GL_POINT_SMOOTH);
	glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);

	// Don't show the backs of polygons.
	////////////////////////////////////
	glEnable(GL_CULL_FACE);

	// We always use the same blend.
	////////////////////////////////
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	// Load textures.
	/////////////////
	printf("Loading textures...");
	LoadTexture("images/space.tga", &backgroundTexture, GT2False);
	printf(".");
	for(i = 0 ; i < NUM_SHIP_TEXTURES ; i++)
	{
		sprintf(buffer, "images/ship%d.tga", i);
		LoadTexture(buffer, &shipTextures[i], GT2True);
		printf(".");
	}
	for(i = 0 ; i < NUM_EXPLOSION_TEXTURES ; i++)
	{
		sprintf(buffer, "images/explosion%d.tga", i);
		LoadTexture(buffer, &explosionTextures[i], GT2True);
		printf(".");
	}
	for(i = 0 ; i < NUM_ROCKET_TEXTURES ; i++)
	{
		sprintf(buffer, "images/rocket%d.tga", i);
		LoadTexture(buffer, &rocketTextures[i], GT2True);
		printf(".");
	}
	for(i = 0 ; i < NUM_ASTEROID_TEXTURES ; i++)
	{
		sprintf(buffer, "images/asteroid%d.tga", i);
		LoadTexture(buffer, &asteroidTextures[i], GT2True);
		printf(".");
	}
	for(i = 0 ; i < NUM_MINE_TEXTURES ; i++)
	{
		sprintf(buffer, "images/mine%d.tga", i);
		LoadTexture(buffer, &mineTextures[i], GT2True);
		printf(".");
	}
	for(i = 0 ; i < NUM_MINE_TEXTURES ; i++)
	{
		sprintf(buffer, "images/spinner%d.tga", i);
		LoadTexture(buffer, &spinnerTextures[i], GT2True);
		printf(".");
	}
	printf("\n");

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

	// Setup stars.
	///////////////
	for(i = 0 ; i < NUM_STARS ; i++)
		InitStar(now, &stars[i]);

	// Setup frame history.
	///////////////////////
	FrameHistoryStart = -1;
}

void ShutdownDisplay
(
	void
)
{
	if(fullScreen)
	{
		glutLeaveGameMode();
	}
}

void DisplayThink
(
	unsigned long now
)
{
	static unsigned long last;
	unsigned long diff;

	// Get the frame time.
	//////////////////////
	now = current_time();
	diff = (now - last);
	if(diff < 5)
		return;
	last = now;

	// Is it time to scroll the chat?
	/////////////////////////////////
	if((now - lastChatScroll) > CHAT_SCROLL_TIME)
	{
		RemoveOldestChat();
		lastChatScroll = now;
	}

	// Update the frame.
	////////////////////
	glutPostRedisplay();
}