Reverse framebuffer vertical coordinates to start with 0 at top

This commit is contained in:
Josh Holtrop 2023-09-13 11:57:47 -04:00
parent c958b9ffcc
commit 4fcea6a283
2 changed files with 24 additions and 18 deletions

View File

@ -6,6 +6,11 @@ module hulk.console;
import hulk.fb;
import hulk.kfont;
/**
* Represent the HULK console.
*
* Coordinate (0,0) is the top-left console position.
*/
struct Console
{
/** Console width in text columns. */
@ -64,7 +69,7 @@ struct Console
if (m_y == m_height)
{
m_y--;
Fb.copy_rows_up(fb_y(m_height - 1u),
Fb.copy_rows_up(fb_y(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);
@ -81,7 +86,7 @@ struct Console
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) + Kfont.baseline_offset + ci.top - ci.height, ci.bitmap, ci.width, ci.height);
Fb.blit_alpha_bitmap(fb_x(x) + ci.left, fb_y(y) + Kfont.line_height - Kfont.baseline_offset - ci.top, ci.bitmap, ci.width, ci.height);
}
/**
@ -97,6 +102,6 @@ struct Console
*/
private static size_t fb_y(size_t y)
{
return Fb.height - ((y + 1u) * Kfont.line_height);
return y * Kfont.line_height;
}
}

View File

@ -9,6 +9,8 @@ import hulk.kfont;
/**
* Represent a graphical frame buffer.
*
* Coordinate (0,0) is the top-left corner of the framebuffer.
*
* TODO: Handle other pixel formats. Currently only BGRx is supported.
*/
struct Fb
@ -80,8 +82,8 @@ struct Fb
/**
* Draw a solid rectangle on the framebuffer.
*
* @param x X coordinate of left side of rectangle.
* @param y Y coordinate of bottom side of rectangle.
* @param x X coordinate of left of rectangle.
* @param y Y coordinate of top of rectangle.
* @param width Width of rectangle.
* @param height Height of rectangle.
* @param color Color of rectangle.
@ -100,7 +102,7 @@ struct Fb
/**
* Draw a horizontal line.
*
* @param x X coordinate of left side of line.
* @param x X coordinate of left of line.
* @param y Y coordinate of line.
* @param width Width of line.
* @param color Color of line.
@ -116,7 +118,7 @@ struct Fb
* Draw a vertical line.
*
* @param x X coordinate of line.
* @param y Y coordinate of bottom of line.
* @param y Y coordinate of top of line.
* @param height Height of line.
* @param color Color of line.
*/
@ -127,7 +129,7 @@ struct Fb
{
m_buffer1[buffer_index] = color;
m_device_buffer[buffer_index] = color;
buffer_index -= m_stride;
buffer_index += m_stride;
}
}
@ -136,8 +138,8 @@ struct Fb
* The foreground will be white (based on the alpha value), and the
* background black.
*
* @param x X coordinate of left side of target location.
* @param y Y coordinate of bottom side of target location.
* @param x X coordinate of left of target location.
* @param y Y coordinate of top of target location.
* @param alpha_bitmap 8-bit alpha-channel bitmap.
* @param width Bitmap width.
* @param height Bitmap height.
@ -145,7 +147,6 @@ struct Fb
static void blit_alpha_bitmap(size_t x, size_t y, const(ubyte) * alpha_bitmap,
size_t width, size_t height)
{
y += height - 1u;
size_t bitmap_index;
for (size_t iy = 0u; iy < height; iy++)
{
@ -158,7 +159,7 @@ struct Fb
bitmap_index++;
}
memcpy32(&m_device_buffer[row_buffer_index], &m_buffer1[row_buffer_index], width);
y--;
y++;
}
}
@ -167,8 +168,8 @@ struct Fb
* The foreground will be white (based on the alpha value), and the
* background black.
*
* @param x X coordinate of left side of target location.
* @param y Y coordinate of bottom side of target location.
* @param x X coordinate of left of target location.
* @param y Y coordinate of top of target location.
* @param alpha_bitmap 8-bit alpha-channel bitmap.
* @param width Bitmap width.
* @param height Bitmap height.
@ -182,14 +183,14 @@ struct Fb
/**
* Copy framebuffer rows up in the framebuffer.
*
* @param y Y coordinate of bottom side of region to copy up.
* @param y Y coordinate of top of region to copy up.
* @param height Height of region to copy up.
* @param offset Number of rows to copy region up by.
*/
static void copy_rows_up(size_t y, size_t height, size_t offset)
{
size_t dest_buffer_index = buffer_index(0u, y + height + offset - 1u);
size_t src_buffer_index = buffer_index(0u, y + height - 1u);
size_t dest_buffer_index = buffer_index(0u, y - offset);
size_t src_buffer_index = buffer_index(0u, y);
memcpy32(&m_buffer1[dest_buffer_index],
&m_buffer1[src_buffer_index],
(m_stride * height));
@ -208,6 +209,6 @@ struct Fb
*/
private static size_t buffer_index(size_t x, size_t y)
{
return (m_height - y - 1u) * m_stride + x;
return y * m_stride + x;
}
}