Ok, I've been stumped for hours.
Basically, I've got 4 dots on the screen (dot is a class). I want to make it possible to click and drag the dots, but if there is one or more dots overlapping, clicking will move them both, and they cannot be separated. Naturally, I make a pointer to the dot that's currently selected to prevent this (the pointer is declared in main()). Now, the dot class has a private attribute, which is:
Dot** ptr;
And in its constructor, the main() pointer is passed as the address like so:
main()
1 2
|
Dot* ptr = NULL;
Dot(ptr, 40, 40);
|
Dot.h
|
inline Dot(Dot* _ptr, int _x = 0, int _y = 0) {ptr = &_ptr; x = _x; y = _y;};
|
Now, what I have Dot do every step is:
If the mouse pointer is within range, and the pointer doesn't point to any other dots, set the pointer to point to
this and then move to the mouse pointers position:
1 2 3
|
if ( sel() && ptr != NULL && (*ptr) == NULL ) (*ptr) = this;
if ( ptr != NULL && (*ptr) == this) (*ptr)->setXY(mouse_x, mouse_y);
|
Now, all this works fine, until I do this:
If the mouse button is not pressed, make the pointer = NULL, a.k.a, no dot is selected.
1 2 3 4
|
if (!mouse_b & 1)
{
if (ptr != NULL && (*ptr) != NULL) free(*ptr);
}
|
This code crashes the program every time, and if I comment it out it runs, and logically the first dot clicked on should be movable, but it isn't. This is my first time using double pointers, could someone please tell me if I'm just making a stupid mistake?
Thanks very much.
EDIT: IGNORE the "if (!mouse_b & 1)". I am using Allegro, which uses such odd code.