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

📄 gd01-01.cpp

📁 游戏开发数据结构-Data.Structures.for.Game.Programmers
💻 CPP
字号:
// ============================================================================
//  GD01-01.cpp
//  The Graph Demonstration
// ============================================================================
#include "SDLGUI.h"


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


// ============================================================================
//  Global Variables
// ============================================================================

// define the initial graph sizes
int g_xsize = 100;
int g_ysize = 100;

// keep track of the checkbox states for each of the graphs
bool g_graphs[6];

// this is the GUI
SDLGUI* g_gui;

const int ARIAL = 0;




// draw the graphs.
void DrawGraph()
{
    char number[] = "0000";
    static SDL_Surface* text;
    float x, y, lx, ly;
    float temp;
    float rx = 536.0f / (float)g_xsize;
    float ry = 300.0f / (float)g_ysize;

    // draw the axes
    g_gui->Line( 63, 128, 63, 428, BLACK );
    g_gui->Line( 64, 128, 64, 428, BLACK );
    g_gui->Line( 64, 428, 600, 428, BLACK );
    g_gui->Line( 64, 429, 600, 429, BLACK );

    // draw the number '0' at the origin
    text = TTF_RenderText_Shaded( g_gui->GetFont(ARIAL), "0", BLACK, WHITE );
    g_gui->Blit( text, 48, 430 );
    SDL_FreeSurface( text );

    // draw the x-scale size
    sprintf( number, "%d", g_xsize );
    text = TTF_RenderText_Shaded( g_gui->GetFont(ARIAL), number, BLACK, WHITE );
    g_gui->Blit( text, 570, 430 );
    SDL_FreeSurface( text );

    // draw the y-scale size
    sprintf( number, "%d", g_ysize );
    text = TTF_RenderText_Shaded( g_gui->GetFont(ARIAL), number, BLACK, WHITE );
    g_gui->Blit( text, 0, 110 );
    SDL_FreeSurface( text );


    // determine which graphs are enabled, and draw them.
    
    // log(n)
    if( g_graphs[0] == true )
    {
        temp = (float)log(2.0);
        ly = (float)log( 1 ) / temp;
        lx = 1.0;
        for( x = 2.0; x < g_xsize; x += 1.0 )
        {
            y = (float)log( x ) / temp;
            if( y <= g_ysize && y >= 0 && ly <= g_ysize && ly >= 0 )
            {
                g_gui->Line( (lx * rx) + 64, 
                              428 - (ly * ry), 
                              (x * rx) + 64, 
                              428 - (y * ry), 
                              BLUE );
            }
            lx = x;
            ly = y;
        }
    }

    // n
    if( g_graphs[1] == true )
    {
        ly = 0;
        lx = 0;
        for( x = 1.0; x < g_xsize; x += 1.0 )
        {
            y = x;
            if( y <= g_ysize && y >= 0 && ly <= g_ysize && ly >= 0 )
            {
                g_gui->Line( (lx * rx) + 64, 
                              428 - (ly * ry), 
                              (x * rx) + 64, 
                              428 - (y * ry), 
                              RED );
            }
            lx = x;
            ly = y;
        }
    }

    // n*log(n)
    if( g_graphs[2] == true )
    {
        temp = log(2.0);
        ly = log( 1 ) / temp;
        lx = 1.0;
        for( x = 2.0; x < g_xsize; x += 1.0 )
        {
            y = x * (log( x ) / temp);
            if( y <= g_ysize && y >= 0 && ly <= g_ysize && ly >= 0 )
            {
                g_gui->Line( (lx * rx) + 64, 
                              428 - (ly * ry), 
                              (x * rx) + 64, 
                              428 - (y * ry), 
                              GREEN );
            }
            lx = x;
            ly = y;
        }
    }

    // n^2
    if( g_graphs[3] == true )
    {
        ly = 0;
        lx = 0;
        for( x = 1.0; x < g_xsize; x += 1.0 )
        {
            y = x * x;
            if( y <= g_ysize && y >= 0 && ly <= g_ysize && ly >= 0 )
            {
                g_gui->Line( (lx * rx) + 64, 
                              428 - (ly * ry), 
                              (x * rx) + 64, 
                              428 - (y * ry), 
                              PINK );
            }
            lx = x;
            ly = y;
        }
    }

    // n^3
    if( g_graphs[4] == true )
    {
        ly = 0;
        lx = 0;
        for( x = 1.0; x < g_xsize; x += 1.0 )
        {
            y = x * x * x;
            if( y <= g_ysize && y >= 0 && ly <= g_ysize && ly >= 0 )
            {
                g_gui->Line( (lx * rx) + 64, 
                              428 - (ly * ry), 
                              (x * rx) + 64, 
                              428 - (y * ry), 
                              ORANGE );
            }
            lx = x;
            ly = y;
        }
    }

    // 2^n
    if( g_graphs[5] == true )
    {
        ly = 0;
        lx = 0;
        for( x = 1.0; x < g_xsize; x += 1.0 )
        {
            y = pow( 2, x );
            if( y <= g_ysize && y >= 0 && ly <= g_ysize && ly >= 0 )
            {
                g_gui->Line( (lx * rx) + 64, 
                              428 - (ly * ry), 
                              (x * rx) + 64, 
                              428 - (y * ry), 
                              DCYAN );
            }
            lx = x;
            ly = y;
        }
    }
}


// ============================================================================
//  Callbacks
// ============================================================================

