home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / TVDEMOS.ZIP / GRAPHAPP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.0 KB  |  161 lines

  1. /*-------------------------------------------------------------------*/
  2. /*                                                                   */
  3. /*   Turbo Vision 1.0                                                */
  4. /*   Turbo Vision Demo                                               */
  5. /*   Copyright (c) 1991 by Borland International                     */
  6. /*                                                                   */
  7. /*   BGI support file for use with Turbo Vision BGI (TVBGI) program. */
  8. /*-------------------------------------------------------------------*/
  9.  
  10. #define Uses_TProgram
  11. #include <tv.h>
  12.  
  13. #if !defined( __GRAPHAPP_H )
  14. #include "Graphapp.h"
  15. #endif  // __GRAPHAPP_H
  16.  
  17. #if !defined( __STRING_H )
  18. #include <string.h>
  19. #endif  // __STRING_H
  20.  
  21. #if !defined( __FSTREAM_H )
  22. #include <fstream.h>
  23. #endif  // __FSTREAM_H
  24.  
  25. #if !defined( __STRSTREA_H )
  26. #include <strstrea.h>
  27. #endif  // __STRSTREA_H
  28.  
  29. #if !defined( __IO_H )
  30. #include <io.h>
  31. #endif  // __IO_H
  32.  
  33.  
  34. //Utility functions
  35.  
  36.  
  37. void freeDriverMem()
  38. {
  39.     if (driverPtr != 0)
  40.     delete (char *)driverPtr;
  41.     driverPtr = NULL;
  42.     driverSize = 0;
  43. }
  44.  
  45. typedef void(*function)();
  46.  
  47.  
  48. Boolean graphAppLoadDriver( int driverNum )
  49. {
  50.     char fileName[MAXSIZE];
  51.     int handle, ccode;
  52.     ostrstream ss(fileName, MAXSIZE);
  53.  
  54.     if (driverNum <= lastDriver)
  55.         {
  56.         if (bgiPath[strlen(bgiPath)-1] != '\\')
  57.             strcat(bgiPath,"\\");
  58.         ss << bgiPath << driverName[driver-1] << ".BGI" << ends;
  59.  
  60.         ifstream f(ss.str(), ios::in|ios::binary);
  61.         if (f)
  62.             {
  63.             handle = f.rdbuf()->fd();
  64.             driverSize = (unsigned int)filelength(handle);
  65.             f.seekg( 0L, ios::beg);
  66.         if (driverSize < (64 * 1024L - 0xF))
  67.                 {
  68.                 driverPtr = NULL;
  69.                 driverPtr = (DriverPtrFunction) new char[driverSize];
  70.                 f.read((char *)driverPtr, ushort(driverSize));
  71.                 if (f)
  72.                     {
  73.                     ccode = registerfarbgidriver(driverPtr);
  74.                     if (ccode >= 0)
  75.                         return True;
  76.                     else
  77.                         freeDriverMem();
  78.                     }
  79.                     f.close();
  80.                 }
  81.             }
  82.         }
  83.     return False;
  84. }
  85.  
  86. //------------------------------------------------------------------//
  87. // Init BGI. If loadAtInit is true, try to locate and load driver.  //
  88. // Returns true if LoadAtInit succeeds or is set to False. Does     //
  89. // not "own" bgiPath, but instead is passed a pointer to a string   //
  90. // that is allocated elsewhere. Does not de-allocate bgiPath when   //
  91. // done.                                                            //
  92. //------------------------------------------------------------------//
  93.  
  94. Boolean graphAppInit( int aDriver, int aMode,
  95.             char *aBGIPath, Boolean loadAtInit )
  96. {
  97.     if (aBGIPath != 0)
  98.         bgiPath = aBGIPath;
  99.     driver = aDriver;
  100.     mode = aMode;
  101.     freeDriverMem();
  102.     if ( loadAtInit == True )
  103.         {
  104.         if ( driver == 0 )
  105.             detectgraph(&driver, &mode);
  106.         if ( driver > 0 )
  107.             return graphAppLoadDriver(driver);
  108.         else
  109.             return False;
  110.         }
  111.     return(True);
  112. }
  113.  
  114. void graphAppDone()
  115. {
  116.     if ( graphActive == True )
  117.         closegraph();
  118.     freeDriverMem();
  119.     graphActive = False;
  120.     bgiPath = emptyString;
  121.     driver = DETECT;
  122.     mode = 0;
  123. }
  124.  
  125. void graphicsStop(void);
  126.  
  127. Boolean graphicsStart(void)
  128. {
  129.     if ( graphActive == True )
  130.         return(True);
  131.  
  132.     initgraph( &driver, &mode, bgiPath );
  133.  
  134.     if ( driver < 0 )
  135.         {
  136.         graphicsStop();
  137.         return False;
  138.         }
  139.     else
  140.         graphActive = True;
  141.     return True;
  142. }
  143.  
  144. Boolean graphicsActive(void)
  145. {
  146.     if ( graphActive == True )
  147.         return True;
  148.     else
  149.         return False;
  150. }
  151.  
  152. void graphicsStop(void)
  153. {
  154.     if ( graphActive == True )
  155.         closegraph();
  156.  
  157.     graphActive = False;
  158.     TProgram::application->redraw();
  159. }
  160.  
  161.