Thread overview | |||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 02, 2008 data containers | ||||
---|---|---|---|---|
| ||||
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? |
July 02, 2008 Re: data containers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Moritz | On Thu, 03 Jul 2008 01:04:09 +0400, Moritz <mpipahl@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 |
July 03, 2008 Re: data containers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Moritz | "Moritz" <mpipahl@gspgmbh.com> wrote in message news:g4gqge$27fi$1@digitalmars.com... > 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? Well, hashtables are built into the language, though they are not necessarily the most performant implementation. List[int] table; Phobos doesn't have any built-in data structures, like hashtables or linked lists. Tango does. How necessary they are, I don't know, I've never actually used them. Most of the stuff I need to get done, I can get done with dynamic arrays and associative arrays. And really, if you can't type up a linked list in about 30 seconds, .. well, good luck to you. |
July 03, 2008 Re: data containers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Moritz | "Moritz" 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'm not a game programmer, but I did make a collections project for D that should support Phobos: www.dsource.org/projects/dcollections If you use it, let me know how it works for you. -Steve |
July 03, 2008 Re: data containers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Moritz | Moritz 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. Maybe a reason to consider Tango? -- Lars Ivar Igesund blog at http://larsivi.net DSource, #d.tango & #D: larsivi Dancing the Tango |
July 03, 2008 Re: data containers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Moritz | Thx for all your replies, but I hoped for a hint on how to implement it, since Im new to D.
@Lars Igesund: I considered Tango, but I had some issues with derelict, so I stopped the installation and decided to give phobos a closer look. But I bought the tango book, and will surely try tango out after I got some of my game ported and running in D.
@Steven: Ill try to use arrays first (for the sake of simplicity and to understand a bit more about D), and Ill try your collection afterwards (supplying the missing data containers in a project is surely a great idea, thank you!)
And pls forgive me if Im asking silly questions, but thats what beginners usually do :-)
Moritz schrieb:
> 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?
|
July 03, 2008 Re: data containers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Moritz | Another (more practical) question: Im trying to use a two dimensional array for my ScreenElements: ScreenElement[][] layer_map; Then, Id like to itereate over all the ScreenElements in this array, but I get syntax errors in the inner foreach-loop, on the single_layer identifier. Errormessage: Syntax error on token "single_layer", ; expected after this token foreach(ScreenElement[] single_layer; layer_map[][]) { foreach(ScreenElement element, single_layer) { element.display(); } } Can anyone tell me how to do this correctly? |
July 03, 2008 Re: data containers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Moritz | On Thu, 03 Jul 2008 21:56:17 +0400, Moritz <mpipahl@gspgmbh.com> wrote:
> Another (more practical) question:
>
> Im trying to use a two dimensional array for my ScreenElements:
>
> ScreenElement[][] layer_map;
>
> Then, Id like to itereate over all the ScreenElements in this array, but I get syntax errors in the inner foreach-loop, on the single_layer identifier.
>
> Errormessage: Syntax error on token "single_layer", ; expected after this token
>
> foreach(ScreenElement[] single_layer; layer_map[][])
> {
> foreach(ScreenElement element, single_layer)
> {
> element.display();
> }
> }
>
> Can anyone tell me how to do this correctly?
Works fine for me:
ScreenElement[][] layer_map;
foreach(ScreenElement[] single_layer; layer_map) // you may drop [][] as they are not needed
{
foreach(ScreenElement element; single_layer) // did you have had a typo here? , -> ;
{
element.display();
}
}
|
July 03, 2008 Re: data containers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Koroskin Denis | Thank you, the error was: "," instead of ";" in foreach(ScreenElement element; single_layer) |
July 03, 2008 Re: data containers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Moritz | So now I have a two dimensional array ready for my classes, but sadly, I cant find any information on how classes can be stored in a multidimensional array and how I can use the instances stored there. Ive just guessed and typed: int add_element(ScreenElement new_element, uint layer) { if(layer <= layer_count) { ScreenElement[] single_layer = layer_map[layer]; single_layer ~= new_element; } else { writefln("cant insert ScreenElement into non existing layer"); } return 0; } Sadly, this doesnt work, I get an Error: ArrayBoundsError upon inserting. Can anyone tell me how this is done, and maybe even where I can find usful examples for more cmplex uses of arrays in D? My biggest problem ATM is the lack of good tutorials or examples, it seems that D is no easy language to get started with, once you want to do more than basics :-( |
Copyright © 1999-2021 by the D Language Foundation