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

📄 gd12-02.cpp

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



// ============================================================================
//  Global Constants
// ============================================================================
const char PROGRAM_NAME[]   = "Binary Tree Traversal 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 = 700;
int g_timer;

BinaryTree<int>* g_tree = 0;

BinaryTree<int>* g_current = 0;

LQueue<BinaryTree<int>*> g_queue;

// Circles
SDL_Surface* g_blackcircle;
SDL_Surface* g_redcircle;


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

void DrawNode( BinaryTree<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
        g_gui->Blit( g_blackcircle, p_x, p_y );
}


void DrawTree( BinaryTree<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_width / 2;
        int w2 = w / 2;

        if( p_tree->m_left )
        {
            DrawTree( p_tree->m_left, p_x, p_y + 128, w );
            g_gui->ArrowLine( p_x + (p_width / 2), p_y + 24, 
                              p_x + w2, p_y + 152, 24, 24, false, true, BLACK );
        }
        if( p_tree->m_right )
        {
            DrawTree( p_tree->m_right, p_x + w, p_y + 128, w );
            g_gui->ArrowLine( p_x + (p_width / 2), p_y + 24, 
                              p_x + w2 + w, p_y + 152, 24, 24, false, true, BLACK );
        }
    }
}


void RandomTree( BinaryTree<int>* p_tree, int p_depth )
{
    BinaryTree<int>* node = 0;
    if( p_depth < 3 )
    {
        int i = rand() % 100;
        if( i < 80 )
        {
            node = new BinaryTree<int>;
            node->m_data = rand() % 100;
            node->m_parent = p_tree;
            p_tree->m_left = node;
            RandomTree( node, p_depth + 1 );
        }
        i = rand() % 100;
        if( i < 80 )
        {
            node = new BinaryTree<int>;
            node->m_data = rand() % 100;
            node->m_parent = p_tree;
            p_tree->m_right = node;
            RandomTree( node, p_depth + 1 );
        }
    }
}



// ============================================================================
//  Functions
// ============================================================================
void Pre( BinaryTree<int>* p_tree )
{
    if( p_tree == 0 )
        return;

    g_queue.Enqueue( p_tree );
    Pre( p_tree->m_left );
    Pre( p_tree->m_right );
}


void In( BinaryTree<int>* p_tree )
{
    if( p_tree == 0 )
        return;

    In( p_tree->m_left );
    g_queue.Enqueue( p_tree );
    In( p_tree->m_right );
}

void Post( BinaryTree<int>* p_tree )
{
    if( p_tree == 0 )
        return;

    Post( p_tree->m_left );
    Post( p_tree->m_right );
    g_queue.Enqueue( p_tree );
}



// ============================================================================
//  Button Callbacks
// ============================================================================
void PreorderButton()
{
    // clear the queue.
    while( g_queue.Count() != 0 )
    {
        g_queue.Dequeue();
    }

    Pre( g_tree );
    g_current = g_queue.Front();
    g_timer = SDL_GetTicks();
}

void InorderButton()
{
    // clear the queue.
    while( g_queue.Count() != 0 )
    {
        g_queue.Dequeue();
    }

    In( g_tree );
    g_current = g_queue.Front();
    g_timer = SDL_GetTicks();
}

void PostorderButton()
{
    // clear the queue.
    while( g_queue.Count() != 0 )
    {
        g_queue.Dequeue();
    }

    Post( g_tree );
    g_current = g_queue.Front();
    g_timer = SDL_GetTicks();
}

void Random()
{
    // clear the queue.
    while( g_queue.Count() != 0 )
    {
        g_queue.Dequeue();
    }

    if( g_tree )
        delete g_tree;
    g_tree = new BinaryTree<int>;
    g_tree->m_data = rand() % 100;
    
    RandomTree( g_tree, 0 );
}


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

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

    srand( time(0) );

    Random();

    //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" );

    // 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 ));

    // add the items to the g_gui
    g_gui->AddButton( 0, 0, "gbup.bmp", "gbdown.bmp", "Preorder", ARIAL,
                      BLACK, WHITE, PreorderButton );
    g_gui->AddButton( 0, 64, "gbup.bmp", "gbdown.bmp", "Inorder", ARIAL,
                      BLACK, WHITE, InorderButton );
    g_gui->AddButton( 0, 128, "gbup.bmp", "gbdown.bmp", "Postorder", ARIAL,
                      BLACK, WHITE, PostorderButton );
    g_gui->AddButton( 128, 0, "gbup.bmp", "gbdown.bmp", "Random Tree", ARIAL,
                      BLACK, WHITE, Random );


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

    // declare event variable
    SDL_Event event;

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

    g_timer = SDL_GetTicks();

    // 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 );
            }
        }


        if( g_queue.Count() != 0 && (SDL_GetTicks() - g_timer) > g_delay )
        {
            g_queue.Dequeue();
            if( g_queue.Count() == 0 )
            {
                g_current = 0;
            }
            else
            {
                g_current = g_queue.Front();
            }
            g_timer = SDL_GetTicks();
        }


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

        DrawTree( g_tree, 0, 64, WIDTH );

        g_gui->Update();
    }

    return 0;
}

⌨️ 快捷键说明

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