home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Homebrewer's Handbook / vr.iso / avril / system.c < prev    next >
C/C++ Source or Header  |  1996-03-19  |  7KB  |  249 lines

  1. /* The "vrl_System" functions for AVRIL applications */
  2.  
  3. /* Written by Bernie Roehl, January 1994 */
  4.  
  5. /* Copyright 1994 by Bernie Roehl */
  6.  
  7. /*
  8.    You may use this code for your own non-commercial projects without
  9.    paying any fees or royalties.  "Non-commercial", in this context,
  10.    means that the software you write is given away for free to anyone
  11.    who wants it.
  12.    
  13.    Commercial use, including shareware, requires a licensing
  14.    fee and a specific written agreement with the author.
  15.  
  16.    All programs created using this software (both commercial and
  17.    non-commercial) must acknowledge the use of the AVRIL library,
  18.    both in the documentation and in a banner screen at the start or
  19.    end of the program.
  20.  
  21.    For more information, contact Bernie Roehl (broehl@uwaterloo.ca).
  22.  
  23. */
  24.  
  25. #include "avril.h"
  26. #include <stdlib.h>  /* atexit(), exit() */
  27. #include <string.h>  /* strstr() */
  28. #include <signal.h>
  29.  
  30. static vrl_Boolean system_running = 0;   /* non-zero while system is initialized and running */
  31.  
  32. void vrl_SystemStartRunning(void)
  33.     {
  34.     system_running = 1;
  35.     }
  36.  
  37. void vrl_SystemStopRunning(void)
  38.     {
  39.     system_running = 0;
  40.     }
  41.  
  42. vrl_Boolean vrl_SystemIsRunning(void)
  43.     {
  44.     return system_running;
  45.     }
  46.  
  47. static vrl_Boolean need_to_redraw = 0;   /* non-zero if the screen needs updating */
  48.  
  49. void vrl_SystemRequestRefresh(void)
  50.     {
  51.     need_to_redraw = 1;
  52.     }
  53.  
  54. vrl_Boolean vrl_SystemQueryRefresh(void)
  55.     {
  56.     return need_to_redraw;
  57.     }
  58.  
  59. vrl_Boolean vrl_SystemStartup(void)
  60.     {
  61.     vrl_MathInit();
  62.     vrl_WorldInit(vrl_WorldGetCurrent());
  63.     if (vrl_VideoSetup(0))
  64.         {
  65.         printf("Could not enter graphics mode!\n");
  66.         return -1;
  67.         }
  68.     atexit(vrl_VideoShutdown);
  69.     if (vrl_DisplayInit(NULL))
  70.         return -1;
  71.     atexit(vrl_DisplayQuit);
  72.     vrl_MouseInit();
  73.     atexit(vrl_MouseQuit);
  74.     if (vrl_TimerInit())
  75.         return -2;
  76.     atexit(vrl_TimerQuit);
  77.     if (vrl_RenderInit(800, 800, 500, 5, 65000))
  78.         return -3;
  79.     atexit(vrl_RenderQuit);
  80.     atexit(vrl_DeviceCloseAll);
  81.     atexit(vrl_SerialCloseAll);
  82.     /* make sure that exit() [and therefore the atexit() functions] get
  83.        called if there are any fatal errors */
  84.     signal(SIGABRT, exit);
  85.     signal(SIGFPE, exit);
  86.     signal(SIGILL, exit);
  87.     signal(SIGINT, exit);
  88.     signal(SIGSEGV, exit);
  89.     signal(SIGTERM, exit);
  90.     vrl_SystemStartRunning();
  91.     vrl_SystemRequestRefresh();
  92.     vrl_SystemRender(NULL);
  93.     return 0;
  94.     }
  95.  
  96. static void check_mouse(void)
  97.     {
  98.     unsigned int mouse_buttons;
  99.     if (vrl_MouseGetUsage())  /* being used as 6D pointing device */
  100.         return;
  101.     if (!vrl_MouseRead(NULL, NULL, NULL))  /* mouse hasn't changed */
  102.         return;
  103.     vrl_MouseRead(NULL, NULL, &mouse_buttons);
  104.     if (mouse_buttons)  /* button down */
  105.         {
  106.         int mouse_x, mouse_y;
  107.         vrl_ScreenPos win_x, win_y;
  108.         vrl_unsigned16bit down_buttons = mouse_buttons;
  109.         vrl_DisplayGetWindow(&win_x, &win_y, NULL, NULL);
  110.         while (mouse_buttons)  /* wait for button release */
  111.             vrl_MouseRead(&mouse_x, &mouse_y, &mouse_buttons);
  112.         if (down_buttons & 0x07)
  113.             vrl_ApplicationMouseUp(mouse_x - win_x, mouse_y - win_y, down_buttons);
  114.         }
  115.     }
  116.  
  117. void vrl_SystemRun(void)
  118.     {
  119.     vrl_ApplicationInit();
  120.     if (vrl_WorldGetStereoConfiguration())
  121.         vrl_StereoConfigure(vrl_WorldGetStereoConfiguration());
  122.     vrl_SystemRequestRefresh();
  123.     while (vrl_SystemIsRunning())
  124.         {
  125.         vrl_Object *list;
  126.         if (vrl_KeyboardCheck())
  127.             vrl_ApplicationKey(vrl_KeyboardRead());
  128.         check_mouse();
  129.         vrl_TaskRun();
  130.         vrl_DevicePollAll();
  131.         list = vrl_WorldUpdate();
  132.         if (vrl_SystemQueryRefresh())
  133.             vrl_SystemRender(list);
  134.         }
  135.     }
  136.  
  137. static vrl_Time last_render_ticks;
  138.  
  139. vrl_Time vrl_SystemGetRenderTime(void)
  140.     {
  141.     return last_render_ticks;
  142.     }
  143.  
  144. vrl_Time vrl_SystemGetFrameRate(void)
  145.     {
  146.     if (last_render_ticks == 0) last_render_ticks = 1;  /* so we don't divide by zero! */
  147.     return vrl_TimerGetTickRate() / last_render_ticks;
  148.     }
  149.  
  150. vrl_RenderStatus *vrl_SystemRender(vrl_Object *list)
  151.     {
  152.     static vrl_Object *lastlist = NULL;
  153.     vrl_Palette *pal;
  154.     vrl_StereoConfiguration *conf;
  155.     vrl_RenderStatus *stat;
  156.     int pagenum;
  157.     int two_eyes = 0;
  158.     vrl_Time render_start = vrl_TimerRead();
  159.     if (list == NULL)
  160.         list = lastlist;
  161.     else
  162.         lastlist = list;
  163.     pal = vrl_WorldGetPalette();
  164.     if (vrl_PaletteHasChanged(pal))
  165.         {
  166.         vrl_VideoSetPalette(0, 255, pal);
  167.         vrl_PaletteSetChanged(pal, 0);
  168.         }
  169.     pagenum = vrl_VideoGetDrawPage();
  170.     if (++pagenum >= vrl_VideoGetNpages())
  171.         pagenum = 0;
  172.     vrl_VideoSetDrawPage(pagenum);
  173.     vrl_RenderSetAmbient(vrl_WorldGetAmbient());
  174.     vrl_DisplayStereoSetDrawEye(VRL_STEREOEYE_BOTH);
  175.     if (vrl_WorldGetScreenClear())
  176.         {
  177.         vrl_DisplayBeginFrame();
  178.         if (vrl_WorldGetHorizon() && !vrl_RenderGetDrawMode())
  179.             vrl_RenderHorizon();
  180.         else
  181.             vrl_DisplayClear(vrl_WorldGetSkyColor());
  182.         vrl_DisplayEndFrame();
  183.         }
  184.     vrl_ApplicationDrawUnder();
  185.     conf = vrl_WorldGetStereoConfiguration();
  186.     if (conf)
  187.         two_eyes = vrl_StereoGetNeyes(conf);
  188.     if (vrl_WorldGetStereo() && vrl_WorldGetLeftCamera() && vrl_WorldGetRightCamera() && two_eyes)
  189.         {
  190.         /* draw left-eye image */
  191.         vrl_DisplayStereoSetDrawEye(VRL_STEREOEYE_LEFT);
  192.         vrl_RenderSetHorizontalShift(vrl_StereoGetTotalLeftShift(conf));
  193.         vrl_DisplayBeginFrame();
  194.         vrl_RenderBegin(vrl_WorldGetLeftCamera(), vrl_WorldGetLights());
  195.         stat = vrl_RenderObjlist(list);
  196.         vrl_DisplayEndFrame();
  197.  
  198.         /* draw right-eye image */
  199.         vrl_DisplayStereoSetDrawEye(VRL_STEREOEYE_RIGHT);
  200.         vrl_RenderSetHorizontalShift(vrl_StereoGetTotalRightShift(conf));
  201.         vrl_RenderBegin(vrl_WorldGetRightCamera(), vrl_WorldGetLights());
  202.         vrl_DisplayBeginFrame();
  203.         stat = vrl_RenderObjlist(list);
  204.         vrl_DisplayEndFrame();
  205.         }
  206.     else  /* not two-eye stereo */
  207.         {
  208.         vrl_DisplayStereoSetDrawEye(VRL_STEREOEYE_BOTH);
  209.         vrl_RenderSetHorizontalShift(0);
  210.         vrl_DisplayBeginFrame();
  211.         vrl_RenderBegin(vrl_WorldGetCamera(), vrl_WorldGetLights());
  212.         stat = vrl_RenderObjlist(list);
  213.         vrl_DisplayEndFrame();
  214.         }
  215.     vrl_DisplayStereoSetDrawEye(VRL_STEREOEYE_BOTH);
  216.     vrl_RenderSetHorizontalShift(0);
  217.     vrl_ApplicationDrawOver(stat);
  218.     vrl_VideoCursorHide();
  219.     vrl_DisplayUpdate();
  220.     vrl_VideoSetViewPage(pagenum);
  221.     vrl_VideoCursorShow();
  222.     last_render_ticks = vrl_TimerRead() - render_start;
  223.     need_to_redraw = 0;
  224.     return stat;
  225.     }
  226.  
  227. void vrl_SystemCommandLine(int argc, char *argv[])
  228.     {
  229.     int i;
  230.     vrl_Camera *cam;
  231.     for (i = 1; i < argc; ++i)  /* i = 1 to skip argv[0] */
  232.         {
  233.         FILE *in = fopen(argv[i], "r");
  234.         if (in == NULL) continue;
  235.         if (strstr(argv[i], ".wld"))
  236.             vrl_ReadWLD(in);
  237.         else if (strstr(argv[i], ".fig"))
  238.             vrl_ReadFIG(in, NULL, NULL);
  239.         else if (strstr(argv[i], ".plg"))
  240.             vrl_ReadObjectPLG(in);
  241.         /* ignore anything else */
  242.         fclose(in);
  243.         }
  244.     if (!vrl_WorldGetCamera())   /* need to have a camera */
  245.         vrl_CameraCreate();
  246.     vrl_WorldUpdate();
  247.     }
  248.  
  249.