⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gd11-01.cpp

📁 游戏开发数据结构-Data.Structures.for.Game.Programmers
💻 CPP
字号:
// ============================================================================
//  GD11-01.cpp
//  Tree Graphical Demonstration.
// ============================================================================
#include "SDLGUI.h"
#include "Tree.h"
#include <stdlib.h>
#include <time.h>



// ============================================================================
//  Global Constants
// ============================================================================
const char PROGRAM_NAME[]   = "Tree Graphical Demonstration";
const int WIDTH             = 800;
const int HEIGHT            = 600;
const int ITEMS             = 32;
const int ARIAL             = 0;


// ============================================================================
//  Global Variables
// ============================================================================
SDLGUI* g_gui;

int g_delay = 1000;
int g_timer;

Tree<int>* g_tree;

// the iterators.
Tree<int>* g_current = 0;
Tree<int>* g_child = 0;

// the tree iterator.
TreeIterator<int> g_itr;


// Circles
SDL_Surface* g_blackcircle;
SDL_Surface* g_redcircle;
SDL_Surface* g_bluecircle;

// ============================================================================
//  Draw Algorithms
// ============================================================================

void DrawNode( Tree<int>* p_node, int p_x, int p_y )
{
    static SDL_Surface* text;
    static char number[] = "00";

    // draw the contents of the circle.
    sprintf( number, "%d", p_node->m_data );
    text = TTF_RenderText_Shaded( g_gui->GetFont(ARIAL), number, BLACK, WHITE );

    g_gui->Blit( text, (g_blackcircle->w - text->w) / 2 + p_x, 
                       (g_blackcircle->h - text->h) / 2 + p_y );
    SDL_FreeSurface( text );


    // draw the circle
    if( g_current == p_node )
        g_gui->Blit( g_redcircle, p_x, p_y );
    else if( g_child == p_node )
        g_gui->Blit( g_bluecircle, p_x, p_y );
    else
        g_gui->Blit( g_blackcircle, p_x, p_y );
}



void DrawTree( Tree<int>* p_tree, int p_x, int p_y, int p_width )
{
    if( p_tree != 0 )
    {
        DrawNode( p_tree, p_x + (p_width / 2) - 24, p_y);

        int w = p_tree->m_children.Size();
        int w2;
        if( w != 0 )
        {
            w = p_width / w;
            w2 = w / 2;
        }
        int x = p_x;

        // get an iterator for the children.
        DListIterator<Tree<int>*> itr = p_tree->m_children.GetIterator();

        // loop through each child and recursively draw it.
        for( itr.Start(); itr.Valid(); itr.Forth() )
        {
            DrawTree( itr.Item(), x, p_y + 128, w );
            g_gui->ArrowLine( p_x + (p_width / 2), p_y + 24, 
                              x + w2, p_y + 152, 24, 24, false, true, BLACK );
            x += w;
        }
    }
}

// recursive function that creates a random tree.
void RandomTree( Tree<int>* p_tree, int p_depth )
{
    Tree<int>* node = 0;
    if( p_depth < 3 )
    {
        // create 1-3 children
        int i = rand() % 3 + 1;

        while( i > 0 )
        {
            node = new Tree<int>;
            node->m_data = rand() % 100;
            node->m_parent = p_tree;
            p_tree->m_children.Append( node );
            RandomTree( node, p_depth + 1 );
            i--;
        }
    }
}

// ============================================================================
//  Button Callbacks
// ============================================================================
void IteratorReset()
{
    g_current = 0;
    g_child = 0;

    if( g_itr.Valid() )
    {
        g_current = g_itr.m_node;

        if( g_itr.ChildValid() )
        {
            g_child = g_itr.m_childitr.Item();
        }
    }
}


void SetRoot()
{
    g_tree->m_data = rand() % 100;
}

void ItrRoot()
{
    if( g_itr.Valid() )
        g_itr.Root();
    else
        g_itr = g_tree;
    IteratorReset();
}

void InsertBefore()
{
    if( g_itr.Valid() )
    {
        Tree<int>* node = new Tree<int>;
        node->m_data = rand() % 100;
        g_itr.InsertChildBefore( node );
    }
}

void InsertAfter()
{
    if( g_itr.Valid() )
    {
        Tree<int>* node = new Tree<int>;
        node->m_data = rand() % 100;
        g_itr.InsertChildAfter( node );
    }
}

void RemoveChild()
{
    if( g_itr.Valid() && g_itr.ChildValid() )
    {
        Tree<int>* node = g_itr.m_childitr.Item();
        g_itr.RemoveChild();
        delete node;
    }
    IteratorReset();
}


void Randomize()
{
    delete g_tree;
    g_tree = new Tree<int>;
    SetRoot();
    
    RandomTree( g_tree, 0 );
    g_itr = g_tree;
    IteratorReset();
}

void Up()
{
    g_itr.Up();
    IteratorReset();
}

void Down()
{
    g_itr.Down();
    IteratorReset();
}

void Back()
{
    g_itr.ChildBack();
    IteratorReset();
}

void Forth()
{
    g_itr.ChildForth();
    IteratorReset();
}

