2014-12-08 184 views
0

我寫了一個模擬選擇排序操作的程序。我添加了函數myKeyboard退出程序,但由於使用函數sleep()模仿動畫,myKeyboard僅在排序完成後才起作用。有什麼方法可以替換函數sleep(),以便我可以在排序過程中退出程序?這是代碼:GLUT鍵盤事件

#include <stdio.h> 
#include <math.h> 
#include <GL/glut.h> 

using namespace std; 

#define N 40 

void run(void); 
void display(int unused); 
void selectionSort(int a[]); 
void swap (int a[], int x, int y); 
void mixArray (int a[]); 
void myKeyboard(unsigned char key, int x, int y); 
int i; 
int T[N]; 


int main(int argc, char* argv[]){ 

glutInit(&argc,argv); 
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 
glutInitWindowSize(800, 800); 
glutCreateWindow("Selection sort"); 
glEnable(GL_DEPTH_TEST); 
glutKeyboardFunc(myKeyboard); 
for (i = 0; i < N; i++) T[i] = i; 
mixArray(T); 
glutDisplayFunc(run); 
glutTimerFunc(200,display,0); 
glutMainLoop(); 
return 0; 

} 


void drawFigure(float x, float dx, float y, float dy, float z, float dz) 
{ 
glBegin(GL_POLYGON); 

glColor3f(1.0f, 1.0f, 0.0f); 

glVertex3f( x + dx, y, z); 
glVertex3f( x + dx, y + dy, z); 
glVertex3f(x, y + dy, z); 
glVertex3f(x, y, z); 

glEnd(); 


glBegin(GL_POLYGON); 
glColor3f( 1.0f, 1.0f, 1.0f); 

glVertex3f( x + dx, y, z + dz); 
glVertex3f( x + dx, y + dy, z + dz); 
glVertex3f(x, y + dy, z + dz); 
glVertex3f(x, y, z + dz); 
glEnd(); 


glBegin(GL_POLYGON); 
glColor3f( 0.7f, 0.3f, 0.8f); 

glVertex3f(x + dx, y, z); 
glVertex3f(x + dx, y + dy, z); 
glVertex3f(x + dx, y + dy, z + dz); 
glVertex3f(x + dx, y, z + dz); 
glEnd(); 


glBegin(GL_POLYGON); 
glColor3f( 0.0f, 1.0f, 0.0f); 


glVertex3f(x, y, z + dz); 
glVertex3f(x, y + dy, z + dz); 
glVertex3f(x, y + dy, z); 
glVertex3f(x, y, z); 
glEnd(); 


glBegin(GL_POLYGON); 
glColor3f(0.0f, 0.0f, 1.0f); 

glVertex3f( x + dx, y + dy, z + dz); 
glVertex3f( x + dx, y + dy, z); 
glVertex3f(x, y + dy, z); 
glVertex3f(x, y + dy, z + dz); 
glEnd(); 


glBegin(GL_POLYGON); 
glColor3f(1.0f, 0.0f, 0.0f); 

glVertex3f(x + dx, y, z); 
glVertex3f(x + dx, y, z + dz); 
glVertex3f(x, y, z + dz); 
glVertex3f(x, y, z); 
glEnd(); 
} 


void display(int unused) 
{ 
glLoadIdentity(); 

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 

for (int i = 0; i < N; i++) 
{ 
    glRotatef(-0.4f, 1.0f, 0.0f, 0.0f); 
    glRotatef(0.1f, 0.0f, 1.0f, 0.0f); 
    drawFigure(-1.0f + i * 1.9f/N, 1.0f/N, -0.5f, float(T[i])/N, -0.8f, 0.1f); 

} 

glFlush(); 
glutSwapBuffers(); 
} 



void run() 
{ 
glClear(GL_COLOR_BUFFER_BIT); 
glColor3f(0.5f, 0.2f, 0.7f); 
selectionSort(T); 
} 


void selectionSort(int T[]) 
{ 
int minVal; 
for (int i = 0; i < N; i++) 
{ 
    minVal = i; 
    for (int j = i+1; j < N; j++) 
    { 
     if (T[j] < T[minVal]) 
     { 
      minVal = j; 
     } 
    } 

    swap(T, i, minVal); 
    display(minVal); 
} 

} 


void swap (int T[], int a, int b) 
{ 
int tmp = T[a]; 
T[a] = T[b]; 
T[b] = tmp; 
} 

void mixArray(int T[]) 
{ 
for (int i = 0; i < N; i++) 
{ 
    swap(T, rand() % N, rand() % N); 
} 
} 

void myKeyboard(unsigned char key, int x, int y) 
{ 
if(key == 27) 
{ 
    exit(EXIT_SUCCESS); 
} 
} 

回答

2

相反的運行不大不小的glutDisplayFunc內可以使用glutTimerFunc建立一個函數被調用每200ms做排序的一步。這將允許在兩個步驟之間處理鍵盤事件。

+0

感謝您的回答。我試着用函數glutTimerFunc來做,現在我更新了代碼。我刪除了睡眠,並設置了顯示,在一步完成後稱爲200毫秒,但它會立即進行排序。 – 2014-12-08 17:21:33

+0

您的'run'函數仍然會一次完成整理。每次調用定時器函數時,您都需要執行排序算法的一個步驟。 – 2014-12-08 18:37:23