/** * HULK Console functionality. */ module hulk.console; import hulk.fb; import hulk.kfont; struct Console { /** Console width in text columns. */ private static __gshared size_t m_width; /** Console height in text rows. */ private static __gshared size_t m_height; /** Current console cursor X position. */ private static __gshared size_t m_x; /** Current console cursor Y position. */ private static __gshared size_t m_y; /** * Initialize the console. */ public static void initialize() { m_width = Fb.width / Kfont.advance; m_height = Fb.height / Kfont.line_height; } /** * Clear the console. */ public static void clear() { Fb.clear(); m_x = 0u; m_y = 0u; } /** * Write a character to the console. * * @param ch Character to write. */ public static void write(char ch) { if (ch == '\n') { m_x = 0u; m_y++; } else { render_char(m_x, m_y, ch); m_x++; if (m_x == m_width) { m_x = 0u; m_y++; } } if (m_y == m_height) { m_y--; Fb.copy_rows_up(fb_y(m_height - 1u), (m_height - 1u) * Kfont.line_height, Kfont.line_height); Fb.rect(0u, fb_y(m_height - 1u), fb_x(m_width), Kfont.line_height, 0u); } } /** * Render a character. * * @param x X position. * @param y Y position. * @param ch Character to render. */ private static void render_char(size_t x, size_t y, char ch) { const(CharInfo) * ci = &Kfont.chars[ch]; Fb.blit_alpha_bitmap(fb_x(x) + ci.left, fb_y(y) + ci.top - ci.height, ci.bitmap, ci.width, ci.height); } /** * Get the framebuffer X coordinate corresponding to the console X position. */ private static size_t fb_x(size_t x) { return x * Kfont.advance; } /** * Get the framebuffer Y coordinate corresponding to the console Y position. */ private static size_t fb_y(size_t y) { return Fb.height - ((y + 1u) * Kfont.line_height); } }