-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathold_xconsole.cpp
200 lines (170 loc) · 5.06 KB
/
old_xconsole.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
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
/**
* This file belongs to the 'xlab' game engine.
* Copyright 2009 xfacter
* Copyright 2016 wickles
* This work is licensed under the LGPLv3
* subject to all terms as reproduced in the included LICENSE file.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include "xtexture.h"
#include "xtext.h"
#include "xconsole.h"
/* ConsoleString */
xConsoleString::xConsoleString(int max_string_len)
{
remain = 0.0f;
alpha = 0.0f;
color = 0;
string_length = (u16)max_string_len;
string = (char*)malloc(string_length*sizeof(char));
*this = "DEAD";
}
xConsoleString::~xConsoleString()
{
free(string);
}
xConsoleString& xConsoleString::operator=(char* new_string)
{
snprintf(string, string_length, new_string);
return *this;
}
xConsoleString& xConsoleString::operator=(const xConsoleString& new_string)
{
remain = new_string.remain;
alpha = new_string.alpha;
color = new_string.color;
snprintf(string, string_length, new_string.string);
return *this;
}
char* xConsoleString::String()
{
return string;
}
bool xConsoleString::Alive()
{
return (alpha > 0.0f);
}
void xConsoleString::Recharge()
{
remain = LIFE_PRECISION;
alpha = LIFE_PRECISION;
}
void xConsoleString::Update(float dt, float remain_fade, float alpha_fade)
{
if (Alive())
{
if (remain > 0) remain -= remain_fade*dt;
else alpha -= alpha_fade*dt;
}
}
u8 xConsoleString::Alpha()
{
if (!Alive()) return 0;
return (u8)(255.0f*(alpha/LIFE_PRECISION));
}
unsigned int xConsoleString::Color()
{
unsigned int col = color & 0x00ffffff;
col = col | (Alpha() << 24);
return col;
}
/* xConsole */
xConsole::xConsole(int max_strings, int max_string_len)
{
num_strings = (u16)max_strings;
string_length = (u16)max_string_len;
console_strings = new xConsoleString*[num_strings];
for (int i = 0; i < num_strings; i++) console_strings[i] = new xConsoleString(string_length);
}
xConsole::xConsole(int max_strings, int max_string_len, float remain_time, float fade_time)
{
num_strings = (u16)max_strings;
string_length = (u16)max_string_len;
console_strings = new xConsoleString*[num_strings];
for (int i = 0; i < num_strings; i++) console_strings[i] = new xConsoleString(string_length);
Settings(remain_time, fade_time);
}
xConsole::~xConsole()
{
for (int i = 0; i < num_strings; i++) delete console_strings[i];
delete[] console_strings;
}
void xConsole::Settings(float remain_time, float fade_time)
{
remain_speed = LIFE_PRECISION/remain_time;
fade_speed = LIFE_PRECISION/fade_time;
}
void xConsole::Printf(unsigned int color, char* string, ... )
{
char buffer[256];
va_list ap;
va_start(ap, string);
vsnprintf(buffer, sizeof(buffer), string, ap);
va_end(ap);
Print(color, buffer);
}
void xConsole::Print(unsigned int color, char* string)
{
for (int i = num_strings - 1; i > 0; i--) *console_strings[i] = *console_strings[i-1];
console_strings[0]->color = color;
*console_strings[0] = string;
console_strings[0]->Recharge();
}
void xConsole::Refresh()
{
for (int i = 0; i < num_strings; i++) console_strings[i]->Recharge();
}
void xConsole::UpdateConsole(float dt)
{
for (int i = 0; i < num_strings; i++) console_strings[i]->Update(dt, remain_speed, fade_speed);
}
void xConsole::RenderConsole(xTexture* font, int x, int y, int width, int height, int line_height, bool aa)
{
if (!font || width < 0 || height <= 0 || line_height < 0) return;
int line = y + height - line_height;
if (width)
{
char buffer[256];
char* str_begin;
char* str_track;
int str_len;
u16 line_length[16];
int lines;
for (int i = 0; i < num_strings; i++)
{
if (line < y || !console_strings[i]->Alive()) break;
str_begin = console_strings[i]->String();
str_track = str_begin;
str_len = strlen(str_begin);
if (str_len > 0)
{
for (lines = 0; lines < 16; lines++)
{
line_length[lines] = xText2DStringLength(font, 0, 0, 1.0f, width, buffer, str_track, sizeof(buffer));
str_track += line_length[lines];
if (line_length[lines] == 0 || (u32)str_track >= (u32)str_begin + str_len) break;
}
while(lines > 0 && line >= y)
{
lines -= 1;
str_track -= line_length[lines];
xText2DStringLength(font, 0, 0, 1.0f, width, buffer, str_track, sizeof(buffer));
xText2DPrint(font, x, line, console_strings[i]->Color(), 0, 1.0f, aa, 0, buffer);
line -= line_height;
}
}
}
}
else
{
for (int i = 0; i < num_strings; i++)
{
if (line < y || !console_strings[i]->Alive()) break;
xText2DPrint(font, x, line, console_strings[i]->Color(), 0, 1.0f, aa, 0, console_strings[i]->String());
line -= line_height;
}
}
}