📄 g06-02.cpp
字号:
// ============================================================================
// G06-02.cpp
// Layered Tilemapping Game Demo, with Linked Lists
// ============================================================================
#include "SDL.h"
#include "SDLHelpers.h"
#include "Array2D.h"
#include "SLinkedList.h"
#include <stdlib.h>
// ============================================================================
// Global Constants
// ============================================================================
const char PROGRAM_NAME[] = "Game Demo 06-02: Linked 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.
Array2D< SLinkedList<int> > g_tilemap( MAPWIDTH, MAPHEIGHT );
// 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;
void DrawTilemap( int p_x, int p_y )
{
int x, y;
int bx = p_x;
int by = p_y;
int index;
SListIterator<int> itr;
// loop through each x,y on the map
for( y = 0; y < MAPHEIGHT; y++ )
{
for( x = 0; x < MAPWIDTH; x++ )
{
// start at the lowest layer, and iterate upwards
itr = g_tilemap.Get( x, y ).GetIterator();
for( itr.Start(); itr.Valid(); itr.Forth() )
{
index = itr.Item();
SDLBlit( g_tiles[index], g_window, bx, by );
}
bx += TILESIZE;
}
bx = p_x;
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 ).Append( rand() % 4 );
g_tilemap.Get( x + (MAPWIDTH / 2), y ).Append( (rand() % 2) + 10 );
}
}
// create a road
for( x = 4; x < 10; x++ )
{
g_tilemap.Get( x, 2 ).Append( 4 );
g_tilemap.Get( x, 6 ).Append( 4 );
}
for( y = 3; y < 7; y++ )
{
g_tilemap.Get( 4, y ).Append( 5 );
g_tilemap.Get( 9, y ).Append( 5 );
}
g_tilemap.Get( 4, 2 ).Append( 6 );
g_tilemap.Get( 9, 2 ).Append( 7 );
g_tilemap.Get( 4, 6 ).Append( 8 );
g_tilemap.Get( 9, 6 ).Append( 9 );
// create another road
for( x = 0; x < (MAPWIDTH / 2); x++ )
{
g_tilemap.Get( x, 8 ).Append( 4 );
}
// add the transparent snow tiles over the grass.
for( y = 0; y < MAPHEIGHT; y++ )
{
if( y != 2 && y != 6 )
g_tilemap.Get( (MAPWIDTH/2) - 1, y ).Append( 12 );
}
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 + -