📄 gd07-02.cpp
字号:
// ============================================================================
// GD07-02.cpp
// Queue Graphical Demonstration.
// ============================================================================
#include "SDLGUI.h"
#include "Queue.h"
#include <stdlib.h>
#include <time.h>
// ============================================================================
// Global Constants
// ============================================================================
const char PROGRAM_NAME[] = "Queue Graphical Demonstration";
const int WIDTH = 800;
const int HEIGHT = 600;
const int ITEMS = 32;
const int ARIAL = 0;
// ============================================================================
// Global Variables
// ============================================================================
SDLGUI* g_gui;
// this is the stack that we will use in the demo
LQueue<int> g_queue;
// Box
SDL_Surface* g_box;
bool g_moving = false;
int g_timer;
// ============================================================================
// Draw Algorithms
// ============================================================================
void DrawQueue( LQueue<int>& p_queue,
int p_x, int p_y )
{
static SDL_Surface* text;
static char number[3] = "00";
DListIterator<int> itr = p_queue.GetIterator();
// draw the stack.
for( itr.Start(); itr.Valid(); itr.Forth() )
{
// 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 );
p_x += g_box->w;
}
}
// ============================================================================
// Button Callbacks
// ============================================================================
void Enqueue()
{
if( g_moving == false )
g_queue.Enqueue( rand() % 100 );
}
void Dequeue()
{
if( g_moving == false )
{
g_moving = true;
g_timer = SDL_GetTicks();
}
}
// ============================================================================
// Main
// ============================================================================
int main( int argc, char* argv[] )
{
int x;
int y;
int t;
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", "Enqueue",
ARIAL, BLACK, WHITE, Enqueue );
g_gui->AddButton( 0, 64, "gbup.bmp", "gbdown.bmp", "Dequeue",
ARIAL, BLACK, WHITE, Dequeue );
// 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();
t = SDL_GetTicks() - g_timer;
if( g_moving == true )
{
x = (t * 48) / 500;
DrawQueue( g_queue, -x, 276 );
}
else
{
DrawQueue( g_queue, 0, 276 );
}
if( t > 500 && g_moving == true )
{
g_queue.Dequeue();
g_moving = false;
}
g_gui->Update();
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -