123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- #include <iostream>
- #include "fonts.hpp"
- #include "field.hpp"
- #include "console.hpp"
- class ConsoleImpl
- {
- public:
- std::vector<CL_Slot> slots;
- CL_Size size;
-
- std::string full_buffer;
-
- Field<char> screen;
- CL_Point cursor_pos;
- CL_Font font;
- ConsoleImpl(int w, int h);
- void putchar(char c);
- void draw();
- };
- ConsoleImpl::ConsoleImpl(int w, int h)
- : size(w, h),
- screen(w, h),
- cursor_pos(0, 0)
- {
- }
- void
- ConsoleImpl::draw()
- {
-
- int font_w = font.get_width("W");
- int font_h = font.get_height();
- for(int y = 0; y < size.height; ++y)
- for(int x = 0; x < size.width; ++x)
- {
- font.draw_character(x * font_w, y * font_h, screen.at(x, y));
- }
- }
- Console::Console( const CL_Rect& rect, CL_Component* parent)
- : CL_Component(rect, parent),
- impl(new ConsoleImpl(40, 24))
- {
- impl->font = Fonts::verdana11_yellow;
- impl->slots.push_back(sig_paint().connect(impl.get(), &ConsoleImpl::draw));
- }
- Console::~Console()
- {
- }
- void
- Console::clearscr()
- {
- for(int y = 0; y < impl->size.height; ++y)
- for(int x = 0; x < impl->size.width; ++x)
- impl->screen.at(x, y) = 0;
- }
- void
- ConsoleImpl::putchar(char c)
- {
- full_buffer += c;
- if (c == '\n')
- {
- cursor_pos.x = 0;
- cursor_pos.y += 1;
- }
- else
- {
- screen.at(cursor_pos.x, cursor_pos.y) = c;
- cursor_pos.x += 1;
- if (cursor_pos.x >= size.width)
- cursor_pos.x = 0;
- }
-
- if (cursor_pos.y >= size.height)
- screen.resize(size.width, size.height, 0, -1);
- }
- void
- Console::write(const std::string& str)
- {
- std::cout << str << std::flush;
- for(std::string::const_iterator i = str.begin(); i != str.end(); ++i)
- {
- if (*i != 0)
- impl->putchar(*i);
- }
- }
|