aboutsummaryrefslogtreecommitdiffstats
path: root/CNativeCode/glf.c
blob: 7430432cd1a203f12dfdf9129690f269e039d9fe (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
/*
==============================================================================
|  GLF Library
|  Version 1.4
| 
|  Author: Roman Podobedov
|  Email: romka@ut.ee
|  WEB: http://romka.demonews.com
|  Release Date: 18 May 2001
|  
|  Copyright (C) 2000-2001, Romka Graphics
|  This library is freely distributable without any license or permissions
|  for non-commercial usage. You can use this library in any non-commercial
|  program. In each program, where You use this library You should keep 
|  this header (author name and coordinates)!
|  For commercial usage, please contact me: romka@ut.ee
==============================================================================
*/

#ifdef _WIN32_
#include <windows.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "glf.h"
#include "gltool.h"

/* ---------- Definitions and structures ----- */

#define MAX_FONTS 256

/* Color structure */
struct color
{
	float r, g, b, a;
};

/* One symbol of font */
struct one_symbol
{
	unsigned char vertexs; /* Number of vertexs         */
	unsigned char facets;  /* Number of facets          */
	unsigned char lines;   /* Number of lines in symbol */

	float *vdata;          /* Pointer to Vertex data    */
	unsigned char *fdata;  /* Pointer to Facets data    */
	unsigned char *ldata;  /* Pointer to Line data      */

	float leftx;           /* Smaller x coordinate      */
	float rightx;          /* Right x coordinate        */
	float topy;            /* Top y coordinate          */
	float bottomy;         /* Bottom y coordinate       */
};

/* Font structure */
struct glf_font
{
	char font_name [97];
	unsigned char sym_total;          /* Total symbols in font */
	struct one_symbol *symbols[256];  /* Pointers to symbols   */
};

/* ------------- Main variables -------------- */

static int refNumber = 0;

static float SymbolDist = 0.2f;  /* Distance between symbols (Variable constant) */
static float SymbolDepth = 0.2f; /* Symbol Depth in 3D space (Variable constant) */
static float SpaceSize = 2.0f;   /* Space size (Variable constant)               */
static float RotateAngle = 0.0f; /* Rotate angle for string (vector font) */
static float RotateAngleB = 0.0f; /* Rotate angle for string (bitmap font) */

/* Array of font pointers, up to MAX_FONTS fonts can be loaded at once */
/* if (fonts[i] == NULL) then this font is not present in memory */

static struct	glf_font *fonts[MAX_FONTS];
static int		curfont;			/* Index of current font pointer */
static char		ap = GLF_CENTER;	/* Anchor point */
static GLboolean m_string_center;	/* String centering (vector fonts) */
static GLboolean m_bitmap_string_center;	/* String centering (bitmap fonts) */

static GLuint	m_direction;	/* String direction (vector fonts) */

static char		console_msg = GLF_NO;
static char		texturing = GLF_NO;
static char		contouring = GLF_NO;
static struct	color contouring_color;

/* Console mode variables */
static int	conWidth, conHeight;	/* Console width and height */
static int	conx = 0, cony = 0;		/* Console current X and Y */
static char	*conData;				/* Console data */
static int	conFont;				/* Console font */
static char	conCursor = GLF_NO;		/* Console cursor Enabled|Disabled */
static int	conCursorBlink;         /* Console cursor blink rate */
static int	conCursorCount;         /* Console cursor blink counter */
static char	conCursorMode = GLF_NO; /* Console Cursor mode (on/off screen) */

/* ----------- Variables for bitmap font ------------- */
#define SEEK_SET_POS 4195

struct coord_rect
{
	float x, y, width, height;
};

struct widths
{
	float *width;
};

/* Constants */
static float sym_space=0.001f;

/* One font variables */
static struct coord_rect Symbols[256];
static GLboolean bmf_texturing;
static int    bmf_curfont;
static GLuint bmf_texture[MAX_FONTS]; /* All fonts */
static GLuint bmf_mask[MAX_FONTS]; /* All fonts (masked) */
static char	  bmf_in_use[MAX_FONTS]; /* 1 - if font is used, 0 - otherwise */
static int	  list_base[MAX_FONTS];
static GLfloat m_max_height[MAX_FONTS]; /* Maximal height of each font */
static struct widths m_widths[MAX_FONTS]; /* Width of each symbols in each font */


/* Initialization of GLF library, should be called before use of library */
void glfInit()
{
	int i;

	refNumber++;
	if(refNumber>1) return;

	for (i=0; i<MAX_FONTS; i++)
	{
		fonts[i] = NULL;
		bmf_in_use[i] = 0;
		m_max_height[i] = 0;
	}
	
	curfont = -1;
	bmf_curfont = -1;
	console_msg = GLF_NO;
	ap = GLF_CENTER;		/* Set anchor point to center of each symbol */
	texturing = GLF_NO;		/* By default texturing is NOT Enabled */
	contouring = GLF_NO;	/* By default contouring is NOT Enabled */
	memset(&contouring_color, 0, sizeof(struct color));
	conData = NULL;
	glfSetConsoleParam(40, 20);
	glfConsoleClear();
	glfEnable(GLF_CONSOLE_CURSOR);
	glfSetCursorBlinkRate(10);
	glfStringCentering(GL_FALSE);
	glfBitmapStringCentering(GL_FALSE);
	glfStringDirection(GLF_LEFT);
}

/* Closing library usage */
void glfClose()
{
	int i;
	
	refNumber--;
	if(refNumber>0) return;

	free(conData);

	for (i=0; i<MAX_FONTS; i++) glfUnloadFontD(i);
	for (i=0; i<MAX_FONTS; i++) glfUnloadBFontD(i);
}

/* 
|	This function check that this machine is little endian
|	Return value:	1 - little endian
|					0 - big endian
*/
static int LittleEndian()
{
	int endianTester = 1;
	char *endianChar = 0;

	endianChar = (char *)&endianTester;
	if (*endianChar) return 1;
	return 0;
}


/*
   --------------------------------------------------------------------------------- 
   ------------------------ Work with vector fonts --------------------------------- 
   --------------------------------------------------------------------------------- 
*/

/* 
|	This function read font file and store information in memory
|	Return: GLF_OK - if all OK
|	Return: GLF_ERROR - if any error 
*/
static int ReadFont(FILE *fontf, struct glf_font *glff)
{
	char buffer[64];
	int i, j;
	unsigned char temp, code, verts, fcets, lns;
	float tempfx, tempfy;
	unsigned char *tp;
	int LEndian; /* True if little endian machine */

	fread(buffer, 3, 1, fontf);
	buffer[3] = 0;
	if (strcmp(buffer, "GLF"))
	{
		/* If header is not "GLF" */
		if (console_msg) printf("Error reading font file: incorrect file format\n");
		return GLF_ERROR;
	}

	/* Check for machine type */
	LEndian = LittleEndian();

	fread(glff->font_name, 96, 1, fontf);
	glff->font_name[96] = 0;

	fread(&glff->sym_total, 1, 1, fontf); /* Read total symbols in font */

	for (i=0; i<MAX_FONTS; i++) glff->symbols[i] = NULL;

	for (i=0; i<28; i++) fread(&temp, 1, 1, fontf);  /* Read unused data */

	/* Now start to read font data */
  
	for (i=0; i<glff->sym_total; i++)
	{
		fread(&code, 1, 1, fontf);  /* Read symbol code   */
		fread(&verts, 1, 1, fontf); /* Read vertexs count */
		fread(&fcets, 1, 1, fontf); /* Read facets count  */
		fread(&lns, 1, 1, fontf);   /* Read lines count   */

		if (glff->symbols[code] != NULL) 
		{
			if (console_msg) printf("Error reading font file: encountered symbols in font\n");
			return GLF_ERROR;
		}

		glff->symbols[code] = (struct one_symbol *)malloc(sizeof(struct one_symbol));
		glff->symbols[code]->vdata = (float *)malloc(8*verts);
		glff->symbols[code]->fdata = (unsigned char *)malloc(3*fcets);
		glff->symbols[code]->ldata = (unsigned char *)malloc(lns);

		glff->symbols[code]->vertexs = verts;
		glff->symbols[code]->facets = fcets;
		glff->symbols[code]->lines = lns;

		/* Read vertexs data */
		glff->symbols[code]->leftx = 10;
		glff->symbols[code]->rightx = -10;
		glff->symbols[code]->topy = 10;
		glff->symbols[code]->bottomy = -10;

		for (j=0; j<verts; j++)
		{
			fread(&tempfx, 4, 1, fontf);
			fread(&tempfy, 4, 1, fontf);
          
			/* If machine is bigendian -> swap low and high words in
			tempfx and tempfy */
			if (!LEndian) 
			{
				tp = (unsigned char *)&tempfx;
				temp = tp[0]; tp[0] = tp[3]; tp[3] = temp;
				temp = tp[1]; tp[1] = tp[2]; tp[2] = temp;
				tp = (unsigned char *)&tempfy;
				temp = tp[0]; tp[0] = tp[3]; tp[3] = temp;
				temp = tp[1]; tp[1] = tp[2]; tp[2] = temp;
			}
			glff->symbols[code]->vdata[j*2] = tempfx;
			glff->symbols[code]->vdata[j*2+1] = tempfy;

			if (tempfx < glff->symbols[code]->leftx) glff->symbols[code]->leftx = tempfx;
			if (tempfx > glff->symbols[code]->rightx) glff->symbols[code]->rightx = tempfx;
			if (tempfy < glff->symbols[code]->topy) glff->symbols[code]->topy = tempfy;
			if (tempfy > glff->symbols[code]->bottomy) glff->symbols[code]->bottomy = tempfy;
		}
		for (j=0; j<fcets; j++)
			fread(&glff->symbols[code]->fdata[j*3], 3, 1, fontf);
		for (j=0; j<lns; j++)
			fread(&glff->symbols[code]->ldata[j], 1, 1, fontf);
	}
	return GLF_OK;
}


/* 
| Function loads font to memory from file
| Return value: GLF_ERROR  - if error
|               >=0 - returned font descriptor (load success)
*/
int glfLoadFont(FILE * fontf)
{
	int i;
	char flag; /* Temporary flag */

	/* First we find free font descriptor */
	flag = 0; /* Descriptor not found yet */
	for (i=0; i<MAX_FONTS; i++)
		if (fonts[i] == NULL)
		{
			/* Initialize this font */
			fonts[i] = (struct glf_font *)malloc(sizeof(struct glf_font));
			flag = 1;
			break;
		}

	if (!flag) return GLF_ERROR; /* Free font not found */
	if (ReadFont(fontf, fonts[i]) == GLF_OK) 
	{
		curfont = i; /* Set curfont to just loaded font */
		return i;
	}
  
	if (fonts[i] != NULL)
	{
		free(fonts[i]);
		fonts[i] = NULL;
	}
	return GLF_ERROR;
}

/* 
| Unload current font from memory
| Return value: GLF_OK  - if all OK
|               GLF_ERROR -  if error
*/
int glfUnloadFont()
{
	int i;
  
	if ((curfont<0) || (fonts[curfont] == NULL)) return GLF_ERROR;
  
	for (i=0; i<256; i++)
	{
		if (fonts[curfont]->symbols[i] != NULL)
		{
			free(fonts[curfont]->symbols[i]->vdata);
			free(fonts[curfont]->symbols[i]->fdata);
			free(fonts[curfont]->symbols[i]->ldata);
			free(fonts[curfont]->symbols[i]);
		}
	}

	free(fonts[curfont]);
	fonts[curfont] = NULL;
	curfont = -1;
	return GLF_OK;
}

/* Unload font by font descriptor */
int glfUnloadFontD(int font_descriptor)
{
	int temp;

	if ((font_descriptor < 0) || (fonts[font_descriptor] == NULL)) return GLF_ERROR;

	temp = curfont;
	curfont = font_descriptor;
	glfUnloadFont();
	if (temp != font_descriptor) curfont = temp;
	else curfont = -1;
	return GLF_OK;
}

void glfDrawWiredSymbol(char s)
{
	int i, cur_line;
	float *tvp; /* temporary vertex pointer */
	float x, y;
  
	if ((curfont < 0) || (fonts[curfont] == NULL)) return;
	if (fonts[curfont]->symbols[s] == NULL) return;

	disp__glBegin(GL_LINE_LOOP);
	tvp = fonts[curfont]->symbols[s]->vdata;
	cur_line = 0;
	for (i=0; i<fonts[curfont]->symbols[s]->vertexs; i++)
	{
		x = *tvp++;
		y = *tvp++;
		disp__glVertex2f(x, y);
		if (fonts[curfont]->symbols[s]->ldata[cur_line] == i)
		{
			disp__glEnd();
			cur_line++;
			if (cur_line < fonts[curfont]->symbols[s]->lines) disp__glBegin(GL_LINE_LOOP);
			else break; /* No more lines */
		}
	}
}

/* Draw wired symbol by font_descriptor */
void glfDrawWiredSymbolF(int font_descriptor, char s)
{
	int temp;

	temp = curfont;
	curfont = font_descriptor;
	glfDrawWiredSymbol(s);
	curfont = temp;
}

static void DrawString(const char *s, void (*funct) (char))
{
	int i;
	float sda, sdb;
	float distance;

	if (!s) return;
	if (!*s) return;
	if (curfont == -1) return;

	/* Calculate correction (if string centering enabled) */
	if (m_string_center)
	{
		distance = 0;
		for (i=0; i<(int)strlen(s); i++)
		{
			if ((fonts[curfont]->symbols[s[i]] == NULL) || (s[i] == ' ')) 
			{
				if (m_direction == GLF_LEFT || m_direction == GLF_UP) distance += SpaceSize;
				else distance -= SpaceSize;
			}
			else
				if (i < ((int)strlen(s)-1))
				{
					if (s[i+1] == ' ') 
					{
						if (m_direction == GLF_LEFT || m_direction == GLF_UP) distance += SymbolDist;
						else distance -= SymbolDist;
					}
					else
					{
						if (fonts[curfont]->symbols[s[i+1]] == NULL) continue;
	
						if (m_direction == GLF_LEFT || m_direction == GLF_RIGHT)
						{
							sda = (float)fabs(fonts[curfont]->symbols[s[i]]->rightx);
							sdb = (float)fabs(fonts[curfont]->symbols[s[i+1]]->leftx);
							if (m_direction == GLF_LEFT) distance += sda+sdb+SymbolDist;
							else distance -= sda+sdb+SymbolDist;
						}
						else
						{
							sda = (float)fabs(fonts[curfont]->symbols[s[i]]->topy);
							sdb = (float)fabs(fonts[curfont]->symbols[s[i]]->bottomy);
							if (m_direction == GLF_DOWN) distance -= sda+sdb+SymbolDist;
							else distance += sda+sdb+SymbolDist;
						}
					}
				}
		}
	}

	disp__glPushMatrix();
	
	/* Rotate if needed */
	if (RotateAngle != 0.0f) disp__glRotatef(RotateAngle, 0, 0, 1);

	/* Correct string position */
	if (m_string_center)
	{
		switch (m_direction)
		{
			case GLF_LEFT : disp__glTranslatef(-distance/2, 0, 0); break;
			case GLF_RIGHT : disp__glTranslatef(distance/2, 0, 0); break;
			case GLF_UP : disp__glTranslatef(0, distance/2, 0); break;
			case GLF_DOWN : disp__glTranslatef(0, -distance/2, 0); break;
		}
	}
	else if (s[0] != ' ')
	{
		switch (m_direction)
		{
			case GLF_LEFT : disp__glTranslatef(-(1-(float)fabs(fonts[curfont]->symbols[s[0]]->leftx)), 0, 0); break;
			case GLF_RIGHT : disp__glTranslatef((1-(float)fabs(fonts[curfont]->symbols[s[0]]->rightx)), 0, 0); break;
			case GLF_UP : disp__glTranslatef(0, (1-(float)fabs(fonts[curfont]->symbols[s[0]]->topy)), 0); break;
			case GLF_DOWN : disp__glTranslatef(0, -(1-(float)fabs(fonts[curfont]->symbols[s[0]]->bottomy)), 0); break;
		}
	}

	/* Start to draw our string */
	for (i=0; i<(int)strlen(s); i++)
	{
		if (s[i] != ' ') (*funct) (s[i]);
		if ((fonts[curfont]->symbols[s[i]] == NULL) || (s[i] == ' '))
		{
			switch (m_direction)
			{
				case GLF_LEFT : disp__glTranslatef(SpaceSize, 0, 0); break;
				case GLF_RIGHT : disp__glTranslatef(-SpaceSize, 0, 0); break;
				case GLF_UP : disp__glTranslatef(0, SpaceSize, 0); break;
				case GLF_DOWN : disp__glTranslatef(0, -SpaceSize, 0); break;
			}
		}
		else
		{
			if (i < ((int)strlen(s)-1))		
			{
				if (s[i+1] == ' ') 
				{
					switch (m_direction)
					{
						case GLF_LEFT : disp__glTranslatef(SymbolDist, 0, 0); break;
						case GLF_RIGHT : disp__glTranslatef(-SymbolDist, 0, 0); break;
						case GLF_UP : disp__glTranslatef(0, SymbolDist, 0); break;
						case GLF_DOWN : disp__glTranslatef(0, -SymbolDist, 0); break;
					}
				}
				else
				{
					if (fonts[curfont]->symbols[s[i+1]] == NULL) continue;
					
					if (m_direction == GLF_LEFT || m_direction == GLF_RIGHT)
					{
						if (m_direction == GLF_LEFT)
						{
							sda = (float)fabs(fonts[curfont]->symbols[s[i]]->rightx);
							sdb = (float)fabs(fonts[curfont]->symbols[s[i+1]]->leftx);
						}
						else
						{
							sda = (float)fabs(fonts[curfont]->symbols[s[i+1]]->rightx);
							sdb = (float)fabs(fonts[curfont]->symbols[s[i]]->leftx);
						}
					
						if (m_direction == GLF_LEFT) disp__glTranslatef(sda+sdb+SymbolDist, 0, 0);
						else disp__glTranslatef(-(sda+sdb+SymbolDist), 0, 0);
					}
					else
					{
						if (m_direction == GLF_DOWN) 
						{
							sda = (float)fabs(fonts[curfont]->symbols[s[i]]->topy);
							sdb = (float)fabs(fonts[curfont]->symbols[s[i+1]]->bottomy);
						}
						else
						{
							sda = (float)fabs(fonts[curfont]->symbols[s[i+1]]->topy);
							sdb = (float)fabs(fonts[curfont]->symbols[s[i]]->bottomy);		
						}
						
						if (m_direction == GLF_DOWN) disp__glTranslatef(0, -(sda+sdb+SymbolDist), 0);
						else disp__glTranslatef(0, sda+sdb+SymbolDist, 0);
					}
					
				}
			}
		}
	}
	disp__glPopMatrix();
}

void glfDrawWiredString(const char *s)
{
	DrawString(s, &glfDrawWiredSymbol);
}

/* Draw wired string by font_descriptor */
void glfDrawWiredStringF(int font_descriptor, const char *s)
{
	int temp;

	temp = curfont;
	curfont = font_descriptor;
	DrawString(s, &glfDrawWiredSymbol);
	curfont = temp;
}

void glfDrawSolidSymbol(char s)
{
	unsigned char *b; /* Face pointer   */
	float *vp;        /* Vertex pointer */
	int i, j;
	float x, y;
	float temp_color[4];

	if ((curfont<0) || (fonts[curfont] == NULL)) return;
  
	if (fonts[curfont]->symbols[s] == NULL) return;

	b = fonts[curfont]->symbols[s]->fdata;
	vp = fonts[curfont]->symbols[s]->vdata;

	disp__glBegin(GL_TRIANGLES);   
	for (i=0; i<fonts[curfont]->symbols[s]->facets; i++)
	{
		for (j=0; j<3; j++)
		{
			x = vp[*b*2];
			y = vp[*b*2+1];
			if (texturing == GLF_YES) disp__glTexCoord2f((x+1)/2, (y+1)/2);
			disp__glVertex2f(x, y);
			b++;
		}
	}
	disp__glEnd();

	/* Draw contour, if enabled */
	if (contouring == GLF_YES) 
	{
		disp__glGetFloatv(GL_CURRENT_COLOR, temp_color);
		disp__glColor4f(contouring_color.r, contouring_color.g, contouring_color.b, contouring_color.a);
		glfDrawWiredSymbol(s);
		disp__glColor4fv(temp_color);
	}
}

/* Draw solid symbol by font_descriptor */
void glfDrawSolidSymbolF(int font_descriptor, char s)
{
	int temp;

	temp = curfont;
	curfont = font_descriptor;
	glfDrawSolidSymbol(s);
	curfont = temp;
}

void glfDrawSolidString(const char *s)
{
	DrawString(s, &glfDrawSolidSymbol);
}

/* Draw solid string by font_descriptor */
void glfDrawSolidStringF(int font_descriptor, const char *s)
{
	int temp;

	temp = curfont;
	curfont = font_descriptor;
	DrawString(s, &glfDrawSolidSymbol);
	curfont = temp;
}


/* ------------ 3D Wired text drawing ---------------------- */

void glfDraw3DWiredSymbol(char s)
{
	int i, cur_line;
	float *tvp; /* temp vertex pointer */
	float x, y;
  
	if ((curfont<0) || (fonts[curfont] == NULL)) return;
	if (fonts[curfont]->symbols[(int)s] == NULL) return;

	/* Draw front symbol */
	disp__glBegin(GL_LINE_LOOP);
	tvp = fonts[curfont]->symbols[s]->vdata;
	cur_line = 0;
	for (i=0; i<fonts[curfont]->symbols[s]->vertexs; i++)
	{
		x = *tvp;
		tvp++;
		y = *tvp;
		tvp++;
		disp__glVertex3f(x, y, 1);
		if (fonts[curfont]->symbols[s]->ldata[cur_line] == i)
		{
			disp__glEnd();
			cur_line++;
			if (cur_line < fonts[curfont]->symbols[s]->lines) disp__glBegin(GL_LINE_LOOP);
			else break; /* No more lines */
		}
	}

	/* Draw back symbol */
	disp__glBegin(GL_LINE_LOOP);
	tvp = fonts[curfont]->symbols[s]->vdata;
	cur_line = 0;
	for (i=0; i<fonts[curfont]->symbols[s]->vertexs; i++)
	{
		x = *tvp;
	    tvp++;
		y = *tvp;
		tvp++;
		disp__glVertex3f(x, y, 1+SymbolDepth);
		if (fonts[curfont]->symbols[s]->ldata[cur_line] == i)
		{
			disp__glEnd();
			cur_line++;
			if (cur_line < fonts[curfont]->symbols[s]->lines) disp__glBegin(GL_LINE_LOOP);
			else break; /* No more lines */
		}
	}

	/* Draw lines between back and front symbols */
	disp__glBegin(GL_LINES);
	tvp = fonts[curfont]->symbols[s]->vdata;
	for (i=0; i<fonts[curfont]->symbols[s]->vertexs; i++)
	{
		x = *tvp;
		tvp++;
		y = *tvp;
		tvp++;
		disp__glVertex3f(x, y, 1);
		disp__glVertex3f(x, y, 1+SymbolDepth);
	}
	disp__glEnd();
}

/* Draw 3D wired symbol by font_descriptor */
void glfDraw3DWiredSymbolF(int font_descriptor, char s)
{
	int temp;

	temp = curfont;
	curfont = font_descriptor;
	glfDraw3DWiredSymbol(s);
	curfont = temp;
}

void glfDraw3DWiredString(const char *s)
{
	DrawString(s, &glfDraw3DWiredSymbol);
}

/* Draw 3D wired string by font_descriptor */
void glfDraw3DWiredStringF(int font_descriptor, const char *s)
{
	int temp;

	temp = curfont;
	curfont = font_descriptor;
	DrawString(s, &glfDraw3DWiredSymbol);
	curfont = temp;
}

/* ------------ 3D Solid text drawing ---------------------- */

void glfDraw3DSolidSymbol(char s)
{
	int i, j, cur_line, flag;
	float x, y, bx, by;
	unsigned char *b; /* Face pointer   */
	float *vp;        /* Vertex pointer */
	float *tvp;       /* temp vertex pointer */
	float temp_color[4];
	GLboolean light_temp;
  
	if ((curfont<0) || (fonts[curfont] == NULL)) return;
	if (fonts[curfont]->symbols[(int)s] == NULL) return;

	b = fonts[curfont]->symbols[s]->fdata;
	vp = fonts[curfont]->symbols[s]->vdata;

	disp__glBegin(GL_TRIANGLES);   
	disp__glNormal3f(0, 0, 1);
	for (i=0; i<fonts[curfont]->symbols[s]->facets; i++)
	{
		b += 2;
		for (j=0; j<3; j++)
		{
			x = vp[*b*2];
			y = vp[*b*2+1];
			disp__glVertex3f(x, y, 1+SymbolDepth);
			b--;
		}
		b += 4;
	}
	disp__glEnd();

	b = fonts[curfont]->symbols[s]->fdata;
	vp = fonts[curfont]->symbols[s]->vdata;
  
	disp__glBegin(GL_TRIANGLES);   
	disp__glNormal3f(0, 0, -1);
	for (i=0; i<fonts[curfont]->symbols[s]->facets; i++)
	{
		for (j=0; j<3; j++)
		{
			x = vp[*b*2];
			y = vp[*b*2+1];
			disp__glVertex3f(x, y, 1);
			b++;
		}
	}
	disp__glEnd();

	flag = 0;
	disp__glBegin(GL_QUAD_STRIP);
	tvp = fonts[curfont]->symbols[s]->vdata;
	cur_line = 0;
	for (i=0; i<fonts[curfont]->symbols[s]->vertexs; i++)
	{
		x = *tvp;
		tvp++;
		y = *tvp;
		tvp++;
		if (!flag) 
		{
			bx = x;
			by = y;
			flag = 1;
		}
		disp__glNormal3f(x, y, 0);
		disp__glVertex3f(x, y, 1);
		disp__glVertex3f(x, y, 1+SymbolDepth);
		if (fonts[curfont]->symbols[s]->ldata[cur_line] == i)
		{
			disp__glVertex3f(bx, by, 1);
			disp__glVertex3f(bx, by, 1+SymbolDepth);
			flag = 0;
			disp__glEnd();
			cur_line++;
			if (cur_line < fonts[curfont]->symbols[s]->lines) disp__glBegin(GL_QUAD_STRIP);
			else break; /* No more lines */
		}
	}

	/* Draw contour, if enabled */
	if (contouring == GLF_YES) 
	{
		disp__glGetBooleanv(GL_LIGHTING, &light_temp);
		disp__glDisable(GL_LIGHTING);
		disp__glGetFloatv(GL_CURRENT_COLOR, temp_color);
		disp__glColor4f(contouring_color.r, contouring_color.g, contouring_color.b, contouring_color.a);
		glfDraw3DWiredSymbol(s);
		disp__glColor4fv(temp_color);
		if (light_temp) disp__glEnable(GL_LIGHTING);
	}
}

/* Draw 3D solid symbol by font_descriptor */
void glfDraw3DSolidSymbolF(int font_descriptor, char s)
{
	int temp;

	temp = curfont;
	curfont = font_descriptor;
	glfDraw3DSolidSymbol(s);
	curfont = temp;
}

void glfDraw3DSolidString(const char *s)
{
	DrawString(s, &glfDraw3DSolidSymbol);
}

/* Draw 3D solid string by font_descriptor */
void glfDraw3DSolidStringF(int font_descriptor, const char *s)
{
	int temp;

	temp = curfont;
	curfont = font_descriptor;
	DrawString(s, &glfDraw3DSolidSymbol);
	curfont = temp;
}

/* Get the size a string will have on screen */
void glfGetStringBoundsF(int fd, const char *s, float *minx, float *miny, float *maxx, float *maxy)
{
	struct glf_font *font;
	int i;
	float sda, sdb, cw = 0, minxx = 10;
	float top = 10, bottom = -10;

	if (fd < 0 || fd > (MAX_FONTS-1)) return;
	font = fonts[fd];

	if (font == NULL) return;

	if (font->symbols[s[0]])
		minxx = font->symbols[s[0]]->leftx;
	else
		minxx = 0.0;

	for (i=0; i<(int)strlen(s); i++)
	{
		if ((font->symbols[s[i]] == NULL) || (s[i] == ' '))
			cw += SpaceSize;
		else
		{
			sdb = -font->symbols[(int)s[i]]->leftx;
			sda = font->symbols[(int)s[i]]->rightx;

			cw += sda+sdb+SymbolDist;

			/* Update top/bottom bounds */
			if (font->symbols[(int)s[i]]->bottomy > bottom)
				bottom = font->symbols[(int)s[i]]->bottomy;

			if (font->symbols[(int)s[i]]->topy < top)
				top = font->symbols[(int)s[i]]->topy;
		}
	}

	cw += minxx;

	if ((maxx) && (maxy))
	{
		*maxx = cw;
		*maxy = bottom;
	}

	if ((minx) && (miny))
	{
		*minx = minxx;
		*miny = top;
	}
}

void glfGetStringBounds(const char *s, float *minx, float *miny, float *maxx, float *maxy)
{
	glfGetStringBoundsF(curfont, s, minx, miny, maxx, maxy);
}

void glfSetSymbolSpace(float sp)
{
	SymbolDist = sp;
}

float glfGetSymbolSpace()
{
	return SymbolDist;
}

void glfSetSpaceSize(float sp)
{
	SpaceSize = sp;
}

float glfGetSpaceSize()
{
	return SpaceSize;
}

void glfSetSymbolDepth(float dpth)
{
	SymbolDepth = dpth;
}

float glfGetSymbolDepth()
{
	return SymbolDepth;
}

int glfSetCurrentFont(int Font_Descriptor)
{
	if ((Font_Descriptor < 0) || (fonts[Font_Descriptor] == NULL)) return GLF_ERROR;
  
	curfont = Font_Descriptor;
	return GLF_OK;
}

int glfGetCurrentFont()
{
	return curfont;
}

void glfSetAnchorPoint(int anchp)
{
	if ((anchp >= GLF_LEFT_UP) && (anchp <= GLF_RIGHT_DOWN))
		ap = anchp;
}

void glfSetContourColor(float r, float g, float b, float a)
{
	contouring_color.r = r;
	contouring_color.g = g;
	contouring_color.b = b;
	contouring_color.a = a;
}

void glfEnable(int what)
{
	switch (what) 
	{
		case GLF_CONSOLE_MESSAGES: console_msg = GLF_YES; break;
		case GLF_TEXTURING: texturing = GLF_YES; break;
		case GLF_CONSOLE_CURSOR: conCursor = GLF_YES; break;
		case GLF_CONTOURING: contouring = GLF_YES; break;
	}
}

void glfDisable(int what)
{
	switch (what) 
	{
		case GLF_CONSOLE_MESSAGES: console_msg = GLF_NO; break;
		case GLF_TEXTURING: texturing = GLF_NO; break;
		case GLF_CONSOLE_CURSOR: conCursor = GLF_NO; break;
		case GLF_CONTOURING: contouring = GLF_NO; break;
	}
}

/* ---------------- Console functions ---------------------- */

void glfSetConsoleParam(int width, int height)
{
	if (conData) free(conData);

	conWidth = width;
	conHeight = height;
	conData = (char *)malloc(width*height);
	glfConsoleClear();
}

int glfSetConsoleFont(int Font_Descriptor)
{
	if ((Font_Descriptor < 0) || (fonts[Font_Descriptor] == NULL)) return GLF_ERROR;
  
	conFont = Font_Descriptor;
	return GLF_OK;
}

void glfConsoleClear()
{
	memset(conData, 0, conWidth*conHeight);
	conx = 0;
	cony = 0;
}

void glfPrint(const char *s, int lenght)
{
	int i;

	for (i=0; i<lenght; i++)
	{
		if (s[i] > 31)
		{
			conData[cony*conWidth+conx] = s[i];
			conx++;
		} else
		if (s[i] == '\n') conx = conWidth;
		if (conx >= conWidth)
		{
			conx = 0;
			cony++;
			if (cony >= conHeight)
			{
				/* Shift all console contents up */
				memcpy(conData, &conData[conWidth], conWidth*(conHeight-1));
				/* Fill bottom line by spaces */
				memset(&conData[conWidth*(conHeight-1)], 0, conWidth);
				cony = conHeight-1;
			}
		}
	}
}

void glfPrintString(const char *s)
{
	glfPrint(s, strlen(s));
}

void glfPrintChar(char s)
{
	glfPrint(&s, 1);
}

void glfConsoleDraw()
{
	int i, j;
	char s[512];

	for (i=0; i<conHeight; i++)
	{
		memcpy(s, &conData[i*conWidth], conWidth);
		s[conWidth] = 0;
		if ((conCursor == GLF_YES) && (i == cony))
		{
			conCursorCount--;
			if (conCursorCount < 0)
			{
				conCursorCount = conCursorBlink;
				if (conCursorMode == GLF_YES) conCursorMode = GLF_NO;
				else conCursorMode = GLF_YES;
			}

			if (conCursorMode == GLF_YES)
			for (j=0; j<conWidth; j++)
			{
				if (!s[j])
				{
					s[j] = '_'; /* Draw cursor */
					s[j+1] = 0;
					break;
				}
			}
		}
		glfDrawSolidStringF(conFont, s);
		disp__glTranslatef(0, -2, 0);
	}
}

void glfSetCursorBlinkRate(int Rate)
{
	if (Rate > 0)
	{
		conCursorBlink = Rate;
		conCursorCount = Rate;
		conCursorMode = GLF_YES;
	}
}

/* Set string centering for vector fonts */
void glfStringCentering(GLboolean center)
{
	m_string_center = center;
}

/* String direction for vector font (GLF_LEFT, GLF_RIGHT, GLF_UP, GLF_DOWN) */
/* GLF_LEFT by default */
void glfStringDirection(GLuint direction)
{
	if (direction == GLF_LEFT || direction == GLF_RIGHT || 
		direction == GLF_UP || direction == GLF_DOWN) m_direction = direction;
}

/* Get current text direction */
GLuint glfGetStringDirection()
{
	return m_direction;
}


/* Get string centering for vector fonts */
GLboolean glfGetStringCentering()
{
	return m_string_center;
}

/* Set rotate angle for vector fonts */
void glfSetRotateAngle(float angle)
{
	RotateAngle = angle;
}


/*
   --------------------------------------------------------------------------------- 
   ------------------------ Work with bitmapped fonts ------------------------------ 
   --------------------------------------------------------------------------------- 
*/


/* Some color conversions */
static void bwtorgba(unsigned char *b,unsigned char *l,int n)
{
    while (n--)
    {
		l[0] = *b; l[1] = *b;
		l[2] = *b; l[3] = 0xff;
		l += 4; b++;
    }
}

static void latorgba(unsigned char *b, unsigned char *a,unsigned char *l,int n)
{
	while(n--)
	{
		l[0] = *b; l[1] = *b;
		l[2] = *b; l[3] = *a;
		l += 4; b++; a++;
    }
}

static void rgbtorgba(unsigned char *r,unsigned char *g,unsigned char *b,unsigned char *l,int n)
{
    while(n--)
	{
		l[0] = r[0]; l[1] = g[0];
		l[2] = b[0]; l[3] = 0xff;
		l += 4; r++; g++; b++;
    }
}

static void rgbatorgba(unsigned char *r,unsigned char *g,unsigned char *b,unsigned char *a,unsigned char *l,int n)
{
    while(n--)
	{
		l[0] = r[0]; l[1] = g[0];
		l[2] = b[0]; l[3] = a[0];
		l += 4; r++; g++; b++; a++;
    }
}

typedef struct _ImageRec
{
    unsigned short imagic;
    unsigned short type;
    unsigned short dim;
    unsigned short xsize, ysize, zsize;
    unsigned int min, max;
    unsigned int wasteBytes;
    char name[80];
    unsigned long colorMap;
    FILE *file;
    unsigned char *tmp, *tmpR, *tmpG, *tmpB;
    unsigned long rleEnd;
    unsigned int *rowStart;
    int *rowSize;
} ImageRec;

static void ConvertShort(unsigned short *array, long length)
{
    unsigned b1, b2;
    unsigned char *ptr;

    ptr = (unsigned char *)array;
    while (length--) {
	b1 = *ptr++;
	b2 = *ptr++;
	*array++ = (b1 << 8) | (b2);
    }
}

static void ConvertLong(unsigned *array, long length)
{
    unsigned b1, b2, b3, b4;
    unsigned char *ptr;

    ptr = (unsigned char *)array;
    while (length--)
	{
		b1 = *ptr++;
		b2 = *ptr++;
		b3 = *ptr++;
		b4 = *ptr++;
		*array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);
    }
}