// these toggle the graph states.
void ToggleGraph0()     { g_graphs[0] = !g_graphs[0]; }
void ToggleGraph1()     { g_graphs[1] = !g_graphs[1]; }
void ToggleGraph2()     { g_graphs[2] = !g_graphs[2]; }
void ToggleGraph3()     { g_graphs[3] = !g_graphs[3]; }
void ToggleGraph4()     { g_graphs[4] = !g_graphs[4]; }
void ToggleGraph5()     { g_graphs[5] = !g_graphs[5]; }


// These modify the axes
void ArrowUp()
{                    
    if( g_ysize < 100 )
        g_ysize += 10;
    else if( g_ysize < 1000 )
        g_ysize += 100;
    else if( g_ysize < 10000 )
        g_ysize += 1000;
    if( g_ysize > 5000 )
        g_ysize = 5000;
}

void ArrowDown()
{
    if( g_ysize <= 100 )
        g_ysize -= 10;
    else if( g_ysize <= 1000 )
        g_ysize -= 100;
    else if( g_ysize < 10000 )
        g_ysize -= 1000;
    if( g_ysize < 10 )
        g_ysize = 10;
}

void ArrowLeft()
{
    if( g_xsize <= 100 )
        g_xsize -= 10;
    else if( g_xsize <= 1000 )
        g_xsize -= 100;
    else if( g_xsize < 10000 )
        g_xsize -= 1000;
    if( g_xsize < 10 )
        g_xsize = 10;
}

void ArrowRight()
{
    if( g_xsize < 100 )
        g_xsize += 10;
    else if( g_xsize < 1000 )
        g_xsize += 100;
    else if( g_xsize < 10000 )
        g_xsize += 1000;
    if( g_xsize > 5000 )
        g_xsize = 5000;
    if( g_xsize > 5000 )
        g_xsize = 5000;
}


// ============================================================================
//  Main
// ============================================================================
int main( int argc, char* argv[] )
{
    int current = 0;
    int x;
    int y;
    int i, c;

    // initialise the graph array so that none are visible.
    for( i = 0; i < 6; i++ )
        g_graphs[i] = false;

    //initialize systems
    SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER );
    TTF_Init();

    // create the GUI and set the caption.
    g_gui = new SDLGUI( WIDTH, HEIGHT, ITEMS, WHITE );
    SDL_WM_SetCaption( PROGRAM_NAME, 0); 

    // load the fonts
    g_gui->SetFont( "arial.ttf", ARIAL, 26, TTF_STYLE_NORMAL );

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

    // add the checkboxes
    g_gui->AddCheckbox( 5, 0, "checkboxup.bmp", "checkboxdown.bmp",
                         "log n", ARIAL, BLUE, WHITE, ToggleGraph0 );
    g_gui->AddCheckbox( 5, 32, "checkboxup.bmp", "checkboxdown.bmp",
                         "n", ARIAL, RED, WHITE, ToggleGraph1 );
    g_gui->AddCheckbox( 5, 64, "checkboxup.bmp", "checkboxdown.bmp",
                         "n log n", ARIAL, GREEN, WHITE, ToggleGraph2 );
    g_gui->AddCheckbox( 133, 0, "checkboxup.bmp", "checkboxdown.bmp",
                         "n^2", ARIAL, PINK, WHITE, ToggleGraph3 );
    g_gui->AddCheckbox( 133, 32, "checkboxup.bmp", "checkboxdown.bmp",
                         "n^3", ARIAL, ORANGE, WHITE, ToggleGraph4 );
    g_gui->AddCheckbox( 133, 64, "checkboxup.bmp", "checkboxdown.bmp",
                         "2^n", ARIAL, DCYAN, WHITE, ToggleGraph5 );

    // add the labels
    g_gui->AddLabel( 10, 240, "O()", ARIAL, BLACK, WHITE );
    g_gui->AddLabel( 300, 430, "Data size", ARIAL, BLACK, WHITE );


    // add the arrow buttons
    g_gui->AddButton( 500, 0, "arrowup-up.bmp", "arrowup-down.bmp", 
                       " ", ARIAL, BLACK, BLACK, ArrowUp );
    g_gui->AddButton( 500, 114, "arrowdown-up.bmp", "arrowdown-down.bmp", 
                       " ", ARIAL, BLACK, BLACK, ArrowDown );
    g_gui->AddButton( 436, 64, "arrowleft-up.bmp", "arrowleft-down.bmp", 
                       " ", ARIAL, BLACK, BLACK, ArrowLeft );
    g_gui->AddButton( 550, 64, "arrowright-up.bmp", "arrowright-down.bmp", 
                       " ", ARIAL, BLACK, BLACK, ArrowRight );

    // draw the graphs.
    g_gui->Draw();
    DrawGraph();
    g_gui->Update();

    // declare event variable
    SDL_Event event;

    while( 1 )
    {
        //look for an event
        if( SDL_PollEvent( &event ) )
        {
            //an event was found
            if( event.type == SDL_QUIT ) 
                break;
            if( event.type == SDL_MOUSEBUTTONDOWN ) 
            {
                // get the mouse state.
                SDL_GetMouseState( &x, &y );

                // tell the GUI that a button has been pressed
                g_gui->MouseDown( x, y );
            }

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

            // on events, redraw gui.
            g_gui->Draw();
            DrawGraph();
            g_gui->Update();
        }
    }
 
    //done
    return 0;
}

⌨️ 快捷键说明

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