aboutsummaryrefslogtreecommitdiff
path: root/src/parsec/m_option.cpp
blob: d7362134d84e7a2825f7ee718434350a5c858611 (plain)
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
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
/*
 * PARSEC - Options Window
 *
 * $Author: uberlinuxguy $ - $Date: 2004/09/26 03:43:37 $
 *
 * Orginally written by:
 *   Copyright (c) Markus Hadwiger     <msh@parsec.org>   1998-2000
 *   Copyright (c) Andreas Varga       <sid@parsec.org>   1999-2000
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */ 

// C library
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// compilation flags/debug support
#include "config.h"
#include "debug.h"

// general definitions
#include "general.h"
#include "objstruc.h"

// global externals
#include "globals.h"

#include "con_com.h"

// subsystem headers
#include "aud_defs.h"
#include "net_defs.h"
#include "sys_defs.h"
#include "vid_defs.h"

// drawing subsystem
#include "d_font.h"

// rendering subsystem
#include "r_patch.h"
#include "r_supp.h"

// local module header
#include "m_option.h"

// proprietary module headers
#include "con_aux.h"
#include "e_color.h"
#include "e_demo.h"
#include "e_draw.h"
#include "e_supp.h"
#include "m_main.h"
#include "net_conn.h"
#include "sys_bind.h"
#include "vid_plug.h"

#ifdef SYSTEM_TARGET_LINUX
	#include <SDL/SDL.h>
#else
	#include <SDL.h>
#endif

// flags
#define DISABLE_VID_SUBSYS_SWITCHING



// generic string paste area --------------------------------------------------
//
#define PASTE_STR_LEN 2047
static char paste_str[ PASTE_STR_LEN + 1 ];

static char tmp_name[MAX_PLAYER_NAME + 1];

bool mod_player_name;

// string constants -----------------------------------------------------------
//
static char not_available[]			= "not available  ";


// options menu configuration and state variables -----------------------------
//
#define OPT_POS_RIGHT				0
#define OPT_POS_LEFT				( m_sintab_size - 1 )
#define OPT_SLIDE_SPEED				12

#define OPT_MAX_BLINK_COUNT			900
#define OPT_MAX_TEXTLEN 			255

static const char *option_lines[ NUM_OPTIONS ] = {

	"  resolution: ",
	"  color depth: ",
	"  fullscreen: ",
	"  vertical sync: ",
	"  apply video settings  ",
	"  texture detail: ",
	"  geometry detail: ",
	"  sound-fx detail: ",
	"  apply detail settings  ",
	"  protocol: ",
	"  kill limit: ",
	"  solar system: ",
	"  apply network settings  ",
	"  audio settings: ",
	"  controller: ",
	"  invert mouse y axis: ",
	"  mouse sensitivity: ",
	"  mouse centering speed: ",
	//" ",
	"  name: ",
	"  exit options menu  ",
};

enum {

	OPT_SPACING_NONE   	= 0,
	OPT_SPACING_NEWLINE	= 1,
	OPT_SPACING_DIVIDER	= 2
};

static int option_spacing[ NUM_OPTIONS ] = {

	OPT_SPACING_NONE,		// RES_OPT,
	OPT_SPACING_NONE,		// DEPTH_OPT,
	OPT_SPACING_NONE,		// FULL_OPT,
	OPT_SPACING_NEWLINE,	// SYNC_OPT,
	OPT_SPACING_DIVIDER,	// APPLY_VID_OPT,
	OPT_SPACING_NONE,		// TEXDETAIL_OPT,
	OPT_SPACING_NONE,		// GEOMDETAIL_OPT,
	OPT_SPACING_NEWLINE,	// SFXDETAIL_OPT,
	OPT_SPACING_DIVIDER,	// APPLY_DETAIL_OPT
	OPT_SPACING_NONE,		// PROTOCOL_OPT,
	OPT_SPACING_NONE,		// KILL_LIMIT_OPT,
	OPT_SPACING_NEWLINE,	// SOLAR_OPT,
	OPT_SPACING_DIVIDER,	// APPLY_NET_OPT,
	OPT_SPACING_NONE,		// SOUND_OPT,
	OPT_SPACING_NONE,		// CTRL_OPT,
	OPT_SPACING_NONE,		// INVERT_OPT,
	OPT_SPACING_NONE,		// SENSITIVITY_OPT,
	OPT_SPACING_DIVIDER,	// CENTER_OPT,
	//OPT_SPACING_DIVIDER,    // APPLY_MOUSE_OPT (Actually a blank line),
	OPT_SPACING_NEWLINE,	// NAME_OPT,
	OPT_SPACING_DIVIDER,	// EXIT_OPT,
};

static int option_apply_blinking[ NUM_OPTIONS ] = {

	FALSE,
	FALSE,
	FALSE,
	FALSE,
	FALSE,	// APPLY_VID_OPT,
	FALSE,
	FALSE,
	FALSE,
	FALSE,	// APPLY_DETAIL_OPT
	FALSE,
	FALSE,
	FALSE,
	FALSE,	// APPLY_NET_OPT,
	FALSE,
	FALSE,
	FALSE,
	FALSE,
	FALSE,
	FALSE,
};

static int enable_dynamic_options = TRUE;

static int option_dynamic[ NUM_OPTIONS ] = {

	FALSE,
	FALSE,
	FALSE,
	FALSE,
	FALSE,	// APPLY_VID_OPT,
	FALSE,
	FALSE,
	FALSE,
	FALSE,	// APPLY_DETAIL_OPT
	FALSE,
	FALSE,
	FALSE,
	FALSE,	// APPLY_NET_OPT,
	FALSE,
	FALSE,
	TRUE,
	TRUE,
	TRUE,
	FALSE,
};


static const char *binopt_strings[] = {

	"off  ",
	"on  ",
};

static const char *detailopt_strings[] = {

	"high  ",
	"low  ",
};

static const char *qualityopt_strings[] = {

	"high  ",
	"medium  ",
	"low  ",
};

static const char *soundopt_strings[] = {

	"off  ",
	"sfx  ",
	"music  ",
	"music+sfx  ",
};

static const char *ctrlopt_strings[] = {

	"off  ",
	"joystick  ",
	"mouse  ",
	"joy+mouse  ",
};


static int			opt_slidepos 			= OPT_POS_RIGHT;
static int			opt_slidetarget			= OPT_POS_RIGHT;
static refframe_t	opt_lastref 			= REFFRAME_INVALID;

static int			opt_blink_count			= 0;
static int  		opt_blink_ctr			= 0;
static int 			opt_show_warning_text 	= FALSE;
static char 		opt_warning_str[] 		= "disconnect first";

static char 		opt_caption_str[] 		= "OPTIONS MENU";

static char			opt_text_buffer[ OPT_MAX_TEXTLEN + 1 ];

static resinfo_s	cur_res				= resinfo_s();
static int  		cur_depthmode 		= 0;
static int  		cur_winstatus 		= 0;
static int  		cur_flipsync		= 0;
static int  		cur_mipmapbias		= 0;
static int  		cur_geombias		= 0;

PUBLIC int  		cur_sfxquality		= 0;
PUBLIC int  		prev_sfxquality		= 0;

static int			cur_killlimit		= 0;
static int			cur_solarsys		= 0;

static int 			cur_sensitivity		= 0;
static int 			cur_centerspeed		= 0;

static int			vidsubsys_id		= 0;
static int			prev_vidsubsys_id	= 0;

static int			protsubsys_id		= 0;
static int			prev_protsubsys_id	= 0;


struct user_names_s {

	const char *internal_name;
	const char *visible_name;
};

static user_names_s user_names[] = {

	{ "glx", 	"opengl"		},
	{ "udp", 	"tcp/ip"		},

	{ NULL,		NULL			},
};

// number of option menu entries and currently selected entry -----------------
//
static int cur_opmenu_size		= NUM_OPTIONS;
static int cur_opmenu_selindx	= 0;


// return current selection in options menu -----------------------------------
//
int OptionsListSelection()
{
	return cur_opmenu_selindx;
}


// select default item in options menu ----------------------------------------
//
void OptionsListSelectDefault()
{
	cur_opmenu_selindx = 0;
}


// one item up in options menu ------------------------------------------------
//
void OptionsListCursorUp()
{
	if ( --cur_opmenu_selindx < 0 )
		cur_opmenu_selindx = cur_opmenu_size - 1;

	AUD_Select2();
}


// one item down in options menu ----------------------------------------------
//
void OptionsListCursorDown()
{
	if ( ++cur_opmenu_selindx >= cur_opmenu_size )
		cur_opmenu_selindx = 0;

	AUD_Select2();
}



// current metrics of options menu contents -----------------------------------
//
static list_window_metrics_s options_content_metrics = { FALSE };


// detect whether mouse position is over options menu item --------------------
//
int MouseOverOption( int mousex, int mousey )
{
	if ( !options_content_metrics.valid )
		return MOUSE_OVER_NOTHING;

	if ( !SlideFinishedOptions() )
		return MOUSE_OVER_NOTHING;

	int chwidth   = options_content_metrics.chwidth;
	int chheight  = options_content_metrics.chheight;
	int text_x    = options_content_metrics.text_x;
	int text_y    = options_content_metrics.text_y;
	int linewidth = options_content_metrics.maxcontwidth * chwidth;

	// check against options menu items
	for ( int mid = 0; mid < cur_opmenu_size; mid++ ) {

		if ( ( mousex >= text_x ) && ( mousex < ( text_x + linewidth ) ) &&
			 ( mousey >= text_y ) && ( mousey < ( text_y + chheight ) ) ) {

			// set selected index directly
			if ( cur_opmenu_selindx != mid )
				AUD_Select2();
			cur_opmenu_selindx = mid;

			return MOUSE_OVER_OPTION;
		}

		text_y += chheight;
		if ( option_spacing[ mid ] != OPT_SPACING_NONE ) {
			text_y += chheight;
		}
	}

	return MOUSE_OVER_NOTHING;
}


// set position and slide target for options menu to left (in) ----------------
//
void MoveInOptions()
{
	opt_slidepos	= OPT_POS_LEFT;
	opt_slidetarget	= opt_slidepos;
}


// set position and slide target for options menu to right (out) --------------
//
void MoveOutOptions()
{
	opt_slidepos	= OPT_POS_RIGHT;
	opt_slidetarget	= opt_slidepos;
}


// set slide target for options menu to left (in) -----------------------------
//
void SlideInOptions()
{
	opt_slidetarget = OPT_POS_LEFT;
}


// set slide target for options menu to right (out) ---------------------------
//
void SlideOutOptions()
{
	opt_slidetarget = OPT_POS_RIGHT;
}


// check if options menu is at its desired slide position ---------------------
//
int SlideFinishedOptions()
{
	return ( opt_slidepos == opt_slidetarget );
}


// init info on current subsystems for options menu ---------------------------
//
void InitOptionsSubsystemInfo()
{
	// get current mode values
	cur_res			= GameScreenRes;
	cur_depthmode	= GameScreenBPP;
	cur_winstatus	= GameScreenWindowed;
	

	vidsubsys_id		= 0;
	prev_vidsubsys_id	= 0;


	cur_mipmapbias = AUXDATA_TMM_MIPMAP_LOD_BIAS;
	cur_geombias   = AUXDATA_LOD_DISCRETE_GEOMETRY_BIAS;

	// search for currently selected protocol in table
	int id = 0;
	for ( id = 0; protocol_table[ id ].name; id++ )
		if ( protocol_table[ id ].id == sys_BindType_PROTOCOL )
			break;

	protsubsys_id			= id;
	prev_protsubsys_id		= id;

	//NOTE:
	// ( protocol_table[ id ].name == NULL ) must be
	// handled correctly by users of protsubsys_id.

	cur_killlimit = AUX_KILL_LIMIT_FOR_GAME_END;
	cur_solarsys = AUXDATA_BACKGROUND_NEBULA_ID;
	
	enable_dynamic_options = Op_Mouse;
}


// init vidmode state variables for options menu ------------------------------
//
void InitOptionsWindow()
{
	static int mode_init_done = FALSE;
	if ( mode_init_done )
		return;

	// init mode state variables
	cur_res			= GameScreenRes;
	cur_depthmode	= GameScreenBPP;
	cur_winstatus	= GameScreenWindowed;
	cur_flipsync	= FlipSynched;

	int id = 0;
	
	vidsubsys_id = 0;

	// search for currently selected protocol in table
	for ( id = 0; protocol_table[ id ].name; id++ )
		if ( protocol_table[ id ].id == sys_BindType_PROTOCOL )
			break;

	protsubsys_id = prev_protsubsys_id = id;


	cur_sensitivity = inp_mouse_sensitivity;
	cur_centerspeed = inp_mouse_drift;
	
	enable_dynamic_options = Op_Mouse;

	mode_init_done = TRUE;
}


// exec options menu item selection: screen resolution ------------------------
//
PRIVATE
void ExecOptionSelectResolution()
{
	if (!cur_res.isValid())
		return;
	
	int resindex = GetResolutionIndex(cur_res.width, cur_res.height);
	resindex = (resindex + 1) % Resolutions.size();
	
	if (VID_MODE_AVAILABLE(resindex)) {
		cur_res = Resolutions[resindex];
	}
}


// exec options menu item selection: color depth ------------------------------
//
PRIVATE
void ExecOptionSelectColordepth()
{
	if ( cur_depthmode == -1 )
		return;

	int prev_depthmode = cur_depthmode;

	if ( cur_depthmode == MODE_COL_16BPP )
		cur_depthmode = MODE_COL_32BPP;
	else if ( cur_depthmode == MODE_COL_32BPP )
		cur_depthmode = MODE_COL_16BPP;
	
	if (cur_depthmode > MaxScreenBPP)
		cur_depthmode = prev_depthmode;
}


// exec options menu item selection: fullscreen/windowed mode -----------------
//
PRIVATE
void ExecOptionSelectFullscreen()
{
	if ( cur_winstatus == -1 )
		return;

	// toggle winstatus flag
	cur_winstatus = !cur_winstatus;
}


// exec options menu item selection: sync to screen ---------------------------
//
PRIVATE
void ExecOptionSelectFlipsync()
{
	// toggle flipsync flag
	if ( cur_flipsync == -1 )
		return;

	cur_flipsync = !cur_flipsync;
}


// exec options menu item selection: texture detail ---------------------------
//
PRIVATE
void ExecOptionSelectTextureDetail()
{
	if ( ++cur_mipmapbias > 1 ) {
		cur_mipmapbias = 0;
	}
}


// exec options menu item selection: geometry detail --------------------------
//
PRIVATE
void ExecOptionSelectGeometryDetail()
{
	if ( ++cur_geombias > 1 ) {
		cur_geombias = 0;
	}
}


// exec options menu item selection: sound detail -----------------------------
//
PRIVATE
void ExecOptionSelectSoundDetail()
{
	if ( ++cur_sfxquality > 2 ) {
		cur_sfxquality = 0;
	}
}


// exec options menu item selection: select game protocol ---------------------
//
PRIVATE
void ExecOptionSelectProtocol()
{
	// ensure that there is atleast one protocol linked
	if ( protocol_table[ 0 ].id == -1 )
		return;

	// take next available protocol
	if ( protocol_table[ protsubsys_id ].id != -1 )
		protsubsys_id++;
	if ( protocol_table[ protsubsys_id ].id == -1 )
		protsubsys_id = 0;
}


// exec options menu item selection: kill limit -------------------------------
//
PRIVATE
void ExecOptionSelectKillLimit()
{
	cur_killlimit += 10;
	if ( cur_killlimit > 99 ) {
		cur_killlimit = 0;
	}
}


// exec options menu item selection: solar system -----------------------------
//
PRIVATE
void ExecOptionSelectSolarSystem()
{
	// currently 2 to 5 are valid
	if ( ++cur_solarsys > 5 ) {
		cur_solarsys = 2;
	}
}


// exec options menu item selection: enable/disable sound effects -------------
//
PRIVATE
void ExecOptionSelectSoundEnabling()
{
	if ( SoundDisabled )
		return;

	// determine current setting
	int cursetting = 0x00;
	if ( Op_SoundEffects )
		cursetting |= 0x01;
	if ( Op_Music )
		cursetting |= 0x02;

	// go to next setting
	cursetting = ( cursetting + 1 ) & 0x03;

	// determine new setting
	Op_SoundEffects	= ( ( cursetting & 0x01 ) != 0x00 );
	Op_Music		= ( ( cursetting & 0x02 ) != 0x00 );

	// start/stop according to setting
	switch ( cursetting ) {

		//NOTE:
		// the sequence of AUDs_CloseMenuSound() and AUDs_StopAudioStream()
		// must not be changed, or the stream active flag will wrongly stick.

		case 0x00:
			//AUDs_CDStop();
			AUDs_CloseMenuSound();
			AUDs_StopAudioStream();
			break;

		case 0x01:
			//AUDs_CDStop();
			AUDs_CloseMenuSound();
			AUDs_StopAudioStream();
			break;

		case 0x02:
			AUDs_OpenMenuSound();
			break;

		case 0x03:
			AUDs_OpenMenuSound();
			break;

		default:
			ASSERT( 0 );
	}
}


// exec options menu item selection: select controller ------------------------
//
PRIVATE
void ExecOptionSelectControllerEnabling()
{
	// determine current setting
	int cursetting = 0x00;
	if ( Op_Joystick )
		cursetting |= 0x01;
	if ( Op_Mouse )
		cursetting |= 0x02;

	
	bool foundsetting = false;
	
	while (!foundsetting) {
		// go to next setting
		cursetting = ( cursetting + 1 ) & 0x03;
		
		foundsetting = true;

		// prevent setting both mouse and joystick
		if ( cursetting == 3 ) {
			foundsetting = false;
		}

		// ensure joystick is available
		if ( ( cursetting & 0x01 ) && JoystickDisabled ) {
			foundsetting = false;
		}
	};

	// determine new setting
	Op_Joystick	= ( ( cursetting & 0x01 ) != 0x00 );
	Op_Mouse	= ( ( cursetting & 0x02 ) != 0x00 );
	
	enable_dynamic_options = Op_Mouse;
}


// exec options menu item selection: select controller ------------------------
//
PRIVATE
void ExecOptionSelectInvertYAxis()
{
	if ( !enable_dynamic_options )
		return;
	
	inp_mouse_invert_yaxis ^= 1;
}


// exec options menu item selection: select mouse sensitivity -----------------
//
PRIVATE
void ExecOptionSelectSensitivity()
{
	if ( !enable_dynamic_options )
		return;

	cur_sensitivity += 100;
	if ( cur_sensitivity > 2000 ) {
		cur_sensitivity = 100;
	}
	
	inp_mouse_sensitivity = cur_sensitivity;
}


