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

📄 gd08-01.cpp

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



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


unsigned long int Hash( int i )
{
    return i;
}

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

// this is the stack that we will use in the demo
HashTable<int, int> g_table( 10, Hash );

// Box
SDL_Surface* g_box;
SDL_Surface* g_rbox;


int g_key = -1;

char g_number[3] = "0";

// ============================================================================
//  Draw Algorithms
// ============================================================================
void DrawHashTable( HashTable<int, int>& p_table,
                    int p_x, int p_y )
{
    static SDL_Surface* text;
    static char number[4] = "000";

    int i, y;
    
    DListIterator< HashEntry<int, int> > itr;

    // loop through each index in the table
    for( i = 0; i < g_table.m_size; i++ )
    {
        // draw the base cell.
        g_gui->Blit( g_box, p_x, p_y );
        // render the index.
        sprintf( number, "%d", i );
        text = TTF_RenderText_Shaded( g_gui->GetFont(ARIAL), number, BLACK, WHITE );
        g_gui->Blit( text, (g_box->w - text->w) / 2 + p_x, 
                            (g_box->h - text->h) / 2 + p_y );
        SDL_FreeSurface( text );
        

        // draw the arrow line
        g_gui->ArrowLine( p_x + 24, p_y, p_x + 24, p_y - 48, 0, 0, 
                          false, true, BLACK );
        y = p_y - g_box->h - 48;

        // get an iterator to the current cell
        itr = g_table.m_table[i].GetIterator();

        // loop through the linked list and draw the data.
        for( itr.Start(); itr.Valid(); itr.Forth() )
        {
            // draw the box
            if( itr.Item().m_key == g_key )
                g_gui->Blit( g_rbox, p_x, y );
            else
                g_gui->Blit( g_box, p_x, y );

            // render the number.
            sprintf( number, "%d", itr.Item().m_data );
            text = TTF_RenderText_Shaded( g_gui->GetFont(ARIAL), number, BLACK, WHITE );
            g_gui->Blit( text, (g_box->w - text->w) / 2 + p_x, 
                                (g_box->h - text->h) / 2 + y );
            SDL_FreeSurface( text );
            y -= g_box->h;
        }
        p_x += g_box->w;
    }
}





// ============================================================================
//  Button Callbacks
// ============================================================================
void Insert()
{
    int n = atoi( g_number );

    // only insert the number if it's not in the hash table already.
    if( g_table.Find( n ) == 0 )
        g_table.Insert( n, n );
}

void Find()
{
    int n = atoi( g_number );
    g_key = n;
}

void Remove()
{
    int n = atoi( g_number );
    g_table.Remove( n );
}

void Random()
{
    // get a random number
    itoa( rand() % 1000, g_number, 10 );
}

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

    // add the buttons 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", "Remove", 
                       ARIAL, BLACK, WHITE, Remove );
    g_gui->AddButton( 0, 192, "gbup.bmp", "gbdown.bmp", "Random", 
                       ARIAL, BLACK, WHITE, Random );

    // add a text box, and set the focus.
    g_gui->AddTextBox( 0, 256, 128, TTF_FontHeight( g_gui->GetFont(ARIAL)),
                       g_number, 3, ARIAL, BLACK, WHITE, true, 0 );
    g_gui->SetFocus( 4 );

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

    // create the BMP's that we need.
    g_box = SDL_LoadBMP( "boxblack.bmp" );
    g_rbox = SDL_LoadBMP( "boxred.bmp" );

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

        DrawHashTable( g_table, 160, 500 );

        g_gui->Update();
    }

    return 0;
}

⌨️ 快捷键说明

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