hos/src/hulk/console.d
2022-03-25 12:37:18 -04:00

103 lines
2.3 KiB
D

/**
* 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);
}
}