File: console.c

package info (click to toggle)
gltron 0.61-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,948 kB
  • ctags: 1,175
  • sloc: ansic: 7,580; perl: 168; makefile: 149; sh: 15
file content (71 lines) | stat: -rw-r--r-- 1,448 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#define CONSOLE_DEPTH 100
#define CONSOLE_WIDTH 80

#include <stdio.h>
#include <string.h>

static char buffer[CONSOLE_DEPTH][CONSOLE_WIDTH];
static int position;
static int offset;

void consoleInit() {
  int i;

  for(i = 0; i < CONSOLE_DEPTH; i++)
    buffer[i][0] = '\0';

  position = 0;
  offset = 0;
  fprintf(stderr, "console initialized\n");
}

void consoleAddLine(char *text) {
  int i = 0, x=0;

  while(i < CONSOLE_WIDTH - 1 && text[i] != 0) {
    buffer[position][i] = text[i];
    i++;
  }
  buffer[position][i] = '\0';
/*  fprintf(stderr, "added \"%s\" to console at buffer[%i] with "
 *	  " offset of %i\n",
 *	  buffer[position], position, offset);
 */

   position++;
   
   /* reposition the buffer to avoid overruns - tim */
   if(position >= 99){
      for(i=0;i<CONSOLE_DEPTH;i++){
	 strcpy(buffer[x], buffer[i]);
	 buffer[i][0] = '\0';
	 ++x;
      }
      position -= 4;
   }
}

void consoleDisplay(void(*func)(char *line, int call), int height) {
  int i;
  int j = 0;
  for(i = 0; i < height; i++) {
    if(*(buffer[ (position + i - height - offset +
		CONSOLE_DEPTH) % CONSOLE_DEPTH ]) != 0)
      func(buffer[ (position + i - height - offset +
		    CONSOLE_DEPTH) % CONSOLE_DEPTH ], j++);
  }
}
  
void consoleScrollForward(int range) {
  offset -= range;
  if(offset < 0) offset = 0;
}

void consoleScrollBackward(int range) {
  offset += range;
  if(offset > CONSOLE_DEPTH - 1)
    offset = CONSOLE_DEPTH - 1;
}