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

📄 g14-01.cpp

📁 游戏开发数据结构-Data.Structures.for.Game.Programmers
💻 CPP
字号:
// ============================================================================
//  G14-01.cpp
//  Building Queue Demonstration.
// ============================================================================
#include "SDLGUI.h"
#include "Heap.h"
#include <stdlib.h>
#include <time.h>
#include <string.h>



// ============================================================================
//  Global Constants
// ============================================================================
const char PROGRAM_NAME[]   = "Building Queue Demonstration";
const int WIDTH             = 640;
const int HEIGHT            = 480;
const int ITEMS             = 32;
const int ARIAL             = 0;



// ============================================================================
//  classes and enums
// ============================================================================
enum UNIT
{
    ATTACKER,
    WORKER,
    DEFENDER
};

class Factory
{
public:
    Factory()
    {
        m_working = false;
        m_functioning = true;
    }

    UNIT m_currentUnit;         // the unit the factory is currently producing
    int  m_startTime;           // the time the unit started producing
    bool m_working;             // is the factory working on something?
    bool m_functioning;         // is the factory on or off?
};


int CompareUnits( UNIT p_left, UNIT p_right )
{
    return p_left - p_right;
}


// ============================================================================
//  Global Variables
// ============================================================================
SDLGUI* g_gui;
SDL_Surface* g_text[3];

int g_timer;

// the priority queue of units
Heap<UNIT> g_heap( 64, CompareUnits );

// the four factories
Factory g_factories[4];

// ============================================================================
//  Button Callbacks
// ============================================================================
void AddAttacker()
{
    g_heap.Enqueue( ATTACKER );
}

void AddWorker()
{
    g_heap.Enqueue( WORKER );
}

void AddDefender()
{
    g_heap.Enqueue( DEFENDER );
}

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

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

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


    g_gui->AddLabel( 0, 0, "Build:", ARIAL, BLACK, WHITE );

    // add the items to the g_gui
    g_gui->AddButton( 0, 32, "gbup.bmp", "gbdown.bmp", "Attacker", ARIAL,
                      BLACK, WHITE, AddAttacker );
    g_gui->AddButton( 0, 96, "gbup.bmp", "gbdown.bmp", "Worker", ARIAL,
                      BLACK, WHITE, AddWorker );
    g_gui->AddButton( 0, 160, "gbup.bmp", "gbdown.bmp", "Defender", ARIAL,
                      BLACK, WHITE, AddDefender );

    g_gui->AddLabel( 0, 256, "Next:", ARIAL, BLACK, WHITE );
    g_gui->AddLabel( 0, 288, "Attacker", ARIAL, BLACK, WHITE );
    g_gui->AddLabel( 0, 288, "Worker", ARIAL, BLACK, WHITE );
    g_gui->AddLabel( 0, 288, "Defender", ARIAL, BLACK, WHITE );

    g_gui->GetItem( 5 )->SetVisibility( false );
    g_gui->GetItem( 6 )->SetVisibility( false );
    g_gui->GetItem( 7 )->SetVisibility( false );

    g_text[0] = TTF_RenderText_Shaded( g_gui->GetFont(ARIAL), 
                    "Attacker", BLACK, WHITE );
    g_text[1]  = TTF_RenderText_Shaded( g_gui->GetFont(ARIAL), 
                    "Worker", BLACK, WHITE );
    g_text[2]  = TTF_RenderText_Shaded( g_gui->GetFont(ARIAL), 
                    "Defender", BLACK, WHITE );


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

                if( x >= 192 && x <= 256 )
                {
                    x = (y - 64) / 96;
                    if( x < 4 )
                    {
                        g_factories[x].m_functioning = 
                            !g_factories[x].m_functioning;
                    }
                }

            }

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


        // check to see if a factory completes its job
        for( x = 0; x < 4; x++ )
        {
            if( g_factories[x].m_working == true )
            {
                // if 10 seconds have passed
                if( SDL_GetTicks() - g_factories[x].m_startTime > 10000 )
                {
                    // then the factory has completed work
                    g_factories[x].m_working = 0;
                }
            }
        }


        // check to see if any factory can accept a new job
        for( x = 0; x < 4; x++ )
        {
            // if the factory isn't working, is functioning, and there
            // is something waiting in the queue
            if( g_factories[x].m_working == false && 
                g_factories[x].m_functioning == true &&
                g_heap.m_count > 0 )
            {
                // then take the unit off the queue, place it into the
                // factory
                g_factories[x].m_currentUnit = g_heap.Item();
                g_factories[x].m_working = true;
                g_factories[x].m_startTime = SDL_GetTicks();
                g_heap.Dequeue();
            }
        }

        g_gui->GetItem( 5 )->SetVisibility( false );
        g_gui->GetItem( 6 )->SetVisibility( false );
        g_gui->GetItem( 7 )->SetVisibility( false );

        if( g_heap.m_count > 0 )
        {
            // find out which item is next, and display that text.
            g_gui->GetItem( g_heap.Item() + 5 )->SetVisibility( true );
        }
        
        // draw the g_gui at the end of each loop.
        g_gui->Draw();


        // draw the separator line
        g_gui->Line( 128, 0, 128, 480, BLACK );
        g_gui->Line( 129, 0, 129, 480, BLACK );

        // draw the factories
        for( x = 0; x < 4; x++ )
        {
            c = BLACK;
            if( g_factories[x].m_functioning == false )
                c = RED;
            
            y = x * 96 + 64;

            g_gui->Line( 192, y, 192, y + 64, c );
            g_gui->Line( 256, y, 256, y + 64, c );
            g_gui->Line( 192, y, 256, y, c );
            g_gui->Line( 192, y + 64, 256, y + 64, c );

            // draw the progress bar outline
            g_gui->Line( 264, y + 56, 632, y + 56, GREY );
            g_gui->Line( 264, y + 64, 632, y + 64, GREY );
            g_gui->Line( 264, y + 56, 264, y + 64, GREY );
            g_gui->Line( 632, y + 56, 632, y + 64, GREY );

            // draw the progress bar
            if( g_factories[x].m_working == true )
            {
                z = SDL_GetTicks() - g_factories[x].m_startTime;
                z = (z * 362) / 10000;
                g_gui->Box( 268, y + 58, z, 4, BLUE );

                g_gui->Blit( g_text[g_factories[x].m_currentUnit],
                             264, y );
            }
        }

        g_gui->Update();
    }

    return 0;
}

⌨️ 快捷键说明

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