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

📄 gd03-01.cpp

📁 游戏开发数据结构-Data.Structures.for.Game.Programmers
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ============================================================================
//  GD03-01.cpp
//  The Array Graphic Demonstration
// ============================================================================
#include "SDLGUI.h"
#include "Array.h"
#include <stdlib.h>
#include <time.h>



// ============================================================================
//  Global Constants
// ============================================================================
const char PROGRAM_NAME[]   = "Array Graphical Demonstration";
const int WIDTH             = 640;
const int HEIGHT            = 480;
const int ITEMS             = 32;



// ============================================================================
//  The States of the program
// ============================================================================
enum arraystate
{
    // nothing happening.
    NOSTATE,

    // increase/decrease array size
        // show the new array, wait for 'continue'
        SHOWNEWARRAY,
        // fill the new array.
        FILLARRAY,
        // wait for the user to click continue.
        WAITFORDELETE,
        // delete the array.
        DELETEARRAY,

    // insert item
        // wait for animation to start
        WAITFORMOVEUP,
        // move everything up one index
        MOVEITEMSUP,
        // wait for the insertion
        WAITFORINSERT,
        // insert item into index
        INSERTITEM,

    // remove item
        // wait for animation to start
        WAITFORMOVEDOWN,
        // move everything down one index
        MOVEITEMSDOWN,


    WAITFORDONE
};



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

const int ARIAL = 0;

// the arrays. Array1 will be the normal array,
// array 2 will be the "dummy" array, which
// we will use to animate.
Array<int> g_array1( 10 );
Array<int> g_array2( 10 );

// used for the increase/decrease animations.
// holds the minimum number of valid cells
int g_min;

// holds the current index in the array.
int g_current = 0;

// used for counting.
int g_index;

// holds the last known time.
Uint32 g_time;

// the current animation state.
arraystate g_state = NOSTATE;

// the bitmaps used in the demo
SDL_Surface* g_box;
SDL_Surface* g_redbox;
SDL_Surface* g_varrow;
SDL_Surface* g_rarrow;
SDL_Surface* g_larrow;



