Jump to content
  • Advertisement
Sign in to follow this  
garyfletcher

Mouse movement is causing SDL segmentation fault - HELP!!

This topic is 4929 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all. Am new to this forum, have been around the beginners for a while and have decided that SDL is the way to go for my 2-D stuff. To that end I've been building an engine using SDL. I got to a stage where I had TTF fonts, bitmap fonts, sprites, a game loop controlled by a state stack and the usual SDL base window functions. I decided that the only way to test it out is to write a quick game with it so I have decided to implement Cone's GFX lesson 5 (Clickomania) as a 1st attempt to try things out. I've hit a bit of a problem using the mouse. The game starts okay and draws the initial balls on the screen but whenever I move the mouse past the 1st row or column of balls I get a segmentation fault. I've been looking for AGES now but cannot work out what is going on (possible coz this is the 1st time I've tried mouse input). I've narrowed it down to the SDL_BlitSurface() function call in GameEngine::DrawIMG() but don't know why the mouse movement is having this effect. I was hoping that some of you kind (and more experienced) SDLers might take a look and see if you can see what is going on. The project is a little big for a post and can be downloaded here. And I'd be really grateful for any help. The engine itself is still missing some vital bits and pieces like proper resource management and data driven setup but it should still be able to produce this simple game (I think). The project itself is built with dev-c++ v4.9.9.2 and you will need to set up some extra directories under the project directory to hold bitmap fonts and the like: datadata\Bitmap (for bitmaps 1.bmp - 9.bmp) data\BMFont data\BMFont\font1 (for font ini, raw and data files) data\BMFont\font2 (for font ini, raw and data files) Filenames (with paths) can be found in class header files. Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Tried the debugger..had major problems with it, ie. stopping at the correct breakpoint, stepping into functions. So to answer your question, no. But that is probably to do with my lack of knowledge when it comes to the debugger.

Share this post


Link to post
Share on other sites
Yea, Dev-CPP debugger == blah [lol]. I was able to figure out the source of the problem in VS7. In your GameEngine.cpp file, take a look at the void GameEngine::drawBalls() function. If you do this:


/* Rendering functions */
/* Draw our background (The balls) */
void GameEngine::drawBalls()
{
cerr << "DrawBalls() Started" << endl;
cerr << "cols " << cols << endl;
cerr << "rows " << rows << endl;

/* Draw our balls */
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
int loc = grid(i,j);
if( loc > 9 || loc < 0 )
assert(0 && "Invalid grid location");
DrawIMG(balls[loc],j*45+centx,i*45+centy);
}
}

cerr << "DrawBalls() Done" << endl;
}







You will see that you are getting an invalid array access runtime error. At some times, loc returns a negative value, which is not good. I've split that call to grid up to the intermediate function because when dealing with arrays, it's a bad idea not to do that, hence this problem [wink].

You will have to check your grid function to make sure you are using it correctly char& grid(int a, int b){return playf[a*cols+b];}. Moreso, make sure you are always passing it valid parameters whenever it is called. I would add in an assert to make sure a >= 0 as well as b, then check the respective upper bounds of them.

Finally your mouse input is FUBAR'd. The problem is that when you do a mouse move over the screen, it takes it as if you are clicking, which is not supposed to happen. For this input, it is only supposed to call Collapse when you click on a ball. I think that is your priority number 1 now, get the input working correctly. That may be messing up the grid function and such, since everything is based on the input.

So if you have another other questions feel free to ask. If you want to use an old input class I've made, and study how that works, here it is:

class Input
{
private:
Uint8* m_keys;
int m_lmbdown;
int m_lmbclick;
int m_rmbdown;
int m_rmbclick;
int m_curkey;
int m_mx;
int m_my;

public:
Input();
~Input();
void Update();
inline void ClearKey(int key);
inline void ClearKey();
inline bool KeyDown(int key);
inline bool LMBClick() const;
inline bool RMBClick() const;
inline bool LMBDown() const;
inline bool RMBDown() const;
inline int MouseX() const;
inline int MouseY() const;
};

Input::Input()
{
m_curkey = 0;
m_lmbclick = 0;
m_rmbclick = 0;
m_mx = 0;
m_my = 0;
m_lmbdown = 0;
m_rmbdown = 0;
m_keys = 0;
}

Input::~Input()
{
}

inline bool Input::LMBClick() const
{
return (m_lmbclick == 2);
}

inline bool Input::RMBClick() const
{
return (m_rmbclick == 2);
}

inline bool Input::LMBDown() const
{
return (m_lmbdown == 1);
}

inline bool Input::RMBDown() const
{
return (m_rmbdown == 1);
}

inline int Input::MouseX() const
{
return m_mx;
}

inline int Input::MouseY() const
{
return m_my;
}

inline bool Input::KeyDown(int key)
{
m_curkey = key;
return (m_keys[key] == 1);
}

inline void Input::ClearKey()
{
if( m_curkey > 0)
{
m_keys[m_curkey] = 0;
m_curkey = 0;
}
}

inline void Input::ClearKey(int key)
{
m_keys[key] = 0;
m_curkey = 0;
}

void Input::Update()
{
if(m_lmbclick != 1)
m_lmbclick = 0;

if(m_rmbclick != 1)
m_rmbclick = 0;

m_keys = SDL_GetKeyState(NULL);

SDL_GetMouseState(&m_mx, &m_my);

if( SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT))
{
SDL_GetMouseState(&m_mx, &m_my);
m_lmbdown = 1;
m_lmbclick = 1;
}
else
{
if(m_lmbclick == 1)
m_lmbclick = 2;
m_lmbdown = 0;
}

if( SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_RIGHT))
{
SDL_GetMouseState(&m_mx, &m_my);
m_rmbdown = 1;
m_rmbclick = 1;
}
else
{
if(m_rmbclick == 1)
m_rmbclick = 2;
m_rmbdown = 0;
}
}






It should work great - I based it off of that same series of Cone3D tutorials [smile]. Give that a shot and see what you can do. You will just need to call Input::Update() every frame if you try it out. Good luck, it's nice to see you are progressing with this so well!

- Drew

[Edited by - Drew_Benton on June 16, 2005 5:28:57 PM]

Share this post


Link to post
Share on other sites
LOL....cheers Drew.

Am working on the mouse input now. Think I've got a handle on what is supposed to happen...we'll see.

Might "liberate" your class in the post if you don't mind and try to use it elsewhere in my finished engine.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!