// exec options menu item selection: select mouse sensitivity -----------------
//
PRIVATE
void ExecOptionSelectCenterSpeed()
{
	if ( !enable_dynamic_options )
		return;

	cur_centerspeed += 10;
	if ( cur_centerspeed > 100 ) {
		cur_centerspeed = 10;
	}
	
	inp_mouse_drift = cur_centerspeed;
}

// exec options menu item selection: select name-----------------
//
PRIVATE
void ExecOptionSelectName()
{
	// toggle the flag to modify the LocalPlayerName
	mod_player_name = !mod_player_name;
	if(mod_player_name) {
		// if we are modifying the player, copy the local player
		// name to paste_str for editing.
		strncpy(tmp_name, LocalPlayerName, MAX_PLAYER_NAME + 1);
	} else {
		CheckPlayerName(tmp_name);
		//strncpy(LocalPlayerName, tmp_name, MAX_PLAYER_NAME + 1);
	}
		
	
}

// exec options menu item selection: exit options menu ------------------------
//
PRIVATE
void ExecOptionSelectExit()
{
	ExitOptionsMenu();
}


// exec options menu item selection: apply video settings ---------------------
//
PRIVATE
void ExecOptionApplyVideo()
{
	option_apply_blinking[ APPLY_VID_OPT ] = FALSE;
	
	FlipSynched = cur_flipsync;

	// apply video mode change
	int resindex = GetResolutionIndex(cur_res.width, cur_res.height);
	if ( VID_MODE_AVAILABLE( resindex ) ) {
		VID_HotChangeMode(cur_res.width, cur_res.height, !cur_winstatus, cur_depthmode);	}

	// get the default mode that is active after switching
	cur_res			= GameScreenRes;
	cur_depthmode	= GameScreenBPP;
	cur_winstatus	= GameScreenWindowed;
}


// exec options menu item selection: apply detail settings --------------------
//
PRIVATE
void ExecOptionApplyDetail()
{
	option_apply_blinking[ APPLY_DETAIL_OPT ] = FALSE;

	if ( AUXDATA_TMM_MIPMAP_LOD_BIAS != cur_mipmapbias ) {

		// set new mip map bias
		AUXDATA_TMM_MIPMAP_LOD_BIAS = cur_mipmapbias;

		// flush entire texture cache
		R_InvalidateCachedTexture( NULL );

		// make sure texture data that might have been freed
		// by the texture cache is available once again
		ReloadFreedTextureBitmaps();

		// precache textures
		R_PrecacheTextures();
	}

	AUXDATA_LOD_DISCRETE_GEOMETRY_BIAS = cur_geombias;

	// trigger reloading samples if quality has been changed
	if ( cur_sfxquality != prev_sfxquality ) {

		if ( DEMO_ReplayActive() ) {
			DEMO_StopReplay();
		}

		extern int itemvis_loading;
		itemvis_loading = TRUE;

		prev_sfxquality = cur_sfxquality;
	}
}


// defined in CON_AUX.C -------------------------------------------------------
//
extern void RESyncKillLimit();
extern void RESyncNebulaId();


// exec options menu item selection: apply network settings -------------------
//
PRIVATE
void ExecOptionApplyNetwork()
{
	option_apply_blinking[ APPLY_NET_OPT ] = FALSE;

	// apply kill limit change
	if ( AUX_KILL_LIMIT_FOR_GAME_END != cur_killlimit ) {
		AUX_KILL_LIMIT_FOR_GAME_END = cur_killlimit;
		RESyncKillLimit();
	}

	// apply nebula change
	if ( AUXDATA_BACKGROUND_NEBULA_ID != cur_solarsys ) {
		AUXDATA_BACKGROUND_NEBULA_ID = cur_solarsys;
		RESyncNebulaId();
	}

	// plug in new network subsystem only if different
	if (  protsubsys_id  == prev_protsubsys_id  ) {
		return;
	}

	// only if not connected
	if ( NetConnected ) {

		opt_show_warning_text = TRUE;
		opt_blink_ctr = 0;

		return;
	}

	// deinit old subsystem
	NETs_KillAPI();

	// rebind protocol
	sys_BindType_PROTOCOL = protocol_table[ protsubsys_id ].id;
	SYS_Bind_PROTOCOL();

	// init new subsystem
	NETs_InitAPI();

	prev_protsubsys_id   = protsubsys_id;
}


// user has selected option <optno> -------------------------------------------
//
void ExecOptionSelection( int optno )
{
	// reset the mod_player_name flag 
	mod_player_name = 0;

	// invoke corresponding function
	switch ( optno ) {

		case RES_OPT:
			ExecOptionSelectResolution();
			break;

		case DEPTH_OPT:
			ExecOptionSelectColordepth();
			break;

		case FULL_OPT:
			ExecOptionSelectFullscreen();
			break;

		case SYNC_OPT:
			ExecOptionSelectFlipsync();
			break;

		case APPLY_VID_OPT:
			ExecOptionApplyVideo();
			break;

		case TEXDETAIL_OPT:
			ExecOptionSelectTextureDetail();
			break;

		case GEOMDETAIL_OPT:
			ExecOptionSelectGeometryDetail();
			break;

		case SFXDETAIL_OPT:
			ExecOptionSelectSoundDetail();
			break;

		case APPLY_DETAIL_OPT:
			ExecOptionApplyDetail();
			break;

		case PROTOCOL_OPT:
			ExecOptionSelectProtocol();
			break;

		case KILL_LIMIT_OPT:
			ExecOptionSelectKillLimit();
			break;

		case SOLAR_OPT:
			ExecOptionSelectSolarSystem();
			break;

		case APPLY_NET_OPT:
			ExecOptionApplyNetwork();
			break;

		case SOUND_OPT:
			ExecOptionSelectSoundEnabling();
			break;

		case CTRL_OPT:
			ExecOptionSelectControllerEnabling();
			break;

		case INVERT_OPT:
			ExecOptionSelectInvertYAxis();
			break;

		case SENSITIVITY_OPT:
			ExecOptionSelectSensitivity();
			break;

		case CENTER_OPT:
			ExecOptionSelectCenterSpeed();
			break;

		case EXIT_OPT:
			ExecOptionSelectExit();
			break;
		case NAME_OPT:
			ExecOptionSelectName();
			break;

		default:
			ASSERT( 0 );
	}
}


