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

📄 g05-02.cpp

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

// ============================================================================
//  Global Constants
// ============================================================================
const char PROGRAM_NAME[]   = "Game Demo 05-02: Layered Tilemapping Demonstration";
const int WIDTH             = 640;
const int HEIGHT            = 480;

const int TILESIZE          = 64;
const int MAPWIDTH          = 16;
const int MAPHEIGHT         = 16;
const int LAYERS            = 2;
const int TILES             = 16;
const int SCROLL            = 3;

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

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

// the tilemap.
Array3D<int> g_tilemap( MAPWIDTH, MAPHEIGHT, LAYERS ); 

// the bitmaps
SDL_Surface* g_tiles[TILES];


// the global position of the camera.
int g_x = 0;
int g_y = 0;

// the delta's, which determine the scrolling speed.
int g_dx = 0;
int g_dy = 0;


// this function draws the tilemap on the screen.
void DrawTilemap( int p_x, int p_y )
{
    int x, y, z;
    int bx = p_x;
    int by = p_y;
    int index;

    // loop through every layer
    for( z = 0; z < LAYERS; z++ )
    {
        bx = p_x;
        by = p_y;

        // then loop through each x,y on each layer.
        for( y = 0; y < MAPHEIGHT; y++ )
        {
            for( x = 0; x < MAPWIDTH; x++ )
            {
                // get the tile number in the cell
                index = g_tilemap.Get( x, y, z );

                // if the tile is valid, then draw it.
                if( index != -1 )
                {
                    SDLBlit( g_tiles[index], g_window, bx, by );
                }
                bx += TILESIZE;
            }

            // reset the bx counter back to the beginning x pixel
            bx = p_x;

            // increment the by pixel counter
            by += TILESIZE;
        }
    }
}


int main( int argc, char* argv[] )
{
    // declare coordinates.
    int x, y;

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



    // load the bmps
    g_tiles[0] = SDL_LoadBMP( "grass1.bmp" );
    g_tiles[1] = SDL_LoadBMP( "grass2.bmp" );
    g_tiles[2] = SDL_LoadBMP( "grass3.bmp" );
    g_tiles[3] = SDL_LoadBMP( "grass4.bmp" );
    g_tiles[4] = SDL_LoadBMP( "roadh.bmp" );
    g_tiles[5] = SDL_LoadBMP( "roadv.bmp" );
    g_tiles[6] = SDL_LoadBMP( "roadtopleft.bmp" );
    g_tiles[7] = SDL_LoadBMP( "roadtopright.bmp" );
    g_tiles[8] = SDL_LoadBMP( "roadbottomleft.bmp" );
    g_tiles[9] = SDL_LoadBMP( "roadbottomright.bmp" );
    g_tiles[10] = SDL_LoadBMP( "snow1.bmp" );
    g_tiles[11] = SDL_LoadBMP( "snow2.bmp" );
    g_tiles[12] = SDL_LoadBMP( "snowright.bmp" );

    SDL_SetColorKey( g_tiles[12], SDL_SRCCOLORKEY, 
                     SDL_MapRGB( g_tiles[12]->format, 0, 0, 0 ) );

    // randomise the grass and snow
    for( y = 0; y < MAPHEIGHT; y++ )
    {
        for( x = 0; x < (MAPWIDTH / 2); x++ )
        {
            g_tilemap.Get( x, y, 0 ) = rand() % 4;
            g_tilemap.Get( x + (MAPWIDTH / 2), y, 0 ) = (rand() % 2) + 10;
        }
    }

    // create a road
    for( x = 4; x < 10; x++ )
    {
        g_tilemap.Get( x, 2, 0 ) = 4;
        g_tilemap.Get( x, 6, 0 ) = 4;
    }
    for( y = 3; y < 7; y++ )
    {
        g_tilemap.Get( 4, y, 0 ) = 5;
        g_tilemap.Get( 9, y, 0 ) = 5;
    }

    g_tilemap.Get( 4, 2, 0 ) = 6;
    g_tilemap.Get( 9, 2, 0 ) = 7;
    g_tilemap.Get( 4, 6, 0 ) = 8;
    g_tilemap.Get( 9, 6, 0 ) = 9;


    // create another road
    for( x = 0; x < (MAPWIDTH / 2); x++ )
    {
        g_tilemap.Get( x, 8, 0 ) = 4;
    }

    // clear the second layer
    for( y = 0; y < MAPHEIGHT; y++ )
    {
        for( x = 0; x < MAPWIDTH; x++ )
        {
            g_tilemap.Get( x, y, 1 ) = -1;
        }
    }

    // add the transparent snow tiles over the grass.
    for( y = 0; y < MAPHEIGHT; y++ )
    {
        g_tilemap.Get( (MAPWIDTH/2) - 1, y, 1 ) = 12;
    }

    // clear the snow off of the path tiles.
    g_tilemap.Get( (MAPWIDTH/2) - 1, 2, 1 ) = -1;
    g_tilemap.Get( (MAPWIDTH/2) - 1, 6, 1 ) = -1;

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

                if( event.key.keysym.sym == SDLK_UP )
                    g_dy += SCROLL;
                if( event.key.keysym.sym == SDLK_DOWN )
                    g_dy -= SCROLL;
                if( event.key.keysym.sym == SDLK_LEFT )
                    g_dx += SCROLL;
                if( event.key.keysym.sym == SDLK_RIGHT )
                    g_dx -= SCROLL;
            }

            if( event.type == SDL_KEYUP )
            {
                // a key was released.

                if( event.key.keysym.sym == SDLK_UP )
                    g_dy -= SCROLL;
                if( event.key.keysym.sym == SDLK_DOWN )
                    g_dy += SCROLL;
                if( event.key.keysym.sym == SDLK_LEFT )
                    g_dx -= SCROLL;
                if( event.key.keysym.sym == SDLK_RIGHT )
                    g_dx += SCROLL;
            }
        }   // end event loop.


        g_x += g_dx;
        g_y += g_dy;

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

        DrawTilemap( g_x, g_y );

        // 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 + -