/* Open RGB Image */
static ImageRec *ImageOpen(FILE *f)
{
    union
	{
		int testWord;
		char testByte[4];
    } endianTest;

    ImageRec *image;
    int swapFlag;
    int x;

    endianTest.testWord = 1;
    if (endianTest.testByte[0] == 1) swapFlag = 1;
	else swapFlag = 0;

    image = (ImageRec *)malloc(sizeof(ImageRec));
    if (image == NULL)
	{
		fprintf(stderr, "Out of memory!\n");
		exit(1);
    }

	image->file = f;

    fread(image, 1, 12, image->file);

    if (swapFlag) ConvertShort(&image->imagic, 6);

    image->tmp	= (unsigned char *)malloc(image->xsize*256);
    image->tmpR = (unsigned char *)malloc(image->xsize*256);
    image->tmpG = (unsigned char *)malloc(image->xsize*256);
    image->tmpB = (unsigned char *)malloc(image->xsize*256);
    if (image->tmp == NULL || image->tmpR == NULL || image->tmpG == NULL ||
	image->tmpB == NULL) 
	{
		fprintf(stderr, "Out of memory!\n");
		exit(1);
    }

    if ((image->type & 0xFF00) == 0x0100)
	{
		x = image->ysize * image->zsize * sizeof(unsigned);
		image->rowStart = (unsigned *)malloc(x);
		image->rowSize = (int *)malloc(x);
		if (image->rowStart == NULL || image->rowSize == NULL)
		{
		    fprintf(stderr, "Out of memory!\n");
		    exit(1);
		}
		image->rleEnd = 512 + (2 * x);
		fseek(image->file, 512+SEEK_SET_POS, SEEK_SET);
		fread(image->rowStart, 1, x, image->file);
		fread(image->rowSize, 1, x, image->file);
		if (swapFlag)
		{
			ConvertLong(image->rowStart, x/(int)sizeof(unsigned));
			ConvertLong((unsigned *)image->rowSize, x/(int)sizeof(int));
		}
    }
	else
	{
		image->rowStart = NULL;
		image->rowSize = NULL;
    }
    return image;
}

