📄 g17-02.cpp
字号:
// ============================================================================
// G17-02.cpp
// Portal Engine Graph Demo
// ============================================================================
#include "SDL.h"
#include "SDLHelpers.h"
#include "Graph.h"
#include <stdlib.h>
// ============================================================================
// Global Constants
// ============================================================================
const char PROGRAM_NAME[] = "Game Demo 17-02: Portal Engine Graph Demo";
const int WIDTH = 800;
const int HEIGHT = 600;
const int DEPTH = 3;
const int ROOMS = 16;
// ============================================================================
// Classes
// ============================================================================
class Sector
{
public:
int x;
int y;
int w;
int h;
};
// ============================================================================
// Global Variables
// ============================================================================
// this is the main window for the framework
SDL_Surface* g_window = 0;
// this is the map of sectors.
Graph<Sector, int> g_map( ROOMS );
// the starting position of the player
float g_x = 450.0f;
float g_y = 150.0f;
// the index of the sector the player is in
int g_current = 0;
// the coordinates of the player
int g_dx = 0;
int g_dy = 0;
// the timer
int g_timer;
// ============================================================================
// Functions
// ============================================================================
bool IsInSector( float p_x, float p_y, Sector p_s )
{
int x = (int)p_x;
int y = (int)p_y;
if( x >= p_s.x && x <= p_s.x + p_s.w &&
y >= p_s.y && y <= p_s.y + p_s.h )
{
return true;
}
return false;
}
void DrawMap( GraphNode<Sector, int>* p_node, int p_x, int p_y, int p_depth, SDL_Color p_col )
{
// quit out if we've surpassed the depth limit.
if( p_depth == 0 || p_node == 0 )
return;
// draw the current sector
SDLBox( g_window,
p_node->m_data.x - p_x, p_node->m_data.y - p_y,
p_node->m_data.w, p_node->m_data.h,
p_col );
// mark the sector
p_node->m_marked = true;
// loop through each connecting sector and recursively draw it.
DListIterator< GraphArc<Sector,int> > itr;
itr = p_node->m_arcList.GetIterator();
for( itr.Start(); itr.Valid(); itr.Forth() )
{
// if the sector isn't marked yet, recursively draw it.
if( itr.Item().m_node->m_marked == false )
{
DrawMap( itr.Item().m_node, p_x, p_y, p_depth - 1, p_col );
}
}
}
void InitialiseMap()
{
Sector s;
// room 0
s.x = 300;
s.y = 400;
s.w = 300;
s.h = 100;
g_map.AddNode( s, 0 );
// room 1
s.x = 370;
s.y = 500;
s.w = 30;
s.h = 100;
g_map.AddNode( s, 1 );
// room 2
s.x = 500;
g_map.AddNode( s, 2 );
// room 3
s.x = 360;
s.y = 600;
s.w = 180;
s.h = 70;
g_map.AddNode( s, 3 );
// room 4
s.x = 100;
s.y = 440;
s.w = 200;
s.h = 20;
g_map.AddNode( s, 4 );
// room 5
s.x = 600;
g_map.AddNode( s, 5 );
// room 6
s.x = 50;
s.y = 240;
s.w = 50;
s.h = 260;
g_map.AddNode( s, 6 );
// room 7
s.x = 800;
g_map.AddNode( s, 7 );
// room 8
s.x = 430;
s.y = 200;
s.w = 40;
s.h = 200;
g_map.AddNode( s, 8 );
// room 9
s.x = 50;
s.y = 200;
s.w = 150;
s.h = 40;
g_map.AddNode( s, 9 );
// room 10
s.x = 700;
g_map.AddNode( s, 10 );
// room 11
s.x = 200;
s.y = 100;
s.w = 100;
s.h = 200;
g_map.AddNode( s, 11 );
// room 12
s.x = 600;
g_map.AddNode( s, 12 );
// room 13
s.x = 300;
s.y = 130;
s.w = 100;
s.h = 30;
g_map.AddNode( s, 13 );
// room 14
s.x = 500;
g_map.AddNode( s, 14 );
// room 15
s.x = 400;
s.y = 100;
s.w = 100;
s.h = 100;
g_map.AddNode( s, 15 );
// room 1 arcs
g_map.AddArc( 0, 1, 0 );
g_map.AddArc( 0, 2, 0 );
g_map.AddArc( 0, 4, 0 );
g_map.AddArc( 0, 5, 0 );
g_map.AddArc( 0, 8, 0 );
g_map.AddArc( 1, 0, 0 );
g_map.AddArc( 1, 3, 0 );
g_map.AddArc( 2, 0, 0 );
g_map.AddArc( 2, 3, 0 );
g_map.AddArc( 3, 1, 0 );
g_map.AddArc( 3, 2, 0 );
g_map.AddArc( 4, 0, 0 );
g_map.AddArc( 4, 6, 0 );
g_map.AddArc( 5, 0, 0 );
g_map.AddArc( 5, 7, 0 );
g_map.AddArc( 6, 4, 0 );
g_map.AddArc( 6, 9, 0 );
g_map.AddArc( 7, 5, 0 );
g_map.AddArc( 7, 10, 0 );
g_map.AddArc( 8, 0, 0 );
g_map.AddArc( 8, 15, 0 );
g_map.AddArc( 9, 6, 0 );
g_map.AddArc( 9, 11, 0 );
g_map.AddArc( 10, 7, 0 );
g_map.AddArc( 10, 12, 0 );
g_map.AddArc( 11, 9, 0 );
g_map.AddArc( 11, 13, 0 );
g_map.AddArc( 12, 10, 0 );
g_map.AddArc( 12, 14, 0 );
g_map.AddArc( 13, 11, 0 );
g_map.AddArc( 13, 15, 0 );
g_map.AddArc( 14, 12, 0 );
g_map.AddArc( 14, 15, 0 );
g_map.AddArc( 15, 8, 0 );
g_map.AddArc( 15, 13, 0 );
g_map.AddArc( 15, 14, 0 );
}
int main( int argc, char* argv[] )
{
// declare coordinates.
int x, y, dt;
// declare event holder
SDL_Event event;
// initialise the map
InitialiseMap();
// 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 );
while( 1 )
{
//look for an event
if( SDL_PollEvent( &event ) )
{
//an event was found
if( event.type == SDL_QUIT )
break;
if( event.type == SDL_MOUSEBUTTONUP )
{
// get the mouse state.
SDL_GetMouseState( &x, &y );
}
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.
// clear the screen to black.
SDL_FillRect( g_window, NULL, SDL_MapRGB( g_window->format, 0, 0, 0 ) );
// calculate the amount of time that has passed since the last frame,
// and store the timer for this frame.
dt = SDL_GetTicks() - g_timer;
g_timer = SDL_GetTicks();
// move the player at a rate of 64 pixels per second
g_x += ((float)(dt * 64) / 1000.0f) * g_dx;
g_y += ((float)(dt * 64) / 1000.0f) * g_dy;
// find the current sector.
for( x = 0; x < ROOMS; x++ )
{
if( IsInSector( g_x, g_y, g_map.m_nodes[x]->m_data ) )
g_current = x;
}
// draw every sector within 100 jumps, in grey
g_map.ClearMarks();
DrawMap( g_map.m_nodes[g_current], g_x - WIDTH/2, g_y - HEIGHT/2, 100, GREY );
// draw every sector within DEPTH jumps, in white.
// these sectors are "visible".
g_map.ClearMarks();
DrawMap( g_map.m_nodes[g_current], g_x - WIDTH/2, g_y - HEIGHT/2, DEPTH, WHITE );
// draw the player
SDLBox( g_window, WIDTH/2 - 3, HEIGHT/2 - 3, 6, 6, RED );
// 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 + -