USERIO.C 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. #include <stdarg.h>
  24. #include "frame.h"
  25. #include "vector.h"
  26. #include "povproto.h"
  27. #include "parse.h"
  28. #include "povray.h"
  29. #include "tokenize.h"
  30. #include "userio.h"
  31. STREAM_INFO Stream_Info[MAX_STREAMS];
  32. static char vsbuffer[1000];
  33. static void PrintToStream (int stream, char *s);
  34. /****************************************************************************/
  35. /* Prints s to the stream's console or file destination, as specified by type */
  36. static void PrintToStream(int stream, char *s)
  37. {
  38. if (Stream_Info[stream].handle != NULL)
  39. {
  40. fprintf(Stream_Info[stream].handle, s);
  41. fflush(Stream_Info[stream].handle);
  42. }
  43. if (Stream_Info[ALL_STREAM].handle != NULL)
  44. {
  45. fprintf(Stream_Info[ALL_STREAM].handle, s);
  46. fflush(Stream_Info[ALL_STREAM].handle);
  47. }
  48. }
  49. /****************************************************************************/
  50. /* Use this routine to display opening banner & copyright info */
  51. int CDECL Banner(char *format,...)
  52. {
  53. va_list marker;
  54. va_start(marker, format);
  55. vsprintf(vsbuffer, format, marker);
  56. va_end(marker);
  57. POV_BANNER(vsbuffer);
  58. return (0);
  59. }
  60. /****************************************************************************/
  61. /*
  62. * Use this routine to display non-fatal warning message if
  63. * opts.Language_Version is greater than level parameter.
  64. */
  65. int CDECL Warning(DBL level, char *format,...)
  66. {
  67. va_list marker;
  68. va_start(marker, format);
  69. vsprintf(vsbuffer, format, marker);
  70. va_end(marker);
  71. if (level >= opts.Language_Version)
  72. return (0);
  73. PrintToStream(WARNING_STREAM, vsbuffer);
  74. if (Stream_Info[WARNING_STREAM].do_console)
  75. {
  76. POV_WARNING(vsbuffer);
  77. }
  78. return (0);
  79. }
  80. /****************************************************************************/
  81. /* Use this routine to display debug information. */
  82. int CDECL Debug_Info(char *format,...)
  83. {
  84. va_list marker;
  85. va_start(marker, format);
  86. vsprintf(vsbuffer, format, marker);
  87. va_end(marker);
  88. PrintToStream(DEBUG_STREAM, vsbuffer);
  89. if (Stream_Info[DEBUG_STREAM].do_console)
  90. {
  91. POV_DEBUG_INFO(vsbuffer);
  92. }
  93. return (0);
  94. }
  95. /****************************************************************************/
  96. /*
  97. * Use this routine to display general information messages about the current
  98. * rendering if that information applies to the entire render. Items such as
  99. * "Options in effect" or when animation is added it would display frame number
  100. * or clock value. In a windowed environment this info might stay static on the
  101. * screen during the whole session. Status messages such as "Parsing..." or
  102. * "Building slabs, please wait" etc should use Status_Info below.
  103. */
  104. int CDECL Render_Info(char *format,...)
  105. {
  106. va_list marker;
  107. va_start(marker, format);
  108. vsprintf(vsbuffer, format, marker);
  109. va_end(marker);
  110. PrintToStream(RENDER_STREAM, vsbuffer);
  111. if (Stream_Info[RENDER_STREAM].do_console)
  112. {
  113. POV_RENDER_INFO(vsbuffer);
  114. }
  115. return (0);
  116. }
  117. /****************************************************************************/
  118. /*
  119. * Use this routine to display information messages such as "Parsing..." or
  120. * "Building slabs, please wait" etc Windowed environments might implement
  121. * one or two status lines at the bottom of the screen.
  122. */
  123. int CDECL Status_Info(char *format,...)
  124. {
  125. va_list marker;
  126. va_start(marker, format);
  127. vsprintf(vsbuffer, format, marker);
  128. va_end(marker);
  129. POV_STATUS_INFO(vsbuffer);
  130. return (0);
  131. }
  132. /****************************************************************************/
  133. /*
  134. * This routine is used by various specialized fatal error functions to display
  135. * a message to the fatal error reporting device. It does not terminate
  136. * rendering. The function "Error" below prints a message and does the actual
  137. * termination. Use "Error" for most purposes.
  138. */
  139. int CDECL Error_Line(char *format,...)
  140. {
  141. va_list marker;
  142. va_start(marker, format);
  143. vsprintf(vsbuffer, format, marker);
  144. va_end(marker);
  145. PrintToStream(FATAL_STREAM, vsbuffer);
  146. if (Stream_Info[FATAL_STREAM].do_console)
  147. {
  148. POV_FATAL(vsbuffer);
  149. }
  150. return (0);
  151. }
  152. /****************************************************************************/
  153. /*
  154. * This not only prints a fatal error message, it first calls Where_Error and
  155. * it calls Terminate_POV to do the actual termination.
  156. */
  157. int CDECL Error(char *format,...)
  158. {
  159. va_list marker;
  160. if (Stop_Flag)
  161. {
  162. if (POV_SHELLOUT(USER_ABORT_SHL) != FATAL_RET)
  163. {
  164. POV_STATUS_INFO("\nUser abort.\n");
  165. UICB_USER_ABORT
  166. Terminate_Tokenizer(); /* Closes scene file */
  167. Terminate_POV(2);
  168. }
  169. else
  170. {
  171. Error_Line("\nFatal error in User_Abort_Command.\n");
  172. }
  173. }
  174. switch (Stage)
  175. {
  176. case STAGE_STARTUP:
  177. Error_Line("\nStartup error.\n");
  178. break;
  179. case STAGE_BANNER:
  180. Error_Line("\n Banner error.\n");
  181. break;
  182. case STAGE_INIT:
  183. Error_Line("\nInit error.\n");
  184. break;
  185. case STAGE_ENVIRONMENT:
  186. Error_Line("\nEnvironment error.\n");
  187. break;
  188. case STAGE_COMMAND_LINE:
  189. Error_Line("\nCommand line error.\n");
  190. break;
  191. case STAGE_FILE_INIT:
  192. Error_Line("\nFile init error.\n");
  193. break;
  194. case STAGE_PARSING:
  195. case STAGE_INCLUDE_ERR:
  196. Where_Error();
  197. break;
  198. case STAGE_CONTINUING:
  199. Error_Line("\nContinue trace error.\n");
  200. break;
  201. case STAGE_RENDERING:
  202. Error_Line("\nRendering error.\n");
  203. break;
  204. case STAGE_SHUTDOWN:
  205. Error_Line("\nShutdown error.\n");
  206. break;
  207. case STAGE_INI_FILE:
  208. Error_Line("\nINI file error.\n");
  209. break;
  210. case STAGE_CLEANUP_PARSE:
  211. Error_Line("\nCleanup parse error.\n");
  212. break;
  213. case STAGE_SLAB_BUILDING:
  214. Error_Line("\nSlab building error.\n");
  215. break;
  216. case STAGE_TOKEN_INIT:
  217. Error_Line("\nScene file parser initialization error.\n");
  218. break;
  219. case STAGE_FOUND_INSTEAD:
  220. break;
  221. default:
  222. Error_Line("\nUnkown error %d.\n", Stage);
  223. break;
  224. }
  225. va_start(marker, format);
  226. vsprintf(vsbuffer, format, marker);
  227. va_end(marker);
  228. PrintToStream(FATAL_STREAM, vsbuffer);
  229. if (Stream_Info[FATAL_STREAM].do_console)
  230. {
  231. POV_FATAL(vsbuffer);
  232. }
  233. /* This could be just an "if" but we may add special messages later */
  234. switch (Stage)
  235. {
  236. case STAGE_INCLUDE_ERR:
  237. Error_Line("Check that the file is in a directory specifed with a +L switch\n");
  238. Error_Line("or 'Library_Path=' .INI item. Standard include files are in the\n");
  239. Error_Line("include directory or folder. Please read your documentation carefully.\n");
  240. }
  241. UICB_PARSE_ERROR
  242. Terminate_Tokenizer(); /* Closes scene file */
  243. POV_SHELLOUT(FATAL_SHL);
  244. Terminate_POV(1);
  245. return (0);
  246. }
  247. /****************************************************************************/
  248. /* Use this routine to display final rendering statistics */
  249. int CDECL Statistics(char *format,...)
  250. {
  251. va_list marker;
  252. va_start(marker, format);
  253. vsprintf(vsbuffer, format, marker);
  254. va_end(marker);
  255. PrintToStream(STATISTIC_STREAM, vsbuffer);
  256. if (Stream_Info[STATISTIC_STREAM].do_console)
  257. {
  258. POV_STATISTICS(vsbuffer);
  259. }
  260. return (0);
  261. }
  262. /****************************************************************************/
  263. /* Initialization for streams structure */
  264. void Init_Text_Streams()
  265. {
  266. int i;
  267. for (i = 0; i < MAX_STREAMS; i++)
  268. {
  269. Stream_Info[i].handle = NULL;
  270. Stream_Info[i].name = NULL;
  271. Stream_Info[i].do_console = TRUE;
  272. }
  273. }
  274. /****************************************************************************/
  275. /* Opens stream text output files if necessary. */
  276. void Open_Text_Streams()
  277. {
  278. int i;
  279. for (i = 0; i < MAX_STREAMS; i++)
  280. {
  281. if (Stream_Info[i].name != NULL)
  282. {
  283. if (opts.Options & CONTINUE_TRACE)
  284. {
  285. if ((Stream_Info[i].handle =
  286. fopen(Stream_Info[i].name, APPEND_TXTFILE_STRING)) == NULL)
  287. {
  288. Warning(0.0, "Couldn't append stream to file %s.\n", Stream_Info[i].name);
  289. }
  290. }
  291. else
  292. {
  293. if ((Stream_Info[i].handle =
  294. fopen(Stream_Info[i].name, WRITE_TXTFILE_STRING)) == NULL)
  295. {
  296. Warning(0.0, "Couldn't write stream to file %s.\n", Stream_Info[i].name);
  297. }
  298. }
  299. }
  300. }
  301. }
  302. void Destroy_Text_Streams()
  303. {
  304. int i;
  305. for (i = 0; i < MAX_STREAMS; i++)
  306. {
  307. if (Stream_Info[i].name)
  308. {
  309. POV_FREE(Stream_Info[i].name);
  310. Stream_Info[i].name = NULL;
  311. }
  312. }
  313. }
  314. /****************************************************************************/
  315. void POV_Std_Banner(char *s)
  316. {
  317. fprintf(stderr, s);
  318. fflush(stderr);
  319. }
  320. /****************************************************************************/
  321. void POV_Std_Warning(char *s)
  322. {
  323. fprintf(stderr, s);
  324. fflush(stderr);
  325. }
  326. /****************************************************************************/
  327. void POV_Std_Status_Info(char *s)
  328. {
  329. fprintf(stderr, s);
  330. fflush(stderr);
  331. }
  332. /****************************************************************************/
  333. void POV_Std_Render_Info(char *s)
  334. {
  335. fprintf(stderr, s);
  336. fflush(stderr);
  337. }
  338. /****************************************************************************/
  339. void POV_Std_Debug_Info(char *s)
  340. {
  341. fprintf(stderr, s);
  342. fflush(stderr);
  343. }
  344. /****************************************************************************/
  345. void POV_Std_Fatal(char *s)
  346. {
  347. fprintf(stderr, s);
  348. fflush(stderr);
  349. }
  350. /****************************************************************************/
  351. void POV_Std_Statistics(char *s)
  352. {
  353. fprintf(stderr, s);
  354. fflush(stderr);
  355. }
  356. /****************************************************************************/
  357. void Terminate_POV(int i)
  358. {
  359. close_all();
  360. POV_MEM_RELEASE_ALL(i == 0);
  361. pre_init_flag=0;
  362. FINISH_POVRAY(i); /* Must call exit(i) or somehow stop */
  363. }
  364. /***************************************************************************
  365. *
  366. * Dummy display routines for non-graphic Unix ports
  367. *
  368. **************************************************************************/
  369. static DBL Display_Width_Scale, Display_Height_Scale;
  370. static int Prev_X, Prev_Y;
  371. /****************************************************************************/
  372. void POV_Std_Display_Init(int w, int h)
  373. {
  374. Display_Width_Scale = 78.0 / (DBL)w;
  375. Display_Height_Scale = 24.0 / (DBL)h;
  376. Prev_X = 0;
  377. Prev_Y = 0;
  378. fprintf(stderr, "\n");
  379. }
  380. /****************************************************************************/
  381. void POV_Std_Display_Finished()
  382. {
  383. char s[3];
  384. fprintf(stderr, "\007\007");
  385. if (opts.Options & PROMPTEXIT)
  386. {
  387. fgets(s, 2, stdin);
  388. }
  389. }
  390. /****************************************************************************/
  391. void POV_Std_Display_Close()
  392. {
  393. fprintf(stderr, "\n");
  394. }
  395. /****************************************************************************/
  396. void POV_Std_Display_Plot(int x, int y, unsigned int r, unsigned int g, unsigned int b, unsigned int a)
  397. {
  398. int sx = (int)(Display_Width_Scale * ((DBL)x));
  399. int sy = (int)(Display_Height_Scale * ((DBL)y));
  400. char I;
  401. unsigned char G[6] = { ' ', '.', 'o', '*', '@', 'M' };
  402. if (sy > Prev_Y)
  403. {
  404. Prev_Y++;
  405. fprintf(stderr, "\n");
  406. Prev_X = 0;
  407. }
  408. if (sx > Prev_X)
  409. {
  410. I = (int)(((DBL)r * 1.80 + (DBL)g * 3.54 + (DBL)b * 0.66) / 256.0);
  411. fprintf(stderr, "%c", G[(int)I]);
  412. Prev_X++;
  413. }
  414. }
  415. /****************************************************************************/
  416. 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)
  417. {
  418. POV_Std_Display_Plot(x1, y1, r, g, b, a);
  419. }
  420. /*****************************************************************************
  421. *
  422. * FUNCTION
  423. *
  424. * POV_Std_Display_Plot_Box
  425. *
  426. * INPUT
  427. *
  428. * OUTPUT
  429. *
  430. * RETURNS
  431. *
  432. * AUTHOR
  433. *
  434. * Chris Young
  435. *
  436. * DESCRIPTION
  437. *
  438. * Generic box drawing routine which may be overriden in POV_DRAW_BOX
  439. * by a platform specific routine.
  440. *
  441. * CHANGES
  442. *
  443. * Nov 1995 : Creation.
  444. *
  445. ******************************************************************************/
  446. 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)
  447. {
  448. int x,y;
  449. for (x = x1; x <= x2; x++)
  450. {
  451. POV_DISPLAY_PLOT(x, y1, r, g, b, a);
  452. POV_DISPLAY_PLOT(x, y2, r, g, b, a);
  453. }
  454. for (y = y1; y <= y2; y++)
  455. {
  456. POV_DISPLAY_PLOT(x1, y, r, g, b, a);
  457. POV_DISPLAY_PLOT(x2, y, r, g, b, a);
  458. }
  459. }