📄 gd06-02.cpp
字号:
// increment the current index, because we inserted
// an item before the current node.
g_current++;
// reset the timer
g_time = SDL_GetTicks();
// set the state
g_state = BINSERTMOVE;
g_gui->GetItem(0)->SetVisibility( false );
g_gui->GetItem(1)->SetVisibility( false );
g_gui->GetItem(2)->SetVisibility( false );
g_gui->GetItem(3)->SetVisibility( false );
g_gui->GetItem(4)->SetVisibility( false );
g_gui->GetItem(5)->SetVisibility( false );
g_gui->GetItem(6)->SetVisibility( false );
g_gui->GetItem(8)->SetVisibility( true );
}
}
void InsertAfter()
{
if( g_list.Size() < 8 && g_itr.Valid() )
{
// insert new data.
g_list.InsertAfter( g_itr, rand() % 100 );
// reset the timer
g_time = SDL_GetTicks();
// set the state
g_state = INSERTMOVE;
g_gui->GetItem(0)->SetVisibility( false );
g_gui->GetItem(1)->SetVisibility( false );
g_gui->GetItem(2)->SetVisibility( false );
g_gui->GetItem(3)->SetVisibility( false );
g_gui->GetItem(4)->SetVisibility( false );
g_gui->GetItem(5)->SetVisibility( false );
g_gui->GetItem(6)->SetVisibility( false );
g_gui->GetItem(8)->SetVisibility( true );
}
}
void Remove()
{
if( g_list.Size() > 2 && g_itr.Valid() )
{
// reset the timer
g_time = SDL_GetTicks();
// set the state
g_state = REMOVEMOVEDOWN;
g_gui->GetItem(0)->SetVisibility( false );
g_gui->GetItem(1)->SetVisibility( false );
g_gui->GetItem(2)->SetVisibility( false );
g_gui->GetItem(3)->SetVisibility( false );
g_gui->GetItem(4)->SetVisibility( false );
g_gui->GetItem(5)->SetVisibility( false );
g_gui->GetItem(6)->SetVisibility( false );
g_gui->GetItem(10)->SetVisibility( true );
}
}
void Continue()
{
switch( g_state )
{
case INSERTNEWNODE:
g_state = INSERTPOINT;
g_time = SDL_GetTicks();
g_gui->GetItem(8)->SetVisibility( false );
g_gui->GetItem(9)->SetVisibility( true );
break;
case INSERTDONE:
g_gui->GetItem(0)->SetVisibility( true );
g_gui->GetItem(1)->SetVisibility( true );
g_gui->GetItem(2)->SetVisibility( true );
g_gui->GetItem(3)->SetVisibility( true );
g_gui->GetItem(4)->SetVisibility( true );
g_gui->GetItem(5)->SetVisibility( true );
g_gui->GetItem(6)->SetVisibility( true );
g_gui->GetItem(7)->SetVisibility( false );
g_gui->GetItem(9)->SetVisibility( false );
g_state = NOSTATE;
break;
case BINSERTNEWNODE:
g_state = BINSERTPOINT;
g_time = SDL_GetTicks();
g_gui->GetItem(8)->SetVisibility( false );
g_gui->GetItem(9)->SetVisibility( true );
break;
case BINSERTDONE:
g_gui->GetItem(0)->SetVisibility( true );
g_gui->GetItem(1)->SetVisibility( true );
g_gui->GetItem(2)->SetVisibility( true );
g_gui->GetItem(3)->SetVisibility( true );
g_gui->GetItem(4)->SetVisibility( true );
g_gui->GetItem(5)->SetVisibility( true );
g_gui->GetItem(6)->SetVisibility( true );
g_gui->GetItem(7)->SetVisibility( false );
g_gui->GetItem(9)->SetVisibility( false );
g_state = NOSTATE;
break;
case REMOVEPOINT:
g_state = REMOVEMOVE;
g_gui->GetItem(10)->SetVisibility( false );
g_gui->GetItem(11)->SetVisibility( true );
g_time = SDL_GetTicks();
break;
case REMOVEDONE:
g_gui->GetItem(0)->SetVisibility( true );
g_gui->GetItem(1)->SetVisibility( true );
g_gui->GetItem(2)->SetVisibility( true );
g_gui->GetItem(3)->SetVisibility( true );
g_gui->GetItem(4)->SetVisibility( true );
g_gui->GetItem(5)->SetVisibility( true );
g_gui->GetItem(6)->SetVisibility( true );
g_gui->GetItem(7)->SetVisibility( false );
g_gui->GetItem(11)->SetVisibility( false );
g_state = NOSTATE;
break;
}
}
// ============================================================================
// Main
// ============================================================================
int main( int argc, char* argv[] )
{
// declare coordinates.
int x, y;
srand( time( 0 ) );
// initialise the list with 5 items
for( x = 0; x < 5; x++ )
{
g_list.Append( rand() % 100 );
}
// initialise the iterator
g_itr = g_list.GetIterator();
g_itr.Start();
// 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();
// create the GUI and set the caption.
g_gui = new SDLGUI( WIDTH, HEIGHT, ITEMS, WHITE );
SDL_WM_SetCaption( PROGRAM_NAME, 0);
// add the font
g_gui->SetFont( "arial.ttf", ARIAL, 22, TTF_STYLE_NORMAL );
g_gui->SetFont( "arial.ttf", BIGARIAL, 30, TTF_STYLE_NORMAL );
// load the bmps
g_blackcircle = SDL_LoadBMP( "circleblack.bmp" );
g_redcircle = SDL_LoadBMP( "circlered.bmp" );
// set the color keys of the BMPs
SDL_SetColorKey( g_blackcircle, SDL_SRCCOLORKEY,
SDL_MapRGB( g_blackcircle->format, 255, 255, 255 ));
SDL_SetColorKey( g_redcircle, SDL_SRCCOLORKEY,
SDL_MapRGB( g_redcircle->format, 255, 255, 255 ));
// add your GUI items here.
// iterator commands
g_gui->AddButton( 72, 468, "gbup.bmp", "gbdown.bmp", "Start",
ARIAL, BLACK, WHITE, Start );
g_gui->AddButton( 200, 468, "gbup.bmp", "gbdown.bmp", "End",
ARIAL, BLACK, WHITE, End );
g_gui->AddButton( 72, 532, "gbup.bmp", "gbdown.bmp", "Back",
ARIAL, BLACK, WHITE, Back );
g_gui->AddButton( 200, 532, "gbup.bmp", "gbdown.bmp", "Forth",
ARIAL, BLACK, WHITE, Forth );
// list commands
g_gui->AddButton( 472, 468, "gbup.bmp", "gbdown.bmp", "Insert Before",
ARIAL, BLACK, WHITE, InsertBefore );
g_gui->AddButton( 600, 468, "gbup.bmp", "gbdown.bmp", "Insert After",
ARIAL, BLACK, WHITE, InsertAfter );
g_gui->AddButton( 536, 532, "gbup.bmp", "gbdown.bmp", "Remove",
ARIAL, BLACK, WHITE, Remove );
g_gui->AddButton( 336, 500, "gbup.bmp", "gbdown.bmp", "Continue",
ARIAL, BLACK, WHITE, Continue );
g_gui->AddLabel( 256, 64, "First create a new node.",
BIGARIAL, BLACK, WHITE );
g_gui->AddLabel( 226, 64, "Then re-arrange the pointers.",
BIGARIAL, BLACK, WHITE );
g_gui->AddLabel( 226, 64, "First re-arrange the pointers.",
BIGARIAL, BLACK, WHITE );
g_gui->AddLabel( 226, 64, "Then delete the node",
BIGARIAL, BLACK, WHITE );
g_gui->GetItem(7)->SetVisibility( false );
g_gui->GetItem(8)->SetVisibility( false );
g_gui->GetItem(9)->SetVisibility( false );
g_gui->GetItem(10)->SetVisibility( false );
g_gui->GetItem(11)->SetVisibility( false );
// 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();
// time-based state transitions.
if( g_state == INSERTMOVE && (SDL_GetTicks() - g_time) >= 1000 )
{
g_state = INSERTNEWNODE;
g_gui->GetItem(7)->SetVisibility( true );
}
if( g_state == BINSERTMOVE && (SDL_GetTicks() - g_time) >= 1000 )
{
g_state = BINSERTNEWNODE;
g_gui->GetItem(7)->SetVisibility( true );
}
if( g_state == INSERTPOINT && (SDL_GetTicks() - g_time) >= 1000 )
{
g_state = INSERTDONE;
}
if( g_state == BINSERTPOINT && (SDL_GetTicks() - g_time) >= 1000 )
{
g_state = BINSERTDONE;
}
if( g_state == REMOVEMOVEDOWN && (SDL_GetTicks() - g_time) >= 1000 )
{
g_state = REMOVEPOINT;
g_gui->GetItem(7)->SetVisibility( true );
}
if( g_state == REMOVEMOVE && (SDL_GetTicks() - g_time) >= 1000 )
{
g_state = REMOVEDONE;
g_list.Remove( g_itr );
g_gui->GetItem(7)->SetVisibility( true );
}
// calculate the coordinates of the list.
// first calculate the size:
if( g_state != INSERTMOVE &&
g_state != REMOVEMOVE &&
g_state != BINSERTMOVE )
{
x = g_list.Size() * g_blackcircle->w * 2;
}
else
{
if( g_state == INSERTMOVE || g_state == BINSERTMOVE )
{
x = (g_list.Size() - 1) * g_blackcircle->w * 2;
x += ( (SDL_GetTicks() - g_time) * g_blackcircle->w * 2 ) / 1000;
}
if( g_state == REMOVEMOVE )
{
x = g_list.Size() * g_blackcircle->w * 2;
x -= ( (SDL_GetTicks() - g_time) * g_blackcircle->w * 2 ) / 1000;
}
}
// now calculate x position
x = (WIDTH - x) / 2;
y = 250;
DrawList( g_list, x, y, g_current );
// tell the GUI to update itself
g_gui->Update();
}
// done
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -