Skip to content

Commit

Permalink
Fixed draw filled rectangle preventOffBoundPixels
Browse files Browse the repository at this point in the history
  • Loading branch information
Szymekk44 committed May 11, 2024
1 parent f85b607 commit 7a9bb19
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
8 changes: 6 additions & 2 deletions source/Cosmos.System2/Graphics/Canvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,13 +529,17 @@ public virtual void DrawRectangle(Color color, int x, int y, int width, int heig
/// <param name="yStart">The starting point Y coordinate.</param>
/// <param name="width">The width of the rectangle.</param>
/// <param name="height">The height of the rectangle.</param>
public virtual void DrawFilledRectangle(Color color, int xStart, int yStart, int width, int height)
public virtual void DrawFilledRectangle(Color color, int xStart, int yStart, int width, int height, bool preventOffBoundPixels = true)
{
if (height == -1)
{
height = width;
}

if (preventOffBoundPixels)
{
width = Math.Min(width, (int)Mode.Width - xStart);
height = Math.Min(height, (int)Mode.Height - yStart);
}
for (int y = yStart; y < yStart + height; y++)
{
DrawLine(color, xStart, y, xStart + width - 1, y);
Expand Down
11 changes: 8 additions & 3 deletions source/Cosmos.System2/Graphics/SVGAIICanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,17 @@ public override void DrawPoint(Color color, int x, int y)
driver.SetPixel((uint)x, (uint)y, (uint)color.ToArgb());
}

public override void DrawFilledRectangle(Color color, int xStart, int yStart, int width, int height)
public override void DrawFilledRectangle(Color color, int xStart, int yStart, int width, int height, bool preventOffBoundPixels = true)
{
var argb = color.ToArgb();
var frameSize = (int)driver.FrameSize;
if(preventOffBoundPixels)
{
width = Math.Min(width, (int)mode.Width - xStart);
height = Math.Min(height, (int)mode.Height - yStart);
}


// For now write directly into video memory, once _xSVGADriver.Fill will be faster it will have to be changed
for (int i = yStart; i < yStart + height; i++)
{
driver.videoMemory.Fill(GetPointOffset(xStart, i) + (int)frameSize, width, argb);
Expand Down Expand Up @@ -352,7 +357,7 @@ public override void DrawImage(Image image, int x, int y, bool preventOffBoundPi
var height = (int)image.Height;
var frameSize = (int)driver.FrameSize;
var data = image.RawData;
if(preventOffBoundPixels)
if (preventOffBoundPixels)
{
var maxWidth = Math.Min(width, (int)mode.Width - x);
var maxHeight = Math.Min(height, (int)mode.Height - y);
Expand Down
3 changes: 2 additions & 1 deletion source/Cosmos.System2/Graphics/VBECanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,10 @@ public override void DrawArray(Color[] aColors, int aX, int aY, int aWidth, int
}
}

public override void DrawFilledRectangle(Color aColor, int aX, int aY, int aWidth, int aHeight)
public override void DrawFilledRectangle(Color aColor, int aX, int aY, int aWidth, int aHeight, bool preventOffBoundPixels = true)
{
// ClearVRAM clears one uint at a time. So we clear pixelwise not byte wise. That's why we divide by 32 and not 8.
if(preventOffBoundPixels)
aWidth = (int)(Math.Min(aWidth, Mode.Width - aX) * (int)Mode.ColorDepth / 32);
var color = aColor.ToArgb();

Expand Down
7 changes: 6 additions & 1 deletion source/Cosmos.System2/Graphics/VGACanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,13 @@ public override void Disable()
}
}

public override void DrawFilledRectangle(Color aColor, int aXStart, int aYStart, int aWidth, int aHeight)
public override void DrawFilledRectangle(Color aColor, int aXStart, int aYStart, int aWidth, int aHeight, bool preventOffBoundPixels = true)
{
if (preventOffBoundPixels)
{
aWidth = Math.Min(aWidth, (int)Mode.Width - aXStart);
aHeight = Math.Min(aHeight, (int)Mode.Height - aYStart);
}
driver.DrawFilledRectangle(aXStart, aYStart, aWidth, aHeight, driver.GetClosestColorInPalette(aColor));
}

Expand Down

0 comments on commit 7a9bb19

Please sign in to comment.