Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7894919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:30:58+00:00 2026-06-03T07:30:58+00:00

It’s a simple opengl/sdl program. Generally theres a huge deque in which the program

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:31:00+00:00Added an answer on June 3, 2026 at 7:31 am

    You are declaring local variables here inside PollEvents:

    int mouse_xpre = 0;
    int mouse_ypre = 0;
    

    Notice that they are named mouse_xpre and mouse_ypre. Then in the callback you are doing

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

    which 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.