console.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Flexlay - A Generic 2D Game Editor
  2. // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include <iostream>
  17. #include "fonts.hpp"
  18. #include "field.hpp"
  19. #include "console.hpp"
  20. class ConsoleImpl
  21. {
  22. public:
  23. std::vector<CL_Slot> slots;
  24. CL_Size size;
  25. /** Complete log of everything that got written to the console */
  26. std::string full_buffer;
  27. /** Buffer of the stuff currently visible on the screen */
  28. Field<char> screen;
  29. CL_Point cursor_pos;
  30. CL_Font font;
  31. ConsoleImpl(int w, int h);
  32. void putchar(char c);
  33. void draw();
  34. };
  35. ConsoleImpl::ConsoleImpl(int w, int h)
  36. : size(w, h),
  37. screen(w, h),
  38. cursor_pos(0, 0)
  39. {
  40. }
  41. void
  42. ConsoleImpl::draw()
  43. {
  44. //std::cout << "ConsoleImpl::draw()" << std::endl;
  45. int font_w = font.get_width("W");
  46. int font_h = font.get_height();
  47. for(int y = 0; y < size.height; ++y)
  48. for(int x = 0; x < size.width; ++x)
  49. {
  50. font.draw_character(x * font_w, y * font_h, screen.at(x, y));
  51. }
  52. }
  53. Console::Console(/*const CL_Font& font,*/ const CL_Rect& rect, CL_Component* parent)
  54. : CL_Component(rect, parent),
  55. impl(new ConsoleImpl(40, 24))
  56. {
  57. impl->font = Fonts::verdana11_yellow;
  58. impl->slots.push_back(sig_paint().connect(impl.get(), &ConsoleImpl::draw));
  59. }
  60. Console::~Console()
  61. {
  62. }
  63. void
  64. Console::clearscr()
  65. {
  66. for(int y = 0; y < impl->size.height; ++y)
  67. for(int x = 0; x < impl->size.width; ++x)
  68. impl->screen.at(x, y) = 0;
  69. }
  70. void
  71. ConsoleImpl::putchar(char c)
  72. {
  73. full_buffer += c;
  74. if (c == '\n')
  75. {
  76. cursor_pos.x = 0;
  77. cursor_pos.y += 1;
  78. }
  79. else
  80. {
  81. screen.at(cursor_pos.x, cursor_pos.y) = c;
  82. cursor_pos.x += 1;
  83. if (cursor_pos.x >= size.width)
  84. cursor_pos.x = 0;
  85. }
  86. // Move all content one line up
  87. if (cursor_pos.y >= size.height)
  88. screen.resize(size.width, size.height, 0, -1);
  89. }
  90. void
  91. Console::write(const std::string& str)
  92. {
  93. std::cout << str << std::flush;
  94. for(std::string::const_iterator i = str.begin(); i != str.end(); ++i)
  95. {
  96. if (*i != 0)
  97. impl->putchar(*i);
  98. }
  99. }
  100. /* EOF */