2012-05-03 89 views
0

這是一個簡單的opengl/sdl程序。一般來說,一個巨大的deque,其中程序保存顯示數據,例如一個三角形將被插入/從這樣的出列中退出(GL_Begin .... Vertice 1 .... Vertice 2 ... Vertice 3 .. 。GL_End)C++奇怪的函數指針行爲

的產品結構

enum Shapes{ 
    LINE, 
    POLYGON, 
    TRIANGLE 
}; 

struct Item{ 
    int id; 
    int type; 
    Shapes shape; 
    double x; 
    double y; 
    double z; 
}; 

...

void GEngine::CC2D(int id,double x,double y){ // Change Coordinates ... Move the Item 

    for(int i=0;i<Items.size();i++){ 
     Item* item = &Items[i];     // Pointer to the item INSIDE the deque because we need to edit it, we can not create a new instance of Item 

     if(item->id == id && item->type == 2){ 
      item->x += x; 
      item->y += y; 
     } 
    } 

    DrawScene(); 
} 

void GEngine::PollEvents(void (*Event)()){ 
    int mbpressed = 0; // Mouse button pressed flag 
    int mouse_xpre = 0; 
    int mouse_ypre = 0; 
    int move_id = 0; // TESTING 
    while(1){ 
     while(SDL_PollEvent(&event)){ 

      switch(event.type){ 
       case SDL_MOUSEMOTION:{ 
        if(mbpressed == 1){ 
         mouse_x = event.motion.x; // X2 
         mouse_y = event.motion.y; // Y2 
        // (*Event)(); 

        // CC2D( ( X2 - X1 ),( Y2 - Y1 ) 
         CC2D(move_id,(mouse_x-mouse_xpre),(mouse_y-mouse_ypre));  // The difference between the current and the previous mouse position is equal to the DX and DY which will be used to move the vertices. 
         mouse_xpre = mouse_x;  // X1 
         mouse_ypre = mouse_y;  // Y1 

         SDL_Delay(20); 

        } 
        break; 

現在的問題。如果我使用CC2D(move_id,(mouse_x-mouse_xpre),(mouse_y-mouse_ypre));它工作正常,但如果我將CC2D功能添加到事件中,則算法會失敗,而不是使用鼠標移動的項目會遠離屏幕。

換句話說。

case SDL_MOUSEMOTION:{ 
         if(mbpressed == 1){ 
          mouse_x = event.motion.x; // X2 
          mouse_y = event.motion.y; // Y2 
         // (*Event)(); 

         // CC2D( ( X2 - X1 ),( Y2 - Y1 ) 
         CC2D(move_id,(mouse_x-mouse_xpre),(mouse_y-mouse_ypre));  // The difference between the current and the previous mouse position is equal to the DX and DY which will be used to move the vertices. 
         mouse_xpre = mouse_x;  // X1 
         mouse_ypre = mouse_y;  // Y1 

         SDL_Delay(20); 

        } 
        break; 
} 

^工作得很好。

GEngine gEngine; 

void Event(){ 
      gEngine.CC2D(0,(gEngine.mouse_x-gEngine.mouse_xpre),(gEngine.mouse_y-gEngine.mouse_ypre)); 
    } 

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

... code ... 

gEngine.PollEvents(&Event); 

return 0; // NEVER REACHED 
} 

case SDL_MOUSEMOTION:{ 
        if(mbpressed == 1){ 
         mouse_x = event.motion.x; // X2 
         mouse_y = event.motion.y; // Y2 
         (*Event)(); 

         mouse_xpre = mouse_x;  // X1 
         mouse_ypre = mouse_y;  // Y1 

         SDL_Delay(20); 

        } 
        break; 
} 

不...

爲了進一步簡化事情:

Case SDL_MOUSEMOTION: ...... code ..... CC2D(params) ........ 

工作正常,但

Event(){ CC2D(params) } // Main.cpp 
Case SDL_MOUSEMOTION: ...... code ..... (*Event)() ........ // GEngine.cpp 

未能如預期

+1

...... *牆上的文字* ......而不是用鼠標移動的項目移離屏幕。 :) –

+0

什麼是「(* Event)();」,它的只是僞代碼,如「...」,還是語法錯誤?記住一些cccompilers允許做一些werid的東西,而不會產生異常;-) – umlcat

+0

是的抱歉,錯過了。 @umlcat否,這是通過函數指針調用函數的有效語法。 –

回答

3

您在這裏聲明局部變量裏面PollEvents工作:

int mouse_xpre = 0; 
int mouse_ypre = 0; 

注意,它們被命名爲mouse_xpremouse_ypre。然後在回調中你正在做

gEngine.CC2D(0,(gEngine.mouse_x-gEngine.mouse_xpre),(gEngine.mouse_y-gEngine.mouse_ypre)); 
//          ^^^^^^^^^^       ^^^^^^^^^^ 

訪問成員變量。您需要刪除局部變量的聲明,以便它們不隱藏成員變量,並且您將在這兩個地方使用成員變量。

+0

謝謝!這就是不使用構造函數的代價。出於某種奇怪的原因,我決定在已經在類中定義的整數之前添加* int *。 –