'Creating own moveto() function in C for use in 0x13 VGA mode xv6

I need to draw a square using 4 system call methods that I create personally. I cannot use things like graphics.h

The functions I'm required to make are as follows (not including params):

int sys_moveto() // Moves the current graphics position.
int sys_lineto() // Draws a line from the current graphics position to given co-ords.
int sys_setpixel() //set the pixel at the current graphics position co-ords to white.

I seem to have figured out setpixel() and lineto(), the issue is with the moveto() function. I have no idea how to move the current graphics position to a given pixel co-ordinates on the 0x13 VGA mode display. I assume this is done by manipulating some form of memory location?

The assignment specifically states for it to be built this way. So logically, the system moves the 'cursor' to a point on the screen, then colours it in before moving onto the next point, then colouring that in etc...

As this is system calls, this is all done at a protected level!

I just need to know how to move the current graphics position without the use of graphics.h! Any resources or advice?



Solution 1:[1]

The sys_moveto(newX, newY, color) would just do a line from the previous position (in global variables like posX and posY) to the new position, then set the new position (in the global variables, like posX = newX;) for next time.

The problem you'll have is that sys_lineto() will need to support diagonal lines (which is harder); and that sys_setpixel() should not be used as its inefficient to recalculate the address for every pixel along the line (instead of doing an address += step; that avoids repeating most of the address calculation). Note that sys_lineto() could/should be optimized for likely special cases (e.g. horizontal lines can be a "memset()"); and probably should handle clipping (e.g. so you can sys_moveto(INT_MAX, INT_MAX, color) and sys_lineto() will only draw the part of the line that is actually on the screen, if any, without corrupting memory when part of the line is not on the screen.

I'd also recommend writing a calcPixelAddress() that can be used by sys_lineto() (to find the address of the first pixel to draw) and sys_setpixel() (which could become like address = calcPixelAddress(x, y); *address = color;).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Brendan