home *** CD-ROM | disk | FTP | other *** search
/ C++ Games Programming / CPPGAMES.ISO / thx / source / theatrix / theatrix.cpp < prev    next >
C/C++ Source or Header  |  1995-04-26  |  4KB  |  217 lines

  1. #include <stdlib.h>
  2. #include <fastgraf.h>
  3. #include <iostream.h>
  4. #include <constrea.h>
  5. #include <string.h>
  6. #include <conio.h>
  7. #include <typeinfo.h>
  8. #include "director.h"
  9. #include "perform.h"
  10. #include "vocal.h"
  11. #include "music.h"
  12. #include "timesrvr.h"
  13. #include "kdsrvr.h"
  14. #include "jssrvr.h"
  15. #include "settings.h"
  16. #include "standard.h"
  17. #include "media.h"
  18. #include "netsrvr.h"
  19. #include "theatrix.h"
  20.  
  21. Theatrix *Theatrix::current_game;
  22.  
  23. KeystrokeServer  Theatrix::kss;
  24. HotkeyServer     Theatrix::hks;
  25. TimerServer      Theatrix::ts;
  26. MessageServer    Theatrix::ms;
  27. MouseclickServer Theatrix::mcs;
  28. MousemoveServer  Theatrix::mms;
  29. JoystickServer   Theatrix::js;
  30. NetpackServer    Theatrix::ns;
  31.  
  32. void Theatrix::use_video_mode(int vmode)
  33.   {
  34.   videomode=vmode;
  35.   }
  36.  
  37. void Theatrix::set_xms(int mode)
  38.   {
  39.   Media::set_xms(mode);
  40.   }
  41.  
  42. void Theatrix::add_director(Director* d)
  43.   {
  44.   director[dcount++]=d;
  45.   }
  46.  
  47. Theatrix::Theatrix(char* str)
  48.   {
  49.   dcount = 0;
  50.   if (current_game != 0)
  51.     fatal("only one Theatrix object can exist at a time");
  52.   current_game = this;
  53.   fg_waitfor(9);
  54.   constream screen;
  55.   screen.clrscr();
  56.   screen << setattr(31) << setxy(1,1);
  57.   for (int i=1;i<80;i++)
  58.     screen << ' ';
  59.   screen << setxy((80-strlen(str))/2,1) << str;
  60.   screen << setattr(7);
  61.   cout << "\n\n";
  62.   videomode=DEFAULT_VIDEO_MODE;
  63.   }
  64.  
  65. void Theatrix::enable_netpacks()
  66.   {
  67.   ns.netpacks_active = TRUE;
  68.   }
  69.  
  70. void Theatrix::use_commport(int p)
  71.   {
  72.   ns.port=p;
  73.   }
  74.  
  75. void Theatrix::enable_joystick()
  76.   {
  77.   js.joystick_supported = 1;
  78.   }
  79.  
  80. void Theatrix::joystick_extremes(int *x1, int *y1, int *x2, int *y2)
  81.   {
  82.   *x1 = js.lx;
  83.   *y1 = js.ty;
  84.   *x2 = js.rx;
  85.   *y2 = js.by;
  86.   }
  87.  
  88. void Theatrix::go(const Type_info& id)
  89.   {
  90.   int index=find_director_index(id);
  91.   if (index==-1)  fatal("cannot locate initial director");
  92.   go(index);
  93.   }
  94.  
  95. void Theatrix::go(int index)
  96.   {
  97.   fg_waitfor(1);
  98.   ts.startup();
  99.   hks.startup();
  100.   VocalHand::startup();
  101.   MusicHand::startup();
  102.   if (js.joystick_supported)
  103.     js.startup();
  104.   ns.startup();
  105.  
  106.   cout << '\n';
  107.   Hand::initialize_hands();
  108.   cout << "\nstarting...";
  109.   fg_waitfor(18);
  110.  
  111.   int oldmode=fg_getmode();
  112.   fg_setmode(videomode);
  113.   fg_mouseini();
  114.  
  115.   do{
  116.     director[index]->display();
  117.     director[index]->take_over();
  118.     director[index]->hide();
  119.     index=find_director_index(director[index]->get_next_director());
  120.     } while (index>=0);
  121.  
  122.   fg_mousefin();
  123.   fg_setmode(oldmode);
  124.  
  125.   ns.shutdown();
  126.   MusicHand::shutdown();
  127.   VocalHand::shutdown();
  128.   hks.shutdown();
  129.   ts.shutdown();
  130.   }
  131.  
  132. Theatrix::~Theatrix()
  133.   {
  134.   while (kbhit())
  135.     getch();
  136.   fg_waitfor(9);
  137.   current_game = 0;
  138.   Hand::numhands=0;
  139.   }
  140.  
  141. int Theatrix::find_director_index(const Type_info& id)
  142.   {
  143.   static short int index = 0;
  144.   if (dcount == 0)
  145.   index = -1;
  146.   else
  147.     {
  148.     if (id == typeid(FirstDirector))
  149.       index = 0;
  150.     else if (id == typeid(LastDirector))
  151.       index = dcount-1;
  152.     else if (id == typeid(NextDirector))
  153.       {
  154.       if (++index == dcount)
  155.         index = -1;
  156.       }
  157.     else if (id == typeid(PrevDirector))
  158.       --index;
  159.     else if (id == typeid(StopDirector))
  160.       index = -1;
  161.     else
  162.       {
  163.       for (index=0; index < dcount; index++)
  164.         if (id==typeid(*director[index]))
  165.           break;
  166.       if (index == dcount)
  167.         index = -1;
  168.       }
  169.     }
  170.   return index;
  171.   }
  172.  
  173. const char dashedline[] =
  174. "---------------------------------------------------------------------";
  175.  
  176. static void fatalhdr()
  177.   {
  178.   fg_kbinit(0);
  179.   fg_setmode(3);
  180.   cerr << dashedline << endl << " \aFatal Theatrix Error" << endl;
  181.   }
  182.  
  183. void Theatrix::system_shutdown()
  184.   {
  185.   MusicHand::shutdown();
  186.   VocalHand::shutdown();
  187.   if (Theatrix::current_game != 0)
  188.     {
  189.     Theatrix::current_game->hks.shutdown();
  190.     Theatrix::current_game->ts.shutdown();
  191.     }
  192.   exit(1);
  193.   }
  194.  
  195. void Theatrix::fatal(const char *reason)
  196.   {
  197.   fatalhdr();
  198.   cerr << " Reason: " << reason << endl;
  199.   system_shutdown();
  200.   }
  201.  
  202. void Theatrix::fatal(const char *reason, const char *fno, int lno)
  203.   {
  204.   fatalhdr();
  205.   cerr << "      File: " << fno << endl
  206.        << "      Line: " << lno << endl
  207.        << " Assertion: (" << reason << ')' << endl
  208.        << dashedline;
  209.   system_shutdown();
  210.   }
  211.  
  212. void fatal(const char *reason, const char *fno, int lno)
  213.   {
  214.   Theatrix::fatal(reason,fno,lno);
  215.   }
  216.  
  217.