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

📄 gd18-01.cpp

📁 游戏开发数据结构Data Structures for Game Programmers
💻 CPP
字号:
// ============================================================================
//  GD18-01.cpp
//  Simple Finite State Machine Demo
// ============================================================================
#include "SDLGUI.h"
#include "Graph.h"
#include "Array2D.h"
#include <stdlib.h>
#include <time.h>



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


// ============================================================================
//  Classes
// ============================================================================
enum AIState
{
    GUARDING,
    ATTACKING,
    FINDINGHEALTH,
    FINDINGAMMO
};

enum AIEvent
{
    SEEENEMY,
    KILLENEMY,
    FOUNDHEALTH,
    FOUNDAMMO
};




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

Array2D<AIState> g_machine( 4, 4 );

// used for selecting the current node;
int g_currentstate = GUARDING;

// Boxes
SDL_Surface* g_redbox;
SDL_Surface* g_back;


int g_coords[4][2] = { { 144, 151 }, 
                       { 495, 151 }, 
                       { 495, 388 },
                       { 144, 388 } }; 


// ============================================================================
//  Draw Algorithm
// ============================================================================
void DrawMachine()
{
    int x;

    g_gui->Blit( g_back, 0, 0 );

    for( x = 0; x < 4; x++ )
    {
        // draw the box
        if( x == g_currentstate )
        {
            g_gui->Blit( g_redbox, g_coords[x][0], g_coords[x][1] );
        }
    }
}


// ============================================================================
//  Algorithms
// ============================================================================
void InitMachine()
{
    int state;
    int event;

    // fill the machine with default values first
    for( state = 0; state < 4; state++ )
    {
        for( event = 0; event < 4; event ++ )
        {
            g_machine.Get( state, event ) = (AIState)state;
        }
    }


    // now fill the machine in with the real values
    g_machine.Get( GUARDING, SEEENEMY ) = ATTACKING;
    g_machine.Get( ATTACKING, KILLENEMY ) = FINDINGHEALTH;
    g_machine.Get( FINDINGHEALTH, SEEENEMY ) = ATTACKING;
    g_machine.Get( FINDINGHEALTH, FOUNDHEALTH ) = FINDINGAMMO;
    g_machine.Get( FINDINGAMMO, SEEENEMY ) = ATTACKING;
    g_machine.Get( FINDINGAMMO, FOUNDAMMO ) = GUARDING;

}

// ============================================================================
//  Button Callbacks
// ============================================================================
void SeeEnemy()
{
    g_currentstate = g_machine.Get( g_currentstate, SEEENEMY );
}

void KillEnemy()
{
    g_currentstate = g_machine.Get( g_currentstate, KILLENEMY );
}

void FoundHealth()
{
    g_currentstate = g_machine.Get( g_currentstate, FOUNDHEALTH );
}

void FoundAmmo()
{
    g_currentstate = g_machine.Get( g_currentstate, FOUNDAMMO );
}



// ============================================================================
//  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, 18, TTF_STYLE_NORMAL );

    // init the machine
    InitMachine();


    // load the bmps
    g_redbox =   SDL_LoadBMP( "boxred.bmp" );
    g_back   =  SDL_LoadBMP( "background.bmp" );

    // set the color keys of the BMPs
    SDL_SetColorKey( g_redbox, SDL_SRCCOLORKEY, 
                     SDL_MapRGB( g_redbox->format, 255, 255, 255 ));
    SDL_SetColorKey( g_back, SDL_SRCCOLORKEY, 
                     SDL_MapRGB( g_back->format, 255, 255, 255 ));

    // add the items to the g_gui
    g_gui->AddButton( 0, 0, "gbup.bmp", "gbdown.bmp", "See Enemy", ARIAL,
                      BLACK, WHITE, SeeEnemy );
    g_gui->AddButton( 0, 64, "gbup.bmp", "gbdown.bmp", "Kill Enemy", ARIAL,
                      BLACK, WHITE, KillEnemy );
    g_gui->AddButton( 0, 128, "gbup.bmp", "gbdown.bmp", "Found Health", ARIAL,
                      BLACK, WHITE, FoundHealth );
    g_gui->AddButton( 0, 192, "gbup.bmp", "gbdown.bmp", "Found Ammo", ARIAL,
                      BLACK, WHITE, FoundAmmo );


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


        DrawMachine();

        g_gui->Update();
    }

    return 0;
}

⌨️ 快捷键说明

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