home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / TGE129D / TGEFONT.CPP < prev    next >
C/C++ Source or Header  |  1993-08-22  |  5KB  |  201 lines

  1. /*****************************************************************************
  2. *       The Graphics Engine version 1.29ßD                                   *
  3. *                                                                            *
  4. *       The Graphics Engine code and documentation are Copyright (c) 1993    *
  5. *       by Matthew Hildebrand.                                               *
  6. *                                                                            *
  7. *       Unauthorised usage or modification of any or all of The Graphics     *
  8. *       Engine is strictly prohibited.                                       *
  9. *****************************************************************************/
  10.  
  11. #include <alloc.h>
  12. #include <dir.h>
  13. #include <dos.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include "tgefont.h"
  17. #include "tge.h"
  18.  
  19.  
  20. //*****
  21. //***** Constructor
  22. //*****
  23.  
  24. Font::Font(char *filename, unsigned char fg, unsigned char bg)
  25. {
  26.   char signature[8];
  27.   unsigned long fileLen;
  28.   FILE *inFile;
  29.  
  30.   //*** Initialize member variables
  31.   rawData = NULL;
  32.   fgColour = fg;
  33.   bgColour = bg;
  34.  
  35.   //*** Open font file
  36.   if ((inFile=fopen(filename,"rb")) == NULL)
  37.     return;
  38.  
  39.   //*** Parse header
  40.   if (!fread(signature, 8, 1, inFile))        // read in signature
  41.     return;
  42.   if (memcmp(signature, "TGEFONT1", 8))        // is signature there?
  43.     return;                    // no, quit
  44.  
  45.   if (!fread(&realCharWide, 2, 1, inFile))      // read in character width
  46.     return;
  47.   if (!fread(&charDeep, 2, 1, inFile))           // read in character depth
  48.     return;
  49.  
  50.   if (realCharWide % 8)                // ensure even multiple of 8
  51.     charWide = realCharWide + 8-(realCharWide%8);
  52.   else
  53.     charWide = realCharWide;
  54.   charSize = charWide * charDeep;        // precalculate for speed
  55.  
  56.   //*** Ensure format is correct (ie., file size is charWide*charDeep+12)
  57.   fseek(inFile, 0L, SEEK_END);            // seek to end of file
  58.   fileLen = ftell(inFile);            // get file length
  59.   fseek(inFile, 12L, SEEK_SET);            // seek past header
  60.  
  61.   fileLen -= 12L;                // remove header length
  62.   if (fileLen != (charSize/8)*256L)           // verify size OK
  63.     return;
  64.  
  65.   //*** Read in character data
  66.   rawData = farmalloc(fileLen);            // allocate some RAM
  67.   if (rawData == NULL)                // was allocation successful?
  68.     return;                    // no, quit
  69.  
  70.   if (!fread(rawData, (unsigned)fileLen, 1, inFile)) // read in font data
  71.   {                        // clean up and quit on error
  72.     farfree(rawData);
  73.     return;
  74.   }
  75.   fclose(inFile);                // close the file
  76.  
  77.   image = farmalloc(charSize + 4L);        // allocate the image buffer
  78.   if (image == NULL)                // was function successful?
  79.   {                        // no, clean up and quit
  80.     farfree(rawData);
  81.     return;
  82.   }
  83.   else
  84.   {
  85.     ((unsigned*)image)[0] = charWide;        // fill in dimensions
  86.     ((unsigned*)image)[1] = charDeep;
  87.   }
  88. }
  89.  
  90.  
  91. //*****
  92. //***** Destructor
  93. //*****
  94.  
  95. Font::~Font(void)
  96. {
  97.   if (rawData)                    // was memory allocated?
  98.     farfree(rawData);                // yes, free it
  99.   if (image)                    // same procedure
  100.     farfree(image);
  101. }
  102.  
  103.  
  104. //*****
  105. //***** Compute the width, in pixels, of the given string
  106. //*****
  107.  
  108. unsigned Font::wide(char *str)
  109. {
  110.   return (realCharWide * strlen(str));
  111. }
  112.  
  113.  
  114. //*****
  115. //***** Write a string at the specified location
  116. //*****
  117.  
  118. void Font::put(int x, int y, char *str)
  119. {
  120.   unsigned count, length;
  121.  
  122.   length = strlen(str);                       // store for speed
  123.   for (count=0; count<length; count++)        // loop for each character
  124.     put(x+count*realCharWide, y, str[count]);    // put the character
  125. }
  126.  
  127.  
  128. //*****
  129. //***** Write a single character at the specified location
  130. //*****
  131.  
  132. void Font::put(int x, int y, char ch)
  133. {
  134.   register unsigned count;
  135.   unsigned char huge *in, huge *out;
  136.   register unsigned char byte;
  137.  
  138.   //*** Initialize pointers
  139.   (void far*)in = rawData;            // pointer to bit-image buffer
  140.   in += (charSize/8) * (unsigned)ch;        // point to correct bit-image
  141.   (void far*)out = image;            // pointer to image buffer
  142.   out += 4;                    // bump past dimension info
  143.  
  144.   //*** Convert font data from 1 bit/pixel format to 1 byte/pixel format
  145.   for (count=0; count<charSize/8; count++)
  146.   {
  147.     byte = *in;                    // get current byte
  148.  
  149.     if ((byte>>7) & 1)
  150.       *out = fgColour;
  151.     else
  152.       *out = bgColour;
  153.     out++;
  154.  
  155.     if ((byte>>6) & 1)
  156.       *out = fgColour;
  157.     else
  158.       *out = bgColour;
  159.     out++;
  160.  
  161.     if ((byte>>5) & 1)
  162.       *out = fgColour;
  163.     else
  164.       *out = bgColour;
  165.     out++;
  166.  
  167.     if ((byte>>4) & 1)
  168.       *out = fgColour;
  169.     else
  170.       *out = bgColour;
  171.     out++;
  172.  
  173.     if ((byte>>3) & 1)
  174.       *out = fgColour;
  175.     else
  176.       *out = bgColour;
  177.     out++;
  178.  
  179.     if ((byte>>2) & 1)
  180.       *out = fgColour;
  181.     else
  182.       *out = bgColour;
  183.     out++;
  184.  
  185.     if ((byte>>1) & 1)
  186.       *out = fgColour;
  187.     else
  188.       *out = bgColour;
  189.     out++;
  190.  
  191.     if ((byte>>0) & 1)
  192.       *out = fgColour;
  193.     else
  194.       *out = bgColour;
  195.     out++;
  196.  
  197.     in++;
  198.   }
  199.  
  200.   putImageInv(x, y, image);              // draw the character
  201. }