Saturday, January 23, 2010

List?!



I've got some people coming to me and asking about lists, they have a certain difficulty to
 understand lists when all they got was the theory and some weird-looking illustrations.

before you continue reading, if you are totally clueless about what a List is you can check out these sources-

A.) in case you know Hebrew as your mother-language you should check out this blog post->
Snir David's Blog, Data Structure Explained (Hebrew)

B.)or go to good ol' wikipedia and check out what a list is directly->
Wikipedia, List(computing) (English)



not really helping is it? ?! O_O
well I'm here to make things a bit clearer.
for all of you not-computer-scientists/software-engineers out there, the list illustrated above is a doubly-linked list, the same as the one I will be showing you if you keep on reading...

So what do we have thar?
harr, harr, harr
No, really...
The first node to the left(box with an x on it-  [X])  is the left endpoint of the list, you can see it is an endpoint by it having only one connector, connecting from the node to it's right.
the endpoint does not contain any value, and is just there because every node in the list can be described as

struct Node
{
   void* data; //data
   Node *PrevNode;//pointer to previous node
   Node *NextNode;//pointer to next node
};

Since the first node has got to have a predecessor("previous") node, and the last node has got to have a successor("next"), there must be two "ghost" nodes in the list(which are actually null pointers- pointers holding a null value)

the first node will look like this ( the last node is the same, only with null as the value for "NextNode" and it's previous one for "PreviousNode")-&gt

Pseudo-Code

firstNode // the data structure for the first node with data assigned

{
   data=SomeData;
   PrevNode=NULL;

   NextNode=SecondNode;
}


The following C++ code represents a simple doubly-linked list.
it is presented to you(the reader) under no restrictions at all,
you can do with it as you wish.


struct Control //define a class control (a skeleton for it's children, this is the base node)
{

    Control *PrevControl;
    Control *NextControl;
   
    //texture to attach for the button
    LPDIRECT3DTEXTURE9 texture;

    //Button's location
    D3DXVECTOR3 pos;
};



this will be our base node, it holds texture and position data for our control, along with the next and previous nodes.


class DXGUI //The GUI handler, holds all controls and renders them through iteration
{
private:
Control *pControlIterator;
Control *pPrevControl;
public:
    DXGUI();
    virtual ~DXGUI();
   
    //Get the mouse's location
    POINT mouse;

    //Add a control
    void AddControl(Control *ctrl);
    //Function to draw all affilated controls
    void DrawAffilate(LPD3DXSPRITE sh);

    //friend BUTTON to sync
    friend class BUTTON;

};   
This is the handler class for the controls, it is possible to assign controls to each instance of the object and tell it to render all controls assigned to the handler.

It also holds mouse position data for use with all controls(instead of assigning each control a POINT, it will get its data from it's handler).

class BUTTON : public Control //This is the first control with a body, it is a button, can  detect mouseovers                                               //
and mouseclicks, also because it inherits from control, it can be assigned to-                                               //DXGUI for handling.
{
protected:
    //The GUI object to handle the button
    DXGUI *GUI_Handler;
public:
    BUTTON();
    ~BUTTON();

    POINT *mouse;
    //Functions for easiness of handling
   
   
    //Is button pressed?
    bool isPressed();

    //Is mouse over button?
    bool isOver();
   
   
    //Button's draw-related functions

    //Button's creation function
    void CreateButton(DXGUI *pGUI_Handle,float x,float y,LPDIRECT3DTEXTURE9 ButtonTexture);
   
    //Button's draw function
  
};

A simple button, it has all the functions you could expect from a button, you can create a new button(while doing so, it is required that you choose a handler for it, you can also fill it with "NULL" for "no handler"), give it X and Y positions which will later be translated to a D3DXVECTOR3(position vector for use with DirectX, can be found in the D3DX library), and a texture.
You can also check to see if the button is pressed or if the mouse is over the button(remember the POINT struct I put in DXGUI?).


 So we stored N buttons inside a GUI handler object, how does lists help us ?

The list shown above helps in many ways, one of which is the rendering. 

I don't want to have too many stuff flying around so I modded the function a bit, for simplicity's sake it ,doesn't really matters.

void DXGUI::DrawAffilate(LPD3DXSPRITE sh)
{
   
   
 //Create a draw iterator to run in the Controls list
    Control *pDrawIterator;
   
    //set the Draw iterator to the last Control created
    pDrawIterator = pControlIterator;

    //Until you reach the bottom, iterate down the list and draw the buttons
    while(&pDrawIterator->PrevControl != NULL)
    {
     
      //Use the sprite handler to draw the button
      sh->Draw(pDrawIterator->texture,NULL,NULL,&pDrawIterator->pos,D3DCOLOR_XRGB(255,255,255));
     
      //Move down the Controls list
      pDrawIterator = pDrawIterator->PrevControl;
    }

   

   
}

This is the function "DrawAffilate" from DXGUI, it iterates through the control list from the top to the beginning, using each control's previous pointer to get to it, then store it in "pDrawIterator", until we get to the beginning, the first node has it's "PrevNode" set as  "NULL", and this is how we know the beginning has been reached.


Hope it cleared stuff up for you guys, the code is taken from DXGUI.h and DXGUI.cpp which can be found in my open-source algorithm project named Project Vulcan.




No comments:

Post a Comment