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

  1. /* Written by Dave Stampe, December 1993 */
  2.  
  3. /* Contains user render routines which, in combination
  4. // with colormap.c and the video driver, allow you to
  5. // customize the renderer for almost any hardware
  6.  
  7. // reset_screen() added for VR-386 release. 9/1/94 by Dave Stampe
  8.  
  9. /*
  10.  This code is part of the VR-386 project, created by Dave Stampe.
  11.  VR-386 is a desendent of REND386, created by Dave Stampe and
  12.  Bernie Roehl.  Almost all the code has been rewritten by Dave
  13.  Stampre for VR-386.
  14.  
  15.  Copyright (c) 1994 by Dave Stampe:
  16.  May be freely used to write software for release into the public domain
  17.  or for educational use; all commercial endeavours MUST contact Dave Stampe
  18.  (dstampe@psych.toronto.edu) for permission to incorporate any part of
  19.  this software or source code into their products!  Usually there is no
  20.  charge for under 50-100 items for low-cost or shareware products, and terms
  21.  are reasonable.  Any royalties are used for development, so equipment is
  22.  often acceptable payment.
  23.  
  24.  ATTRIBUTION:  If you use any part of this source code or the libraries
  25.  in your projects, you must give attribution to VR-386 and Dave Stampe,
  26.  and any other authors in your documentation, source code, and at startup
  27.  of your program.  Let's keep the freeware ball rolling!
  28.  
  29.  DEVELOPMENT: VR-386 is a effort to develop the process started by
  30.  REND386, improving programmer access by rewriting the code and supplying
  31.  a standard API.  If you write improvements, add new functions rather
  32.  than rewriting current functions.  This will make it possible to
  33.  include you improved code in the next API release.  YOU can help advance
  34.  VR-386.  Comments on the API are welcome.
  35.  
  36.  CONTACT: dstampe@psych.toronto.edu
  37. */
  38.  
  39.  
  40. #include <stdio.h>
  41. #include <dos.h>
  42. #include <stdlib.h>
  43.  
  44. #include "f3dkitd.h"
  45.  
  46. #include "vr_api.h"
  47. #include "pcdevice.h"    // register_render_start,end
  48.  
  49.  
  50. struct Screeninfo *screeninfo;  // ptr to data in video driver
  51.  
  52.  
  53. WORD current_video_page = 0;     // current video page
  54. WORD current_orientation = 0;    // flip flags of current page
  55.  
  56.  
  57.     // these set system colors
  58. WORD screen_clear_color = 0;
  59. WORD sky_color = 3;
  60. WORD ground_color = 5;
  61. WORD highlight_color = 15;
  62.  
  63.     // call before world file is loaded to set defaults
  64.  
  65. void preset_default_colors()
  66. {
  67.   if (screeninfo->colors==256)  // 256 color std palette
  68.     {
  69.       screen_clear_color = 3;  // std palette
  70.       sky_color = 3;           // defaults
  71.       ground_color = 0x88;
  72.       highlight_color = 15;
  73.     }
  74.   else                      // monochrome std palette
  75.     {
  76.       screen_clear_color = 10;
  77.       sky_color = 10;
  78.       ground_color = 5;
  79.       highlight_color = 15;
  80.     }
  81.  
  82.   set_skycolor(sky_color);     // simple horizon on/off
  83.   set_groundcolor(ground_color);
  84. }
  85.  
  86.  
  87. /********************************************************/
  88. /* USER ROUTINES CALLED BY THE RENDERING LIBRARY        */
  89. /* KEEP THIS AS FAST AS POSSIBLE: SOME MAY BE           */
  90. /* CALLED UP TO 1000 TIMES PER SCREEN!                  */
  91. /********************************************************/
  92.  
  93.  
  94. /* USER ROUTINE TO SETUP FOR POLY DRAWING - CALLED ONCE PER FRAME */
  95. void user_setup_blitter()
  96. {
  97.     setup_hdwe(0);
  98. }
  99.  
  100. /* USER ROUTINE TO RECOVER AFTER POLY DRAWING: ONCE PER FRAME */
  101.  
  102. void user_reset_blitter()
  103. {
  104.     reset_hdwe();
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111. static int x1, y1;    // used for outlining polys
  112.  
  113. static void vlineto(int x, int y, int color)
  114. {
  115.     vgaline(x,y,x1,y1,color);
  116.     x1 = x;
  117.     y1 = y;
  118. }
  119.  
  120. /* CALLED FROM RENDERER TO DRAW POLYS, LINES, POINTS */
  121. /* <color> was created by colormap.c: modify it to   */
  122. /* do your own special color modes/effects!          */
  123.  
  124. void user_render_poly(WORD number, WORD *pcoords, SURFACE color, COORD maxz)
  125. {
  126.   int i;
  127.  
  128.   if(number > 20) return;    // sanity check
  129.  
  130.   if (number == 1)                           // dot
  131.     {
  132.       vgapoint(pcoords[0], pcoords[1], color);
  133.       return;
  134.     }
  135.  
  136.   if (number == 2)
  137.     {
  138.       vgaline(pcoords[0],pcoords[1],pcoords[2],pcoords[3],color);
  139.       return;
  140.     }
  141.  
  142.   switch (color & 0x3000)
  143.     {
  144.       case 0x0000:     // normal polygon
  145.       case 0x1000:
  146.       fastpoly(number, pcoords, color&0x0FFF);
  147.       break;
  148.       case 0x2000:    // metallic polygon
  149.       m_fastpoly(number, pcoords, color&0x0FFF, 0xFF, 0x00);
  150.       break;
  151.       case 0x3000:    // pseudo-glass polygon
  152.       m_fastpoly(number, pcoords, color&0x0FFF, 0xAA, 0xFF);
  153.       break;
  154.     }
  155.  
  156.   if (color & 0x8000)         // highlighted: outline poly
  157.     {
  158.       x1 = pcoords[0];
  159.       y1 = pcoords[1];
  160.       for (i = 1; i < number; ++i)
  161.        vlineto(pcoords[i+i], pcoords[i+i+1], highlight_color);
  162.       vlineto(pcoords[0], pcoords[1], highlight_color);
  163.     }
  164. }
  165.  
  166.  
  167.  
  168. /////////////////////////////////////////////////////////////
  169. ////// THESE ROUTINES ARE CALLED BY USER INTERFACE ROUTINES
  170. ////// TO DRAW MENUS, TEXT, ETC ETC
  171.  
  172.  
  173.     /* USER ROUTINE TO DRAW TEXT BOXES */
  174. void user_box(int x1, int y1, int x2, int y2, int color)
  175. {
  176.   int av[8];
  177.  
  178.   setup_hdwe(0);
  179.  
  180.   if (x1 < screeninfo->xmin) x1 = screeninfo->xmin;   // clip
  181.   if (x2 < screeninfo->xmin) x2 = screeninfo->xmin;
  182.   if (y1 < screeninfo->ymin) y1 = screeninfo->ymin;
  183.   if (y2 < screeninfo->ymin) y2 = screeninfo->ymin;
  184.   if (x1 > screeninfo->xmax) x1 = screeninfo->xmax;
  185.   if (x2 > screeninfo->xmax) x2 = screeninfo->xmax;
  186.   if (y1 > screeninfo->ymax) y1 = screeninfo->ymax;
  187.   if (y2 > screeninfo->ymax) y2 = screeninfo->ymax;
  188.  
  189.   av[0] = x1;     // copy to vertex array
  190.   av[1] = y1;
  191.   av[2] = x1;
  192.   av[3] = y2;
  193.   av[4] = x2;
  194.   av[5] = y2;
  195.   av[6] = x2;
  196.   av[7] = y1;
  197.  
  198.   fastpoly(4, &av[0], color);   // draw filled poly
  199.  
  200.   reset_hdwe();
  201. }
  202.  
  203.  
  204.                 // draws a line on the screen (current page)
  205. void user_draw_line(int x1, int y1, int x2, int y2, int color)
  206. {
  207.   setup_hdwe(0);
  208.   vgaline(x1, y1, x2, y2, color);
  209.   reset_hdwe();
  210. }
  211.  
  212.  
  213.  
  214.             // draws a box outline on the screen (current page)
  215. void user_draw_box(int left, int top, int right, int bottom, int color)
  216. {
  217.   setup_hdwe(0);
  218.   vgaline(left, top, right, top, color);
  219.   vgaline(right, top, right, bottom, color);
  220.   vgaline(right, bottom, left, bottom, color);
  221.   vgaline(left, bottom, left, top, color);
  222.   reset_hdwe();
  223. }
  224.  
  225.  
  226.         /* USER ROUTINE TO DRAW TEXT */
  227. void user_text(WORD x, WORD y, WORD color, char *string)
  228. {
  229.   printxyr(x, y, color, string, current_orientation);
  230. }
  231.  
  232.  
  233. /////////////////////////////////////////////////////////////////
  234. /// USER VIDEO MODE SUPPORT ROUTINES:
  235. ///
  236. /// NEVER CHANGE MOST OF THESE UNLESS YOU NEED
  237. /// MORE THAN THE VIDEO DRIVERS CAN DO
  238.  
  239.  
  240.          /* enter and setup graphics screen */
  241. void enter_graphics(unsigned vdmode, int bw)
  242. {
  243.   int i;
  244.  
  245.   set_gmode(vdmode);
  246.   set_vpage(0);
  247.   set_drawpage(0);
  248.   clr_page(0,0);
  249.  
  250.   screeninfo = screen_data();  // sets up screen data
  251.  
  252.   load_DAC_colors(NULL, screeninfo->colors, bw, 0);
  253. }
  254.  
  255.  
  256. void exit_graphics() /* exit and restore text screen */
  257. {
  258.   exit_gmode();
  259. }
  260.  
  261.  
  262. void clear_display(WORD pge)  // clear full video page
  263. {
  264.   if(pge<screeninfo->pages)
  265.       clr_page(pge,screen_clear_color);
  266. }
  267.  
  268.  
  269. void reset_screens()  // clears all of video page(s)
  270. {              // use when areas outside viewports may change
  271.   int i,j;
  272.   BOOL was_visible = cursor_hide();
  273.   j = current_video_page;
  274.  
  275.   for (i = 0; i < screeninfo->pages; ++i)
  276.     {
  277.       current_video_page = i;
  278.       cursor_hide();
  279.       clear_display(i);
  280.       cursor_show();        // cursor background refresh
  281.       cursor_hide();
  282.     }
  283.   current_video_page = j;
  284.   if(was_visible) cursor_show();
  285. }
  286.  
  287.  
  288. void shadowprint(WORD x, WORD y, WORD color, char *t)
  289. {
  290.   int bk = (color>8) ? 0 : 15;     // shadow color
  291.  
  292.   if(current_orientation & XFLIP)
  293.     {
  294.       user_text(x,y,bk,t);
  295.       user_text(x-1,y+1,color,t);
  296.     }
  297.   else
  298.     {
  299.       user_text(x,y,bk,t);
  300.       user_text(x+1,y+1,color,t);
  301.     }
  302. }
  303.  
  304.  
  305.  
  306.