/* * Copyright (C) 2002-2007 The Warp Rogue Team * Part of the Warp Rogue Project * * This software is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License. * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY. * * See the license.txt file for more details. */ /* * curses platform code */ #define USE_COLOR #define CURSES_USED #define Uses_Ui #define Uses_Game #define Uses_Util #include "mheader.h" #include #define SYMBOL_CURSOR '*' #if defined(USE_COLOR) static void colors_init(void); #endif static chtype ScreenBuffer[TEXT_ROWS][TEXT_COLUMNS]; #if defined(USE_COLOR) static chtype AnsiColor[MAX_ANSI_COLORS]; static chtype Color[MAX_COLORS]; #endif static SCREEN_COORD CursorX; static SCREEN_COORD CursorY; /* * IO init */ void io_init(void) { initscr(); notimeout(stdscr, 1); noecho(); cbreak(); nonl(); keypad(stdscr, 1); curs_set(0); #if defined(USE_COLOR) colors_init(); #endif resize_term(TEXT_ROWS, TEXT_COLUMNS); wresize(stdscr, TEXT_ROWS, TEXT_COLUMNS); CursorY = CursorX = 0; } /* * IO clean up */ void io_clean_up(void) { nocbreak(); nl(); echo(); endwin(); } /* * updates the screen */ void update_screen(void) { SCREEN_COORD y, x; for (y = 0; y < TEXT_ROWS; y++) { for (x = 0; x < TEXT_COLUMNS; x++) { mvaddch(y, x, ScreenBuffer[y][x]); } } refresh(); } /* * clears the screen */ void clear_screen(void) { SCREEN_COORD y, x; for (y = 0; y < TEXT_ROWS; y++) { for (x = 0; x < TEXT_COLUMNS; x++) { ScreenBuffer[y][x] = ' '; #if defined(USE_COLOR) ScreenBuffer[y][x] |= Color[C_BLACK]; #endif } } } /* * places the cursor at the specified location */ void place_cursor_at(SCREEN_COORD y, SCREEN_COORD x) { CursorY = y; CursorX = x; } /* * returns the current Y coordinate of the cursor */ SCREEN_COORD cursor_y(void) { return CursorY; } /* * returns the current X coordinate of the cursor */ SCREEN_COORD cursor_x(void) { return CursorX; } /* * renders the cursor at its current position */ void render_cursor(COLOR color) { NOT_USED(color); ScreenBuffer[CursorY][CursorX] = SYMBOL_CURSOR; #if defined(USE_COLOR) ScreenBuffer[CursorY][CursorX] |= Color[color]; #endif } /* * renders a character at the current cursor position */ void render_char(COLOR color, SYMBOL ch) { ScreenBuffer[CursorY][CursorX] = ch; #if defined(USE_COLOR) ScreenBuffer[CursorY][CursorX] |= Color[color]; #else NOT_USED(color); #endif ++CursorX; } /* * renders a character at the specified position */ void render_char_at(COLOR color, SCREEN_COORD y, SCREEN_COORD x, SYMBOL ch) { place_cursor_at(y, x); render_char(color, ch); } /* * get key */ KEY_CODE lowlevel_get_key(void) { int key; key = getch(); switch (key) { case KEY_UP: return WR_KEY_UP; case KEY_DOWN: return WR_KEY_DOWN; case KEY_LEFT: return WR_KEY_LEFT; case KEY_RIGHT: return WR_KEY_RIGHT; #if !defined(NCURSES_VERSION) case KEY_A2: return '8'; case KEY_B1: return '4'; case KEY_B3: return '6'; case KEY_C2: return '2'; case PADENTER: return WR_KEY_ENTER; #endif case KEY_A1: return '7'; case KEY_A3: return '9'; case KEY_B2: return '5'; case KEY_C1: return '1'; case KEY_C3: return '3'; case KEY_ENTER: return WR_KEY_ENTER; case KEY_F(1): return WR_KEY_F1; case KEY_F(2): return WR_KEY_F2; case KEY_F(3): return WR_KEY_F3; case KEY_F(4): return WR_KEY_F4; case KEY_F(5): return WR_KEY_F5; case KEY_F(6): return WR_KEY_F6; case KEY_F(7): return WR_KEY_F7; case KEY_F(8): return WR_KEY_F8; case KEY_F(9): return WR_KEY_F9; case KEY_F(10): return WR_KEY_F10; case KEY_F(11): return WR_KEY_F11; case KEY_F(12): return WR_KEY_F12; case KEY_NPAGE: return WR_KEY_PAGE_DOWN; case KEY_PPAGE: return WR_KEY_PAGE_UP; default: return key; } } /* * clears a section of the screen for text display */ void clear_text_window(SCREEN_COORD y1, SCREEN_COORD x1, SCREEN_COORD y2, SCREEN_COORD x2 ) { SCREEN_COORD y, x; for (y = y1; y <= y2; y++) { for (x = x1; x <= x2; x++) { ScreenBuffer[y][x] = ' '; #if defined(USE_COLOR) ScreenBuffer[y][x] |= Color[C_BLACK]; #endif } } } /* * renders the background */ void render_background(void) { clear_screen(); } /* * waits n seconds */ void sec_sleep(int n_seconds) { NOT_USED(n_seconds); /* NOT IMPLEMENTED */ } #if defined(USE_COLOR) /* * colors init */ static void colors_init(void) { int i; start_color(); init_pair(ANSI_BLACK, COLOR_BLACK, COLOR_BLACK); init_pair(ANSI_RED, COLOR_RED, COLOR_BLACK); init_pair(ANSI_GREEN, COLOR_GREEN, COLOR_BLACK); init_pair(ANSI_BROWN, COLOR_YELLOW, COLOR_BLACK); init_pair(ANSI_BLUE, COLOR_BLUE, COLOR_BLACK); init_pair(ANSI_MAGENTA, COLOR_MAGENTA, COLOR_BLACK); init_pair(ANSI_CYAN, COLOR_CYAN, COLOR_BLACK); init_pair(ANSI_LIGHT_GRAY, COLOR_WHITE, COLOR_BLACK); init_pair(ANSI_DARK_GRAY, ANSI_DARK_GRAY, COLOR_BLACK); for (i = 0; i < MAX_ANSI_COLORS / 2; i++) { AnsiColor[i] = COLOR_PAIR(i); } AnsiColor[MAX_ANSI_COLORS / 2] = COLOR_PAIR(ANSI_DARK_GRAY) | A_BOLD; for (i = MAX_ANSI_COLORS / 2 + 1; i < MAX_ANSI_COLORS; i++) { AnsiColor[i] = COLOR_PAIR(i - MAX_ANSI_COLORS / 2) | A_BOLD; } for (i = 0; i < MAX_COLORS; i++) { Color[i] = AnsiColor[color_ansi(i)]; } } #endif