-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjDisplayGrid.cpp
160 lines (142 loc) · 5.46 KB
/
ObjDisplayGrid.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include "ObjDisplayGrid.h"
#include <curses.h>
#ifdef _WIN32
#include <windows.h>
#endif
std::shared_ptr<ObjDisplayGrid> ObjDisplayGrid::instance = nullptr;
ObjDisplayGrid::ObjDisplayGrid(int _width, int _gameHeight, int _topHeight) :
width(_width), gameHeight(_gameHeight), topHeight(_topHeight) {
//2D array of GridChars
objectGrid = new GridChar **[width]; //make a 2d array that has 1 vectir of GridChar
//std::vector<GridChar>** pop character if it leaves
for (int i = 0; i < width; i++) {
objectGrid[i] = new GridChar * [gameHeight + topHeight];
for (int j = 0; j < gameHeight + topHeight; j++) { //topHeight bad for some reason
objectGrid[i][j] = NULL;
}
}
//std::cout << "ObjDisplayGrid::ObjDisplayGrid" << std::endl;
// initialize ncurses
// set command window size if running on windows, useful when running in Visual Studio
// as far as I am aware, no way to do this on linux
#ifdef _WIN32
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD size = { (short)width, (short)(gameHeight + topHeight) };
SMALL_RECT DisplayArea = { 0, 0, (short)(size.X - 1), (short)(size.Y - 1) };
SetConsoleScreenBufferSize(handle, size);
SetConsoleWindowInfo(handle, TRUE, &DisplayArea);
#endif
//initializes ncurses
initscr();
//makes characters typed immediately available, instead of waiting for enter to be pressed
cbreak();
//stops typed characters from being shown, makes it easier to get keypresses
noecho();
// clears the screen to start
clear();
}
void ObjDisplayGrid::initRoomGrid(std::shared_ptr<Room> room)
{
int width = room->getWidth(); //but attributes not saved from handler for some reason
int height = room->getHeight();
int posX = room->getPosX(); //if i = posX or i = posX - 1
int posY = room->getPosY();
for (int i = posX; i < width + posX; i++) {
for (int j = posY + topHeight; j < height + posY + topHeight; j++) {
char c;
if (i % (width + posX - 1) == 0 || j % (height + topHeight + posY - 1) == 0 || i == posX || j == posY + topHeight) {
c = 'X';
}
else {
c = '.';
}
addObjectToDisplay(new GridChar(c), i, j);
}
}
//refreshes ncurses
update();
}
void ObjDisplayGrid::initPassageGrid(std::shared_ptr<Passage> passage)
{
int width = 6;//6room->getWidth(); but attributes not saved from handler for some reason
int height = 5;//room->getHeight();
int posX = 0;//room->getPosX();
int posY = 0;//room->getPosY();
char c = '#'; //iterate from 1 to end of the list. Current point (i) and (i-1) draw all those points
//not done yet. Still need to make modifications
for (int i = posX; i < width + posX; i++) {
for (int j = posY + topHeight; j < height + posY + topHeight; j++) {
addObjectToDisplay(new GridChar(c), i, j);
}
}
//refreshes ncurses
update();
}
void ObjDisplayGrid::initCreatureGrid(std::shared_ptr<Creature> creature)
{
//make error validation if creature is on our outside dungeon boundries or rooms
char c;
if (creature->getName() == "player") {
c = '@';
}
else {
c = creature->getType();
}
addObjectToDisplay(new GridChar(c), creature->getPosX(), creature->getPosY() + topHeight);
update();
}
//added destructor 10/21/20
ObjDisplayGrid::~ObjDisplayGrid()
{
// free memory from the dynamically sized object grid
for (int i = 0; i < width; i++) {
// delete all character objects in the grid
for (int j = 0; j < gameHeight; j++) {
delete objectGrid[i][j];
}
// delete the column
delete[] objectGrid[i];
}
// delete the array of columns
delete[] objectGrid;
objectGrid = NULL;
// free ncurses data
endwin();
}
std::shared_ptr<ObjDisplayGrid> ObjDisplayGrid::getObjDisplayGrid(int _gameHeight, int _width, int _topHeight) {
if (instance == nullptr) {
instance = std::shared_ptr<ObjDisplayGrid>(new ObjDisplayGrid(_width, _gameHeight, _topHeight));
instance->gameHeight = _gameHeight;
instance->width = _width;
instance->topHeight = _topHeight;
}
// std::cout << "ObjDisplayGrid::getObjDisplayGrid" << std::endl;
return instance;
}
void ObjDisplayGrid::setTopMessageHeight(int _topHeight) {
instance->topHeight = _topHeight;
//std::cout << "ObjDisplayGrid::setTopMessageHeight" << std::endl;
//std::cout << "TopMessageHeight: " << std::to_string(instance -> topHeight) << std::endl;
}
void ObjDisplayGrid::addObjectToDisplay(GridChar* ch, int x, int y) {
// note grid objects start from 0,0 and go until width,height
// x between 0 and width
if ((0 <= x) && (x < width)) {
// y between 0 and height
if ((0 <= y) && (y < gameHeight)) {
// delete existing character if present
if (objectGrid[x][y] != NULL) {
delete objectGrid[x][y];
}
// add new character to the internal character list
objectGrid[x][y] = ch;
// draws the character on the screen, note it is relative to 0,0 of the terminal
mvaddch(y, x, ch->getChar());
}
}
}
void ObjDisplayGrid::update() {
// refreshes ncurses
refresh();
}