home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 79 / maccd 79.iso / multimedial / GL Tron / Source / gltron / console.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-05-13  |  1.4 KB  |  72 lines  |  [TEXT/CWIE]

  1. #define CONSOLE_DEPTH 100
  2. #define CONSOLE_WIDTH 80
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. static char buffer[CONSOLE_DEPTH][CONSOLE_WIDTH];
  8. static int position;
  9. static int offset;
  10.  
  11. void consoleInit() {
  12.   int i;
  13.  
  14.   for(i = 0; i < CONSOLE_DEPTH; i++)
  15.     buffer[i][0] = '\0';
  16.  
  17.   position = 0;
  18.   offset = 0;
  19.   fprintf(stderr, "console initialized\n");
  20. }
  21.  
  22. void consoleAddLine(char *text) {
  23.   int i = 0, x=0;
  24.  
  25.   while(i < CONSOLE_WIDTH - 1 && text[i] != 0) {
  26.     buffer[position][i] = text[i];
  27.     i++;
  28.   }
  29.   buffer[position][i] = '\0';
  30. /*  fprintf(stderr, "added \"%s\" to console at buffer[%i] with "
  31.  *      " offset of %i\n",
  32.  *      buffer[position], position, offset);
  33.  */
  34.  
  35.    position++;
  36.    
  37.    /* reposition the buffer to avoid overruns - tim */
  38.    if(position >= 99){
  39.       for(i=0;i<CONSOLE_DEPTH;i++){
  40.      strcpy(buffer[x], buffer[i]);
  41.      buffer[i][0] = '\0';
  42.      ++x;
  43.       }
  44.       position -= 4;
  45.    }
  46. }
  47.  
  48. void consoleDisplay(void(*func)(char *line, int call), int height) {
  49.   int i;
  50.   int j = 0;
  51.   for(i = 0; i < height; i++) {
  52.     if(*(buffer[ (position + i - height - offset +
  53.         CONSOLE_DEPTH) % CONSOLE_DEPTH ]) != 0)
  54.       func(buffer[ (position + i - height - offset +
  55.             CONSOLE_DEPTH) % CONSOLE_DEPTH ], j++);
  56.   }
  57. }
  58.   
  59. void consoleScrollForward(int range) {
  60.   offset -= range;
  61.   if(offset < 0) offset = 0;
  62. }
  63.  
  64. void consoleScrollBackward(int range) {
  65.   offset += range;
  66.   if(offset > CONSOLE_DEPTH - 1)
  67.     offset = CONSOLE_DEPTH - 1;
  68. }
  69.  
  70.   
  71.  
  72.