// ============================================================================
//  Draw Algorithms
// ============================================================================
void DrawArray( Array<int>& p_array,        // the array to draw
                int p_x,                    // x and y coords.
                int p_y,
                int p_current,              // the current index.
                bool p_horizontal )         // horizontal or vertical?
{
    static SDL_Surface* text;
    static char number[] = "00";
    int index;

    // draw the array.
    for( index = 0; index < p_array.Size(); index++ )
    {
        // draw the contents of the box.
        if( p_array[index] < 0 || p_array[index] > 99 )
            text = TTF_RenderText_Shaded( g_gui->GetFont(ARIAL), "x", RED, WHITE );
        else
        {
            sprintf( number, "%d", p_array[index] );
            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 box
        if( index != p_current )
            g_gui->Blit( g_box, p_x, p_y );
        else
            g_gui->Blit( g_redbox, p_x, p_y );

        // determine if the x or the y coordinate is incremented
        if( p_horizontal == true )
            p_x += g_box->w;
        else
            p_y += g_box->h;
    }
}


// draws the down arrows
void DrawArrows( int p_x, int p_y, int p_indexes )
{
    int index;
    for( index = 0; index <= p_indexes; index++ )
    {
        g_gui->Blit( g_varrow, p_x, p_y );
        p_x += g_box->w;
    }
}

// draws the right-pointing arrows
void DrawRArrows( int p_x, int p_y, int p_start, int p_indexes )
{
    int index;
    // skip ahead over boxes that dont have arrows.
    p_x += (g_box->w * p_start);
    for( index = p_start; index <= p_indexes; index++ )
    {
        g_gui->Blit( g_rarrow, p_x, p_y );
        p_x += g_box->w;
    }
}

// draws the left-pointing arrows
void DrawLArrows( int p_x, int p_y, int p_start, int p_max )
{
    int index;
    // skip ahead over boxes that dont have arrows.
    p_x += (g_box->w * p_start);
    for( index = p_start; index <= p_max; index++ )
    {
        g_gui->Blit( g_larrow, p_x, p_y );
        p_x += g_box->w;
    }
}

// ----------------------------------------------------------------------------

// this is a helper function which modifies
// the GUI and the timer when a resize animation
// is started.
void StartResize()
{
    g_state = SHOWNEWARRAY;

    // hide all the normal buttons.
    g_gui->GetItem( 0 )->SetVisibility( false );
    g_gui->GetItem( 1 )->SetVisibility( false );
    g_gui->GetItem( 2 )->SetVisibility( false );
    g_gui->GetItem( 3 )->SetVisibility( false );

    // show 'continue' button.
    g_gui->GetItem( 5 )->SetVisibility( true );

    // hide instructions.
    g_gui->GetItem( 9 )->SetVisibility( false );
    g_gui->GetItem( 10 )->SetVisibility( false );

    // show the "create array" text.
    g_gui->GetItem( 6 )->SetVisibility( true );

    g_time = SDL_GetTicks();
}


// user pressed the increase size button
void Inc() 
{
    int index;
    // array cannot go past size 10.
    if( g_array1.Size() < 10 )
    {
        // resize array 2 and invalidate the data.
        g_array2.Resize( g_array1.Size() + 1 );
        for( index = 0; index < g_array2.Size(); index++ )
            g_array2[index] = -1;

        g_min = g_array1.Size();
        StartResize();
    }
}

// user pressed the decrease size button
void Dec() 
{
    int index;
    // array cannot go past size 1.
    if( g_array1.Size() > 1 )
    {
        // resize array 2 and invalidate the data.
        g_array2.Resize( g_array1.Size() - 1 );
        for( index = 0; index < g_array2.Size(); index++ )
            g_array2[index] = -1;

        g_min = g_array2.Size();
        StartResize();
    }
}


// user pressed the Insert button
void Ins() 
{
    g_state = WAITFORMOVEUP;

    // hide all the normal buttons.
    g_gui->GetItem( 0 )->SetVisibility( false );
    g_gui->GetItem( 1 )->SetVisibility( false );
    g_gui->GetItem( 2 )->SetVisibility( false );
    g_gui->GetItem( 3 )->SetVisibility( false );

    // show 'continue' button.
    g_gui->GetItem( 5 )->SetVisibility( true );

    // hide instructions.
    g_gui->GetItem( 9 )->SetVisibility( false );
    g_gui->GetItem( 10 )->SetVisibility( false );

    // show the "move up" text.
    g_gui->GetItem( 11 )->SetVisibility( true );
};


// user clicked the Remove button
void Rem() 
{
    g_state = WAITFORMOVEDOWN;

    // hide all the normal buttons.
    g_gui->GetItem( 0 )->SetVisibility( false );
    g_gui->GetItem( 1 )->SetVisibility( false );
    g_gui->GetItem( 2 )->SetVisibility( false );
    g_gui->GetItem( 3 )->SetVisibility( false );

    // show 'continue' button.
    g_gui->GetItem( 5 )->SetVisibility( true );

    // hide instructions.
    g_gui->GetItem( 9 )->SetVisibility( false );
    g_gui->GetItem( 10 )->SetVisibility( false );

    // show the "move down" text.
    g_gui->GetItem( 13 )->SetVisibility( true );
};


// user pressed the continue button
void Continue() 
{ 
    int index;
    switch( g_state )
    {
    case SHOWNEWARRAY:
        g_index = 0;
        g_state = FILLARRAY;
        g_time = SDL_GetTicks();
        
        // hide the 'continue' button and the 'create' text.
        g_gui->GetItem( 5 )->SetVisibility( false );
        g_gui->GetItem( 6 )->SetVisibility( false );

        // show the 'fill' text.
        g_gui->GetItem( 7 )->SetVisibility( true );
        break;

    case WAITFORDELETE:
        // set the next state
        g_index = 400;
        g_state = DELETEARRAY;

        // hide the 'continue' button and the 'fill' text.
        g_gui->GetItem( 5 )->SetVisibility( false );
        g_gui->GetItem( 7 )->SetVisibility( false );

        // show the 'delete' text.
        g_gui->GetItem( 8 )->SetVisibility( true );
        
        // resize array1 to the new size, and copy everything over.

⌨️ 快捷键说明

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