SVN / public / code / vorlesung-05 / TerminalManager.cpp

Revision 3043
Date2026-05-19T16:29:20+02:00
Committercu1017
Download
// Copyright 2026, University of Freiburg
// Chair of Algorithms and Data Structures
// Author: Christoph Ullinger <ullingec@cs.uni-freiburg.de>

#include "./TerminalManager.h"
#include <ncurses.h>

// ____________________________________________________________________________
int TerminalManager::White = COLOR_WHITE;
int TerminalManager::Red = COLOR_RED;
int TerminalManager::Green = COLOR_GREEN;

// ____________________________________________________________________________
TerminalManager::TerminalManager() {
  // Initialize ncurses and some settings suitable for our game.
  initscr();
  cbreak();
  noecho();
  curs_set(false);
  nodelay(stdscr, true);
  keypad(stdscr, true);

  // Initialize all the colors we need for the game.
  start_color();
  init_pair(1, COLOR_WHITE, COLOR_BLACK);
  init_pair(2, COLOR_RED, COLOR_BLACK);
  init_pair(3, COLOR_GREEN, COLOR_BLACK);

  // Set the logical dimensions of the screen.
  numRows_ = LINES;
  numCols_ = COLS / 2;
}

// _____________________________________________________________________________
void TerminalManager::drawPixel(int row, int col, int color) {
  if (color == COLOR_WHITE) {
    attron(COLOR_PAIR(1));
  } else if (color == COLOR_RED) {
    attron(COLOR_PAIR(2));
  } else if (color == COLOR_GREEN) {
    attron(COLOR_PAIR(3));
  }
  attron(A_REVERSE);
  mvprintw(row, 2 * col, "  ");
}

// _____________________________________________________________________________
void TerminalManager::refresh() { ::refresh(); }

// _____________________________________________________________________________
UserInput TerminalManager::getUserInput() {
  UserInput userInput;
  userInput.keycode_ = getch();
  return userInput;
}

// _____________________________________________________________________________
bool UserInput::isEscape() { return keycode_ == 27; }
bool UserInput::isKeyUp() { return keycode_ == KEY_UP; }
bool UserInput::isKeyDown() { return keycode_ == KEY_DOWN; }
bool UserInput::isKeyLeft() { return keycode_ == KEY_LEFT; }
bool UserInput::isKeyRight() { return keycode_ == KEY_RIGHT; }

// _____________________________________________________________________________
TerminalManager::~TerminalManager() { endwin(); }