📄 gd20-01.cpp
字号:
// ============================================================================
// GD20-01.cpp
// Bubble Sort Demo
// ============================================================================
#include "SDLGUI.h"
#include "Queue.h"
#include <stdlib.h>
#include <time.h>
// ============================================================================
// Global Constants
// ============================================================================
const char PROGRAM_NAME[] = "Bubble Sort Graphical Demonstration";
const int WIDTH = 640;
const int HEIGHT = 480;
const int ITEMS = 32;
const int ARIAL = 0;
const int ARRAYSIZE = 128;
// holds the indexes of two items that are to be swapped.
class Swap
{
public:
int a;
int b;
};
// ============================================================================
// Global Variables
// ============================================================================
SDLGUI* g_gui;
int g_array[ARRAYSIZE];
int g_temparray[ARRAYSIZE];
SDL_Color g_colors[ARRAYSIZE];
LQueue<Swap> g_queue;
int g_timer;
bool g_sorting = false;
// ============================================================================
// Draw Algorithm
// ============================================================================
void Draw()
{
int i;
for( i = 0; i < ARRAYSIZE; i++ )
{
g_gui->Box( i * 5, HEIGHT - (g_array[i] * 2),
5, g_array[i] * 2, BLACK );
g_gui->Box( i * 5 + 1, HEIGHT - (g_array[i] * 2) + 1,
3, (g_array[i] * 2) - 2, g_colors[g_array[i]] );
}
}
// ============================================================================
// Other Algorithms
// ============================================================================
void InitColors()
{
SDL_Color c;
int i;
for( i = 0; i < ARRAYSIZE; i++ )
{
c.r = rand() % 255;
c.g = rand() % 255;
c.b = rand() % 255;
g_colors[i] = c;
}
}
void BubbleSort( int* p_array )
{
int top = ARRAYSIZE - 1;
int index;
int temp;
Swap s;
while( top != 0 )
{
for( index = 0; index < top; index++ )
{
if( p_array[index] > p_array[index + 1] )
{
s.a = index;
s.b = index + 1;
g_queue.Enqueue( s );
temp = p_array[index];
p_array[index] = p_array[index + 1];
p_array[index + 1] = temp;
}
}
top--;
}
}
// ============================================================================
// Button Callbacks
// ============================================================================
void RandomizeArray()
{
// do nothing if array is sorting.
if( g_sorting != false )
return;
int i;
for( i = 0; i < ARRAYSIZE; i++ )
{
g_array[i] = rand() % 128;
g_temparray[i] = g_array[i];
}
}
void SortArray()
{
BubbleSort( g_temparray );
g_sorting = true;
g_timer = SDL_GetTicks();
}
// ============================================================================
// Main
// ============================================================================
int main( int argc, char* argv[] )
{
int x;
int y;
srand( time(0) );
InitColors();
RandomizeArray();
//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, 18, TTF_STYLE_NORMAL );
// add the items to the g_gui
g_gui->AddButton( 0, 0, "gbup.bmp", "gbdown.bmp", "Randomize", ARIAL,
BLACK, WHITE, RandomizeArray );
g_gui->AddButton( 128, 0, "gbup.bmp", "gbdown.bmp", "Sort", ARIAL,
BLACK, WHITE, SortArray );
// set our at exit function
atexit( SDL_Quit ) ;
// 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();
if( g_queue.Count() == 0 )
g_sorting = false;
if( g_sorting == true && SDL_GetTicks() - g_timer >= 15 )
{
x = g_array[g_queue.Front().a];
g_array[g_queue.Front().a] = g_array[g_queue.Front().b];
g_array[g_queue.Front().b] = x;
g_timer = SDL_GetTicks();
g_queue.Dequeue();
}
Draw();
g_gui->Update();
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -