Adds support for console colors.

This commit is contained in:
Felix Queißner 2016-05-02 09:32:27 +02:00
parent 9e3bfe3f82
commit a17b936cfb
4 changed files with 52 additions and 18 deletions

View file

@ -2,6 +2,20 @@
#include "screen.hpp"
struct FColor
{
FColor() : color(Color::White) { }
FColor(Color color) : color(color) { }
Color color;
};
struct BColor
{
BColor() : color(Color::Black) { }
BColor(Color color) : color(color) { }
Color color;
};
class Console
{
public:
@ -10,22 +24,38 @@ private:
Screen * const screen;
int x;
int y;
Color fg, bg;
public:
Console(Screen *screen);
void put(char c);
void newline();
};
static inline Console operator << (Console & console, char c) {
console.put(c);
return console;
}
static inline Console operator << (Console & console, const char *str) {
while(*str) {
console << *str++;
inline Console operator << (char c)
{
this->put(c);
return *this;
}
return console;
}
inline Console operator << (const char *str)
{
while(*str) {
*this << *str++;
}
return *this;
}
inline Console operator << (const FColor &color)
{
this->fg = color.color;
return *this;
}
inline Console operator << (const BColor &color)
{
this->bg = color.color;
return *this;
}
};

View file

@ -5,6 +5,9 @@
extern "C" void init(void)
{
const char * hw = "Hello World!\nHello new line!";
Console::main << hw;
Console::main
<< "Hello World!\n"
<< FColor(Color::Yellow) << "Hello color!" << FColor() << "\n"
<< BColor(Color::Blue) << "Hello blue!" << BColor() << "\n"
<< "Hello default color.";
}

View file

@ -4,7 +4,8 @@ Console Console::main(&Screen::main);
Console::Console(Screen *screen) :
screen(screen),
x(0), y(0)
x(0), y(0),
fg(Color::White), bg(Color::Black)
{
}
@ -20,8 +21,8 @@ void Console::put(char c)
default:
ScreenChar &sc = (*this->screen)(this->x++, this->y);
sc.c = c;
sc.fg = (int)Color::Black;
sc.bg = (int)Color::LightRed;
sc.fg = (int)this->fg;
sc.bg = (int)this->bg;
break;
}
if(this->x >= this->screen->width) {

View file

@ -11,7 +11,7 @@ Screen::Screen(ScreenChar *buffer, int width, int height) :
{
this->clear();
}
void Screen::clear() {
for(int i = 0; i < this->width * this->height; i++) {
this->buffer[i].c = ' ';