Hey,
This might not be as intensive of help as you’d like, but it should
help you out.
Your function, handle_input() gets called once for each separate
event. Mouse buttons and mouse motion are separate events, so you’re
getting a crash because ‘some_dot’ is always NULL when the mouse
motion code is run.
Make sure that ‘some_dot’ is not NULL when you use it:
if(some_dot != NULL)
{
SDL_Rect box = some_dot->GetBox();
//and make it track the mouse offset
box.x += x;
box.y += y;
}
This won’t make the code work, though. ‘some_dot’ is still always
NULL in the motion code. To change this, you need ‘some_dot’ to exist
outside of this function. That can be done using a global variable
(discouraged), a function argument, or a static variable. I suggest
you try it first with a static variable, since that is quick:
static My_Dot *some_dot = NULL;
You’re using C++, right? If you actually want the object pointed to
by ‘some_dot’ to be changed, you need to use either pointers or
references to that data. Does My_Dot::GetBox() return a reference?
The declaration would look like:
SDL_Rect& GetBox(); // Notice the &
If it does return a reference, then you need to receive it as a reference:
if(some_dot != NULL)
{
SDL_Rect& box = some_dot->GetBox(); // Use a reference variable
//and make it track the mouse offset
box.x += x; // Now some_dot’s box will actually be changed
box.y += y;
}
If it doesn’t return a reference or it stores the position data in
something other than an SDL_Rect (it is often a good idea to store it
in two floats instead), then you need to access the data another way,
like:
if(some_dot != NULL)
{
//and make it track the mouse offset
some_dot->setX(some_dot->getX() + x);
some_dot->setY(some_dot->getY() + y);
}
Good luck. Feel free to ask any more questions.
Jonny DOn Tue, Dec 8, 2009 at 10:38 AM, dekyco wrote:
Hello,
I am working on this problem of moving/dragging a sprite with a mouse, but
due to my
inexpirience with this I did not came up with workable solution yet,
although previous answer to my post was very helpful understanding the
problem.
Maybe somebody can give me some more hint on how to finish this task. Here
is my code for input handling done so far:
void My_Dot::handle_input()
{
int x = 0, y = 0;
//If a mouse button was pressed
if( event.type == SDL_MOUSEBUTTONDOWN )
{
//If the left mouse button was pressed
if( event.button.button == SDL_BUTTON_LEFT )
{
//Get the mouse offsets
x = event.button.x;
y = event.button.y;
//temporary object
My_Dot *some_dot = NULL;
//If the mouse is over the button
//mark that
d_pushed = true;
for (int i = 0;i < 3;i++) //there are 3 My_Dot objects in My_Dot *array
{
//get collission box
SDL_Rect box = array[i]->GetBox();
//if the mouse pointer is within the bounds of collission box
if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y +
box.h ) )
{
//put the array element in the temporary object
some_dot = array[i];
}
}
}
}
//If a mouse button was released
if( event.type == SDL_MOUSEBUTTONUP )
{
//If the left mouse button was released
if( event.button.button == SDL_BUTTON_LEFT )
{
//mark the left button release
d_pushed = false;
//make the object null
some_dot = NULL;
}
}
if( event.type == SDL_MOUSEMOTION && d_pushed)
{
//Get the mouse offsets
x = event.motion.x;
y = event.motion.y;
//now get the some_dot collission box
SDL_Rect box = some_dot->GetBox();
//and make it track the mouse offset
box.x += x;
box.y += y;
}
}
this code results in program segfaulting during the drag attempt.So I think
it is the SDL_MOUSEMOTION part that is not working.If I am understanding
this correctly, it is the collission box which is moving, tracking the mouse
offset.
Some portions of this code are property of ‘Lazy Foo’ Productions’ SDL
tutorial.
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org