Don't be afraid, that's really simple ! We'll have to get a (big) buffer in memory (like any other window manager) which represents your monitor screen status. This buffer is not a binary image of thr screen nor it does not contaain all of the real screen information. This buffer only describes positions between windows, and screen border in a simple way. This buffer is an array of chained lists. Each of the chained list representing a screen line. The bufffer's screen size will vary with screen resolution. For a 1024x768 (any number of colors) we will need something like 64k...That will allow a max of approximately 200 windows on screen (that quite a lot !). For those who have undertood the main idea (which is really important, otherwise you'll not understand what will go next) here is anther nice picture showing the buffer:
Each cell will have this structure (which can be changed if we really want to make ANY windows shape):
The buffer I described will not be the only one. We need to keep information about windows real size Hopefully, that buffer (a stack) will be very small, as for each new window on screen we have to put :
As I said before, the big buffer will only describes windows positions, not its contents, don't forget this, we'll need that later. Each of the chained lists describes a line of the screen, and eeach cell in any lists describes a "segment" of window. that's really hard to explain such a thing with only words, so I'll make again another nice picture, showing a complete example of screen representation...
One thing before going on, the buffer has, for each lists (or screen line) at least one cell even when there's nothing on screen. That is because I consider the background like a window. This simplify a lot of things. Here is the representation of an empty screen (background has a window iD of "0"):