📄 g05-01.cpp
字号:
// ============================================================================
// G05-01.cpp
// Basic Tilemapping Game Demo
// ============================================================================
#include "SDL.h"
#include "SDLHelpers.h"
#include "Array2D.h"
#include <stdlib.h>
// ============================================================================
// Global Constants
// ============================================================================
const char PROGRAM_NAME[] = "Game Demo 05-01: Tilemapping Demonstration";
const int WIDTH = 640;
const int HEIGHT = 480;
const int TILESIZE = 64;
const int MAPWIDTH = 16;
const int MAPHEIGHT = 16;
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<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;
// loop through every x, y
for( y = 0; y < MAPHEIGHT; y++ )
{
for( x = 0; x < MAPWIDTH; x++ )
{
// blit the tile
SDLBlit( g_tiles[g_tilemap.Get( x, y )], 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" );
// randomise the grass and snow
for( y = 0; y < MAPHEIGHT; y++ )
{
for( x = 0; x < (MAPWIDTH / 2); x++ )
{
g_tilemap.Get( x, y ) = rand() % 4;
g_tilemap.Get( x + (MAPWIDTH / 2), y ) = (rand() % 2) + 10;
}
}
// create a road
for( x = 4; x < 10; x++ )
{
g_tilemap.Get( x, 2 ) = 4;
g_tilemap.Get( x, 6 ) = 4;
}
for( y = 3; y < 7; y++ )
{
g_tilemap.Get( 4, y ) = 5;
g_tilemap.Get( 9, y ) = 5;
}
g_tilemap.Get( 4, 2 ) = 6;
g_tilemap.Get( 9, 2 ) = 7;
g_tilemap.Get( 4, 6 ) = 8;
g_tilemap.Get( 9, 6 ) = 9;
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 );
}
// change the scrolling speed if neccessary
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.
// change the scrolling speed if neccessary
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 ) );
// draw the tilemap with the global offsets.
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 + -