fixed: all ready for input-testing

This commit is contained in:
2024-07-05 19:31:09 -04:00
parent 13519b619d
commit b792b0a8e7
7 changed files with 193 additions and 39 deletions

97
src/console_output.cc Normal file
View File

@ -0,0 +1,97 @@
#include "console_output.h"
#include <string>
#include <ncurses.h>
console_output::console_output(std::ostream &new_out): out{new_out} {}
/* Attributes
black 30 40
red 31 41
green 32 42
yellow 33 43
blue 34 44
magenta 35 45
cyan 36 46
white 37 47
reset 0 (everything back to normal)
bold/bright 1 (often a brighter shade of the same colour)
underline 4
inverse 7 (swap foreground and background colours)
bold/bright off 21
underline off 24
inverse off 27
Format:
\033[X;Ym
X Y are numbers from above
*/
std::string console_output::get_code(const int attr) {
if (attr < 255)
return "\033[0m";
std::string result = "\033[";
if (attr & A_STANDOUT)
result += "1;";
if (attr & A_UNDERLINE)
result += "4;";
if (attr & COLOR_PAIR(COLOR_BLACK))
result += "30;";
if (attr & COLOR_PAIR(COLOR_RED))
result += "31;";
if (attr & COLOR_PAIR(COLOR_GREEN))
result += "32;";
if (attr & COLOR_PAIR(COLOR_YELLOW))
result += "33;";
if (attr & COLOR_PAIR(COLOR_BLUE))
result += "34;";
if (attr & COLOR_PAIR(COLOR_MAGENTA))
result += "35;";
if (attr & COLOR_PAIR(COLOR_CYAN))
result += "36;";
if (attr & COLOR_PAIR(COLOR_WHITE))
result += "37;";
result[result.length() - 1] = 'm';
return result;
}
void console_output::render() {
for (std::size_t idx = 0; idx < contents.size(); ++idx) {
if (idx % DISPLAY_WIDTH == 0 && idx)
out << std::endl;
out << get_code(contents[idx]) + (char)contents[idx];
}
}
void console_output::print_char(const position &pos, const char ch,
const int attr) {
if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT)
return;
contents[pos.y * DISPLAY_WIDTH + pos.x] = attr | ch;
}
void console_output::print_str(const position &pos, const std::string &str,
const int attr) {
if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT)
return;
int head = pos.y * DISPLAY_WIDTH + pos.x;
for (std::size_t i = 0; i < str.length(); ++i)
contents[i + head] = attr | str[i];
}

22
src/console_output.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef __CONSOLE_OUTPUT_H__
#define __CONSOLE_OUTPUT_H__
#include <iostream>
#include "display.h"
class console_output final : public display {
private:
std::ostream &out;
std::string get_code(const int attr);
public:
console_output(std::ostream &new_out);
void render() override;
void clear() override;
void print_char(const position &pos,
const char ch, const int attr) override;
void print_str(const position &pos,
const std::string &str, const int attr) override;
};
#endif

View File

@ -60,6 +60,8 @@ const int MAP_HEIGHT = 25;
const int MAP_WIDTH = 79;
const int DISPLAY_HEIGHT = 30;
const int DISPLAY_WIDTH = 79;
const int DISPLAY_BUFFER_SIZE = DISPLAY_HEIGHT * DISPLAY_WIDTH +
DISPLAY_WIDTH * 3;
// TODO: list all extra features
// using constants to keep track of features

View File

@ -1,21 +1,4 @@
#include "display.h"
display::display(): contents{DISPLAY_HEIGHT} {}
void display::clear() {
for (auto line : contents)
line.clear();
}
void display::print(std::ostream &out) const {
for (auto line : contents)
out << line;
}
void display::print_line(const std::string &line, const int linenum) {
contents[linenum] = line;
}
void display::print_position(const position &pos, const char ch) {
contents[pos.y][pos.x] = ch;
}
display::display():
contents{DISPLAY_BUFFER_SIZE, 0} {}

View File

@ -1,35 +1,35 @@
/*
* CS 246 Final Project
* File: display.h
* Purpose: handles map functionality
*/
#ifndef __DISPLAY_H__
#define __DISPLAY_H__
#include <iostream>
#include <string>
#include <vector>
#include "position.h"
#include "constants.h"
#include "cursor.h"
class display final {
private:
std::vector<std::string> contents;
cursor &curse;
class display {
protected:
// use an array of ints to keep track of what's on the screen
// This will have a bit of buffer for the last line
// just in case it overflows
std::vector<int> contents;
public:
display();
display(cursor &new_curse, int argc, char **argv);
void clear();
// use this instead of overloading for clarity
void print(std::ostream &out) const;
void print_line(const std::string &line, const int linenum);
virtual ~display() = default;
void render() const ;
// Only call this to refresh the entire output
virtual void render() = 0;
// will override any character that was there
void print_position(const position &pos, const char ch);
// Clears the contents buffer
virtual void clear() = 0;
virtual void print_char(const position &pos, const char ch,
const int attr =
A_NORMAL | COLOR_PAIR(COLOR_WHITE)) = 0;
virtual void print_str(const position &pos, const std::string &str,
const int attr =
A_NORMAL | COLOR_PAIR(COLOR_WHITE)) = 0;
// default arguments are to be set in the base class's declaration
};
#endif

29
src/file_output.cc Normal file
View File

@ -0,0 +1,29 @@
#include "file_output.h"
void file_output::render() {
for (std::size_t idx = 0; idx < contents.size(); ++idx) {
if (idx % DISPLAY_WIDTH == 0 && idx)
out << std::endl;
out << (char)contents[idx];
}
}
void file_output::print_char(const position &pos, const char ch,
const int attr) {
if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT)
return;
contents[pos.y * DISPLAY_WIDTH + pos.x] = ch;
}
void file_output::print_str(const position &pos, const std::string &str,
const int attr) {
if (pos.x >= DISPLAY_WIDTH || pos.y >= DISPLAY_HEIGHT)
return;
int head = pos.y * DISPLAY_WIDTH + pos.x;
for (std::size_t i = 0; i < str.length(); ++i)
contents[i + head] = str[i];
}

21
src/file_output.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef __FILE_OUTPUT_H__
#define __FILE_OUTPUT_H__
#include <fstream>
#include "display.h"
class file_output final : public display {
private:
std::ofstream out;
public:
file_output(std::ofstream &&new_out);
void render() override;
void clear() override;
void print_char(const position &pos,
const char ch, const int attr) override;
void print_str(const position &pos,
const std::string &str, const int attr) override;
};
#endif