-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathConsole.cpp
More file actions
163 lines (129 loc) · 3.24 KB
/
Console.cpp
File metadata and controls
163 lines (129 loc) · 3.24 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
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
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <vector>
#include <cpp3ds/Config.hpp>
#include <cpp3ds/Window/Event.hpp>
#include <cpp3ds/Graphics/Console.hpp>
#include <cpp3ds/Window/GlContext.hpp>
#include <cpp3ds/Graphics/RenderTarget.hpp>
#include <cpp3ds/Resources.hpp>
#include <stdio.h>
#include <sstream>
#ifndef EMULATION
#include <sys/iosupport.h>
extern u32 __linear_heap_size;
#endif
namespace cpp3ds {
std::vector<String> g_stdout;
}
extern "C" {
#ifndef EMULATION
ssize_t console_write(struct _reent *r, int fd, const char *ptr, size_t len) {
cpp3ds::String s;
int i = 0;
while (i < len) {
s += ptr[i];
i++;
}
cpp3ds::g_stdout.push_back(s);
return len;
}
static const devoptab_t dotab_stdout = {
"con", 0, NULL, NULL, console_write, NULL, NULL, NULL
};
#endif
}
namespace cpp3ds
{
bool Console::m_initialized = false;
////////////////////////////////////////////////////////////
Console::Console()
{
//
}
////////////////////////////////////////////////////////////
Console::~Console()
{
if (m_initialized)
{
//
}
}
////////////////////////////////////////////////////////////
void Console::initialize()
{
if (!m_initialized) {
m_initialized = true;
#ifndef EMULATION
devoptab_list[STD_OUT] = &dotab_stdout;
devoptab_list[STD_ERR] = &dotab_stdout;
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
#endif
}
}
////////////////////////////////////////////////////////////
void Console::create()
{
initialize();
priv::ResourceInfo font = priv::core_resources["opensans.ttf"];
m_font.loadFromMemory(font.data, font.size);
m_memoryText.setFont(m_font);
m_memoryText.setCharacterSize(12);
m_limit = 1000;
}
////////////////////////////////////////////////////////////
void Console::update(float delta)
{
for (String& s : g_stdout)
write(s);
g_stdout.clear();
if (m_lines.size() > m_limit)
m_lines.erase(m_lines.begin(), m_lines.end() - m_limit);
#ifndef EMULATION
std::ostringstream ss;
ss << (__linear_heap_size - linearSpaceFree()) / 1024 << "kb / " << __linear_heap_size / 1024 << "kb";
m_memoryText.setString(ss.str());
m_memoryText.setPosition(395 - m_memoryText.getGlobalBounds().width, 5);
#endif
}
////////////////////////////////////////////////////////////
void Console::write(String text)
{
Text line(text, m_font, 10);
m_lines.push_back(line);
}
////////////////////////////////////////////////////////////
bool Console::processEvent(Event& event)
{
if (event.type == Event::KeyPressed) {
if (event.key.code == Keyboard::DPadDown && Keyboard::isKeyDown(Keyboard::R)) {
m_visible = !m_visible;
return false;
}
}
// Allow event to also be processed by the game
return true;
}
////////////////////////////////////////////////////////////
void Console::setVisible(bool visible)
{
m_visible = visible;
}
////////////////////////////////////////////////////////////
void Console::draw(RenderTarget& target, RenderStates states) const
{
if (!m_visible)
return;
int h = 240;
int i = m_lines.size();
while (h > 0 && i > 0) {
Text text = m_lines[--i];
h -= text.getGlobalBounds().height;
text.setPosition(0, h);
target.draw(text);
}
target.draw(m_memoryText);
}
} // namespace cpp3ds