void ChildStart()
{
    g_itr.ChildStart();
    IteratorReset();
}

void ChildEnd()
{
    g_itr.ChildEnd();
    IteratorReset();
}



// ============================================================================
//  Main
// ============================================================================



int main( int argc, char* argv[] )
{
    int x;
    int y;

    g_tree = new Tree<int>;
    g_itr = g_tree;
    SetRoot();
    RandomTree( g_tree, 0 );
    ItrRoot();

    srand( time(0) );

    //initialize systems
    SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER );
    SDL_WM_SetCaption( PROGRAM_NAME, 0); 
    TTF_Init();
    g_gui = new SDLGUI( WIDTH, HEIGHT, ITEMS, WHITE );
    g_gui->SetFont( "arial.ttf", ARIAL, 20, TTF_STYLE_NORMAL );


    // load the bmps
    g_blackcircle = SDL_LoadBMP( "circleblack.bmp" );
    g_redcircle =   SDL_LoadBMP( "circlered.bmp" );
    g_bluecircle =  SDL_LoadBMP( "circleblue.bmp" );

    // set the color keys of the BMPs
    SDL_SetColorKey( g_blackcircle, SDL_SRCCOLORKEY, 
                     SDL_MapRGB( g_blackcircle->format, 255, 255, 255 ));
    SDL_SetColorKey( g_redcircle, SDL_SRCCOLORKEY, 
                     SDL_MapRGB( g_redcircle->format, 255, 255, 255 ));
    SDL_SetColorKey( g_bluecircle, SDL_SRCCOLORKEY, 
                     SDL_MapRGB( g_bluecircle->format, 255, 255, 255 ));

    // add the items to the g_gui
    g_gui->AddButton( 0, 0, "gbup.bmp", "gbdown.bmp", "Insert Before", ARIAL,
                      BLACK, WHITE, InsertBefore );
    g_gui->AddButton( 0, 64, "gbup.bmp", "gbdown.bmp", "Insert After", ARIAL,
                      BLACK, WHITE, InsertAfter );
    g_gui->AddButton( 128, 0, "gbup.bmp", "gbdown.bmp", "Remove Child", ARIAL,
                      BLACK, WHITE, RemoveChild );
    g_gui->AddButton( 128, 64, "gbup.bmp", "gbdown.bmp", "Randomize", ARIAL,
                      BLACK, WHITE, Randomize );
    g_gui->AddButton( 256, 0, "gbup.bmp", "gbdown.bmp", "Goto Root", ARIAL,
                      BLACK, WHITE, ItrRoot );
    
    g_gui->AddButton( 544, 0, "gbup.bmp", "gbdown.bmp", "Up", ARIAL,
                      BLACK, WHITE, Up );
    g_gui->AddButton( 544, 64, "gbup.bmp", "gbdown.bmp", "Down", ARIAL,
                      BLACK, WHITE, Down );
    g_gui->AddButton( 416, 64, "gbup.bmp", "gbdown.bmp", "Back", ARIAL,
                      BLACK, WHITE, Back );
    g_gui->AddButton( 672, 64, "gbup.bmp", "gbdown.bmp", "Forth", ARIAL,
                      BLACK, WHITE, Forth );
    
    g_gui->AddButton( 416, 0, "gbup.bmp", "gbdown.bmp", "Child Start", ARIAL,
                      BLACK, WHITE, ChildStart );
    g_gui->AddButton( 672, 0, "gbup.bmp", "gbdown.bmp", "Child End", ARIAL,
                      BLACK, WHITE, ChildEnd );


    // set our at exit function
    atexit( SDL_Quit ) ;

    // declare event variable
    SDL_Event event;

    g_gui->Draw();
    g_gui->Update();

    // loop until we get a quit message.
    while( 1 )
    {
        //look for an event
        if( SDL_PollEvent( &event ) )
        {
            //an event was found
            if( event.type == SDL_QUIT ) 
                break;

            // mouse button was pressed
            if( event.type == SDL_MOUSEBUTTONDOWN ) 
            {
                // get the mouse g_state.
                SDL_GetMouseState( &x, &y );

                // tell the GUI that a button has been pressed
                g_gui->MouseDown( x, y );

            }

            // mouse button was released
            if( event.type == SDL_MOUSEBUTTONUP ) 
            {
                // get the mouse state.
                SDL_GetMouseState( &x, &y );

                // tell the GUI that a button has been released
                g_gui->MouseUp( x, y );
            }

            if( event.type == SDL_KEYDOWN )
            {
                // a key was pressed.
                if( event.key.keysym.sym == SDLK_ESCAPE )
                {
                    // if ESC was pressed, quit the program.
                    SDL_Event quit;
                    quit.type = SDL_QUIT;
                    SDL_PushEvent( &quit );
                }

                // tell the GUI that a key was pressed.
                g_gui->KeyDown( event.key.keysym.sym, 
                                event.key.keysym.mod,
                                event.key.keysym.unicode );
            }
        }



        // draw the g_gui at the end of each loop.
        g_gui->Draw();

        DrawTree( g_tree, 0, 128, WIDTH );

        g_gui->Update();
    }

    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -