📄 g20-01.cpp
字号:
// ============================================================================
// G20-01.cpp
// Sorting Depth Demo
// ============================================================================
#include "SDL.h"
#include "SDLHelpers.h"
#include "sorts.h"
#include <stdlib.h>
#include <time.h>
// ============================================================================
// Global Constants
// ============================================================================
const char PROGRAM_NAME[] = "Game Demo 20-01: Sorting Depth Demo";
const int WIDTH = 640;
const int HEIGHT = 480;
const int PLAYERS = 24;
// ============================================================================
// Classes
// ============================================================================
class Player
{
public:
int type;
float x;
float y;
};
// ============================================================================
// Global Variables
// ============================================================================
// this is the main window for the framework
SDL_Surface* g_window = 0;
// an array of players
Array<Player> g_players( PLAYERS );
// an array of player pointers, sorted.
Array<Player*> g_sortedplayers( PLAYERS );
SDL_Surface* g_bmps[4];
SDL_Surface* g_grass;
int g_timer;
// the players position
int g_dx = 0;
int g_dy = 0;
// ============================================================================
// Functions
// ============================================================================
// compare the y values only.
int CompareY( Player* l, Player* r )
{
if( l->y < r->y )
return -1;
if( l->y > r->y )
return 1;
return 0;
}
void DrawMap()
{
int x, y;
for( y = 0; y < HEIGHT; y += 64 )
{
for( x = 0; x < WIDTH; x += 64 )
{
SDLBlit( g_grass, g_window, x, y );
}
}
}
void DrawPlayers()
{
int index;
Player* p;
for( index = 0; index < PLAYERS; index++ )
{
p = g_sortedplayers[index];
SDLBlit( g_bmps[p->type], g_window, p->x, p->y - g_bmps[p->type]->h );
}
}
void InitPlayers()
{
int index;
// give the players a random tyoe, and position.
for( index = 1; index < PLAYERS; index++ )
{
g_players[index].type = rand() % 3 + 1;
g_players[index].x = rand() % WIDTH - 64;
g_players[index].y = rand() % HEIGHT + 64;
}
// make every pointer in the sorted array point to a player
for( index = 0; index < PLAYERS; index++ )
{
g_sortedplayers[index] = &(g_players[index]);
}
}
int main( int argc, char* argv[] )
{
// declare coordinates.
int dt;
srand( time(0) );
g_players[0].type = 0;
g_players[0].x = 320;
g_players[0].y = 240;
InitPlayers();
// 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_bmps[0] = SDL_LoadBMP( "hero2.bmp" );
g_bmps[1] = SDL_LoadBMP( "monster1.bmp" );
g_bmps[2] = SDL_LoadBMP( "monster2.bmp" );
g_bmps[3] = SDL_LoadBMP( "tree.bmp" );
g_grass = SDL_LoadBMP( "grass1.bmp" );
SDL_SetColorKey( g_bmps[0], SDL_SRCCOLORKEY,
SDL_MapRGB( g_bmps[0]->format, 255, 0, 255 ) );
SDL_SetColorKey( g_bmps[1], SDL_SRCCOLORKEY,
SDL_MapRGB( g_bmps[1]->format, 255, 255, 255 ) );
SDL_SetColorKey( g_bmps[2], SDL_SRCCOLORKEY,
SDL_MapRGB( g_bmps[2]->format, 255, 255, 255 ) );
SDL_SetColorKey( g_bmps[3], SDL_SRCCOLORKEY,
SDL_MapRGB( g_bmps[3]->format, 255, 255, 255 ) );
g_timer = SDL_GetTicks();
while( 1 )
{
//look for an event
if( SDL_PollEvent( &event ) )
{
//an event was found
if( event.type == SDL_QUIT )
break;
if( event.type == SDL_KEYUP )
{
if( event.key.keysym.sym == SDLK_LEFT )
{
g_dx += 1;
}
if( event.key.keysym.sym == SDLK_RIGHT )
{
g_dx -= 1;
}
if( event.key.keysym.sym == SDLK_UP )
{
g_dy += 1;
}
if( event.key.keysym.sym == SDLK_DOWN )
{
g_dy -= 1;
}
}
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_LEFT )
{
g_dx -= 1;
}
if( event.key.keysym.sym == SDLK_RIGHT )
{
g_dx += 1;
}
if( event.key.keysym.sym == SDLK_UP )
{
g_dy -= 1;
}
if( event.key.keysym.sym == SDLK_DOWN )
{
g_dy += 1;
}
}
} // end event loop.
// calculate the new position of the player
dt = SDL_GetTicks() - g_timer;
g_timer = SDL_GetTicks();
g_players[0].x += ((float)(dt * 128) / 1000.0f) * g_dx;
g_players[0].y += ((float)(dt * 128) / 1000.0f) * g_dy;
// clear the screen to white.
SDL_FillRect( g_window, NULL, SDL_MapRGB( g_window->format, 0, 0, 0 ) );
// draw the map
DrawMap();
// sort the players
QuickSort( g_sortedplayers, 0, PLAYERS, CompareY );
// draw the players
DrawPlayers();
// 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 + -