fernialex
fernialex

Reputation: 1

SDL2 crash whit InputHandler mouse motion

No warnings, no errors. Compile fine, but in the bottom of update function, the following lines make crash the program:

(*m_mousePosition).setX(event.motion.x);
(*m_mousePosition).setY(event.motion.y);

This is the complete code:

#include <SDL2/SDL.h>
#include <vector>
#include "Vector2D.h"

using namespace std;

#ifndef INPUTHANDLER_H
#define INPUTHANDLER_H

enum mouse_buttons{
   LEFT = 0,
   MIDDLE = 1,
   RIGHT = 2
};

class InputHandler{

public:

    static InputHandler* Instance(){
        if(s_pInstance==0){
            s_pInstance= new InputHandler();
        }
        return s_pInstance;
    }

    void update(){
        if(event.type == SDL_MOUSEBUTTONDOWN){
            if(event.button.button == SDL_BUTTON_LEFT){
                m_mouseButtonStates[LEFT]=true;
            }
            if(event.button.button == SDL_BUTTON_MIDDLE){
                m_mouseButtonStates[MIDDLE]=true;
            }
            if(event.button.button == SDL_BUTTON_RIGHT){
                m_mouseButtonStates[RIGHT]=true;
            }
        }
        if(event.type == SDL_MOUSEBUTTONUP){
            if(event.button.button == SDL_BUTTON_LEFT){
                m_mouseButtonStates[LEFT]=false;
            }
            if(event.button.button == SDL_BUTTON_MIDDLE){
                m_mouseButtonStates[MIDDLE]=false;
            }
            if(event.button.button == SDL_BUTTON_RIGHT){
                m_mouseButtonStates[RIGHT]=false;
            }
        }
        if(event.type == SDL_MOUSEMOTION){
            (*m_mousePosition).setX(event.motion.x);
            (*m_mousePosition).setY(event.motion.y);
        }
    }

    SDL_Event* getEvent(){
        return &event;
    }

    void clear();


    bool getMouseButtonStates(int buttonNumber){
        return m_mouseButtonStates[buttonNumber];
    }

    Vector2D* getMousePosition(){
        return m_mousePosition;
    }

private:

    SDL_Event event;

    vector<bool> m_mouseButtonStates;

    Vector2D* m_mousePosition;

    InputHandler(){
        for(int i=0; i<3; i++){
            m_mouseButtonStates.push_back(false);
        }
    }
    ~InputHandler(){}

    static InputHandler* s_pInstance;

};

typedef InputHandler TheInputHandler;

#endif  /* INPUTHANDLER_H */

Maybe its a problem whit pointers, but I don't know, I can't solve

==============================NEW EDIT======================================

Now I don't use pointers, but I must initialize the Vector2D whit two parametres. I put it in the InputHandler constructor:

InputHandler::InputHandler():m_position(0,0){
    for(int i=0; i<3; i++){
        m_mouseButtonStates.push_back(false);
    }
}

And give me the following error:

InputHandler.cpp:12:30: error: member initializer 'm_position' does not name a non-static data member or base class
InputHandler::InputHandler():m_position(0,0){
                             ^~~~~~~~~~~~~~~

The definition of Vector2D class:

#include<math.h>

using namespace std;

#ifndef VECTOR2D_H
#define VECTOR2D_H

class Vector2D {
public:
    Vector2D(float x, float y):m_x(x), m_y(y){}

    float getX(){return m_x;}
    float getY(){return m_y;}

    void setX(float x){m_x=x;}
    void setY(float y){m_y=y;}

    float length(){return sqrt(m_x*m_x + m_y*m_y);}

private:

    float m_x;
    float m_y;

};

#endif  /* VECTOR2D_H */

Upvotes: 0

Views: 412

Answers (1)

Brad Allred
Brad Allred

Reputation: 7534

yes, the problem is "pointers" as you put it.

m_mousePosition is declared but you never assign anything to it so it is NULL/garbage. you probably dont need to use pointers here.

Upvotes: 1

Related Questions