It’s a simple opengl/sdl program. Generally theres a huge deque in which the program holds the display data for example a triangle will be inserted / readed in/from the dequeue like this ( GL_Begin …. Vertice 1 …. Vertice 2 … Vertice 3 … GL_End )
The Item struct
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;
Now the issue. If i use CC2D(move_id,(mouse_x-mouse_xpre),(mouse_y-mouse_ypre)); it works fine but if I add the CC2D function into the Event one the algorithm fails and instead of the Item moving with the mouse it moves away from the screen.
In other words.
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;
}
^ Works just fine.
But
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
}
with
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;
}
does not …
To further simplify things:
Case SDL_MOUSEMOTION: ...... code ..... CC2D( params) ........
works fine but
Event(){ CC2D( params ) } // Main.cpp
Case SDL_MOUSEMOTION: ...... code ..... (*Event)() ........ // GEngine.cpp
fails to work as intended
You are declaring local variables here inside
PollEvents:Notice that they are named
mouse_xpreandmouse_ypre. Then in the callback you are doingwhich accesses member variables. You need to remove the declaration of the local variables so that they don’t hide the member variables and you’ll be using member variables in both places.