// create options menu item text: screen resolution ---------------------------
//
PRIVATE
void CreateOptionTextResolution( int id )
{
	ASSERT( strlen( option_lines[ id ] ) <= OPT_MAX_TEXTLEN );
	strcpy( opt_text_buffer, option_lines[ id ] );

	if ( !cur_res.isValid() )
		sprintf( paste_str, "default" );
	else
		sprintf( paste_str, "%dx%d", cur_res.width, cur_res.height );

	ASSERT( strlen( paste_str ) + 2 + strlen( opt_text_buffer ) <= OPT_MAX_TEXTLEN );
	strcat( opt_text_buffer, paste_str );
	strcat( opt_text_buffer, "  " );
}


// create options menu item text: color depth ---------------------------------
//
PRIVATE
void CreateOptionTextColordepth( int id )
{
	ASSERT( strlen( option_lines[ id ] ) <= OPT_MAX_TEXTLEN );
	strcpy( opt_text_buffer, option_lines[ id ] );

	if ( cur_depthmode == -1 ) {
		sprintf( paste_str, "default" );
	} else {

		int colbits = cur_depthmode;
		sprintf( paste_str, "%d bit", colbits );
	}

	ASSERT( strlen( paste_str ) + 2 + strlen( opt_text_buffer ) <= OPT_MAX_TEXTLEN );
	strcat( opt_text_buffer, paste_str );
	strcat( opt_text_buffer, "  " );
}


// create options menu item text: fullscreen/windowed mode --------------------
//
PRIVATE
void CreateOptionTextFullscreen( int id )
{
	ASSERT( strlen( option_lines[ id ] ) <= OPT_MAX_TEXTLEN );
	strcpy( opt_text_buffer, option_lines[ id ] );

	if ( cur_winstatus == -1 ) {
		ASSERT( 9 + strlen( opt_text_buffer ) <= OPT_MAX_TEXTLEN );
		strcat( opt_text_buffer, "default  " );
	} else {
		ASSERT( strlen( binopt_strings[ cur_winstatus == 0 ] ) + strlen( opt_text_buffer ) <= OPT_MAX_TEXTLEN );
		strcat( opt_text_buffer, binopt_strings[ cur_winstatus == 0 ] );
	}
}


// create options menu item text: sync to screen ------------------------------
//
PRIVATE
void CreateOptionTextFlipsync( int id )
{
	ASSERT( strlen( option_lines[ id ] ) + strlen( binopt_strings[ cur_flipsync ] ) <= OPT_MAX_TEXTLEN );
	strcpy( opt_text_buffer, option_lines[ id ] );
	strcat( opt_text_buffer, binopt_strings[ cur_flipsync ] );
}


// create options menu item text: texture detail ------------------------------
//
PRIVATE
void CreateOptionTextTextureDetail( int id )
{
	ASSERT( strlen( option_lines[ id ] ) + strlen( detailopt_strings[ cur_mipmapbias ] ) <= OPT_MAX_TEXTLEN );
	strcpy( opt_text_buffer, option_lines[ id ] );
	strcat( opt_text_buffer, detailopt_strings[ cur_mipmapbias ] );
}


// create options menu item text: geometry detail -----------------------------
//
PRIVATE
void CreateOptionTextGeometryDetail( int id )
{
	ASSERT( strlen( option_lines[ id ] ) + strlen( detailopt_strings[ cur_geombias ] ) <= OPT_MAX_TEXTLEN );
	strcpy( opt_text_buffer, option_lines[ id ] );
	strcat( opt_text_buffer, detailopt_strings[ cur_geombias ] );
}


// create options menu item text: sound detail --------------------------------
//
PRIVATE
void CreateOptionTextSoundDetail( int id )
{
	ASSERT( strlen( option_lines[ id ] ) + strlen( qualityopt_strings[ cur_sfxquality ] ) <= OPT_MAX_TEXTLEN );
	strcpy( opt_text_buffer, option_lines[ id ] );
	strcat( opt_text_buffer, qualityopt_strings[ cur_sfxquality ] );
}


// create options menu item text: game protocol -------------------------------
//
PRIVATE
void CreateOptionTextProtocol( int id )
{
	ASSERT( strlen( option_lines[ id ] ) <= OPT_MAX_TEXTLEN );
	strcpy( opt_text_buffer, option_lines[ id ] );

	if ( protocol_table[ protsubsys_id ].name == NULL ) {
		ASSERT( strlen( not_available ) + strlen( opt_text_buffer ) <= OPT_MAX_TEXTLEN );
		strcat( opt_text_buffer, not_available );
		return;
	}

	ASSERT( strlen( protocol_table[ protsubsys_id ].name ) + 2 + strlen( opt_text_buffer ) <= OPT_MAX_TEXTLEN );
	strcat( opt_text_buffer, protocol_table[ protsubsys_id ].name );
	strcat( opt_text_buffer, "  " );
}


// create options menu item text: kill limit ----------------------------------
//
PRIVATE
void CreateOptionTextKillLimit( int id )
{
	ASSERT( strlen( option_lines[ id ] ) + 2 <= OPT_MAX_TEXTLEN );
	strcpy( opt_text_buffer, option_lines[ id ] );

	if ( cur_killlimit == 0 ) {
		sprintf( paste_str, "off" );
	} else {
		sprintf( paste_str, "%d", cur_killlimit );
	}

	strcat( opt_text_buffer, paste_str );
	strcat( opt_text_buffer, "  " );
}


// create options menu item text: kill limit ----------------------------------
//
PRIVATE
void CreateOptionTextSolarSystem( int id )
{
	ASSERT( strlen( option_lines[ id ] ) + 2 <= OPT_MAX_TEXTLEN );
	strcpy( opt_text_buffer, option_lines[ id ] );

	sprintf( paste_str, "%d", cur_solarsys - 1 );

	strcat( opt_text_buffer, paste_str );
	strcat( opt_text_buffer, "  " );
}


// create options menu item text: enable/disable sound effects ----------------
//
PRIVATE
void CreateOptionTextSoundEnabling( int id )
{
	// determine current setting
	int cursetting = 0x00;
	if ( Op_SoundEffects )
		cursetting |= 0x01;
	if ( Op_Music )
		cursetting |= 0x02;

	ASSERT( strlen( option_lines[ id ] ) + strlen( soundopt_strings[ cursetting ] ) <= OPT_MAX_TEXTLEN );
	strcpy( opt_text_buffer, option_lines[ id ] );
	strcat( opt_text_buffer, soundopt_strings[ cursetting ] );
}


