Skip to content

Commit

Permalink
More summaries, alpha rectangles
Browse files Browse the repository at this point in the history
  • Loading branch information
Szymekk44 committed Jul 8, 2024
1 parent 2feb8c1 commit dda9df1
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 75 deletions.
23 changes: 23 additions & 0 deletions source/Cosmos.Core/MemoryBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,13 @@ public unsafe void Copy(ManagedMemoryBlock block)
MemoryOperations.Copy(xDest, aDataPtr, (int)block.Size);
}

/// <summary>
/// Copies a specified number of integers from the memory block into an integer array.
/// </summary>
/// <param name="aByteOffset">The byte offset in the memory block from where the copy starts.</param>
/// <param name="aData">The integer array where the data will be copied to.</param>
/// <param name="aIndex">The starting index in the destination integer array.</param>
/// <param name="aCount">The number of integers to copy.</param>
public unsafe void Get(int aByteOffset, int[] aData, int aIndex, int aCount)
{
int* xSource = (int*)(Base + aByteOffset);
Expand All @@ -279,6 +286,22 @@ public unsafe void Get(int aByteOffset, int[] aData, int aIndex, int aCount)
}
}

/// <summary>
/// Copies a specified number of bytes from the memory block into an array.
/// </summary>
/// <param name="aByteOffset">The byte offset in the memory block from where the copy starts.</param>
/// <param name="aData">The array where the data will be copied to.</param>
/// <param name="aIndex">The starting index in the destination array.</param>
/// <param name="aCount">The number of bytes to copy.</param>
public unsafe void Get(int aByteOffset, byte[] aData, int aIndex, int aCount)
{
byte* xSource = (byte*)(Base + aByteOffset);
fixed (byte* aDataPtr = aData)
{
MemoryOperations.Copy(aDataPtr + aIndex, xSource, aCount);
}
}

/// <summary>
/// Move bytes array down the memory block.
/// </summary>
Expand Down
54 changes: 23 additions & 31 deletions source/Cosmos.System2/Graphics/Canvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -500,40 +500,17 @@ public virtual void DrawSquare(Color color, int x, int y, int size)
/// <param name="height">The height of the rectangle.</param>
public virtual void DrawRectangle(Color color, int x, int y, int width, int height)
{
/*
* we must draw four lines connecting any vertex of our rectangle to do this we first obtain the position of these
* vertex (we call these vertexes A, B, C, D as for geometric convention)
*/
// Draw top edge from (x, y) to (x + width, y)
DrawLine(color, x, y, x + width, y);

/* The check of the validity of x and y are done in DrawLine() */
// Draw left edge from (x, y) to (x, y + height)
DrawLine(color, x, y, x, y + height);

/* The vertex A is where x,y are */
int xa = x;
int ya = y;
// Draw bottom edge from (x, y + height) to (x + width, y + height)
DrawLine(color, x, y + height, x + width, y + height);

/* The vertex B has the same y coordinate of A but x is moved of width pixels */
int xb = x + width;
int yb = y;

/* The vertex C has the same x coordiate of A but this time is y that is moved of height pixels */
int xc = x;
int yc = y + height;

/* The Vertex D has x moved of width pixels and y moved of height pixels */
int xd = x + width;
int yd = y + height;

/* Draw a line betwen A and B */
DrawLine(color, xa, ya, xb, yb);

/* Draw a line between A and C */
DrawLine(color, xa, ya, xc, yc);

/* Draw a line between B and D */
DrawLine(color, xb, yb, xd, yd);

/* Draw a line between C and D */
DrawLine(color, xc, yc, xd, yd);
// Draw right edge from (x + width, y) to (x + width, y + height)
DrawLine(color, x + width, y, x + width, y + height);
}

/// <summary>
Expand Down Expand Up @@ -626,6 +603,14 @@ public virtual void CroppedDrawImage(Image image, int x, int y, int maxWidth, in

}

/// <summary>
/// Retrieves a specified region of the canvas as a bitmap.
/// </summary>
/// <param name="x">The x-coordinate of the top-left corner of the region.</param>
/// <param name="y">The y-coordinate of the top-left corner of the region.</param>
/// <param name="width">The width of the region to retrieve.</param>
/// <param name="height">The height of the region to retrieve.</param>
/// <returns>A bitmap containing the specified region of the canvas.</returns>
public virtual Bitmap GetImage(int x, int y, int width, int height)
{
Bitmap bitmap = new Bitmap((uint)x, (uint)y, ColorDepth.ColorDepth32);
Expand All @@ -636,6 +621,13 @@ public virtual Bitmap GetImage(int x, int y, int width, int height)
return bitmap;
}

/// <summary>
/// Scales an image to the specified new width and height.
/// </summary>
/// <param name="image">The image to be scaled.</param>
/// <param name="newWidth">The width of the scaled image.</param>
/// <param name="newHeight">The height of the scaled image.</param>
/// <returns>An array of integers representing the scaled image's pixel data. (Raw bitmap data)</returns>
static int[] ScaleImage(Image image, int newWidth, int newHeight)
{
int[] pixels = image.RawData;
Expand Down
75 changes: 54 additions & 21 deletions source/Cosmos.System2/Graphics/SVGAIICanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,32 +104,49 @@ public override void DrawFilledRectangle(Color color, int xStart, int yStart, in

public override void DrawRectangle(Color color, int x, int y, int width, int height)
{
int rawColor = color.ToArgb();

/* Draw the top edge (A to B) */
for (int posX = x; posX < x + width; posX++)
if(color.A < 255)
{
DrawRawPoint((uint)rawColor, posX, y);
}
// Draw top edge from (x, y) to (x + width, y)
DrawLine(color, x, y, x + width, y);

/* Draw the bottom edge (C to D) */
int newY = y + height;
for (int posX = x; posX < x + width; posX++)
{
DrawRawPoint((uint)rawColor, posX, newY);
}
// Draw left edge from (x, y) to (x, y + height)
DrawLine(color, x, y, x, y + height);

/* Draw the left edge (A to C) */
for (int posY = y; posY < y + height; posY++)
{
DrawRawPoint((uint)rawColor, x, posY);
}
// Draw bottom edge from (x, y + height) to (x + width, y + height)
DrawLine(color, x, y + height, x + width, y + height);

/* Draw the right edge (B to D) */
int newX = x + width;
for (int posY = y; posY < y + height; posY++)
// Draw right edge from (x + width, y) to (x + width, y + height)
DrawLine(color, x + width, y, x + width, y + height);
}
else
{
DrawRawPoint((uint)rawColor, newX, posY);
int rawColor = color.ToArgb();

/* Draw the top edge (A to B) */
for (int posX = x; posX < x + width; posX++)
{
DrawRawPoint((uint)rawColor, posX, y);
}

/* Draw the bottom edge (C to D) */
int newY = y + height;
for (int posX = x; posX < x + width; posX++)
{
DrawRawPoint((uint)rawColor, posX, newY);
}

/* Draw the left edge (A to C) */
for (int posY = y; posY < y + height; posY++)
{
DrawRawPoint((uint)rawColor, x, posY);
}

/* Draw the right edge (B to D) */
int newX = x + width;
for (int posY = y; posY < y + height; posY++)
{
DrawRawPoint((uint)rawColor, newX, posY);
}
}
}

Expand Down Expand Up @@ -417,6 +434,14 @@ public override void DrawImage(Image image, int x, int y, bool preventOffBoundPi
}
}

/// <summary>
/// Draws a cropped image on the canvas at the specified position with maximum width and height.
/// </summary>
/// <param name="image">The image to be drawn.</param>
/// <param name="x">The x-coordinate of the top-left corner where the image will be drawn.</param>
/// <param name="y">The y-coordinate of the top-left corner where the image will be drawn.</param>
/// <param name="maxWidth">The maximum width of the cropped area.</param>
/// <param name="maxHeight">The maximum height of the cropped area.</param>
public override void CroppedDrawImage(Image image, int x, int y, int maxWidth, int maxHeight)
{
var width = maxWidth;
Expand All @@ -429,6 +454,14 @@ public override void CroppedDrawImage(Image image, int x, int y, int maxWidth, i
}
}

