Now, let's get some windows open. This is going to depend on what operating system you're using. We'll support Windows and X11/Linux. So, yeah, this article will be a boring one.

The platform layer will be required to implement 3 functions.

void Initialise(); // Initialise the platform layer.
Window *WindowCreate(const char *cTitle, int width, int height); // Open a new window and return a heap allocated 'window object'.
int MessageLoop(); // Keep the windows ticking along.

The user of the library will use these functions in the following manner:

int main() {
	WindowCreate("Hello, world", 300, 200); // Open the first window.
	WindowCreate("Another window", 300, 200); // Open the second window.
	return MessageLoop(); // Pass control over to the platform layer until the application exits.

For now, we'll consider the application to exit when any window is closed.

What's in the Window structure, you ask? Good question. Not much -- it's mostly platform specific gubbins.

typedef struct Window {
	uint32_t *bits; // The bitmap image of the window's content.
	int width, height; // The size of the area of the window we can draw onto.

	HWND hwnd;
	bool trackingLeave; // We'll need this when we implement mouse input later on in the tutorial.

	X11Window window;
	XImage *image;
} Window;

We'll also need to include some platform specific headers. In typical fashion, these export symbols that clash with our naming schemes, so we'll have to #define/#undef our way out of trouble.

#define Rectangle W32Rectangle
#include <windows.h>
#undef Rectangle

#define Window X11Window
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>
#undef Window

Let's put an array of pointers to the window objects into the global state structure. And, sigh, some X11 nonsense too...

typedef struct GlobalState {
	Window **windows;
	size_t windowCount; // The number of open windows; the number of pointers in the above array.

	Display *display;
	Visual *visual;
	Atom windowClosedID;
} GlobalState;

We can now implement the three functions Initialise, WindowCreate and MessageLoop. These functions are all completely platform specific, and really aren't that terribly interesting or enlightening. They're in the source code below, should you want to read them. The most notable thing that happens is how we add the window objects to the global array.

Window *window = (Window *) calloc(1, sizeof(Window));
global.windowCount++; = realloc(, sizeof(Window *) * global.windowCount);[global.windowCount - 1] = window;

The window will be completely empty at this point. We'll add rendering in few articles from now :)


Part 1: It begins.

Part 3: Adding the infrastructure of the element hierarchy.