/* Close Image and free data */
static void ImageClose(ImageRec *image)
{
	free(image->tmp);
	free(image->tmpR);
	free(image->tmpG);
	free(image->tmpB);
    free(image->rowSize);
    free(image->rowStart);
    free(image);
}

/* Pixels row decoding (if used RLE encoding) */
static void ImageGetRow(ImageRec *image, unsigned char *buf, int y, int z)
{
    unsigned char *iPtr, *oPtr, pixel;
    int count;

    if ((image->type & 0xFF00) == 0x0100)
	{
		fseek(image->file, (long)image->rowStart[y+z*image->ysize]+SEEK_SET_POS, SEEK_SET);
		fread(image->tmp, 1, (unsigned int)image->rowSize[y+z*image->ysize], image->file);

		iPtr = image->tmp;
		oPtr = buf;
		for (;;)
		{
			pixel = *iPtr++;
		    count = (int)(pixel & 0x7F);
		    if (!count) return;
		    if (pixel & 0x80) while (count--) *oPtr++ = *iPtr++;
			else
			{
				pixel = *iPtr++;
				while (count--) *oPtr++ = pixel;
		    }
		}
    }
	else
	{
		fseek(image->file, 512+(y*image->xsize)+(z*image->xsize*image->ysize)+SEEK_SET_POS, SEEK_SET);
		fread(buf, 1, image->xsize, image->file);
    }
}

