ascii-game/input_output.c
2017-03-01 16:43:37 +01:00

72 lines
1.0 KiB
C

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/select.h>
#include<signal.h>
static volatile char EOF_received = 0;
#include "conf.h"
void put_line(char * line)
{
int i;
putc('|', stdout);
for(i = 0; i < WIDTH; i++)
{
// FIXME: add colors
if(line[i] == CHAR_ME)
{
printf(ESCAPE_GREEN);
}
if(line[i] == CHAR_ENEMY)
{
printf(ESCAPE_RED);
}
putc(line[i], stdout);
if(line[i] == CHAR_ME || line[i] == CHAR_ENEMY)
{
printf(ESCAPE_RESET);
}
}
putc('|', stdout);
putc('\n', stdout);
}
void clear_screen(void)
{
int i, j;
// clear the screen
for(i = 0; i < LINES + 3; i++)
{
for(j = 0; j < WIDTH + 3 + CHRS_TO_CLEAR_ESCAPE; j++)
{
putc('\b', stdout);
}
printf("\r\033[A");
}
}
char kb_hit(void)
{
struct timeval tv = { 0L, 0L };
fd_set fds;
FD_ZERO(&fds);
FD_SET(0, &fds);
return select(1, &fds, NULL, NULL, &tv);
}
char get_last_key_or_default(void)
{
int key = KEY_DEFAULT;
while(kb_hit())
{
key = getc(stdin);
}
if(key == EOF)
{
EOF_received = 1;
}
return key;
}