Well, I honestly haven’t had much time to work on this. I’ve “wasted” plenty of time mulling over the design, but never actually wrote any implementation.
I knew I would pretty busy so I figured it might happen… but I think I have a pretty solid design plan now, and I just want some constructive criticism on it.
At first I thought about writing it in C++ - I’ve done much more work in C++ and honestly find it a little difficult to write anything big in C due to lack of basic object-oriented features being built into the language (mostly implicit constructors and destructors). I actually wrote about 15 hours worth of code before I decided to scrap this due to the fact that I want something all SDL developers can use, even the ones that don’t want to touch C++ (and figured most of those developers have their reasons).
So then I thought about how I’d handle it in C without dropping any of my plans. The first idea was to make a basic structure containing the “small stuff” a widget would need - position, width, height, etc. Then the other widgets would just contain an instance of this structure as their respective struct’s first member. This works great for C, but when C++'s strict typesafety came to mind… I decided it was best to scrap this for ease of programming in both languages (without making some C++ wrapper or having to implement separate functions for each widget type).
Then I thought about using unions. This did seem like the best option of them all, until I realized that I had no clue how much memory was available on the iPhone and other potential target devices for SDL. After investigating and finding people saying that the original iPhone had 128mb of RAM with the system using as much as 40% of that, and even less on my beloved G1, I decided that I needed to do whatever I could to trim down on the memory usage so that the application using the library could use as much as possible for its graphics and internal operations – this is where I spent a lot of time thinking. Why should a simple widget like a button consume as much memory as a more complex widget like a window? Was it even wise for it do so with this little memory? I figured it would probably have been fine, but ended up scrapping the design just-in-case.
Making an inheritance model consume little memory seemed difficult, until I thought about a programming concept I had nearly forgotten because it’s not an enforced feature of C++ – interfaces. A table of functions defining the operation of a specific type of widget, and occupying the same memory for each instance of that type of widget, would increase the memory usage just by implementing them by a bit, but overall less (especially in the case of a complex GUI).
So, here’s a small sample of the concept:
Code:
struct __UI_WInterface;
typedef struct __UI_Widget {
struct __UI_WInterface* interface; /* generic interface /
struct __UI_Widget container; /* widget containing this one /
size_t wDataSize; / size of wData in bytes /
void wData; /* pointer to data structure for the widget, used internally by the implementation */
} SDL_UI_Widget;
struct __UI_WContainInterface;
struct __UI_WSkinner;
typedef struct __UI_WInterface {
struct __UI_WContainInterface* containInterface; /* interface used only by widgets containing other widgets, but common enough for explicit definition /
struct __UI_WSkinner skinner; /* user-defined function for skinning a widget, maybe belongs in SDL_UI_Widget? /
void (GetPos)(SDL_UI_Widget, int x, int* y); /* get the position of the widget relative to its container /
void (SetPos)(SDL_UI_Widget, int x, int y); / set position of the widget relative to its owner; there is no need for a function
void (GetAbsPos)(SDL_UI_Widget, int* x, int* y); /* get the absolute position of the widget (from the top-left corner of window) /
void (GetSize)(SDL_UI_Widget, int w, int* h); /* get the dimensions of the widget /
void (SetSize)(SDL_UI_Widget, int w, int h); / set the dimensions of the widget /
void (RenderFunction)(SDL_UI_Widget); / perform rendering /
void (EventHandler)(SDL_UI_Widget, SDL_Event); /* handle an event from SDL /
size_t wIExtendedSize; / size of data pointed to by wIExtended /
void wIExtended; /* extended interface function */
} SDL_UI_WInterface;
[…]
/* implementation must define a version of this function for each type of widget – widget must be pre-allocated memory */
typedef int (SDL_UI_WidgetFactory)(SDL_UI_Widget widget);
/* register a pre-filled WInterface with identifier and widget factory, where it will be stored in an associative array /
int SDL_UI_RegisterInterface(SDL_UI_WInterface wInterface, const char* identifier, SDL_UI_WidgetFactory factory);
/* fetch an interface by its identifier - leave this as an external function only for the sake of custom widget implementations /
SDL_UI_WInterface SDL_UI_GetInterface(const char* identifier);
/* abstract widget creation by identifier – widget must be preallocated memory /
int SDL_UI_CreateWidget(SDL_UI_Widget widget, const char* identifier);
[…]
The design is simple enough, I shouldn’t need to explain further.
The size of an individual widget instance is cut to that of 4 pointers (since the size of size_t should be the same as a pointer). If there’s only 5 widgets being used, this is a huge waste. If there’s 50, it’s probably getting a bit useful. 500 (is this a common number for any type of application?), it’s probably important. Honestly though I probably spent a bit too much time focusing on the memory consumption issue… and I don’t even know if this will benefit many realistic SDL 1.3 apps.
Also, as one might see, the functions I’ve shown as required for implementation are influenced by the Win32 API. Win32 API was my first experience with GUI programming and it seemed like a good way to do it without largely changing the method of widget creation between implementations…