ascii-game/input_output.c

110 lines
2.6 KiB
C

/*
* Copyright(c) 2017 Daniel Knuettel
*/
/* This program is free software.
* Anyways if you think this program is worth it
* and we meet shout a drink for me.
*/
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Dieses Programm ist Freie Software: Sie können es unter den Bedingungen
* der GNU Affero General Public License, wie von der Free Software Foundation,
* Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
* veröffentlichten Version, weiterverbreiten und/oder modifizieren.
*
* Dieses Programm wird in der Hoffnung, dass es nützlich sein wird, aber
* OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
* Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
* Siehe die GNU Affero General Public License für weitere Details.
*
* Sie sollten eine Kopie der GNU Affero General Public License zusammen mit diesem
* Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
*/
#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;
}