-
Notifications
You must be signed in to change notification settings - Fork 0
/
gooseEscapeConsole.hpp
75 lines (65 loc) · 2.04 KB
/
gooseEscapeConsole.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
STUDENTS: Don't change the code in this file (unless you are very
comfortable with using the BearLibTerminal)
*/
#ifndef GOOSE_CONSOLE
#define GOOSE_CONSOLE
#include <iostream>
using namespace std;
#include <BearLibTerminal.h>
#include "gooseEscapeUtil.hpp"
/*
Going further: Learn the other syntax for implementing a class that is
more appropriate for working with multiple files, and improve the class code.
*/
class Console
{
private:
string messages[NUM_CONSOLE_Y];
int messageRow;
public:
Console()
{
// each string element in messages is initialized to "" by the string constructor
messageRow = 0;
}
void writeLine(string new_message_to_print)
{
//clear the whole console
terminal_clear_area(MIN_CONSOLE_X, MIN_CONSOLE_Y, NUM_CONSOLE_X, NUM_CONSOLE_Y);
// update content of console rows
if(messageRow < NUM_CONSOLE_Y)
{
messages[messageRow] = new_message_to_print;
messageRow++;
}
else
{
for(int index = 0; index < NUM_CONSOLE_Y-1; index++)
messages[index] = messages[index+1];
messages[NUM_CONSOLE_Y-1] = new_message_to_print;
}
// output all message rows to the console
for(int line = 0; line < NUM_CONSOLE_Y; line++)
terminal_print(MIN_CONSOLE_X, MIN_CONSOLE_Y + line, messages[line].c_str());
terminal_refresh();
}
/*
Having more than one console is a bad idea. So you really shouldn't be
calling these functions.
*/
Console(Console const & src)
{
// memory allocation of array is fixed, so copy constructor and assignment operator are the same
*this = src;
}
Console& operator=(Console const & src)
{
cerr << "Warning! You have more than one Console object" << endl;
for (int index = 0; index < NUM_CONSOLE_Y; index++)
messages[index] = src.messages[index];
messageRow = src.messageRow;
return *this;
}
};
#endif