/// <summary>
/// Retrieves a specified region of the canvas as a bitmap.
/// </summary>
/// <param name="x">The x-coordinate of the top-left corner of the region.</param>
/// <param name="y">The y-coordinate of the top-left corner of the region.</param>
/// <param name="width">The width of the region to retrieve.</param>
/// <param name="height">The height of the region to retrieve.</param>
/// <returns>A bitmap containing the specified region of the canvas.</returns>
public override Bitmap GetImage(int x, int y, int width, int height)
{
var frameSize = (int)driver.FrameSize;
Expand Down
71 changes: 48 additions & 23 deletions source/Cosmos.System2/Graphics/VBECanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ 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, 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);
if (preventOffBoundPixels)
aWidth = (int)(Math.Min(aWidth, Mode.Width - aX) * (int)Mode.ColorDepth / 32);
var color = aColor.ToArgb();

for (int i = aY; i < aY + aHeight; i++)
Expand All @@ -289,34 +289,59 @@ public override void DrawFilledRectangle(Color aColor, int aX, int aY, int aWidt
}
}

/// <summary>
/// Draws a rectangle on the canvas with the specified color and dimensions.
/// </summary>
/// <param name="color">The color of the rectangle.</param>
/// <param name="x">The x-coordinate of the top-left corner of the rectangle.</param>
/// <param name="y">The y-coordinate of the top-left corner of the rectangle.</param>
/// <param name="width">The width of the rectangle.</param>
/// <param name="height">The height of the rectangle.</param>
public override void DrawRectangle(Color color, int x, int y, int width, int height)
{
int rawColor = color.ToArgb();

/* Draw the top edge (A to B) */
for (int posX = x; posX < x + width; posX++)
if (color.A < 255)
{
DrawRawPoint((uint)rawColor, posX, y);
}
// Draw top edge from (x, y) to (x + width, y)
DrawLine(color, x, y, x + width, y);

/* Draw the bottom edge (C to D) */
int newY = y + height;
for (int posX = x; posX < x + width; posX++)
{
DrawRawPoint((uint)rawColor, posX, newY);
}
// Draw left edge from (x, y) to (x, y + height)
DrawLine(color, x, y, x, y + height);

/* Draw the left edge (A to C) */
for (int posY = y; posY < y + height; posY++)
{
DrawRawPoint((uint)rawColor, x, posY);
}
// Draw bottom edge from (x, y + height) to (x + width, y + height)
DrawLine(color, x, y + height, x + width, y + height);

/* Draw the right edge (B to D) */
int newX = x + width;
for (int posY = y; posY < y + height; posY++)
// Draw right edge from (x + width, y) to (x + width, y + height)
DrawLine(color, x + width, y, x + width, y + height);
}
else
{
DrawRawPoint((uint)rawColor, newX, posY);
int rawColor = color.ToArgb();

/* Draw the top edge (A to B) */
for (int posX = x; posX < x + width; posX++)
{
DrawRawPoint((uint)rawColor, posX, y);
}

/* Draw the bottom edge (C to D) */
int newY = y + height;
for (int posX = x; posX < x + width; posX++)
{
DrawRawPoint((uint)rawColor, posX, newY);
}

/* Draw the left edge (A to C) */
for (int posY = y; posY < y + height; posY++)
{
DrawRawPoint((uint)rawColor, x, posY);
}

/* Draw the right edge (B to D) */
int newX = x + width;
for (int posY = y; posY < y + height; posY++)
{
DrawRawPoint((uint)rawColor, newX, posY);
}
}
}

Expand Down

0 comments on commit dda9df1

Please sign in to comment.