📄 g08-01.cpp
字号:
// ============================================================================
// G08-01.cpp
// Storing Resources Using Hash Tables
// ============================================================================
#include "SDLGUI.h"
#include "HashTable.h"
#include <stdlib.h>
#include <string.h>
// ============================================================================
// Global Constants
// ============================================================================
const char PROGRAM_NAME[] = "Game Demo 08-01: Resource Demo";
const int WIDTH = 800;
const int HEIGHT = 600;
const int ITEMS = 32;
const int ARIAL = 0;
// ============================================================================
// Classes
// ============================================================================
class String
{
public:
char m_string[64];
// empty constructor
String()
{
strcpy( m_string, "" );
}
// copy-string constructor
String( char* p_string )
{
strcpy( m_string, p_string );
}
// == operator
bool operator== ( String& p_right )
{
return !strcmp( m_string, p_right.m_string);
}
};
// ============================================================================
// String Hasher
// ============================================================================
unsigned long int StringHash( String p_string )
{
unsigned long int hash = 0;
int i;
int length = strlen( p_string.m_string );
for( i = 0; i < length; i++ )
{
hash += ( (i + 1) * p_string.m_string[i] );
}
return hash;
}
// ============================================================================
// Global Variables
// ============================================================================
SDLGUI* g_gui;
// The Resource hash table:
HashTable< String, SDL_Surface* > g_table( 7, StringHash );
char g_name[16] = "";
SDL_Surface* g_resource = 0;
// ============================================================================
// Functions
// ============================================================================
void Find()
{
// create a string with the contents of the textbox.
String str( g_name );
// create a hash entry.
HashEntry< String, SDL_Surface* >* entry;
// search for the resource
entry = g_table.Find( str );
if( entry != 0 )
g_resource = entry->m_data;
else
g_resource = 0;
}
int main( int argc, char* argv[] )
{
// declare coordinates.
int x, y;
// declare event holder
SDL_Event event;
// set our at exit function
atexit( SDL_Quit );
//initialize systems
SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER );
TTF_Init();
SDL_EnableUNICODE( true );
// create the GUI and set the caption.
g_gui = new SDLGUI( WIDTH, HEIGHT, ITEMS, WHITE );
SDL_WM_SetCaption( PROGRAM_NAME, 0);
g_gui->SetFont( "arial.ttf", ARIAL, 20, TTF_STYLE_NORMAL );
// add the resources to the hash table.
g_resource = SDL_LoadBMP( "sky.bmp" );
g_table.Insert( "sky", g_resource );
g_resource = SDL_LoadBMP( "water.bmp" );
g_table.Insert( "water", g_resource );
g_resource = SDL_LoadBMP( "fire.bmp" );
g_table.Insert( "fire", g_resource );
g_resource = SDL_LoadBMP( "water2.bmp" );
g_table.Insert( "water2", g_resource );
g_resource = SDL_LoadBMP( "snow.bmp" );
g_table.Insert( "snow", g_resource );
g_resource = SDL_LoadBMP( "vortex.bmp" );
g_table.Insert( "vortex", g_resource );
g_resource = SDL_LoadBMP( "stone.bmp" );
g_table.Insert( "stone", g_resource );
// clear the resource pointer.
g_resource = 0;
// add a text box, and set the focus.
g_gui->AddTextBox( 0, 0, 128, TTF_FontHeight( g_gui->GetFont(ARIAL)),
g_name, 15, ARIAL, BLACK, WHITE, true, Find );
g_gui->SetFocus( 0 );
// main message loop.
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 );
// tell the GUI that a button has been pressed
g_gui->MouseDown( x, y );
}
if( event.type == SDL_MOUSEBUTTONUP )
{
// get the mouse state.
SDL_GetMouseState( &x, &y );
// tell the GUI that a button has been released
g_gui->MouseUp( 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 );
}
// tell the GUI that a key was pressed.
g_gui->KeyDown( event.key.keysym.sym,
event.key.keysym.mod,
event.key.keysym.unicode );
}
} // end event loop.
// tell the GUI to redraw itself
g_gui->Draw();
if( g_resource != 0 )
g_gui->Blit( g_resource, 188, 44 );
// tell the GUI to update itself
g_gui->Update();
}
// done
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -