home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Zone / VRZONE.ISO / mac / ZIP / MISC3D / AVRIL11.ZIP / SYSTEM.C < prev    next >
C/C++ Source or Header  |  1994-08-13  |  5KB  |  204 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 <stdlib.h>  /* getenv() */
  26. #include <string.h>  /* strstr() */
  27. #include <signal.h>
  28.  
  29. #include "avril.h"
  30.  
  31. static vrl_Boolean system_running = 0;   /* non-zero while system is initialized and running */
  32.  
  33. void vrl_SystemStartRunning(void)
  34.     {
  35.     system_running = 1;
  36.     }
  37.  
  38. void vrl_SystemStopRunning(void)
  39.     {
  40.     system_running = 0;
  41.     }
  42.  
  43. vrl_Boolean vrl_SystemIsRunning(void)
  44.     {
  45.     return system_running;
  46.     }
  47.  
  48. static vrl_Boolean need_to_redraw = 0;   /* non-zero if the screen needs updating */
  49.  
  50. void vrl_SystemRequestRefresh(void)
  51.     {
  52.     need_to_redraw = 1;
  53.     }
  54.  
  55. vrl_Boolean vrl_SystemQueryRefresh(void)
  56.     {
  57.     return need_to_redraw;
  58.     }
  59.  
  60. vrl_Boolean vrl_SystemStartup(void)
  61.     {
  62.     vrl_MathInit();
  63.     if (vrl_DisplayInit(0))
  64.         {
  65.         printf("Could not enter graphics mode!\n");
  66.         return -1;
  67.         }
  68.     atexit(vrl_DisplayQuit);
  69.     vrl_MouseInit();
  70.     atexit(vrl_MouseQuit);
  71.     if (vrl_TimerInit())
  72.         return -2;
  73.     atexit(vrl_TimerQuit);
  74.     if (vrl_RenderInit(800, 800, 500, 5, 65000))
  75.         return -3;
  76.     atexit(vrl_RenderQuit);
  77.     atexit(vrl_DeviceCloseAll);
  78.     atexit(vrl_SerialCloseAll);
  79.     /* make sure that exit() [and therefore the atexit() functions] get
  80.        called if there are any fatal errors */
  81.     signal(SIGABRT, exit);
  82.     signal(SIGFPE, exit);
  83.     signal(SIGILL, exit);
  84.     signal(SIGINT, exit);
  85.     signal(SIGSEGV, exit);
  86.     signal(SIGTERM, exit);
  87.     vrl_SystemStartRunning();
  88.     vrl_SystemRequestRefresh();
  89.     vrl_SystemRender(NULL);
  90.     return 0;
  91.     }
  92.  
  93. static void check_mouse(void)
  94.     {
  95.     unsigned int mouse_buttons;
  96.     if (vrl_MouseGetUsage())  /* being used as 6D pointing device */
  97.         return;
  98.     if (!vrl_MouseRead(NULL, NULL, NULL))  /* mouse hasn't changed */
  99.         return;
  100.     vrl_MouseRead(NULL, NULL, &mouse_buttons);
  101.     if (mouse_buttons)  /* button down */
  102.         {
  103.         int mouse_x, mouse_y;
  104.         int win_x, win_y;
  105.         unsigned int down_buttons = mouse_buttons;
  106.         vrl_DisplayGetWindow(&win_x, &win_y, NULL, NULL);
  107.         while (mouse_buttons)  /* wait for button release */
  108.             vrl_MouseRead(&mouse_x, &mouse_y, &mouse_buttons);
  109.         if (down_buttons & 0x07)
  110.             vrl_ApplicationMouseUp(mouse_x - win_x, mouse_y - win_y, down_buttons);
  111.         }
  112.     }
  113.  
  114. void vrl_SystemRun(void)
  115.     {
  116.     vrl_ApplicationInit();
  117.     while (vrl_SystemIsRunning())
  118.         {
  119.         vrl_Object *list;
  120.         if (vrl_KeyboardCheck())
  121.             vrl_ApplicationKey(vrl_KeyboardRead());
  122.         check_mouse();
  123.         vrl_TaskRun();
  124.         vrl_DevicePollAll();
  125.         list = vrl_WorldUpdate();
  126.         if (vrl_SystemQueryRefresh())
  127.             vrl_SystemRender(list);
  128.         }
  129.     }
  130.  
  131. static vrl_Time last_render_ticks;
  132.  
  133. vrl_Time vrl_SystemGetRenderTime(void)
  134.     {
  135.     return last_render_ticks;
  136.     }
  137.  
  138. vrl_Time vrl_SystemGetFrameRate(void)
  139.     {
  140.     if (last_render_ticks == 0) last_render_ticks = 1;  /* so we don't divide by zero! */
  141.     return vrl_TimerGetTickRate() / last_render_ticks;
  142.     }
  143.  
  144. #include "vrlstats.h"  // DEBUG ONLY!!!
  145.  
  146. vrl_RenderStatus *vrl_SystemRender(vrl_Object *list)
  147.     {
  148.     static vrl_Object *lastlist = NULL;
  149.     int pagenum;
  150.     vrl_RenderStatus *stat;
  151.     vrl_Time render_start;
  152.     if (list == NULL)
  153.         list = lastlist;
  154.     else
  155.         lastlist = list;
  156.     pagenum = vrl_DisplayGetDrawPage();
  157.     if (++pagenum >= vrl_DisplayGetNpages())
  158.         pagenum = 0;
  159.     vrl_DisplaySetDrawPage(pagenum);
  160.     render_start = vrl_TimerRead();
  161.     vrl_RenderBegin(vrl_WorldGetCamera(), vrl_WorldGetLights());
  162.     vrl_RenderSetAmbient(vrl_WorldGetAmbient());
  163.     if (vrl_WorldGetScreenClear())
  164.         {
  165.         if (vrl_WorldGetHorizon() && !vrl_DisplayGetDrawMode())
  166.             vrl_RenderHorizon();
  167.         else
  168.             vrl_DisplayClear(vrl_WorldGetSkyColor());
  169.         }
  170.     vrl_ApplicationDrawUnder();
  171.     stat = vrl_RenderObjlist(list);
  172.     last_render_ticks = vrl_TimerRead() - render_start;
  173.     vrl_ApplicationDrawOver(stat);
  174.     vrl_MouseCursorHide();
  175.     vrl_DisplayUpdate();
  176.     vrl_DisplaySetViewPage(pagenum);
  177.     vrl_MouseCursorShow();
  178.     need_to_redraw = 0;
  179.     return stat;
  180.     }
  181.  
  182. void vrl_SystemCommandLine(int argc, char *argv[])
  183.     {
  184.     int i;
  185.     vrl_Camera *cam;
  186.     for (i = 1; i < argc; ++i)  /* i = 1 to skip argv[0] */
  187.         {
  188.         FILE *in = fopen(argv[i], "r");
  189.         if (in == NULL) continue;
  190.         if (strstr(argv[i], ".wld"))
  191.             vrl_ReadWLD(in);
  192.         else if (strstr(argv[i], ".fig"))
  193.             vrl_ReadFIG(in, NULL, NULL);
  194.         else if (strstr(argv[i], ".plg"))
  195.             vrl_ReadObjectPLG(in);
  196.         /* ignore anything else */
  197.         fclose(in);
  198.         }
  199.     if (!vrl_WorldGetCamera())   /* need to have a camera */
  200.         vrl_CameraCreate();
  201.     vrl_WorldUpdate();
  202.     }
  203.  
  204.