📄 gd07-01.cpp
字号:
// ============================================================================
// GD07-01.cpp
// Stack Graphical Demonstration.
// ============================================================================
#include "SDLGUI.h"
#include "Stack.h"
#include <stdlib.h>
#include <time.h>
// ============================================================================
// Global Constants
// ============================================================================
const char PROGRAM_NAME[] = "Stack Graphical Demonstration";
const int WIDTH = 800;
const int HEIGHT = 600;
const int ITEMS = 32;
const int ARIAL = 0;
enum StackState
{
NOSTATE,
PUSHING,
POPPING
};
// ============================================================================
// Global Variables
// ============================================================================
SDLGUI* g_gui;
// this is the stack that we will use in the demo
LStack<int> g_stack;
// the timer
int g_timer;
// the number being inserted or removed from the stack
int g_number;
// Box
SDL_Surface* g_box;
StackState g_state = NOSTATE;
// ============================================================================
// Draw Algorithms
// ============================================================================
void DrawStack( LStack<int>& p_stack,
int p_x, int p_y )
{
static SDL_Surface* text;
static char number[3] = "00";
DListIterator<int> itr = p_stack.GetIterator();
// draw the stack.
for( itr.Start(); itr.Valid(); itr.Forth() )
{
p_y -= g_box->h;
// draw the box
g_gui->Blit( g_box, p_x, p_y );
// render the number.
sprintf( number, "%d", itr.Item() );
text = TTF_RenderText_Shaded( g_gui->GetFont(ARIAL), number, BLACK, WHITE );
g_gui->Blit( text, (g_box->w - text->w) / 2 + p_x,
(g_box->h - text->h) / 2 + p_y );
SDL_FreeSurface( text );
}
}
// ============================================================================
// Button Callbacks
// ============================================================================
void Push()
{
if( g_state == NOSTATE )
{
g_number = rand() % 100;
g_state = PUSHING;
g_timer = SDL_GetTicks();
}
}
void Pop()
{
if( g_state == NOSTATE )
{
g_number = g_stack.Top();
g_stack.Pop();
g_state = POPPING;
g_timer = SDL_GetTicks();
}
}
// ============================================================================
// Main
// ============================================================================
int main( int argc, char* argv[] )
{
int x;
int y;
int t;
static SDL_Surface* text;
static char number[3] = "00";
srand( time(0) );
//initialize systems
SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER );
SDL_WM_SetCaption( PROGRAM_NAME, 0);
TTF_Init();
g_gui = new SDLGUI( WIDTH, HEIGHT, ITEMS, WHITE );
g_gui->SetFont( "arial.ttf", ARIAL, 26, TTF_STYLE_NORMAL );
// add the buttons to the g_gui
g_gui->AddButton( 0, 0, "gbup.bmp", "gbdown.bmp", "Push",
ARIAL, BLACK, WHITE, Push );
g_gui->AddButton( 0, 64, "gbup.bmp", "gbdown.bmp", "Pop",
ARIAL, BLACK, WHITE, Pop );
// set our at exit function
atexit( SDL_Quit ) ;
// create the BMP's that we need.
g_box = SDL_LoadBMP( "boxblack.bmp" );
// declare event variable
SDL_Event event;
g_gui->Draw();
g_gui->Update();
// loop until we get a quit message.
while( 1 )
{
//look for an event
if( SDL_PollEvent( &event ) )
{
//an event was found
if( event.type == SDL_QUIT )
break;
// mouse button was pressed
if( event.type == SDL_MOUSEBUTTONDOWN )
{
// get the mouse g_state.
SDL_GetMouseState( &x, &y );
// tell the GUI that a button has been pressed
g_gui->MouseDown( x, y );
}
// mouse button was released
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 );
}
}
// draw the g_gui at the end of each loop.
g_gui->Draw();
// draw the "stack" outline
g_gui->Line( 344, 120, 344, 600, BLACK );
g_gui->Line( 456, 120, 456, 600, BLACK );
t = SDL_GetTicks() - g_timer;
switch( g_state )
{
case NOSTATE:
DrawStack( g_stack, 376, 120 + (g_stack.Count() * 48) );
break;
case PUSHING:
sprintf( number, "%d", g_number );
text = TTF_RenderText_Shaded( g_gui->GetFont(ARIAL), number, BLACK, WHITE );
// parabola
if( t < 500 )
{
x = 248 + ((t * 128) / 500);
t -= 250;
y = (int)(0.001024f * (float)( t * t )) + 8;
g_gui->Blit( g_box, x, y );
g_gui->Blit( text, (g_box->w - text->w) / 2 + x,
(g_box->h - text->h) / 2 + y );
DrawStack( g_stack, 376, 120 + (g_stack.Count() * 48) );
}
else
// pushing down
{
t -= 500;
y = (t * 48) / 500;
g_gui->Blit( g_box, 376, 72 + y );
g_gui->Blit( text, (g_box->w - text->w) / 2 + 376,
(g_box->h - text->h) / 2 + 72 + y );
DrawStack( g_stack, 376, 120 + (g_stack.Count() * 48) + y );
}
SDL_FreeSurface( text );
break;
case POPPING:
sprintf( number, "%d", g_number );
text = TTF_RenderText_Shaded( g_gui->GetFont(ARIAL), number, BLACK, WHITE );
// popping up
if( t < 500 )
{
y = 48 - ( (t * 48) / 500 );
g_gui->Blit( g_box, 376, 72 + y );
g_gui->Blit( text, (g_box->w - text->w) / 2 + 376,
(g_box->h - text->h) / 2 + 72 + y );
DrawStack( g_stack, 376, 120 + (g_stack.Count() * 48) + y );
}
else
// parabola
{
t -= 500;
x = 376 + ((t * 128) / 500);
t -= 250;
y = (int)(0.001024f * (float)( t * t )) + 8;
g_gui->Blit( g_box, x, y );
g_gui->Blit( text, (g_box->w - text->w) / 2 + x,
(g_box->h - text->h) / 2 + y );
DrawStack( g_stack, 376, 120 + (g_stack.Count() * 48) );
}
SDL_FreeSurface( text );
break;
}
if( g_state == PUSHING && (SDL_GetTicks() - g_timer) > 1000 )
{
g_state = NOSTATE;
g_stack.Push( g_number );
}
if( g_state == POPPING && (SDL_GetTicks() - g_timer) > 1000 )
{
g_state = NOSTATE;
}
g_gui->Update();
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -