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

📄 gd12-01.cpp

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



// ============================================================================
//  Global Constants
// ============================================================================
const char PROGRAM_NAME[]   = "Binary 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_timer;

BinaryTree<int>* g_tree;

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


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

// ============================================================================
//  Button Callbacks
// ============================================================================
void Random()
{
    if( g_tree )
        delete g_tree;
    g_tree = new BinaryTree<int>;
    g_tree->m_data = rand() % 100;
    g_current = g_tree;
    RandomTree( g_tree, 0 );
}


void InsertLeft()
{
    BinaryTree<int>* node;
    if( g_current != 0 )
    {
        if( g_current->m_left == 0 )
        {
            node = new BinaryTree<int>;
            node->m_data = rand() % 100;
            node->m_parent = g_current;
            g_current->m_left = node;
        }
    }
}


void InsertRight()
{
    BinaryTree<int>* node;
    if( g_current != 0 )
    {
        if( g_current->m_right == 0 )
        {
            node = new BinaryTree<int>;
            node->m_data = rand() % 100;
            node->m_parent = g_current;
            g_current->m_right = node;
        }
    }
}


void GoLeft()
{
    if( g_current != 0 )
    {
        g_current = g_current->m_left;
    }
}

void GoRight()
{
    if( g_current != 0 )
    {
        g_current = g_current->m_right;
    }
}


void Remove()
{
    if( g_current != 0 && g_current != g_tree )
    {
        if( g_current->m_parent->m_left == g_current )
            g_current->m_parent->m_left = 0;
        else
            g_current->m_parent->m_right = 0;

        delete g_current;
    }
}

void GotoRoot()
{
    g_current = g_tree;
}


void GoUp()
{
    if( g_current != 0 )
        g_current = g_current->m_parent;
}


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



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


    srand( time(0) );

    g_tree = new BinaryTree<int>;
    g_tree->m_data = rand() % 100;
    g_current = g_tree;
    RandomTree( g_tree, 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 Left", ARIAL,
                      BLACK, WHITE, InsertLeft );
    g_gui->AddButton( 128, 0, "gbup.bmp", "gbdown.bmp", "Insert Right", ARIAL,
                      BLACK, WHITE, InsertRight );
    g_gui->AddButton( 0, 64, "gbup.bmp", "gbdown.bmp", "Go Left", ARIAL,
                      BLACK, WHITE, GoLeft );
    g_gui->AddButton( 128, 64, "gbup.bmp", "gbdown.bmp", "Go Right", ARIAL,
                      BLACK, WHITE, GoRight );
    g_gui->AddButton( 256, 0, "gbup.bmp", "gbdown.bmp", "Randomise", ARIAL,
                      BLACK, WHITE, Random );
    g_gui->AddButton( 256, 64, "gbup.bmp", "gbdown.bmp", "Remove", ARIAL,
                      BLACK, WHITE, Remove );
    g_gui->AddButton( 384, 0, "gbup.bmp", "gbdown.bmp", "Goto Root", ARIAL,
                      BLACK, WHITE, GotoRoot );
    g_gui->AddButton( 384, 64, "gbup.bmp", "gbdown.bmp", "Go Up", ARIAL,
                      BLACK, WHITE, GoUp );


    // 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 + -