-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.cpp
More file actions
109 lines (89 loc) · 2.67 KB
/
gui.cpp
File metadata and controls
109 lines (89 loc) · 2.67 KB
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
#include "pch.h"
#include "main.hpp"
//Panel Height, Bar Width, Message X Position, Message Log Height
static const int pHeight(7), bWidth(20), msgX(bWidth+2), msgHeight(pHeight-1);
Gui::Gui() {
con = new TCODConsole(engine.sW, pHeight);
}
Gui::~Gui() {
delete con;
log.clear();
}
void Gui::render() {
con->setDefaultBackground(TCODColor::yellow);
con->clear();
renderBar(1, 1, bWidth, "HP", engine.player->mortal->hp, engine.player->mortal->maxHp,
TCODColor::lightRed, TCODColor::darkerRed);
con->setDefaultBackground(TCODColor::white);
con->hline(0, 0, engine.sW, TCOD_BKGND_SET);
int y(1);
float cCoef(0.4f); //"color coefficient" makes text look darker.
for (auto &msg : log) { //<-- this is a "ranged for" loop, by the way.
con->setDefaultForeground(msg->col * cCoef);
con->print(msgX, y, msg->text);
y++;
if (cCoef < 1.0f) {
cCoef += 0.3f;
}
}
renderMouseLook();
TCODConsole::blit(con, 0, 0, engine.sW, pHeight, TCODConsole::root, 0, engine.sH - pHeight);
}
void Gui::renderBar(int x, int y, int width, const char *name, float value,
float maxValue, const TCODColor &barColor,
const TCODColor &backColor) {
con->setDefaultBackground(backColor);
con->rect(x, y, width, 1, false, TCOD_BKGND_SET);
int barWidth = (int)(value / maxValue * width);
if (barWidth > 0) {
con->setDefaultBackground(barColor);
con->rect(x, y, barWidth, 1, false, TCOD_BKGND_SET);
}
con->setDefaultForeground(TCODColor::white);
con->printEx(x + width / 2, y, TCOD_BKGND_NONE, TCOD_CENTER,
"%s : %g/%g", name, value, maxValue);
}
void renderLine(int x, int y, int w, const TCODColor &lineColor) {
//con->hline()
}
void Gui::renderMouseLook() {
if (!engine.dungeon->isInFov(engine.mouse.cx, engine.mouse.cy)) { return; }
char buf[128] = "";
bool first = true;
for (auto &ent : engine.entL) {
if (ent->x == engine.mouse.cx && ent->y == engine.mouse.cy) {
if (!first) {
strcat_s(buf, ", ");
}
else {
first = false;
}
strcat_s(buf, ent->name);
}
}
con->setDefaultForeground(TCODColor::lightGrey);
con->print(1, 0, buf);
}
Gui::Message::Message(const char *text, const TCODColor &col) : text(_strdup(text)), col(col) {}
Gui::Message::~Message() { delete [] text; }
void Gui::message(const TCODColor &col, const char *text, ...) {
va_list ap;
char buf[128];
va_start(ap, text);
vsprintf_s(buf, text, ap);
va_end(ap);
char *lineBegin = buf;
char *lineEnd;
do {
if (size(log) == msgHeight) {
log.erase(begin(log));
}
lineEnd = strchr(lineBegin, '\n');
if (lineEnd) {
*lineEnd = '\0';
}
std::shared_ptr<Message> msg = std::make_shared<Message>(lineBegin, col);
log.push_back(msg);
lineBegin = lineEnd + 1;
} while (lineEnd);
}