data containers

Koroskin Denis 2korden at gmail.com
Wed Jul 2 16:04:54 PDT 2008


On Thu, 03 Jul 2008 01:04:09 +0400, Moritz <mpipahl at gspgmbh.com> wrote:

> Hello,
>
> Im currently working on a gui framework for my game, and Im trying to  
> find a versatile way to build my screens.
>
> I thought about a generic "Screen" class, which has functions like  
> "add_element()" to insert a sprite or text into a layer on the screen.
>
> Rendering is then done from the lowest layer to the highest one.
> My question is now how I can implement these Layers, since I didnt find  
> LikedLists or Hastables in Phobos.
>
> My Idea in C++ would be to have a hashtable int to linked list, which  
> gives me the linked list for a layer upon inserting the layer number,  
> and the linked list then contains my ScreenObjects (a base class for  
> images, text, buttons and so on).
> What would be the best way to do this in D?

I like the way it is done in a PopCap Framework (available for free from  
developer.popcap.com; Zuma, Peggle, and all the others PopCap games are  
made with this framework).

Everything is a Widget. Widgets can be built into a hierarchically, one  
nested in another. Child widgets are stored in a linked list (for easy  
appending and removal during iteration over collection) in an order they  
are added. This is the same order they are drawn to screen (topmost widget  
is at the end and drawn last).

There are two main phases: Update and Drawing. During update, all widgets'  
Update() method is called recursively starting from the root of the  
hierarchy. The same goes for a Drawing.

Every widget defines a void Draw(Graphics g); method. All the drawing is  
done in this method. For example, you may have one big Screen class and do  
the rendering in a single method. Or, prefferred, have one lots of small  
widgets on a board, each of them draws itself only. There may be some  
ImageWidget (constructed from an image and renders static image),  
AnimatedWidget (constructed from a frame sequence), etc.

Fonts are not widgets, but are drawn as a part of some widget. For  
example, a button with a caption is a single object. Caption is drawn  
during a button.draw(g) call.

Updates and Drawings are transparent to user. User just defines a new  
class, overrides its draw method and put newly constructed object to a  
board. That's it!

Changing a widget order on the screen is as simple as moving an object  
inside a list:

class Widget {
     ...
     void bringToFront(Widget child) {
        widgets.erase(child);
        widgets.pushBack(child);
     }
     ...
}

That's it. Hope this info will help you make your design desicions.
P.S. Take a look into ArcLib, too: http://www.dsource.org/projects/arclib


More information about the Digitalmars-d-learn mailing list