home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / source / devel5 / userint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-14  |  5.3 KB  |  250 lines

  1. /* User interface routines for 3DVIEW */
  2.  
  3. /* Written by Bernie Roehl, January 1992 (broehl@sunee.waterloo.edu) */
  4.  
  5. /* Copyright 1992 by Dave Stampe and Bernie Roehl.
  6.    May be freely used to write software for release into the public domain;
  7.    all commercial endeavours MUST contact Bernie Roehl and Dave Stampe
  8.    for permission to incorporate any part of this software into their
  9.    products!
  10.  
  11.      ATTRIBUTION:  If you use any part of this source code or the libraries
  12.      in your projects, you must give attribution to REND386, Dave Stampe,
  13.      and Bernie Roehl in your documentation, source code, and at startup
  14.      of your program.  Let's keep the freeware ball rolling!
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #include <dos.h>
  20. #include <conio.h>     /* kbhit() */
  21. #include <bios.h>      /* bioskey() */
  22. #include <string.h>    /* strlen() */
  23.  
  24. #include "rend386.h"
  25. #include "userint.h"
  26. #include "pointer.h"
  27. #include "cursor.h"
  28.  
  29. /* Colors */
  30. #define INTERIOR   4
  31. #define TEXT      14
  32.  
  33. #define TEXTHEIGHT 9
  34. #define TEXTWIDTH 8
  35.  
  36. extern int left_page, right_page;
  37. extern PDRIVER *menu_device;
  38.  
  39. #define CTRLF10   0x6700
  40.  
  41. unsigned getkey(void)
  42. {
  43.     unsigned c, d;
  44.     union REGS regs;
  45.     int shifted;
  46.  
  47.     c = bioskey(0);
  48.     regs.h.ah = 2;
  49.     int86(0x16, ®s, ®s);
  50.     shifted = (regs.h.al & 3);
  51.     d = c & 0xFF;
  52.     if (d == 0)
  53.     {
  54.         c |= shifted; /* normal case */
  55.         if (c == CTRLF10)
  56.         {
  57.             screendump();
  58.             return 0;
  59.         }
  60.         return c; /* special case (shifted keypad) */
  61.     }
  62.     if (!shifted) return d;
  63.     if (d == '8' || d == '4' || d == '6' || d == '2') return (c << 8) + shifted;
  64.     else return d;
  65. }
  66.  
  67. void neatbox(int w, int h, int *x, int *y)
  68. {
  69.     extern int v_page;
  70.     *x = (screeninfo->xcent - (w >> 1)) & 0x0FF8;
  71.     *y = screeninfo->ycent - (h >> 1);
  72.     right_page = left_page = v_page; /* stop stereo for now */
  73.     user_box(*x-2, *y-2, *x+w+8, *y+h+8, 0);
  74.     user_box(*x-5, *y-5, *x+w+5, *y+h+5, INTERIOR);
  75. }
  76.  
  77. static int last_jb, last_mb;    /* used to detect click for exit */
  78.  
  79. void poptext(char *text[])
  80. {
  81.  int i, h = 0, w = 0, x, y, page;
  82.  
  83.  page = cursor_hide();
  84.  for (i = 0; text[i]; ++i)
  85.   {
  86.    h += TEXTHEIGHT;
  87.    if (strlen(text[i])*TEXTWIDTH > w)
  88.    w = strlen(text[i])*TEXTWIDTH;
  89.   }
  90.  if (w > 300) w = 300;
  91.  neatbox(w , h , &x, &y);
  92.  for (i = 0; text[i]; ++i)
  93.   {
  94.    user_text(x, y, TEXT, text[i]);
  95.    y += TEXTHEIGHT;
  96.   }
  97.  cursor_show(page);
  98.  set_goodbye();
  99. }
  100.  
  101. set_goodbye()
  102. {
  103.  extern int have_joystick;
  104.  int x,y;
  105.  
  106.  if(have_joystick)    /* update click status */
  107.   {
  108.    joystick_data joy;
  109.    joy.port = 0;
  110.    joy.scale = 0;
  111.    joystick_read(&joy);
  112.    last_jb = joy.buttons;
  113.   }
  114.  last_mb = 0;
  115.  move_2D(menu_device,&x,&y,&last_mb);
  116. }
  117.  
  118. int goodbye()  /* returns 1 if click, else key, else 0 if none */
  119. {                            /* used to exit menus, etc */
  120.  extern int have_joystick;
  121.  int x, y, b;
  122.  
  123.  if(have_joystick)    /* test for joystick click */
  124.   {
  125.    joystick_data joy;
  126.    joy.port = 0;
  127.    joy.scale = 0;
  128.    joystick_read(&joy);
  129.    b = joy.buttons;
  130.    if((b^last_jb)&b) return 1;
  131.    last_jb = b;
  132.   }
  133.  
  134.  b = 0;
  135.  move_2D(menu_device,&x,&y,&b);  /* test for mouse click */
  136.  if((b^last_mb)&b) return 1;
  137.  last_mb = b;
  138.  
  139.  if(kbhit())            /* check for key click */
  140.   {
  141.    return getkey();
  142.   }
  143.  
  144.  return 0;
  145. }
  146.  
  147.  
  148. int menu(char *text[]) /* returns key/click */
  149. {
  150.     int i, h = 0, w = 0, x, y, page;
  151.     int top, left;
  152.     unsigned buttons;
  153.     int num = 0;
  154.     char *c;
  155.  
  156.     page = cursor_hide();
  157.     for (i = 0; text[i]; ++i)
  158.     {
  159.         h += TEXTHEIGHT;
  160.         if (strlen(text[i])*TEXTWIDTH > w)
  161.             w = strlen(text[i])*TEXTWIDTH;
  162.     }
  163.     if (w > 300) w = 300;
  164.     neatbox(w, h, &x, &y);
  165.     top = y; 
  166.     left = x;
  167.     for (i = 0; text[i]; ++i)
  168.     {
  169.         user_text(x, y, TEXT, text[i]);
  170.         y += TEXTHEIGHT;
  171.         num++;
  172.     }
  173.     cursor_show(page);
  174.     do
  175.         {
  176.         while (move_2D(menu_device, &x, &y, &buttons) == 0) if (kbhit()) return toupper(getkey());
  177.         ;
  178.         do
  179.             {
  180.             move_2D(menu_device,&x, &y, &buttons);
  181.             if (kbhit()) return toupper(getkey());
  182.         }
  183.         while (!buttons);
  184.         do
  185.             {
  186.             move_2D(menu_device,&x, &y, &buttons);
  187.             if (kbhit()) return toupper(getkey());
  188.         }
  189.         while (buttons);
  190.         if (y < top || x < left || x > left+w || y > h+top) return 0;
  191.         i = (y-top) / TEXTHEIGHT;
  192.     }
  193.     while ((i < 0) || (i >= num));
  194.     for (c = text[i]; (!(isupper(*c))) && (*c); c++)
  195.         ;
  196.     i = *c;
  197.     return(i);
  198.  
  199.     return toupper(getkey());
  200. }
  201.  
  202.  
  203. void popmsg(char *msg)
  204. {
  205.     int x, y, page;
  206.     page = cursor_hide();
  207.     neatbox(strlen(msg)*TEXTWIDTH+16, TEXTHEIGHT+16, &x, &y);
  208.     user_text(x+8, y+8, TEXT, msg);
  209.     cursor_show(page);
  210. }
  211.  
  212. unsigned askfor(char *prompt, char *buff, int n)
  213. {
  214.     unsigned c, page;
  215.     int x, y, i;
  216.  
  217.     if(n+strlen(prompt)>36) n = 36-strlen(prompt);
  218.     page = cursor_hide();
  219.     neatbox(strlen(prompt)*TEXTWIDTH + n * TEXTWIDTH + 10,
  220.         TEXTHEIGHT + 5, &x, &y);
  221.     user_text(x, y, TEXT, prompt);
  222.     x += strlen(prompt) * TEXTWIDTH;
  223.     buff[i = 0] = '\0';
  224.     set_goodbye();
  225.     while (1)
  226.      {
  227.       c = goodbye();
  228.       if(c==0x1B || c==1)    /* abort */
  229.        {
  230.         buff[0] = 0;
  231.         break;
  232.        }
  233.       if (c == '\r') break;
  234.       if (c == '\b' && i > 0)
  235.        {
  236.         user_box(x, y, x+TEXTWIDTH*strlen(buff), y+TEXTHEIGHT, INTERIOR);
  237.         buff[--i] = '\0';
  238.         user_text(x, y, TEXT,buff);
  239.        }
  240.         if (isprint(c) && i < n)
  241.        {
  242.         buff[i++] = c;
  243.         buff[i] = '\0';
  244.         user_text(x, y, TEXT, buff);
  245.       }
  246.      }
  247.     cursor_show(page);
  248.     return c;
  249. }
  250.