// create options menu item text: select controller ---------------------------
//
PRIVATE
void CreateOptionTextControllerEnabling( int id )
{
	// determine current setting
	int cursetting = 0x00;
	if ( Op_Joystick )
		cursetting |= 0x01;
	if ( Op_Mouse )
		cursetting |= 0x02;

	ASSERT( strlen( option_lines[ id ] ) + strlen( ctrlopt_strings[ cursetting ] ) <= OPT_MAX_TEXTLEN );
	strcpy( opt_text_buffer, option_lines[ id ] );
	strcat( opt_text_buffer, ctrlopt_strings[ cursetting ] );
}


// create options menu item text: invert y axis of mouse ----------------------
//
PRIVATE
void CreateOptionTextInvertYAxis( int id )
{
	ASSERT( strlen( option_lines[ id ] ) + strlen( binopt_strings[ inp_mouse_invert_yaxis ] ) <= OPT_MAX_TEXTLEN );
	strcpy( opt_text_buffer, option_lines[ id ] );
	strcat( opt_text_buffer, binopt_strings[ inp_mouse_invert_yaxis ] );
}


// create options menu item text: set mouse sensitivity -----------------------
//
PRIVATE
void CreateOptionTextMouseSensitivity( int id )
{
	ASSERT( strlen( option_lines[ id ] ) + 2 <= OPT_MAX_TEXTLEN );
	strcpy( opt_text_buffer, option_lines[ id ] );

	sprintf( paste_str, "%d", cur_sensitivity );

	strcat( opt_text_buffer, paste_str );
	strcat( opt_text_buffer, "  " );
}


// create options menu item text: set mouse centering speed -------------------
//
PRIVATE
void CreateOptionTextCenterSpeed( int id )
{
	ASSERT( strlen( option_lines[ id ] ) + 2 <= OPT_MAX_TEXTLEN );
	strcpy( opt_text_buffer, option_lines[ id ] );

	sprintf( paste_str, "%d", cur_centerspeed );

	strcat( opt_text_buffer, paste_str );
	strcat( opt_text_buffer, "  " );
}

// create options menu item text: set mouse centering speed -------------------
//
PRIVATE
void CreateOptionTextName( int id )
{
	ASSERT( strlen( option_lines[ id ] ) + 2 <= OPT_MAX_TEXTLEN );
	strcpy( opt_text_buffer, option_lines[ id ] );
	if(!mod_player_name) {	
		sprintf( paste_str, "%s", LocalPlayerName );
	} else {
		sprintf(paste_str, "%s", tmp_name);
	}

	strcat( opt_text_buffer, paste_str );
	strcat( opt_text_buffer, "  " );
}
// create options menu item text: default -------------------------------------
//
PRIVATE
void CreateOptionTextDefault( int id )
{
	ASSERT( strlen( option_lines[ id ] ) <= OPT_MAX_TEXTLEN );
	strcpy( opt_text_buffer, option_lines[ id ] );
}


// create text for one line in the options menu -------------------------------
//
PRIVATE
void CreateOptionsText( int id )
{
	switch ( id ) {

		case RES_OPT:
			CreateOptionTextResolution( id );
			break;

		case DEPTH_OPT:
			CreateOptionTextColordepth( id );
			break;

		case FULL_OPT:
			CreateOptionTextFullscreen( id );
			break;

		case SYNC_OPT:
			CreateOptionTextFlipsync( id );
			break;

		case TEXDETAIL_OPT:
			CreateOptionTextTextureDetail( id );
			break;

		case GEOMDETAIL_OPT:
			CreateOptionTextGeometryDetail( id );
			break;

		case SFXDETAIL_OPT:
			CreateOptionTextSoundDetail( id );
			break;

		case PROTOCOL_OPT:
			CreateOptionTextProtocol( id );
			break;

		case KILL_LIMIT_OPT:
			CreateOptionTextKillLimit( id );
			break;

		case SOLAR_OPT:
			CreateOptionTextSolarSystem( id );
			break;

		case SOUND_OPT:
			CreateOptionTextSoundEnabling( id );
			break;

		case CTRL_OPT:
			CreateOptionTextControllerEnabling( id );
			break;

		case INVERT_OPT:
			CreateOptionTextInvertYAxis( id );
			break;

		case SENSITIVITY_OPT:
			CreateOptionTextMouseSensitivity( id );
			break;
		
		case CENTER_OPT:
			CreateOptionTextCenterSpeed( id );
			break;
		case NAME_OPT:
			CreateOptionTextName( id );
			break;
		default:
			CreateOptionTextDefault( id );
			break;
	}
}

// filters unsafe charaters from the key pressed buffer.
//void OptionsNameCharaterFilter(dword key){




// used to modify name, modifies the past_str buffer thing
void OptionsKeyPressed(char key)
{

	/*
	// don't write console open/close key
	if (character == '`' || character == '~')
		return;

	int curlinelen = strlen( con_lines[ con_bottom ] + PROMPT_SIZE );

	// typeable characters
	if ( ( character & CON_ASCII_MASK ) && (character <= CON_ASCII_MASK) && (cursor_x < edit_max_x ) ) {
		if ( insert_mode && ( cursor_x < curlinelen ) ) {
			if ( curlinelen < edit_max_x ) {
				strcpy( paste_str, con_lines[ con_bottom ] + PROMPT_SIZE + cursor_x );
				strcpy( con_lines[ con_bottom ] + PROMPT_SIZE + cursor_x + 1, paste_str );
				con_lines[ con_bottom ][ PROMPT_SIZE + cursor_x++ ] = (byte) character;
			}
		} else {
			con_lines[ con_bottom ][ PROMPT_SIZE + cursor_x++ ] = (byte) character;
		}
	}*/
	// filter some ascii keys.
	if( (key > 32 && key < 45) ||   
		(key == 46 || key == 47 ) ||
		(key > 57 && key < 65) ||
		(key > 90 && key < 95) || 
		(key == 96) || 
		(key > 121) ) {
			return;
	}


	if(key == SDLK_BACKSPACE) { 
		if(tmp_name[0] != '\0'){
			tmp_name[strlen(tmp_name)-1]='\0';
		}
	} else if (key == SDLK_RETURN) {
		ExecOptionSelectName();
	
	} else if(key == SDLK_ESCAPE) {
		mod_player_name = 0; // exit with no modification
	} else {
		if(strlen(tmp_name) + 1 < MAX_PLAYER_NAME ) {
			tmp_name[strlen(tmp_name)] = (char)key;
		}
	}


}