/* Read SGI (RGB) Image from file */
static unsigned *read_texture(FILE *f, int *width, int *height, int *components)
{
    unsigned *base, *lptr;
    unsigned char *rbuf, *gbuf, *bbuf, *abuf;
    ImageRec *image;
    int y;

    image = ImageOpen(f);
    
    if (!image)	return NULL;
    (*width) = image->xsize;
    (*height) = image->ysize;
    (*components) = image->zsize;
    
	base = (unsigned *)malloc(image->xsize*image->ysize*sizeof(unsigned));
    rbuf = (unsigned char *)malloc(image->xsize*sizeof(unsigned char));
    gbuf = (unsigned char *)malloc(image->xsize*sizeof(unsigned char));
    bbuf = (unsigned char *)malloc(image->xsize*sizeof(unsigned char));
    abuf = (unsigned char *)malloc(image->xsize*sizeof(unsigned char));
    
	if(!base || !rbuf || !gbuf || !bbuf) return NULL;
    lptr = base;
    for (y=0; y<image->ysize; y++)
	{
		if(image->zsize >= 4)
		{
			ImageGetRow(image, rbuf, y, 0);
			ImageGetRow(image, gbuf, y, 1);
			ImageGetRow(image, bbuf, y, 2);
			ImageGetRow(image, abuf, y, 3);
			rgbatorgba(rbuf,gbuf,bbuf,abuf,(unsigned char *)lptr,image->xsize);
			lptr += image->xsize;
		}
		else if(image->zsize == 3)
		{
			ImageGetRow(image,rbuf, y, 0);
			ImageGetRow(image,gbuf, y, 1);
			ImageGetRow(image,bbuf, y, 2);
			rgbtorgba(rbuf,gbuf,bbuf,(unsigned char *)lptr,image->xsize);
			lptr += image->xsize;
		}
		else if(image->zsize == 2)
		{
			ImageGetRow(image,rbuf, y, 0);
			ImageGetRow(image,abuf, y, 1);
			latorgba(rbuf,abuf,(unsigned char *)lptr,image->xsize);
			lptr += image->xsize;
		}
		else
		{
			ImageGetRow(image, rbuf, y, 0);
			bwtorgba(rbuf,(unsigned char *)lptr,image->xsize);
			lptr += image->xsize;
		}
    }
    ImageClose(image);
    free(rbuf);
    free(gbuf);
    free(bbuf);
    free(abuf);

    return (unsigned *) base;
}

