forked from voodooattack/ADWIF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcod.hpp
283 lines (270 loc) · 10.3 KB
/
tcod.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* Copyright (c) 2013, Abdullah A. Hassan <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TCODRENDERER_H
#define TCODRENDERER_H
#include <libtcod.hpp>
#include <iostream>
#include "renderer.hpp"
#include "input.hpp"
#include <memory>
#include <boost/thread.hpp>
namespace ADWIF
{
class TCODRenderer: public Renderer
{
friend class TCODInput;
public:
virtual const std::string name() const { return "libtcod"; }
virtual bool init()
{
// TCODConsole::setCustomFont("font.png", TCOD_FONT_LAYOUT_ASCII_INROW | TCOD_FONT_TYPE_GREYSCALE, 32, 2048);
TCODConsole::setCustomFont("font.png", TCOD_FONT_LAYOUT_ASCII_INROW | TCOD_FONT_TYPE_GREYSCALE);
TCODConsole::initRoot (80, 48, "A Dance with Ice and Fire", false, TCOD_RENDERER_SDL);
TCODConsole::root->setDefaultBackground(TCODColor::black);
TCODConsole::root->setDefaultForeground(TCODColor::silver);
TCODConsole::root->setBackgroundFlag(TCOD_BKGND_SET);
myConsole = TCODConsole::root;
return true;
}
virtual void shutdown() {
if (TCODConsole::root != myConsole)
delete myConsole;
delete TCODConsole::root;
TCODConsole::root = nullptr;
}
virtual int width() const { return TCODConsole::root->getWidth(); }
virtual int height() const { return TCODConsole::root->getHeight(); }
virtual bool resized() const { return false; }
virtual void clear() { myConsole->clear(); }
virtual void refresh() { myConsole->flush(); }
virtual void style(Colour fg, Colour bg, int styleMask)
{
myBackgroundColour = colourToTCOD(bg, true, styleMask);
myForegroundColour = colourToTCOD(fg, false, styleMask);
myStyle = styleMask;
}
virtual void style(int x, int y, int len, Colour fg, Colour bg, int styleMask)
{
myConsole->setCharBackground(x, y, colourToTCOD(bg, true, styleMask));
myConsole->setCharForeground(x, y, colourToTCOD(fg, false, styleMask));
myStyle = styleMask;
if (styleMask & Style::AltCharSet)
{
for (int xx = x; xx < x + len; xx++)
{
int c = myConsole->getChar(xx, y);
extChar(c);
myConsole->setChar(xx, y, c);
}
}
}
virtual void startWindow(int x, int y, int w, int h) {
myConsole = new TCODConsole(w, h);
myConsole->setDefaultBackground(TCODColor::black);
myConsole->setDefaultForeground(TCODColor::silver);
myConsole->setBackgroundFlag(TCOD_BKGND_SET);
myConX = x;
myConY = y;
}
virtual void endWindow()
{
if (myConsole != TCODConsole::root)
{
TCODConsole::root->blit(myConsole, 0, 0, myConsole->getWidth(), myConsole->getHeight(), TCODConsole::root, myConX, myConY);
delete myConsole;
myConsole = TCODConsole::root;
}
}
virtual void drawChar(int x, int y, int c) {
if (myStyle & Style::AltCharSet)
extChar(c);
if (c == ' ')
myConsole->putCharEx(x, y, c, myForegroundColour, myForegroundColour);
else
myConsole->putCharEx(x, y, c, myForegroundColour, myBackgroundColour);
}
virtual void drawText(int x, int y, const std::string & text)
{
TCODColor bg = myConsole->getDefaultBackground(), fg = myConsole->getDefaultForeground();
myConsole->setDefaultBackground(myBackgroundColour);
myConsole->setDefaultForeground(myForegroundColour);
if (myStyle & Style::AltCharSet)
{
std::string ext = text;
for(unsigned int i = 0; i < ext.size(); i++)
extChar(ext[i]);
myConsole->printRect(x, y, myConsole->getWidth(), myConsole->getHeight(), ext.c_str());
}
else
myConsole->printRect(x, y, myConsole->getWidth(), myConsole->getHeight(), text.c_str());
myConsole->setDefaultBackground(bg);
myConsole->setDefaultForeground(fg);
}
virtual void drawMessage(const std::string & message);
virtual void drawEntity(const class Entity *, int x, int y) { }
virtual bool supportsMultiLayers() const { return true; }
private:
TCODColor myBackgroundColour;
TCODColor myForegroundColour;
int myStyle;
TCODConsole * myConsole;
int myConX, myConY;
private:
template<typename T>
void extChar(T & c)
{
switch (c)
{
case 'l': c = (T)TCOD_CHAR_NW; break;
case 'q': c = (T)TCOD_CHAR_HLINE; break;
case 'x': c = (T)TCOD_CHAR_VLINE; break;
case 'k': c = (T)TCOD_CHAR_NE; break;
case 'j': c = (T)TCOD_CHAR_SE; break;
case 'm': c = (T)TCOD_CHAR_SW; break;
}
}
TCODColor colourToTCOD(Colour c, bool bg, int styleMask)
{
// std::cout << std::boolalpha << bg << " " << styleMask << std::endl;
if (!bg)
{
if (styleMask & Style::Dark)
{
switch (c)
{
case Default: return TCODColor::darkestGrey;
case Black: return TCODColor::black;
case Red: return TCODColor::darkestRed;
case Green: return TCODColor::darkestGreen;
case Yellow: return TCODColor::darkestYellow;
case Blue: return TCODColor::darkBlue;
case Magenta: return TCODColor::darkestMagenta;
case Cyan: return TCODColor::darkestCyan;
case White: return TCODColor::darkestGrey;
default: return TCODColor::black;
}
}
else if (styleMask & Style::Dim)
{
switch (c)
{
case Default: return TCODColor::darkerGrey;
case Black: return TCODColor::black;
case Red: return TCODColor::darkerRed;
case Green: return TCODColor::darkerGreen;
case Yellow: return TCODColor::darkerYellow;
case Blue: return TCODColor::blue;
case Magenta: return TCODColor::darkerMagenta;
case Cyan: return TCODColor::darkerCyan;
case White: return TCODColor::grey;
default: return TCODColor::black;
}
}
else if (styleMask & Style::Bold)
{
switch (c)
{
case Default: return TCODColor::white;
case Black: return TCODColor::darkestGrey;
case Red: return TCODColor::red;
case Green: return TCODColor::green;
case Yellow: return TCODColor::yellow;
case Blue: return TCODColor::lightBlue;
case Magenta: return TCODColor::magenta;
case Cyan: return TCODColor::cyan;
case White: return TCODColor::white;
default: return TCODColor::black;
}
}
else /*if (styleMask & Style::Normal)*/
{
switch (c)
{
case Default: return TCODColor::grey;
case Black: return TCODColor::black;
case Red: return TCODColor::darkRed;
case Green: return TCODColor::darkGreen;
case Yellow: return TCODColor::darkYellow;
case Blue: return TCODColor::darkBlue;
case Magenta: return TCODColor::darkMagenta;
case Cyan: return TCODColor::darkCyan;
case White: return TCODColor::silver;
default: return TCODColor::black;
}
}
}
else
{
switch (c)
{
case Default: return TCODColor::white;
case Black: return TCODColor::black;
case Red: return TCODColor::red;
case Green: return TCODColor::green;
case Yellow: return TCODColor::yellow;
case Blue: return TCODColor::blue;
case Magenta: return TCODColor::magenta;
case Cyan: return TCODColor::cyan;
case White: return TCODColor::silver;
default: return TCODColor::black;
}
}
}
};
class TCODInput: public Input
{
public:
TCODInput(const std::shared_ptr<class Renderer> & renderer):
myRenderer(std::dynamic_pointer_cast<TCODRenderer>(renderer)), myTimeout(0) { }
virtual bool init() {
// TCODConsole::setKeyboardRepeat(0, 0);
return true;
}
virtual void shutdown() { }
virtual int key()
{
TCOD_key_t key;
if (myTimeout == -1)
TCODSystem::waitForEvent(TCOD_EVENT_KEY_PRESS, &key, NULL, true);
else
{
// if (myTimeout)
// boost::this_thread::sleep_for(boost::chrono::milliseconds(myTimeout));
TCODSystem::checkForEvent(TCOD_EVENT_KEY_PRESS, &key, NULL);
}
if (key.vk == TCODK_SPACE)
return ' ';
if (key.vk == TCODK_KPENTER)
return Key::Enter;
if (key.vk != TCODK_NONE && key.vk != TCODK_CHAR)
return (key.vk<<16);
else if (key.c)
return key.c;
return 0;
}
virtual int getTimeout() const { return myTimeout; }
virtual void setTimeout(int timeout) { myTimeout = timeout; }
virtual std::string prompt(const std::string & text, unsigned int maxLen, const std::string & suffix = std::string());
virtual bool promptYn(const std::string & text, bool caseSensetive);
private:
std::shared_ptr<TCODRenderer> myRenderer;
int myTimeout;
};
}
#endif // TCODRENDERER_H