// slide the options menu to its specifed target x position -------------------
//
PRIVATE
void DoOptionsSliding()
{
	if ( opt_slidepos == opt_slidetarget ) {
		opt_lastref = REFFRAME_INVALID;
		return;
	}

	if ( opt_slidepos < opt_slidetarget ) {

		// slide right
		refframe_t refframecount = SYSs_GetRefFrameCount();
		if ( opt_lastref == REFFRAME_INVALID ) {
			opt_lastref = refframecount;
		} else {
			refframe_t delta = refframecount - opt_lastref;
			for ( ; delta >= OPT_SLIDE_SPEED; delta -= OPT_SLIDE_SPEED ) {
				opt_slidepos++;
				if ( opt_slidepos >= opt_slidetarget ) {
					opt_slidepos = opt_slidetarget;
					opt_lastref  = REFFRAME_INVALID;
					break;
				}
				opt_lastref += OPT_SLIDE_SPEED;
			}
		}

	} else {

		// slide left
		refframe_t refframecount = SYSs_GetRefFrameCount();
		if ( opt_lastref == REFFRAME_INVALID ) {
			opt_lastref = refframecount;
		} else {
			refframe_t delta = refframecount - opt_lastref;
			for ( ; delta >= OPT_SLIDE_SPEED; delta -= OPT_SLIDE_SPEED ) {
				opt_slidepos--;
				if ( opt_slidepos <= opt_slidetarget ) {
					opt_slidepos = opt_slidetarget;
					opt_lastref  = REFFRAME_INVALID;
					break;
				}
				opt_lastref += OPT_SLIDE_SPEED;
			}
		}
	}
}


// get blink states, reset each automatically if option and state identical ---
//
PRIVATE
void UpdateBlinkStates()
{
	// blink state of apply video options
	int diffvid = 0;
	diffvid += ( vidsubsys_id	!= prev_vidsubsys_id );
	diffvid += ((cur_res		!= GameScreenRes));
	diffvid += ( cur_depthmode	!= GameScreenBPP );
	diffvid += ( (dword)cur_winstatus != GameScreenWindowed );
	diffvid += ( cur_flipsync	!= FlipSynched );

	option_apply_blinking[ APPLY_VID_OPT ] = ( diffvid > 0 );

	// blink state of apply detail options
	int diffdet = 0;
	diffdet += ( cur_mipmapbias != AUXDATA_TMM_MIPMAP_LOD_BIAS );
	diffdet += ( cur_geombias   != AUXDATA_LOD_DISCRETE_GEOMETRY_BIAS );
	diffdet += ( cur_sfxquality != prev_sfxquality );

	option_apply_blinking[ APPLY_DETAIL_OPT ] = ( diffdet > 0 );

	// blink state of apply net options
	int diffnet = 0;
	diffnet += ( protsubsys_id   != prev_protsubsys_id );
	diffnet += ( cur_killlimit 	 != AUX_KILL_LIMIT_FOR_GAME_END );
	diffnet += ( cur_solarsys	 != AUXDATA_BACKGROUND_NEBULA_ID );

	option_apply_blinking[ APPLY_NET_OPT ] = ( diffnet > 0 );
}


// type for writestring function pointer --------------------------------------
//
typedef void (*WSFP)( ... );

//NOTE:
// this declaration is in global scope because of the extern "C".
// only gcc needs the otherwise redundant curly braces.


// draw horizontal divider line built from minus signs ------------------------
//
static
void DrawDividerLine( WSFP wstrfp, int lx, int ly, int len )
{
	ASSERT( wstrfp != NULL );

	ASSERT( len <= PASTE_STR_LEN );
	int dpos = 0;
	for ( dpos = 0; dpos < len; dpos++ ) {
		paste_str[ dpos ] = '-';
	}
	paste_str[ dpos ] = 0;

	wstrfp( paste_str, lx, ly, TRTAB_PANELTEXT );
}


// draw translucent background window for options menu ------------------------
//
PRIVATE
void DrawOptionsFrame()
{
	int frame_l		  = options_content_metrics.frame_l;
	int frame_r		  = options_content_metrics.frame_r;
	int frame_t		  = options_content_metrics.frame_t;
	int frame_b		  = options_content_metrics.frame_b;
	int chwidth		  = options_content_metrics.chwidth;
	int chheight	  = options_content_metrics.chheight;
	int text_x		  = options_content_metrics.text_x;
	int text_y		  = options_content_metrics.text_y;
	int maxcontwidth  = options_content_metrics.maxcontwidth;
	int maxcontheight = options_content_metrics.maxcontheight;

	int backx = text_x - frame_l * chwidth;
	int backy = text_y - frame_t * chheight;
	int backw = ( frame_l + frame_r + maxcontwidth ) * chwidth;
	int backh = ( frame_t + frame_b + maxcontheight ) * chheight;

	DRAW_ClippedTrRect( backx, backy, backw, backh, TRTAB_PANELBACK );
}


// draw caption of options window ---------------------------------------------
//
PRIVATE
void DrawOptionsCaption( WSFP wstrfp )
{
	unsigned int chwidth		  = options_content_metrics.chwidth;
	unsigned int chheight	  = options_content_metrics.chheight;
	int text_x		  = options_content_metrics.text_x;
	int text_y		  = options_content_metrics.text_y;
	unsigned int maxcontwidth  = options_content_metrics.maxcontwidth;
	unsigned int maxcontheight = options_content_metrics.maxcontheight;

	// display options menu caption
	ASSERT( maxcontwidth >= strlen( opt_caption_str ) );
	int xofs = ( ( maxcontwidth - strlen( opt_caption_str ) ) * chwidth ) / 2;
	wstrfp( opt_caption_str, text_x + xofs, text_y, TRTAB_PANELTEXT );
	text_y += chheight * 2;

	// this destroys paste_str
	DrawDividerLine( wstrfp, text_x, text_y, maxcontwidth );
	text_y += chheight;

	options_content_metrics.text_y = text_y;
}


