The following functions have been implemented both in ring 0 and in ring 3.
ClearScreen: To clear the screen we go through all the bytes of video memory and set them to a certain colour, in this case the colour is black.
SetPixel: To set a pixel at a specific x and y position, we write a byte that contains a colour to a memory location starting at 0x0A0000.
Sign and Abs: These two functions provide the possibility to draw lines in any direction regardless of the sign of the equation or coordinates that are being passed in.
SetLineStyles: This is a function that can support different line styles by either drawing a continuous line, a dotted line or a dashed line using modulus. Every time the result of the modulus isn’t zero the function prints the pixel on the screen, if the modulus is zero the pixel isn’t displayed on the screen. This function uses a switch and there are three different cases for the three types of line.
DrawLine: This function uses Bresenham’s algorithm. Bresenham determines the closest approximation to a straight line between two points. It starts with an initial point and then it selects a pixel after that point that is suitable to draw to. It selects a suitable pixel by evaluating whether the midpoint between two pixels lies below or above the line. To test that the code worked properly including the Sign and Abs functions I created several lines that go in different directions to see if they would all appear on the screen and if they would display correctly.
DrawRectangle: The DrawLine method is used to draw a rectangle, more specifically it draws the four different sides needed to create the rectangle by using a starting x and y coordinates and the correspondent width and height.
DrawFilledRectangle: Drawing a filled rectangle works by setting a starting point of x and y, specifying the desired width and height of the rectangle afterwards and the rectangle is drawn using the parameters given to the function.
DrawCircle: This function uses the midpoint circle algorithm which is a variant of Bresenham’s line algorithm. It starts by taking in the radius and the x and y of the centre of the circle to obtain the first point on the circumference of the circle after which it calculates the rest of the points in counter clockwise order for the first octant, while it simultaneously mirrors the points to the other octants.
DrawFilledCircle: Filling a circle uses the same algorithm and logic as the draw circle function with the difference being that to fill a circle we use the DrawLine function instead of using SetPixel like in DrawCircle.
DrawPolygon: To draw a polygon we start with an x and y coordinates, which is the first point of the polygon. It then loops through the array of coordinates and draws a line between each of them creating the requested polygon. To complete the polygon, it draws another line from the last used x, y position to the first x, y position used in that same function. You can test drawing a polygon by drawing different shapes, with a different number of vertices and sides to see whether there are limitations to the function or not.
FillPolygon: In this assignment, the flood-fill algorithm has been used to fill in a polygon. This algorithm takes four parameters: a starting x and y, a background colour, and a replacement colour.
The algorithm looks at all the points in the array that are connected to the starting point through the background colour and then changes all of them to the new selected colour. This algorithm is the four-way stacked based implementation for a 2D array. The disadvantages of flood fill are that if an inside pixel is in some other colour than the one specified then the fill terminates and the polygon remains unfilled and it doesn’t work for large polygons.
SetColourPalette: To define a colour we use four bytes, one byte for blue, another for green, another one for red and a fourth byte is reserved for the index. To set one colour in the palette, we write the colour index to port 0x3C8 and then write the red, green, and blue values, in order, to port 0x3C9.
SetResolution: Currently the code only supports resolutions lower than or equal to 320×200 those being, 320×200 and 256×200. It does this by giving us the opportunity to change the width and height of the screen in this method.