home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Issue 2 / Freelog_HS_3_Setp_Oct_Nov_2000_CD2.mdx / Arcade / Orbit / src / console.c < prev    next >
C/C++ Source or Header  |  1999-09-27  |  5KB  |  254 lines

  1. /*
  2.  
  3. ORBIT, a freeware space combat simulator
  4. Copyright (C) 1999  Steve Belczyk <steve1@genesis.nred.ma.us>
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20. */
  21.  
  22. #include "orbit.h"
  23. #include <stdarg.h>
  24.  
  25. InitConsole()
  26. /*
  27.  *  Initialize the message console
  28.  */
  29. {
  30.     int i;
  31.  
  32.     console.next = 0;
  33.  
  34.     for (i=0; i<CONSLINES; i++)
  35.     {
  36.         console.buf[i][0] = 0;
  37.         console.age[i] = 0.0;
  38.     }
  39. }
  40.  
  41. void Cprint (char *c, ...)
  42. /*
  43.  *  Print a message to the console
  44.  */
  45. {
  46.     int i;
  47.     va_list ap;
  48.     char buf[256];
  49.  
  50.     /* Don't bother if NULL */
  51.     if (c == NULL) return;
  52.  
  53.     va_start (ap, c);
  54.     vsprintf (buf, c, ap);
  55.     va_end (ap);    
  56.  
  57.     /* Scroll messages if at end */
  58.     if (console.next == CONSLINES)
  59.     {
  60.         for (i=0; i<CONSLINES-1; i++)
  61.         {
  62.             strcpy (console.buf[i], console.buf[i+1]);
  63.             console.age[i] = console.age[i+1];
  64.         }
  65.         console.next--;
  66.     }
  67.  
  68.     /* Add this line to buffer */
  69.     strcpy (console.buf[console.next], buf);
  70.     console.age[console.next] = 0.0;
  71.     console.next++;
  72. }
  73.  
  74. DisplayConsole()
  75. /*
  76.  *  Display the console messages
  77.  */
  78. {
  79.     int i, y;
  80.  
  81.     /* Bump age */
  82.     for (i=0; i<console.next; i++) console.age[i] += deltaT;
  83.  
  84.     /* Else display each line */
  85.     y = ScreenHeight - CONSHEIGHT;
  86.     for (i=0; i<console.next; i++)
  87.     {
  88.         if (console.age[i] < CONSAGE)
  89.         {
  90.             glColor3d (0.0, 0.8, 0.2);
  91.             glRasterPos2i (1, y);
  92.             Print (GLUT_BITMAP_HELVETICA_10, console.buf[i]);
  93.             y -= CONSHEIGHT;
  94.         }
  95.     }
  96. }
  97.  
  98. InitMessage()
  99. /*
  100.  *  Initialize the message system
  101.  */
  102. {
  103.     strcpy (message.text, "No message");
  104.     message.len = glutBitmapLength (GLUT_BITMAP_HELVETICA_10,
  105.                     message.text);
  106.     message.age = MSG_MAXAGE + 1.0;
  107. }
  108.  
  109. DrawMessage()
  110. /*
  111.  *  Draw the message on the screen
  112.  */
  113. {
  114.     int rows, x, y, wrap, pixels_per_row;
  115.     char *p;
  116.  
  117.     /* Bump age */
  118.     if (!text.yes) message.age += deltaT;
  119.  
  120.     /* Never mind if too old */
  121.     if (message.age > MSG_MAXAGE) return;
  122.  
  123.     wrap = 0;
  124.     pixels_per_row = 3 * ScreenWidth / 4;
  125.  
  126.     /* Guess out how many rows this message will take */
  127.     rows = message.len / pixels_per_row;
  128.  
  129.     /* Figure out where to start */
  130.     y = (ScreenHeight / 2) + (CONSHEIGHT / 2) * (rows + 1);
  131.     if (rows == 0)
  132.     {
  133.         /* Center if short */
  134.         x = (ScreenWidth - message.len) / 2;
  135.     }
  136.     else
  137.     {
  138.         /* Start at left margin */
  139.         x = ScreenWidth / 8;
  140.     }
  141.  
  142.     /* Display the message */
  143.     glColor3f (1.0, 0.5, 0.0);
  144.     glRasterPos2i (x, y);
  145.     p = message.text;
  146.     while (*p)
  147.     {
  148.         /* Look for newline */
  149.         if (*p == '\\')
  150.         {
  151.             wrap = 0;
  152.             x = ScreenWidth / 8;
  153.             y -= CONSHEIGHT;
  154.             glRasterPos2i (x, y);
  155.             p++;
  156.             continue;
  157.         }
  158.  
  159.         /* Break at space */
  160.         if (wrap && (*p == ' '))
  161.         {
  162.             x = ScreenWidth / 8;
  163.             y -= CONSHEIGHT;
  164.             glRasterPos2i (x, y);
  165.             wrap = 0;
  166.         }
  167.         glutBitmapCharacter (GLUT_BITMAP_HELVETICA_10, *p);
  168.         x += glutBitmapWidth (GLUT_BITMAP_HELVETICA_10, *p);
  169.         p++;
  170.         if (x >= (pixels_per_row + ScreenWidth/8)) wrap = 1;
  171.     }
  172. }
  173.  
  174. void Mprint (char *msg, ...)
  175. /*
  176.  *  Print a message in the center of the screen
  177.  */
  178. {
  179.     char buf[4096];
  180.     int pixels_per_row;
  181.     unsigned int i;
  182.     va_list ap;
  183.  
  184.     if (msg == NULL) return;
  185.  
  186.     va_start (ap, msg);
  187.     vsprintf (buf, msg, ap);
  188.     va_end (ap);    
  189.  
  190.     pixels_per_row = 3 * ScreenWidth / 4;
  191.     
  192.     message.age = 0.0;
  193.     strcpy (message.text, buf);
  194.     message.len = glutBitmapLength (GLUT_BITMAP_HELVETICA_10,
  195.                     message.text);
  196.  
  197.     /* Fudge length upward for each newline */
  198.     for (i=0; i<strlen(buf); i++)
  199.     {
  200.         /* Guess that each newline will add half a row to length */
  201.         if (msg[i] == '\\') message.len += pixels_per_row / 2;
  202.     }
  203.  
  204.     /* Give the sound */
  205.     if (sound && !text.yes) PlayAudio (SOUND_COMM);
  206. }
  207.  
  208. void DoChat (void)
  209. /*
  210.  *  Process chat buffer
  211.  */
  212. {
  213.     int c;
  214.  
  215.     /* Show message to us */
  216.     Mprint (" ");
  217.     Cprint ("%s: %s", player.name, text.buf);
  218.     Log ("DoChat: %s: %s", player.name, text.buf);
  219.  
  220.     if (am_client)
  221.     {
  222.         /* Send to server */
  223.         SendASCIIPacket (clientme.socket, "CHAT %s", text.buf);
  224.     }
  225.     else if (am_server)
  226.     {
  227.         /* Send to each active client */
  228.         for (c=0; c<NCLIENTS; c++)
  229.         {
  230.             if (client[c].active && (c != server.client))
  231.             {
  232.                 SendASCIIPacket (client[c].socket, "CMSG %s: %s",
  233.                     player.name, text.buf);
  234.             }
  235.         }
  236.     }
  237. }
  238.  
  239. GetText (prompt, func)
  240. char *prompt;
  241. void (*func)(void);
  242. /*
  243.  *  Start getting text from player
  244.  */
  245. {
  246.     text.yes = 1;
  247.     text.index = 0;
  248.     text.buf[0] = 0;
  249.     strcpy (text.prompt, prompt);
  250.     text.func = func;
  251.     Mprint ("%s", text.prompt);
  252. }
  253.  
  254.