// draw all options menu items (text lines) -----------------------------------
//
void DrawOptionsItems( WSFP wstrfp )
{
	unsigned int chwidth		 = options_content_metrics.chwidth;
	unsigned int chheight	 = options_content_metrics.chheight;
	int text_x		 = options_content_metrics.text_x;
	int text_y		 = options_content_metrics.text_y;
	unsigned int maxcontwidth = options_content_metrics.maxcontwidth;

	// display all options
	for ( int id = 0; id < cur_opmenu_size; id++ ) {

		CreateOptionsText( id );

		// draw/erase cursor
		char cursor = ( cur_opmenu_selindx == id ) ? '-' : ' ';
		*opt_text_buffer = cursor;
		*( opt_text_buffer + strlen( opt_text_buffer ) - 1 ) = cursor;

		ASSERT( maxcontwidth >= strlen( opt_text_buffer ) );
		int xofs = ( ( maxcontwidth - strlen( opt_text_buffer ) ) * chwidth ) / 2;

		int oldalpha = PanelTextColor.A;
		
		if ( option_dynamic[ id ] && !enable_dynamic_options ) {
			PanelTextColor.A = 0x20;
		}

		if ( option_apply_blinking[ id ] ) {

			if ( opt_blink_count < OPT_MAX_BLINK_COUNT/2 ) {
				// double bright
				wstrfp( opt_text_buffer, text_x + xofs, text_y, TRTAB_PANELTEXT );
				wstrfp( opt_text_buffer, text_x + xofs, text_y, TRTAB_PANELTEXT );
			}

		} else {

			// double bright if line of cursor
			if ( cur_opmenu_selindx == id && enable_dynamic_options )
				wstrfp( opt_text_buffer, text_x + xofs, text_y, TRTAB_PANELTEXT );
			wstrfp( opt_text_buffer, text_x + xofs, text_y, TRTAB_PANELTEXT );
		}

		if ( option_dynamic[ id ] && !enable_dynamic_options ) {
			PanelTextColor.A = oldalpha;
		}

		text_y += chheight;
		if ( option_spacing[ id ] != OPT_SPACING_NONE ) {
			if ( option_spacing[ id ] == OPT_SPACING_DIVIDER ) {
				// this destroys paste_str
				DrawDividerLine( wstrfp, text_x, text_y, maxcontwidth );
			}
			text_y += chheight;
		}
	}
}


// draw warning text in options menu if active --------------------------------
//
PRIVATE
void DrawOptionsWarnings( WSFP wstrfp )
{
	unsigned int chwidth		  = options_content_metrics.chwidth;
	unsigned int chheight	  = options_content_metrics.chheight;
	int text_x		  = options_content_metrics.text_x;
	int text_y		  = options_content_metrics.text_y;
	unsigned int maxcontwidth  = options_content_metrics.maxcontwidth;
	unsigned int maxcontheight = options_content_metrics.maxcontheight;

	// draw connect warning
	if ( opt_show_warning_text ) {

		if ( opt_blink_ctr >= 4 ) {

			opt_show_warning_text = FALSE;
			opt_blink_ctr = 0;

		} else {

			if ( opt_blink_count < OPT_MAX_BLINK_COUNT/2 ) {

				ASSERT( maxcontwidth >= strlen( opt_warning_str ) );

				int xofs = ( ( maxcontwidth - strlen( opt_warning_str ) ) * chwidth ) / 2;
				int stry = text_y + ( maxcontheight - 1 ) * chheight;

				// double bright
				wstrfp( opt_warning_str, text_x + xofs, stry, TRTAB_PANELTEXT );
				wstrfp( opt_warning_str, text_x + xofs, stry, TRTAB_PANELTEXT );
			}
		}
	}
}


// calc and store metrics for options window ----------------------------------
//
PRIVATE
void CalcOptionsContentMetrics()
{
	// bounds for content
	int maxcontwidth  = strlen( opt_caption_str );
	int maxcontheight = 32;

	// ensure maximum content width is big enough
	for ( int id = 0; id < cur_opmenu_size; id++ ) {

		CreateOptionsText( id );

		int optexlen = strlen( opt_text_buffer );
		if ( optexlen > maxcontwidth ) {
			maxcontwidth = optexlen;
		}
	}

	extern int hud_line_dist;

	// font metrics
	int chwidth  = CharsetInfo[ HUD_CHARSETNO ].width;
	int chheight = hud_line_dist;

	D_SetWStrContext( CharsetInfo[ HUD_CHARSETNO ].charsetpointer,
					  CharsetInfo[ HUD_CHARSETNO ].geompointer,
					  NULL,
					  CharsetInfo[ HUD_CHARSETNO ].width,
					  CharsetInfo[ HUD_CHARSETNO ].height );

	// frame metrics
	int frame_l = 1;
	int frame_r = 1;
	int frame_t = 2;
	int frame_b = 1;

	// text position
	int text_x = Screen_Width - ( frame_r + maxcontwidth ) * chwidth - 30;
	if ( opt_slidepos != -1 )
		text_x += m_sintab[ opt_slidepos ];
	int text_y = 62;

	// store metrics
	options_content_metrics.valid		  = TRUE;
	options_content_metrics.frame_l		  = frame_l;
	options_content_metrics.frame_r		  = frame_r;
	options_content_metrics.frame_t		  = frame_t;
	options_content_metrics.frame_b		  = frame_b;
	options_content_metrics.chwidth		  = chwidth;
	options_content_metrics.chheight	  = chheight;
	options_content_metrics.text_x		  = text_x;
	options_content_metrics.text_y		  = text_y;
	options_content_metrics.maxcontwidth  = maxcontwidth;
	options_content_metrics.maxcontheight = maxcontheight;
}


// draw options menu window ---------------------------------------------------
//
void DrawOptionsMenuWindow()
{
	DoOptionsSliding();
	UpdateBlinkStates();

	CalcOptionsContentMetrics();

	// check whether actual drawing disabled
	if ( AUX_DISABLE_FLOATING_MENU_DRAWING )
		return;

	// determine whether translucency should be used
	int translucent = VID_TRANSLUCENCY_SUPPORTED;

	// write text transparent only for color depths below 32 bit per pixel
	WSFP wstrfp = translucent ? (WSFP)&D_WriteTrString : (WSFP)&D_WriteString;

	// draw frame
	if ( translucent ) {
		DrawOptionsFrame();
	}

	// draw caption
	DrawOptionsCaption( wstrfp );

	// draw items
	DrawOptionsItems( wstrfp );

	// draw warnings if active
	DrawOptionsWarnings( wstrfp );

	// maintain blink counter
	if ( ( opt_blink_count += CurScreenRefFrames ) > OPT_MAX_BLINK_COUNT ) {
		opt_blink_count = 0;
		opt_blink_ctr++;
	}
}