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

📄 gd13-01.cpp

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



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



// ============================================================================
//  classes and enums
// ============================================================================
enum BSTSTATE
{
    NOSTATE,
    INSERTING,
    FINDING
};

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

int g_timer;

int CompInt( int l, int r )
{
    return l - r;
}

BinarySearchTree<int> g_tree( CompInt );

// the current node.
BinaryTree<int>* g_current = 0;

// Circles
SDL_Surface* g_blackcircle;
SDL_Surface* g_redcircle;

LQueue<BinaryTree<int>*> g_queue;

BSTSTATE g_state = NOSTATE;

char g_number[3] = "50";
int g_int = 50;

// ============================================================================
//  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 + 96, w );
            g_gui->ArrowLine( p_x + (p_width / 2), p_y + 24, 
                              p_x + w2, p_y + 124, 24, 24, false, true, BLACK );
        }
        if( p_tree->m_right )
        {
            DrawTree( p_tree->m_right, p_x + w, p_y + 96, w );
            g_gui->ArrowLine( p_x + (p_width / 2), p_y + 24, 
                              p_x + w2 + w, p_y + 124, 24, 24, false, true, BLACK );
        }
    }
}


// this function traces through the BST, adding nodes to the 
// global queue as it visits them, giving the user a graphical
// demonstration.
void BSTTrace( int p_value )
{
    // empty the queue
    while( g_queue.Count() != 0 )
        g_queue.Dequeue();

    BinaryTree<int>* current = g_tree.m_root;
    int temp;

    while( current != 0 )
    {
        // enqueue the current node.
        g_queue.Enqueue( current );
        temp = g_tree.m_compare( p_value, current->m_data );
        if( temp == 0 )
            return;
        if( temp < 0 )
            current = current->m_left;
        else
            current = current->m_right;
    }
    return;
}

// ============================================================================
//  Button Callbacks
// ============================================================================
void Insert()
{
    if( g_state == NOSTATE )
    {
        g_int = atoi( g_number );
        BSTTrace( g_int );
        g_state = INSERTING;
        g_timer = SDL_GetTicks();
        if( g_queue.Count() != 0 )
            g_current = g_queue.Front();
    }
}

void Find()
{
    if( g_state == NOSTATE )
    {
        g_int = atoi( g_number );
        BSTTrace( g_int );
        g_state = FINDING;
        g_timer = SDL_GetTicks();
        if( g_queue.Count() != 0 )
            g_current = g_queue.Front();
    }
}

void Random()
{
    int i = rand() % 100;
    sprintf( g_number, "%d", i );
}

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

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

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

    // 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", "Insert", ARIAL,
                      BLACK, WHITE, Insert );
    g_gui->AddButton( 0, 64, "gbup.bmp", "gbdown.bmp", "Find", ARIAL,
                      BLACK, WHITE, Find );
    g_gui->AddButton( 0, 128, "gbup.bmp", "gbdown.bmp", "Random", ARIAL,
                      BLACK, WHITE, Random );


    // add the text box
    g_gui->AddTextBox( 128, 0, 128, 32, g_number, 2, 
                       ARIAL, BLACK, WHITE, true, 0 );

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

        if( g_state == INSERTING || g_state == FINDING )
        {
            if( SDL_GetTicks() - g_timer > 500 )
            {
                g_timer = SDL_GetTicks();
                g_queue.Dequeue();
                if( g_queue.Count() != 0 )
                {
                    g_current = g_queue.Front();
                }
                else 
                {
                    if( g_state == INSERTING )
                    {
                        g_tree.Insert( g_int );
                        g_current = g_tree.Find( g_int );
                    }
                    else
                    {
                        if( g_tree.Find( g_int ) == 0 )
                            g_current = 0;
                    }                        
                    g_state = NOSTATE;
                }
            }
        }

        DrawTree( g_tree.m_root, 0, 0, WIDTH );

        g_gui->Update();
    }

    return 0;
}

⌨️ 快捷键说明

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