📄 gd03-01.cpp
字号:
g_array1.Resize( g_array2.Size() );
for( index = 0; index < g_array1.Size(); index++ )
g_array1[index] = g_array2[index];
g_time = SDL_GetTicks();
break;
case WAITFORDONE:
// set the next state
g_state = NOSTATE;
// show all the normal buttons.
g_gui->GetItem( 0 )->SetVisibility( true );
g_gui->GetItem( 1 )->SetVisibility( true );
g_gui->GetItem( 2 )->SetVisibility( true );
g_gui->GetItem( 3 )->SetVisibility( true );
// hide 'continue' button.
g_gui->GetItem( 5 )->SetVisibility( false );
// show instructions.
g_gui->GetItem( 9 )->SetVisibility( true );
g_gui->GetItem( 10 )->SetVisibility( true );
// hide other labels
g_gui->GetItem( 6 )->SetVisibility( false );
g_gui->GetItem( 7 )->SetVisibility( false );
g_gui->GetItem( 8 )->SetVisibility( false );
g_gui->GetItem( 11 )->SetVisibility( false );
g_gui->GetItem( 12 )->SetVisibility( false );
g_gui->GetItem( 13 )->SetVisibility( false );
break;
case WAITFORMOVEUP:
// set next state
g_state = MOVEITEMSUP;
g_index = g_array1.Size() - 1;
// hide 'continue' button.
g_gui->GetItem( 5 )->SetVisibility( false );
g_time = SDL_GetTicks();
break;
case WAITFORINSERT:
// insert a random value in the array
g_state = WAITFORDONE;
g_array1[g_current] = rand() % 100;
break;
case WAITFORMOVEDOWN:
// set next state
g_state = MOVEITEMSDOWN;
g_index = g_current - 1;
// hide 'continue' button.
g_gui->GetItem( 5 )->SetVisibility( false );
g_time = SDL_GetTicks();
break;
}
};
// ----------------------------------------------------------------------------
int main( int argc, char* argv[] )
{
int x;
int y;
srand( time(0) );
//initialize systems
SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER );
SDL_WM_SetCaption( "Array Graphical Demonstration", 0);
TTF_Init();
g_gui = new SDLGUI( WIDTH, HEIGHT, ITEMS, WHITE );
g_gui->SetFont( "arial.ttf", ARIAL, 26, TTF_STYLE_NORMAL );
g_gui->AddButton( 0, 0, "gbup.bmp", "gbdown.bmp", "increase",
ARIAL, BLACK, WHITE, Inc );
g_gui->AddButton( 128, 0, "gbup.bmp", "gbdown.bmp", "decrease",
ARIAL, BLACK, WHITE, Dec );
g_gui->AddButton( 256, 0, "gbup.bmp", "gbdown.bmp", "insert",
ARIAL, BLACK, WHITE, Ins );
g_gui->AddButton( 385, 0, "gbup.bmp", "gbdown.bmp", "remove",
ARIAL, BLACK, WHITE, Rem );
// due to stupidity on my part, this button is here only to
// serve as a placeholder. It is never used. Ignore it.
// pretend that it doesn't exist.
g_gui->AddButton( 512, 0, "gbup.bmp", "gbdown.bmp", "fremove",
ARIAL, BLACK, WHITE, 0 );
g_gui->AddButton( 0, 64, "gbup.bmp", "gbdown.bmp", "continue",
ARIAL, BLACK, WHITE, Continue );
g_gui->GetItem( 4 )->SetVisibility( false );
g_gui->GetItem( 5 )->SetVisibility( false );
g_gui->AddLabel( 0, 128, "First, you need to create a new array.",
ARIAL, BLACK, WHITE );
g_gui->AddLabel( 0, 128, "Then copy everything over to the new array.",
ARIAL, BLACK, WHITE );
g_gui->AddLabel( 0, 128, "Lastly, you delete the old array.",
ARIAL, BLACK, WHITE );
g_gui->AddLabel( 0, 64, "Click on a cell to select it.",
ARIAL, BLACK, WHITE );
g_gui->AddLabel( 0, 96, "Press 'r' to put a random number in the current cell.",
ARIAL, BLACK, WHITE );
g_gui->AddLabel( 0, 120, "First, you need to move everything up one cell.",
ARIAL, BLACK, WHITE );
g_gui->AddLabel( 0, 120, "Then you insert the new item",
ARIAL, BLACK, WHITE );
g_gui->AddLabel( 0, 120, "Move everything down one cell.",
ARIAL, BLACK, WHITE );
g_gui->GetItem( 6 )->SetVisibility( false );
g_gui->GetItem( 7 )->SetVisibility( false );
g_gui->GetItem( 8 )->SetVisibility( false );
g_gui->GetItem( 11 )->SetVisibility( false );
g_gui->GetItem( 12 )->SetVisibility( false );
g_gui->GetItem( 13 )->SetVisibility( false );
// set our at exit function
atexit( SDL_Quit ) ;
// fill the arrray with numbers.
int index;
for( index = 0; index < 10; index ++ )
g_array1[index] = rand() % 100;
// set our at exit function
atexit( SDL_Quit ) ;
// create the BMP's that we need.
g_box = SDL_LoadBMP( "box.bmp" );
g_redbox = SDL_LoadBMP( "redbox.bmp" );
g_varrow = SDL_LoadBMP( "varrow.bmp" );
g_rarrow = SDL_LoadBMP( "grarrow.bmp" );
g_larrow = SDL_LoadBMP( "glarrow.bmp" );
// set the color keys of the BMPs
SDL_SetColorKey( g_box, SDL_SRCCOLORKEY,
SDL_MapRGB( g_box->format, 255, 255, 255 ));
SDL_SetColorKey( g_redbox, SDL_SRCCOLORKEY,
SDL_MapRGB( g_redbox->format, 255, 255, 255 ));
SDL_SetColorKey( g_rarrow, SDL_SRCCOLORKEY,
SDL_MapRGB( g_rarrow->format, 255, 255, 255 ));
SDL_SetColorKey( g_larrow, SDL_SRCCOLORKEY,
SDL_MapRGB( g_larrow->format, 255, 255, 255 ));
// declare event variable
SDL_Event event;
g_gui->Draw();
DrawArray( g_array1, 20, 200, g_current, true );
g_gui->Update();
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 );
// check to see if user clicked on a cell
if( g_state == NOSTATE )
{
if( y >= 200 &&
y < 248 &&
x >= 20 &&
x < (48 * g_array1.Size() + 20) )
{
g_current = (x - 20) / 48;
}
}
}
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 );
if( event.key.keysym.sym == SDLK_r && g_state == NOSTATE )
{
if( g_current >= 0 && g_current < g_array1.Size() )
g_array1[g_current] = rand() % 100;
}
}
}
g_gui->Draw();
if( g_state != DELETEARRAY )
DrawArray( g_array1, 20, 200, g_current, true );
if( g_state == SHOWNEWARRAY || g_state == FILLARRAY ||
g_state == WAITFORDELETE )
DrawArray( g_array2, 20, 400, -1, true );
if( g_state == FILLARRAY || g_state == WAITFORDELETE )
DrawArrows( 20, 250, g_index );
if( g_state == DELETEARRAY )
DrawArray( g_array1, 20, g_index, g_current, true );
if( g_state == MOVEITEMSUP || g_state == WAITFORINSERT )
DrawRArrows( 20, 152, g_index, g_array1.Size() - 2 );
if( g_state == MOVEITEMSDOWN )
DrawLArrows( 20, 152, g_current, g_index );
g_gui->Update();
switch( g_state )
{
case FILLARRAY:
g_index = (SDL_GetTicks() - g_time) / 600;
if( g_index >= g_min )
{
// fillindex went too far, set it to the max value.
g_index = g_min - 1;
// go to next state.
g_state = WAITFORDELETE;
g_gui->GetItem( 5 )->SetVisibility( true );
// fill array, in case the animation was choppy
// and the array didn't get filled.
for( index = 0; index < g_min; index++ )
g_array2[index] = g_array1[index];
}
else
{
// fill in the array up to fillindex
for( index = g_index; index >= 0; index-- )
g_array2[index] = g_array1[index];
}
break;
case DELETEARRAY:
g_index = 400 - ((SDL_GetTicks() - g_time) / 10);
if( g_index < 200 )
{
g_index = 200;
g_state = WAITFORDONE;
g_gui->GetItem( 5 )->SetVisibility( true );
}
break;
case MOVEITEMSUP:
if( (SDL_GetTicks() - g_time) > 750 )
{
g_index--;
if( g_index < g_current )
{
g_state = WAITFORINSERT;
g_index = g_current;
g_gui->GetItem( 5 )->SetVisibility( true );
g_gui->GetItem( 11 )->SetVisibility( false );
g_gui->GetItem( 12 )->SetVisibility( true );
}
else
{
g_array1[g_index + 1] = g_array1[g_index];
g_time = SDL_GetTicks();
}
}
break;
case MOVEITEMSDOWN:
if( (SDL_GetTicks() - g_time) > 750 )
{
g_index++;
if( g_index == g_array1.Size() - 1 )
{
g_state = WAITFORDONE;
g_index = g_array1.Size() - 2;
g_gui->GetItem( 5 )->SetVisibility( true );
g_gui->GetItem( 13 )->SetVisibility( false );
}
else
{
g_array1[g_index] = g_array1[g_index + 1];
g_time = SDL_GetTicks();
}
}
break;
}
}
//done
return( 0 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -