C ++奇怪的函数指针行为

时间:2012-05-03 17:32:19

标签: c++ opengl pointers sdl

这是一个简单的opengl / sdl程序。一般来说,一个巨大的双端队列,其中程序保存显示数据,例如三角形将插入/加入出队像这样(GL_Begin .... Vertice 1 .... Vertice 2 ... Vertice 3 .. .GL_End)

Item结构

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功能添加到事件1中,算法将失败,而不是使用鼠标移动项目,它会移离屏幕。

换句话说。

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 :(得分: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));
//                                      ^^^^^^^^^^                           ^^^^^^^^^^

访问成员变量。您需要删除局部变量的声明,以便它们不隐藏成员变量,并且您将在两个位置使用成员变量。