📄 g13-01.cpp
字号:
// ============================================================================
// G13-01.cpp
// Storing Resources Using Binary Search Trees
// ============================================================================
#include "SDLGUI.h"
#include "BinarySearchTree.h"
#include <stdlib.h>
#include <string.h>
// ============================================================================
// Global Constants
// ============================================================================
const char PROGRAM_NAME[] = "Game Demo 13-01: Resource Demo Revisited";
const int WIDTH = 800;
const int HEIGHT = 600;
const int ITEMS = 32;
const int ARIAL = 0;
// ============================================================================
// Classes
// ============================================================================
class Resource
{
public:
char m_string[64];
SDL_Surface* m_surface;
};
// ============================================================================
// Resource Compare'er
// ============================================================================
int ResourceCompare( Resource p_left, Resource p_right )
{
return strcmp( p_left.m_string, p_right.m_string );
}
// ============================================================================
// Global Variables
// ============================================================================
SDLGUI* g_gui;
// The Resource hash table:
BinarySearchTree< Resource > g_tree( ResourceCompare );
// the name of the current resource
char g_name[16] = "";
SDL_Surface* g_resource = 0;
// ============================================================================
// Functions
// ============================================================================
void Find()
{
// create a resource with the contents of the textbox
Resource res;
strcpy( res.m_string, g_name );
BinaryTree<Resource>* node = 0;
// search for the resource
node = g_tree.Find( res );
if( node != 0 )
g_resource = node->m_data.m_surface;
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 );
Resource res;
// add the resources to the tree.
res.m_surface = SDL_LoadBMP( "sky.bmp" );
strcpy( res.m_string, "sky" );
g_tree.Insert( res );
res.m_surface = SDL_LoadBMP( "water.bmp" );
strcpy( res.m_string, "water" );
g_tree.Insert( res );
res.m_surface = SDL_LoadBMP( "fire.bmp" );
strcpy( res.m_string, "fire" );
g_tree.Insert( res );
res.m_surface = SDL_LoadBMP( "water2.bmp" );
strcpy( res.m_string, "water2" );
g_tree.Insert( res );
res.m_surface = SDL_LoadBMP( "snow.bmp" );
strcpy( res.m_string, "snow" );
g_tree.Insert( res );
res.m_surface = SDL_LoadBMP( "vortex.bmp" );
strcpy( res.m_string, "vortex" );
g_tree.Insert( res );
res.m_surface = SDL_LoadBMP( "stone.bmp" );
strcpy( res.m_string, "stone" );
g_tree.Insert( res );
// 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 + -