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

📄 g07-02.cpp

📁 游戏开发数据结构-Data.Structures.for.Game.Programmers
💻 CPP
字号:
// ============================================================================
// G07-02.cpp
// Command Queue Demo
// ============================================================================
#include "SDL.h"
#include "SDLHelpers.h"
#include "Queue.h"
#include <stdlib.h>

// ============================================================================
//  Global Constants
// ============================================================================
const char PROGRAM_NAME[]   = "Game Demo 07-02: Command Queue Demo";
const int WIDTH             = 640;
const int HEIGHT            = 480;


// ============================================================================
//  Classes
// ============================================================================
class Coordinates
{
public:
    int x;
    int y;
};


class Player
{
public:
    int x;
    int y;
    LQueue<Coordinates> m_queue;
};



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

// this is the main window for the framework
SDL_Surface* g_window = 0;

Player g_player;

SDL_Surface* g_ufo;

int g_timer;


int g_xm, g_xa;
int g_ym, g_ya;

// ============================================================================
//  Functions
// ============================================================================

// calculates some variables
void Calculate()
{
    // calculate the difference from the players position to the 
    // goal
    g_xm = g_player.m_queue.Front().x - g_player.x;
    g_ym = g_player.m_queue.Front().y - g_player.y;

    // store the starting place of the player
    g_xa = g_player.x;
    g_ya = g_player.y;

    // reset the timer
    g_timer = SDL_GetTicks();
}



int main( int argc, char* argv[] )
{
    // declare coordinates.
    int x, y, t;
    Coordinates c;
    DListIterator<Coordinates> itr;

    g_player.x = 320;
    g_player.y = 240;

    // declare event holder
    SDL_Event event;


    // initialize the video system.
    SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER );
    SDL_WM_SetCaption( PROGRAM_NAME, 0); 

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

    // set the video mode.
    g_window = SDL_SetVideoMode( WIDTH, HEIGHT, 0, SDL_ANYFORMAT );

    g_ufo = SDL_LoadBMP( "ufo.bmp" );
    SDL_SetColorKey( g_ufo, SDL_SRCCOLORKEY, 
                     SDL_MapRGB( g_ufo->format, 0, 0, 0 ) );

    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( &c.x, &c.y );
                g_player.m_queue.Enqueue( c );
                if( g_player.m_queue.Count() == 1 )
                    Calculate();
            }
            if( event.type == SDL_MOUSEBUTTONUP ) 
            {
                // get the mouse state.
                SDL_GetMouseState( &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 );
                }

            }

        }   // end event loop.


        // perform the calculations
        if( g_player.m_queue.Count() != 0 )
        {
            // calculate the amount of time that passed since the
            // last command was dequeued.
            t = SDL_GetTicks() - g_timer;

            // make sure t never goes above 1000.
            if( t > 1000 )
            {
                t = 1000;
            }

            // calculate the players position by multiplying
            // the distance from start to finish times the number
            // of milliseonds that have passed; then divide that by 1000
            // to get a fraction of the distance. Finally, add the
            // original coordinates of the player.
            g_player.x = ((t * g_xm) / 1000) + g_xa;
            g_player.y = ((t * g_ym) / 1000) + g_ya;

            // if the player reached the end of the current path,
            // calculate the next command.
            if( g_player.x == g_player.m_queue.Front().x &&
                g_player.y == g_player.m_queue.Front().y )
            {
                g_player.m_queue.Dequeue();
                if( g_player.m_queue.Count() > 0 )
                    Calculate();
            }
        }

        // clear the screen to white.
        SDL_FillRect( g_window, NULL, SDL_MapRGB( g_window->format, 0, 0, 0 ) );


        // draw the lines
        itr = g_player.m_queue.GetIterator();
        itr.Start();
        if( itr.Valid() )
        {
            x = itr.Item().x;
            y = itr.Item().y;
            SDLLine( g_window, g_player.x, g_player.y, 
                     x, y, GREEN );
        }
        
        for( itr.Forth(); itr.Valid(); itr.Forth() )
        {
            SDLLine( g_window, x, y, 
                     itr.Item().x, itr.Item().y, GREEN );
            x = itr.Item().x;
            y = itr.Item().y;
        }

        SDLBlit( g_ufo, g_window, g_player.x - 32, g_player.y - 32 );


        // update the entire window.
        SDL_UpdateRect( g_window, 0, 0, 0, 0 );
    }
  
    // done
    return 0;
}

⌨️ 快捷键说明

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