-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathconsole.cpp
More file actions
executable file
·209 lines (167 loc) · 4.34 KB
/
console.cpp
File metadata and controls
executable file
·209 lines (167 loc) · 4.34 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
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
#include "console.h"
void Console::newLine( void ) {
this->chary++;
this->charx = 0;
// Check for overflows.
if ( this->chary > MAX_LINE ) {
this->clear();
}
}
void Console::printChar( char c, uint32 color ) {
// Look at all the important characters.
switch ( c ) {
case '\n':
this->newLine();
return;
break;
case '\b':
if ( this->charx > 0 ) {
this->charx--;
// Erase the character.
this->canvas->ClearCharacter( this->charx * 8 + this->padding,
this->chary * 16 + this->padding );
// Refresh
this->canvas->Draw();
}
return;
break;
case '\t':
if ( this->charx + 4 < MAX_CHAR_PER_LINE ) {
this->charx += 4;
}
return;
break;
}
// Check for overflows.
if ( this->charx > MAX_CHAR_PER_LINE ) {
this->newLine();
}
if ( this->chary > MAX_LINE ) {
this->clear( );
return;
}
// Draw it.
this->canvas->DrawCharacter( (int)(this->charx * 8 + this->padding),
(int)(this->chary * 16 + this->padding),
c, color );
// Refresh the screen.
this->canvas->Draw();
// Increment.
this->charx++;
}
// Standard printf function.
void Console::kprint( const char* string ) {
this->kprint( (char*) string );
}
void Console::kprint( char c ) {
this->printChar( c, 0xFFFFFF );
}
// Clearscreen function.
void Console::clear( void ) {
this->charx = 0;
this->chary = 1;
this->canvas->Clear( 0x000000 );
this->canvas->Draw();
}
void Console::kprint( char* string ) {
// Iterate over the string.
while ( *string != '\0' ) {
// Print the character.
this->printChar( *(string++), 0xFFFFFF );
}
}
void Console::kbase( long prim, long base, long size ) {
// Validate the base.
if ( base < 1 ) {
this->kprint("error: Unsupported base in mathematical operation.\n");
return;
}
// Create the kfloat
Math::kfloat digit(prim);
char symbols[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P' };
if ( digit.getIsLarge() ) {
this->kbase(digit.getBig1(), base, Math::getDigitCount<mint>( digit.getBig1(), base ) - 1);
this->kbase(digit.getBig2(), base, Math::getDigitCount<mint>( digit.getBig2(), base ) - 1);
return;
} else if ( size == 0 ) {
size = Math::getDigitCount<mint>(prim, base) - 1;
}
while ( size >= 0 ) {
mint denominator = Math::pow( base, size - 1, false );
Math::kfloat top, bottom, val;
top = ( prim <= base ) ? prim * base : prim;
bottom = denominator;
val = top / bottom;
// Print the character.
// Check if the character overflows.
if ( val.getMajor() > base ) {
this->printChar( 'e', 0x00FF00 );
} else {
this->printChar( symbols[(char)val.getMajor()], 0xFF0000 );
}
prim = prim - (val.getMajor() * denominator );
// Decrement
size--;
}
}
void Console::kbase( long value, long base ) {
this->kbase( value, base, 0 );
}
template<class T>
void Console::kprintf( char* string, T value ) {
// Iterate over each character.
char p, c;
while ( *(string) != '\0' ) {
// Check for special characters.
if ( *(string) == '%' ) {
// Peek.
if ( *(string+1) == 'x' || *(string + 1) == 'X' ) {
// Hex print
this->kbase( (mint)value, 16 );
string+=2;
continue;
} else if ( *(string+1) == 'd' || *(string + 1) == 'D' ) {
this->kbase( (mint)value, 10 );
string+=2;
continue;
} else if ( *(string+1) == 'o' || *(string + 1) == 'O' ) {
this->kbase( (mint)value, 8 );
string+=2;
continue;
} else if ( *(string + 1) == '%' ) {
this->printChar( '%', 0xFFFFFF );
string+=2;
continue;
} else if ( *(string + 1) == 'b' || *(string + 1 ) == 'B' ) {
this->kbase( (mint)value, 2 );
string+=2;
continue;
}
}
// Otherwise, normal.
this->printChar( *(string), 0xFFFFFF );
string++;
}
}
void Console::kout( const char* string ) {
const char* prefix = "[DONE]\t";
while ( *(prefix) != '\0' ) {
this->printChar( *(prefix++), 0xFF0000 );
}
this->kprint( string );
this->printChar('\n', 0xFF0000 );
}
template<class T>
void Console::kprintf( const char* string, T value ) {
this->kprintf<T>( (char*)string, value );
}
// Constructor.
Console::Console( gpu2dCanvas* surface ) {
// Setup the variables.
this->charx = 0;
this->chary = 0;
this->padding = 10;
this->canvas = surface;
// Clear the screen.
this->clear();
}