📄 g04-01.cpp
字号:
// ============================================================================
// g04-01.cpp
// Demonstrating the use of bitvectors in games using the Quicksave method.
// ============================================================================
#include "SDL.h"
#include "SDLHelpers.h"
#include "Bitvector.h"
#include "Array.h"
#include <stdlib.h>
#include <time.h>
// ============================================================================
// Global Constants
// ============================================================================
const char PROGRAM_NAME[] = "SAVING PLAYERS! - Game Demo 04-01";
const int WIDTH = 640;
const int HEIGHT = 480;
// ============================================================================
// The Player Class.
// ============================================================================
class Player
{
public:
int m_life;
int m_money;
int m_experience;
int m_level;
};
// ============================================================================
// Global Variables
// ============================================================================
// this is the main window for the framework
SDL_Surface* g_window = 0;
// The monster array, and the number of monsters in the game.
Array<Player> g_playerarray( 64 );
Bitvector g_modifiedstates( 64 );
// the player and box bitmaps
SDL_Surface* g_playerbmp = 0;
SDL_Surface* g_boxbmp = 0;
// ============================================================================
// Game Functions
// ============================================================================
// initialize the game
void GameInit()
{
int index;
// init each player with random values
for( index = 0; index < 64; index++ )
{
g_playerarray[index].m_life = 11 + rand() % 10;
g_playerarray[index].m_money = rand() % 100;
g_playerarray[index].m_experience = 0;
g_playerarray[index].m_level = 1 + rand() % 5;
}
// tell the game that all the players have changed
// since the last time they were saved (ie: never).
g_modifiedstates.SetAll();
}
// the "Set" functions set a variable of the player, and also
// set the modified bit for the player to true.
void SetLife( int p_player, int p_life )
{
g_playerarray[p_player].m_life = p_life;
g_modifiedstates.Set( p_player, true );
}
void SetMoney( int p_player, int p_money )
{
g_playerarray[p_player].m_money = p_money;
g_modifiedstates.Set( p_player, true );
}
void SetExperience( int p_player, int p_experience )
{
g_playerarray[p_player].m_experience = p_experience;
g_modifiedstates.Set( p_player, true );
}
void SetLevel( int p_player, int p_level )
{
g_playerarray[p_player].m_level = p_level;
g_modifiedstates.Set( p_player, true );
}
bool SavePlayers( const char* p_filename )
{
int index;
// open the file, exit if it can't be opened.
FILE* savefile = fopen( p_filename, "wb" );
if( savefile == 0 )
return false;
// loop through the states. when we find one that has
// been modified, save it to disk.
for( index = 0; index < 64; index++ )
{
if( g_modifiedstates[index] == true )
{
// skip ahead to the address of the current player.
fseek( savefile, sizeof(Player) * index, SEEK_SET );
// write the player.
fwrite( &(g_playerarray[index]), sizeof(Player), 1, savefile );
}
}
// clear the states vector, indicating that they are all
// written to disk.
g_modifiedstates.ClearAll();
// return success.
return true;
}
int main( int argc, char* argv[] )
{
// initialise the randomiser and the game.
srand( time(0) );
GameInit();
// declare coordinates.
int x, y;
int index;
// 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 player and the box:
g_playerbmp = SDL_LoadBMP( "hero1.bmp" );
SDL_SetColorKey( g_playerbmp, SDL_SRCCOLORKEY,
SDL_MapRGB( g_playerbmp->format, 255, 0, 255 ) );
g_boxbmp = SDL_LoadBMP( "box.bmp" );
SDL_SetColorKey( g_boxbmp, SDL_SRCCOLORKEY,
SDL_MapRGB( g_boxbmp->format, 255, 0, 255 ) );
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 );
// we need to figure out which player was clicked on.
index = (((y - 10) / 70) * 13) + (x - 7) / 49;
if( index < 64 )
{
// randomise the stats
SetLife( index, 11 + rand() % 10 );
SetMoney( index, rand() % 100 );
SetExperience( index, rand() % 100 );
SetLevel( index, 1 + rand() % 5 );
}
}
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_s )
{
// if 's' key was pressed, save the players.
SavePlayers( "players.sav" );
}
}
} // end event loop.
// do all game-related stuff here.
// clear the screen to white.
SDL_FillRect( g_window, NULL, SDL_MapRGB( g_window->format, 255, 255, 255 ) );
// loop through the array and draw all the players.
for( index = 0; index < 64; index++ )
{
SDLBlit( g_playerbmp, g_window,
(index % 13) * 49 + 7,
(index / 13) * 70 + 10 );
if( g_modifiedstates[index] == true )
{
SDLBlit( g_boxbmp, g_window,
(index % 13) * 49 + 5,
(index / 13) * 70 + 6 );
}
}
// 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 + -