Fio:Iterators
From Fio
"To iterate is humane, to recurse - divine!"
Iterator
Contents |
One Design Principle of Fio is:
- Rendering is a preorder iterating process; Picking is a postorder one.
while rendering is to draw the game world and picking is to dispatch mouse event so that the player can interact with the game.
In Fio we use a typical GoF Iterator:
template<typename T> class Iterator{ public: virtual ~Iterator(){} virtual void first() = 0; virtual void next() = 0; virtual bool isDone() = 0; virtual T current() = 0; };
we can call first() to position an iterator to its beginning; next() to step to the next element in current iterating order. And when isDone() returns true, there's no element left to iterate. current() returns the element the iterator points to.
Armed with a preorder itor we can render the game with little effort
// draw root drawComponent(surface, root); // draw content PreorderItor<Component*> pre(panel); for(pre.first(); !pre.isDone(); pre.next()){ drawComponent(surface, pre.current()); }
The render itor should know how to iterate on the structure of the world and it begins from the root of the world.
Root of The World
In Fio, the world is orgnized in Composite pattern:
Component *<--------- ^ | ----|-------- | | | | Leaf Composite --
Leaf classes are those components who have no child component, such as Button, Lable, Icon etc. Comosite classes can contain other component(s) - e.g. Panel, List, ListItem, GamePanel, Scene, Layer, Cell.
Using composite, the world is orgnized into a tree structure and the root is a panel. We start from main menu of fallout2 as an example:
root panel(main) ->(6) button |->(1) cursor
when click a button to load a new game:
root panel(load) ->(1) cursor
and when the game loaded, it's more complex:
root panel(play) -> face panel -> inv button | |-> pip button | |-> option button | |-> change weapon | |-> skill dex | |-> item panel -> item icon | | -> ap lable | | -> targeted lable | | -> weapon mode lable | |-> message list ->* list item -> lable | |-> hp counter | |-> ac counter | |-> combat icon | |-> game panel -> scene -> glass layer ->* shadow | | |->* floater messages | | | |-> roof layer ->* cell ->* tile | |-> wall layer ->* cell ->* tile | | |->* active object | |-> cursor layer -> hex cursor | |-> tile layer ->* cell -> tile | |-> cursor