home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Graphics / Graphics.zip / povsrc31.zip / userio.c < prev    next >
C/C++ Source or Header  |  2000-10-16  |  15KB  |  598 lines

  1. /****************************************************************************
  2. *                userio.c
  3. *
  4. *  This module contains I/O routines.
  5. *
  6. *  from Persistence of Vision(tm) Ray Tracer
  7. *  Copyright 1996,1999 Persistence of Vision Team
  8. *---------------------------------------------------------------------------
  9. *  NOTICE: This source code file is provided so that users may experiment
  10. *  with enhancements to POV-Ray and to port the software to platforms other
  11. *  than those supported by the POV-Ray Team.  There are strict rules under
  12. *  which you are permitted to use this file.  The rules are in the file
  13. *  named POVLEGAL.DOC which should be distributed with this file.
  14. *  If POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  15. *  Team Coordinator by email to team-coord@povray.org or visit us on the web at
  16. *  http://www.povray.org. The latest version of POV-Ray may be found at this site.
  17. *
  18. * This program is based on the popular DKB raytracer version 2.12.
  19. * DKBTrace was originally written by David K. Buck.
  20. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  21. *
  22. *****************************************************************************/
  23.  
  24. #include <stdarg.h>
  25. #include "frame.h"
  26. #include "vector.h"
  27. #include "povproto.h"
  28. #include "parse.h"
  29. #include "povray.h"
  30. #include "tokenize.h"
  31. #include "userio.h"
  32.  
  33. STREAM_INFO Stream_Info[MAX_STREAMS];
  34. static char vsbuffer[1000];
  35. static void PrintToStream (int stream, char *s);
  36.  
  37. /****************************************************************************/
  38. /* Prints s to the stream's console or file destination, as specified by type */
  39.  
  40. static void PrintToStream(int stream, char *s)
  41. {
  42.   if (Stream_Info[stream].handle != NULL)
  43.   {
  44.     fprintf(Stream_Info[stream].handle, s);
  45.     fflush(Stream_Info[stream].handle);
  46.   }
  47.  
  48.   if (Stream_Info[ALL_STREAM].handle != NULL)
  49.   {
  50.     fprintf(Stream_Info[ALL_STREAM].handle, s);
  51.     fflush(Stream_Info[ALL_STREAM].handle);
  52.   }
  53. }
  54.  
  55. /****************************************************************************/
  56. /* Use this routine to display opening banner & copyright info */
  57. int CDECL Banner(char *format,...)
  58. {
  59.   va_list marker;
  60.  
  61.   va_start(marker, format);
  62.   vsprintf(vsbuffer, format, marker);
  63.   va_end(marker);
  64.   
  65.   POV_BANNER(vsbuffer);
  66.   
  67.   return (0);
  68. }
  69.  
  70. /****************************************************************************/
  71.  
  72. /*
  73.  * Use this routine to display non-fatal warning message if
  74.  * opts.Language_Version is greater than level parameter.
  75.  */
  76. int CDECL Warning(int level, char *format,...)
  77. {
  78.   va_list marker;
  79.  
  80.   va_start(marker, format);
  81.   vsprintf(vsbuffer, format, marker);
  82.   va_end(marker);
  83.   
  84.   if (level >= opts.Language_Version)
  85.     return (0);
  86.   
  87.   PrintToStream(WARNING_STREAM, vsbuffer);
  88.   
  89.   if (Stream_Info[WARNING_STREAM].do_console)
  90.   {
  91.     POV_WARNING(vsbuffer);
  92.   }
  93.   
  94.   return (0);
  95. }
  96.  
  97. /****************************************************************************/
  98. /* Use this routine to display debug information. */
  99. int CDECL Debug_Info(char *format,...)
  100. {
  101.   va_list marker;
  102.  
  103.   va_start(marker, format);
  104.   vsprintf(vsbuffer, format, marker);
  105.   va_end(marker);
  106.   
  107.   PrintToStream(DEBUG_STREAM, vsbuffer);
  108.   
  109.   if (Stream_Info[DEBUG_STREAM].do_console)
  110.   {
  111.     POV_DEBUG_INFO(vsbuffer);
  112.   }
  113.   
  114.   return (0);
  115. }
  116.  
  117. /****************************************************************************/
  118.  
  119. /*
  120.  * Use this routine to display general information messages about the current
  121.  * rendering if that information applies to the entire render.  Items such as
  122.  * "Options in effect" or when animation is added it would display frame number
  123.  * or clock value. In a windowed environment this info might stay static on the
  124.  * screen during the whole session. Status messages such as "Parsing..." or
  125.  * "Building slabs, please wait" etc should use Status_Info below.
  126.  */
  127. int CDECL Render_Info(char *format,...)
  128. {
  129.   va_list marker;
  130.  
  131.   va_start(marker, format);
  132.   vsprintf(vsbuffer, format, marker);
  133.   va_end(marker);
  134.   
  135.   PrintToStream(RENDER_STREAM, vsbuffer);
  136.   
  137.   if (Stream_Info[RENDER_STREAM].do_console)
  138.   {
  139.     POV_RENDER_INFO(vsbuffer);
  140.   }
  141.   
  142.   return (0);
  143. }
  144.  
  145. /****************************************************************************/
  146.  
  147. /*
  148.  * Use this routine to display information messages such as "Parsing..." or
  149.  * "Building slabs, please wait" etc   Windowed environments might implement
  150.  * one or two status lines at the bottom of the screen.
  151.  */
  152. int CDECL Status_Info(char *format,...)
  153. {
  154.   va_list marker;
  155.  
  156.   va_start(marker, format);
  157.   vsprintf(vsbuffer, format, marker);
  158.   va_end(marker);
  159.   
  160.   POV_STATUS_INFO(vsbuffer);
  161.   
  162.   return (0);
  163. }
  164.  
  165. /****************************************************************************/
  166.  
  167. /*
  168.  * This routine is used by various specialized fatal error functions to display
  169.  * a message to the fatal error reporting device. It does not terminate
  170.  * rendering.  The function "Error" below prints a message and does the actual
  171.  * termination.  Use "Error" for most purposes.
  172.  */
  173.  
  174. int CDECL Error_Line(char *format,...)
  175. {
  176.   va_list marker;
  177.  
  178.   va_start(marker, format);
  179.   vsprintf(vsbuffer, format, marker);
  180.   va_end(marker);
  181.   
  182.   PrintToStream(FATAL_STREAM, vsbuffer);
  183.   
  184.   if (Stream_Info[FATAL_STREAM].do_console)
  185.   {
  186.     POV_FATAL(vsbuffer);
  187.   }
  188.   
  189.   return (0);
  190. }
  191.  
  192. /* NK fix crash caused by '%' character in the error line */
  193. int Error_Line_No_Param(char *errorline)
  194. {
  195.   PrintToStream(FATAL_STREAM, errorline);
  196.   
  197.   if (Stream_Info[FATAL_STREAM].do_console)
  198.   {
  199.     POV_FATAL(errorline);
  200.   }
  201.   
  202.   return (0);
  203. }
  204.  
  205. /****************************************************************************/
  206.  
  207. /*
  208.  * This not only prints a fatal error message, it first calls Where_Error and
  209.  * it calls Terminate_POV to do the actual termination.
  210.  */
  211.  
  212. int CDECL Error(char *format,...)
  213. {
  214.   va_list marker;
  215.   
  216.   if (Stop_Flag)
  217.   {
  218.     if (POV_SHELLOUT(USER_ABORT_SHL) != FATAL_RET)
  219.     {
  220.       POV_STATUS_INFO("\nUser abort.\n");
  221.  
  222.       UICB_USER_ABORT
  223.  
  224.       Terminate_Tokenizer(TRUE); /* Closes scene file */
  225.  
  226.       Terminate_POV(2);
  227.     }
  228.     else
  229.     {
  230.       Error_Line("\nFatal error in User_Abort_Command.\n");
  231.     }
  232.   }
  233.  
  234.   switch (Stage)
  235.   {
  236.     case STAGE_STARTUP:
  237.       Error_Line("\nStartup error.\n");
  238.       break;
  239.  
  240.     case STAGE_BANNER:
  241.       Error_Line("\n Banner error.\n");
  242.       break;
  243.  
  244.     case STAGE_INIT:
  245.       Error_Line("\nInit error.\n");
  246.       break;
  247.  
  248.     case STAGE_ENVIRONMENT:
  249.       Error_Line("\nEnvironment error.\n");
  250.       break;
  251.  
  252.     case STAGE_COMMAND_LINE:
  253.       Error_Line("\nCommand line error.\n");
  254.       break;
  255.  
  256.     case STAGE_FILE_INIT:
  257.       Error_Line("\nFile init error.\n");
  258.       break;
  259.  
  260.     case STAGE_PARSING:
  261.     case STAGE_INCLUDE_ERR:
  262.       Where_Error();
  263.       break;
  264.  
  265.     case STAGE_CONTINUING:
  266.       Error_Line("\nContinue trace error.\n");
  267.       break;
  268.  
  269.     case STAGE_RENDERING:
  270.       Error_Line("\nRendering error.\n");
  271.       break;
  272.  
  273.     case STAGE_SHUTDOWN:
  274.       Error_Line("\nShutdown error.\n");
  275.       break;
  276.  
  277.     case STAGE_INI_FILE:
  278.       Error_Line("\nINI file error.\n");
  279.       break;
  280.  
  281.     case STAGE_CLEANUP_PARSE:
  282.       Error_Line("\nCleanup parse error.\n");
  283.       break;
  284.  
  285.     case STAGE_SLAB_BUILDING:
  286.       Error_Line("\nSlab building error.\n");
  287.       break;
  288.  
  289.     case STAGE_TOKEN_INIT:
  290.       Error_Line("\nScene file parser initialization error.\n");
  291.       break;
  292.  
  293.     case STAGE_FOUND_INSTEAD:
  294.       break;
  295. #ifdef PostProcessPatch
  296.     case STAGE_POST_PROCESS:
  297.       Error_Line("\nPost process error.\n");
  298.       break;
  299. #endif
  300.     default:
  301.       Error_Line("\nUnkown error %d.\n", Stage);
  302.       break;
  303.   }
  304.  
  305.   va_start(marker, format);
  306.   vsprintf(vsbuffer, format, marker);
  307.   va_end(marker);
  308.   
  309.   PrintToStream(FATAL_STREAM, vsbuffer);
  310.   
  311.   if (Stream_Info[FATAL_STREAM].do_console)
  312.   {
  313.     POV_FATAL(vsbuffer);
  314.   }
  315.  
  316.   /* This could be just an "if" but we may add special messages later */
  317.  
  318.   switch (Stage)
  319.   {
  320.     case STAGE_INCLUDE_ERR:
  321.       Error_Line("Check that the file is in a directory specifed with a +L switch\n");
  322.       Error_Line("or 'Library_Path=' .INI item.  Standard include files are in the\n");
  323.       Error_Line("include directory or folder. Please read your documentation carefully.\n");
  324.   }
  325.  
  326.   UICB_PARSE_ERROR
  327.  
  328.   Terminate_Tokenizer(TRUE); /* Closes scene file */
  329.  
  330.   POV_SHELLOUT(FATAL_SHL);
  331.  
  332.   Terminate_POV(1);
  333.   
  334.   return (0);
  335. }
  336.  
  337. /****************************************************************************/
  338. /* Use this routine to display final rendering statistics */
  339. int CDECL Statistics(char *format,...)
  340. {
  341.   va_list marker;
  342.  
  343.   va_start(marker, format);
  344.   vsprintf(vsbuffer, format, marker);
  345.   va_end(marker);
  346.   
  347.   PrintToStream(STATISTIC_STREAM, vsbuffer);
  348.   
  349.   if (Stream_Info[STATISTIC_STREAM].do_console)
  350.   {
  351.     POV_STATISTICS(vsbuffer);
  352.   }
  353.   
  354.   return (0);
  355. }
  356.  
  357. /****************************************************************************/
  358. /* Initialization for streams structure */
  359.  
  360. void Init_Text_Streams()
  361. {
  362.   int i;
  363.   
  364.   for (i = 0; i < MAX_STREAMS; i++)
  365.   {
  366.     Stream_Info[i].handle = NULL;
  367.     Stream_Info[i].name = NULL;
  368.     Stream_Info[i].do_console = TRUE;
  369.   }
  370. }
  371.  
  372. /****************************************************************************/
  373. /* Opens stream text output files if necessary. */
  374.  
  375. void Open_Text_Streams()
  376. {
  377.   int i;
  378.  
  379.   for (i = 0; i < MAX_STREAMS; i++)
  380.   {
  381.     if (Stream_Info[i].name != NULL)
  382.     {
  383.       if (opts.Options & CONTINUE_TRACE)
  384.       {
  385.         if ((Stream_Info[i].handle =
  386.              fopen(Stream_Info[i].name, APPEND_TXTFILE_STRING)) == NULL)
  387.         {
  388.           Warning(0, "Couldn't append stream to file %s.\n", Stream_Info[i].name);
  389.         }
  390.       }
  391.       else
  392.       {
  393.         if ((Stream_Info[i].handle =
  394.              fopen(Stream_Info[i].name, WRITE_TXTFILE_STRING)) == NULL)
  395.         {
  396.           Warning(0, "Couldn't write stream to file %s.\n", Stream_Info[i].name);
  397.         }
  398.       }
  399.     }
  400.   }
  401. }
  402.  
  403. void Destroy_Text_Streams()
  404. {
  405.   int i;
  406.  
  407.   for (i = 0; i < MAX_STREAMS; i++)
  408.   {
  409.     if (Stream_Info[i].name)
  410.     {
  411. /*YS sept 23 2000 bugfix: files were never closed !*/
  412.         if ( Stream_Info[i].handle != NULL)
  413.         {
  414.             fclose(    Stream_Info[i].handle);
  415.         }
  416. /*YS*/
  417.       POV_FREE(Stream_Info[i].name);
  418.  
  419.       Stream_Info[i].name = NULL;
  420.     }
  421.   }
  422. }
  423.  
  424.  
  425. /****************************************************************************/
  426. void POV_Std_Banner(char *s)
  427. {
  428.   fprintf(stderr, s);
  429.   fflush(stderr);
  430. }
  431.  
  432. /****************************************************************************/
  433. void POV_Std_Warning(char *s)
  434. {
  435.   fprintf(stderr, s);
  436.   fflush(stderr);
  437. }
  438.  
  439. /****************************************************************************/
  440. void POV_Std_Status_Info(char *s)
  441. {
  442.   fprintf(stderr, s);
  443.   fflush(stderr);
  444. }
  445.  
  446. /****************************************************************************/
  447. void POV_Std_Render_Info(char *s)
  448. {
  449.   fprintf(stderr, s);
  450.   fflush(stderr);
  451. }
  452.  
  453. /****************************************************************************/
  454. void POV_Std_Debug_Info(char *s)
  455. {
  456.   fprintf(stderr, s);
  457.   fflush(stderr);
  458. }
  459.  
  460. /****************************************************************************/
  461. void POV_Std_Fatal(char *s)
  462. {
  463.   fprintf(stderr, s);
  464.   fflush(stderr);
  465. }
  466.  
  467. /****************************************************************************/
  468. void POV_Std_Statistics(char *s)
  469. {
  470.   fprintf(stderr, s);
  471.   fflush(stderr);
  472. }
  473.  
  474. /****************************************************************************/
  475. void Terminate_POV(int i)
  476. {
  477.   close_all();
  478.   
  479.   POV_MEM_RELEASE_ALL(i == 0);
  480.  
  481.   pre_init_flag=0;
  482.   FINISH_POVRAY(i); /* Must call exit(i) or somehow stop */
  483. }
  484.  
  485. /***************************************************************************
  486.  *
  487.  * Dummy display routines for non-graphic Unix ports
  488.  *
  489.  **************************************************************************/
  490.  
  491. static DBL Display_Width_Scale, Display_Height_Scale;
  492. static int Prev_X, Prev_Y;
  493.  
  494. /****************************************************************************/
  495. void POV_Std_Display_Init(int w, int  h)
  496. {
  497.   Display_Width_Scale = 78.0 / (DBL)w;
  498.   Display_Height_Scale = 24.0 / (DBL)h;
  499.   Prev_X = 0;
  500.   Prev_Y = 0;
  501.   fprintf(stderr, "\n");
  502. }
  503.  
  504. /****************************************************************************/
  505. void POV_Std_Display_Finished()
  506. {
  507.   char s[3];
  508.  
  509.   fprintf(stderr, "\007\007");
  510.   if (opts.Options & PROMPTEXIT)
  511.   {
  512.     fgets(s, 2, stdin);
  513.   }
  514. }
  515.  
  516. /****************************************************************************/
  517. void POV_Std_Display_Close()
  518. {
  519.   fprintf(stderr, "\n");
  520. }
  521.  
  522. /****************************************************************************/
  523. void POV_Std_Display_Plot(int x, int y, unsigned int r, unsigned int g, unsigned int b, unsigned int a)
  524. {
  525.   int sx = (int)(Display_Width_Scale * ((DBL)x));
  526.   int sy = (int)(Display_Height_Scale * ((DBL)y));
  527.   char I;
  528.   unsigned char G[6] = { ' ', '.', 'o', '*', '@', 'M' };
  529.  
  530.   if (sy > Prev_Y)
  531.   {
  532.     Prev_Y++;
  533.     
  534.     fprintf(stderr, "\n");
  535.     
  536.     Prev_X = 0;
  537.   }
  538.   
  539.   if (sx > Prev_X)
  540.   {
  541.     I = (int)(((DBL)r * 1.80 + (DBL)g * 3.54 + (DBL)b * 0.66) / 256.0);
  542.  
  543.     fprintf(stderr, "%c", G[(int)I]);
  544.     
  545.     Prev_X++;
  546.   }
  547. }
  548.  
  549. /****************************************************************************/
  550. void POV_Std_Display_Plot_Rect(int x1, int y1, int x2, int y2, unsigned int r, unsigned int g, unsigned int b, unsigned int a)
  551. {
  552.   POV_Std_Display_Plot(x1, y1, r, g, b, a);
  553. }
  554.  
  555. /*****************************************************************************
  556. *
  557. * FUNCTION
  558. *
  559. *   POV_Std_Display_Plot_Box
  560. *
  561. * INPUT
  562. *   
  563. * OUTPUT
  564. *   
  565. * RETURNS
  566. *   
  567. * AUTHOR
  568. *
  569. *   Chris Young
  570. *   
  571. * DESCRIPTION
  572. *
  573. *   Generic box drawing routine which may be overriden in POV_DRAW_BOX
  574. *   by a platform specific routine.
  575. *
  576. * CHANGES
  577. *
  578. *   Nov 1995 : Creation.
  579. *
  580. ******************************************************************************/
  581. void POV_Std_Display_Plot_Box(int x1, int y1, int x2, int y2, unsigned int r, unsigned int g, unsigned int b, unsigned int a)
  582. {
  583.      int x,y;
  584.    
  585.      for (x = x1; x <= x2; x++)
  586.      {
  587.        POV_DISPLAY_PLOT(x, y1, r, g, b, a);
  588.        POV_DISPLAY_PLOT(x, y2, r, g, b, a);
  589.      }
  590.  
  591.      for (y = y1; y <= y2; y++)
  592.      {
  593.        POV_DISPLAY_PLOT(x1, y, r, g, b, a);
  594.        POV_DISPLAY_PLOT(x2, y, r, g, b, a);
  595.      }
  596.   }
  597.  
  598.