/* Font texture conversion to mask texture */
unsigned* texture_to_mask(unsigned* tex, int width, int height)
{
	int nSize, i;
	unsigned *ret;

	nSize = width * height;
	ret = (unsigned *)malloc(nSize * sizeof(unsigned));
	for (i=0; i<nSize; i++)	ret[i] = tex[i] & 0x00ffffff ? 0 : 0x00ffffff;

	return ret;
}

/* Load BMF file format, function return bitmap font descriptor */
int glfLoadBFont(FILE * fontf)
{
	char Header[4];
	char FontName[97];
	int i, flag;
	int LEndian;
	float tx, ty, tw, th;
	unsigned char temp, *tp;
	unsigned *texture;	/* Texture image */
	unsigned *mask;	/* Mask texture */
	int twidth, theight, tcomp;	/* Image parameters */
	float *temp_width;

	LEndian = LittleEndian();

	/* Get header */
	fread(Header, 1, 3, fontf);
	Header[3] = 0;
	if (strcmp(Header, "BMF")) return GLF_ERROR; /* Not BMF format */

	/* Get font name */
	fread(FontName, 1, 96, fontf);
	FontName[96] = 0;

	/* Allocate space for temp widths */
	
	temp_width = (float *)malloc(sizeof(float)*256);

	/* Read all 256 symbols information */
	for (i=0; i<256; i++)
	{
		fread(&tx, 4, 1, fontf);
		fread(&ty, 4, 1, fontf);
		fread(&tw, 4, 1, fontf);
		fread(&th, 4, 1, fontf);

		if (!LEndian)
		{
			tp = (unsigned char *)&tx;
			temp = tp[0]; tp[0] = tp[3]; tp[3] = temp;
			temp = tp[1]; tp[1] = tp[2]; tp[2] = temp;
			tp = (unsigned char *)&ty;
			temp = tp[0]; tp[0] = tp[3]; tp[3] = temp;
			temp = tp[1]; tp[1] = tp[2]; tp[2] = temp;
			tp = (unsigned char *)&tw;
			temp = tp[0]; tp[0] = tp[3]; tp[3] = temp;
			temp = tp[1]; tp[1] = tp[2]; tp[2] = temp;
			tp = (unsigned char *)&th;
			temp = tp[0]; tp[0] = tp[3]; tp[3] = temp;
			temp = tp[1]; tp[1] = tp[2]; tp[2] = temp;
		}
		
		Symbols[i].x = tx;
		Symbols[i].y = ty;
		Symbols[i].width  = tw;
		Symbols[i].height = th;
		temp_width[i] = tw;
	}

	/* Read texture image from file and build texture */
	texture = read_texture(fontf, &twidth, &theight, &tcomp);
	/* Generate mask texture */
	mask = texture_to_mask(texture, twidth, theight);

	/* Find unused font descriptor */
	flag = 0;
	for (i=0; i<MAX_FONTS; i++)
		if (bmf_in_use[i] == 0)
		{
			/* Initialize this font */
			bmf_in_use[i] = 1;
			bmf_curfont = i;
			flag = 1;
			break;
		}
	if (!flag) /* Not enought space for new texture */
	{
		free(texture);
		free(mask);
		free(temp_width);
		return -1;
	}

	m_widths[bmf_curfont].width = temp_width;

	/* Generating textures for font and mask */
	disp__glGenTextures(1, &bmf_texture[bmf_curfont]);
	disp__glGenTextures(1, &bmf_mask[bmf_curfont]);


	disp__glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	/* Build font texture */
	disp__glBindTexture(GL_TEXTURE_2D, bmf_texture[bmf_curfont]);
	disp__glTexImage2D(GL_TEXTURE_2D, 0, 3, twidth, theight, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
	
	/* Linear filtering for better quality */
	disp__glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	disp__glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	
	/* Build mask texture */
	disp__glBindTexture(GL_TEXTURE_2D, bmf_mask[bmf_curfont]);
	disp__glTexImage2D(GL_TEXTURE_2D, 0, 3, twidth, theight, 0, GL_RGBA, GL_UNSIGNED_BYTE, mask);
	disp__glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	disp__glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	free(texture);
	free(mask);

	/* Now build list for each symbol */
	list_base[bmf_curfont] = disp__glGenLists(256);
	for (i=0; i<256; i++)
	{
		disp__glNewList(list_base[bmf_curfont]+i, GL_COMPILE);
		
		disp__glBegin(GL_QUADS);
			disp__glTexCoord2f(Symbols[i].x, Symbols[i].y); disp__glVertex2f(0, 0);
			disp__glTexCoord2f(Symbols[i].x+Symbols[i].width, Symbols[i].y); disp__glVertex2f(Symbols[i].width, 0);
			disp__glTexCoord2f(Symbols[i].x+Symbols[i].width, Symbols[i].y+Symbols[i].height); disp__glVertex2f(Symbols[i].width, Symbols[i].height);
			disp__glTexCoord2f(Symbols[i].x, Symbols[i].y+Symbols[i].height); disp__glVertex2f(0, Symbols[i].height);
		disp__glEnd();
		disp__glTranslatef(Symbols[i].width+sym_space, 0, 0);

		disp__glEndList();
		if (Symbols[i].height > m_max_height[bmf_curfont]) m_max_height[bmf_curfont] = Symbols[i].height;
	}
	return bmf_curfont;
}

/* Unloads bitmap font by descriptor */
int glfUnloadBFontD(int bmf_descriptor)
{
	if ((bmf_descriptor < 0) || (bmf_in_use[bmf_descriptor] == 0)) return GLF_ERROR;

	bmf_in_use[bmf_descriptor] = 0;

	disp__glDeleteTextures(1, &bmf_texture[bmf_descriptor]);
	disp__glDeleteTextures(1, &bmf_mask[bmf_descriptor]);

	if (bmf_descriptor == bmf_curfont) bmf_curfont = -1;

	return GLF_OK;
}

/* Unloads current bitmap font */
int glfUnloadBFont()
{
	return glfUnloadBFontD(bmf_curfont);
}


/* Start bitmap drawing function */
int glfBeginBFont(int Font_Descriptor)
{
	int ret = glfSetCurrentBFont(Font_Descriptor);
	if(ret!=GLF_OK) return ret;

	/* Enable 2D Texturing */
	disp__glGetBooleanv(GL_TEXTURE_2D, &bmf_texturing);
	disp__glEnable(GL_TEXTURE_2D);

	disp__glBindTexture(GL_TEXTURE_2D, bmf_texture[bmf_curfont]);

	return GLF_OK;
}

/* Stop bitmap drawing function */
void glfEndBFont()
{
	/* Return previuos state of texturing */
	if (bmf_texturing) disp__glEnable(GL_TEXTURE_2D);
	else disp__glDisable(GL_TEXTURE_2D);
}

/* Select current BMF font */
int glfSetCurrentBFont(int Font_Descriptor)
{
	if ((Font_Descriptor < 0) || (bmf_in_use[Font_Descriptor] == 0)) return GLF_ERROR;

	bmf_curfont = Font_Descriptor;
	return GLF_OK;
}

/* Get current BMF font */
int glfGetCurrentBFont()
{
	return bmf_curfont;
}

/* Draw one bitmapped symbol by current font */
void glfDrawBSymbol(char s)
{
	if ((bmf_curfont < 0) || (bmf_in_use[bmf_curfont] == 0)) return;
	
	disp__glCallList(list_base[bmf_curfont]+(unsigned char)s);
}

/* Draw bitmapped string */
void glfDrawBString(const char *s)
{
	GLfloat temp_trans;
	int i;

	temp_trans = 0;

	if ((bmf_curfont < 0) || (bmf_in_use[bmf_curfont] == 0)) return;

	/* Calculate length of all string */
	for (i=0; i<(int)strlen(s); i++)
		temp_trans += m_widths[bmf_curfont].width[s[i]] + sym_space;
	
	disp__glListBase(list_base[bmf_curfont]);
	if (m_bitmap_string_center == GL_TRUE)
	{
		disp__glPushMatrix();
		disp__glTranslatef(-temp_trans/2, 0, 0);
	}
	disp__glCallLists(strlen(s), GL_UNSIGNED_BYTE, (const unsigned char *)s);
	if (m_bitmap_string_center == GL_TRUE)	disp__glPopMatrix();
}

void glfDrawBMaskSymbol(char s)
{
	if ((bmf_curfont < 0) || (bmf_in_use[bmf_curfont] == 0)) return;
	
	disp__glPushMatrix();
	disp__glPushAttrib(GL_CURRENT_BIT);
	
	/* Draw the text as a mask in black */
	disp__glColor3ub(0xff, 0xff, 0xff);
	disp__glBlendFunc(GL_DST_COLOR, GL_ZERO);
	disp__glBindTexture(GL_TEXTURE_2D, bmf_mask[bmf_curfont]);

	disp__glCallList(list_base[bmf_curfont]+(unsigned char)s);

	disp__glPopAttrib();
	disp__glPopMatrix();
	
	disp__glBindTexture(GL_TEXTURE_2D, bmf_texture[bmf_curfont]);
	
	/* Now draw the text over only the black bits in the requested color */
	disp__glBlendFunc(GL_ONE, GL_ONE);

	disp__glCallList(list_base[bmf_curfont]+(unsigned char)s);
}

void glfDrawBMaskString(const char *s)
{
	GLfloat temp_trans;
	int i;

	temp_trans = 0;

	if ((bmf_curfont < 0) || (bmf_in_use[bmf_curfont] == 0)) return;

	/* Calculate length of all string */
	for (i=0; i<(int)strlen(s); i++)
		temp_trans += m_widths[bmf_curfont].width[s[i]] + sym_space;

	disp__glPushMatrix();
	disp__glPushAttrib(GL_CURRENT_BIT);
	
	/* Draw the text as a mask in black */
	disp__glColor3ub(0xff, 0xff, 0xff);
	disp__glBlendFunc(GL_DST_COLOR, GL_ZERO);
	disp__glBindTexture(GL_TEXTURE_2D, bmf_mask[bmf_curfont]);

	disp__glListBase(list_base[bmf_curfont]);
	if (m_bitmap_string_center == GL_TRUE)
	{
		disp__glPushMatrix();
		disp__glTranslatef(-temp_trans/2, 0, 0);
	}
	disp__glCallLists(strlen(s), GL_UNSIGNED_BYTE, (const unsigned char *)s);
	if (m_bitmap_string_center == GL_TRUE)	disp__glPopMatrix();

	disp__glPopAttrib();
	disp__glPopMatrix();

	disp__glBindTexture(GL_TEXTURE_2D, bmf_texture[bmf_curfont]);
	
	/* Now draw the text over only the black bits in the requested color */
	disp__glBlendFunc(GL_ONE, GL_ONE);
	disp__glListBase(list_base[bmf_curfont]);
	if (m_bitmap_string_center == GL_TRUE)
	{
		disp__glPushMatrix();
		disp__glTranslatef(-temp_trans/2, 0, 0);
	}
	disp__glCallLists(strlen(s), GL_UNSIGNED_BYTE, (const unsigned char *)s);
	if (m_bitmap_string_center == GL_TRUE)	disp__glPopMatrix();
}

/* Set string centering for bitmap fonts */
void glfBitmapStringCentering(GLboolean center)
{
	m_bitmap_string_center = center;
}

/* Set string centering for bitmap fonts */
GLboolean glfBitmapGetStringCentering()
{
	return m_bitmap_string_center;
}

/* Set rotate angle for bitmap fonts */
void glfSetBRotateAngle(float angle)
{
	RotateAngleB = angle;
}