1

I have a decent implementation of the Breakout program that normally runs perfectly all the way through. I have noticed, however, that occasionally the program will freeze. After a joint effort of playing with gdb and printf, I have determined that the culprit seems to be the setLabel() function in SPL. I have no idea why...

This is the function where the freeze (sometimes) happens:

void updateScoreboard(GWindow window, GLabel label, int points)
{
    // update label
    char s[12];
    sprintf(s, "%i", points);
    printf("a%s\n", s);
    setLabel(label, s);
    printf("b%s\n", s);
}

The printf's were for figuring out where the freeze occurs. I have seen it freeze at 9 points, 21 points, 47 points, or never freeze at all. On my last attempt, I received "a21" on the console and the score label updated to 21. I did not receive "b21" on the console, which means that setLabel executed and then froze the program.

Does anyone know why this might be happening? By the way, the code below is what calls the function above:

if (strcmp(getType(contact), "GRoundRect") == 0)
        {
            velocityY = -velocityY;
            setLocation(ball, getX(ball), getY(ball) + 1);
            removeGWindow(window, contact);
            points += 1;
            bricks--;
            updateScoreboard(window, label, points);
        }

Also, I found this setLabel source code while poking around:

void GLabel::setLabel(std::string str) {
    this->str = str;
    pp->glabel_setLabel(this, str);
    GDimension size = pp->glabel_getSize(this);
    width = size.getWidth();
    height = size.getHeight();
}
0

So I moved some things around and added extra features in the function call area and I haven't had the freeze happen over many, many tests. I don't know why the order of the call matters, but at least the problem is solved. The question still remains...

For anyone having a similar issue, see below for where I changed the order in the // hits block section:

Old code

if (strcmp(getType(contact), "GRoundRect") == 0)
        {
            velocityY = -velocityY;
            setLocation(ball, getX(ball), getY(ball) + 1);
            removeGWindow(window, contact);
            points += 1;
            bricks--;
            updateScoreboard(window, label, points);
        }

New code

if (strcmp(getType(contact), "GRoundRect") == 0)
        {
            int side = collisionSide(window, ball, contact);

            if (side > 0)
            {
                velocityX = -velocityX;
            }
            else
                velocityY = -velocityY;
            setLocation(ball, getX(ball), getY(ball) + 1);
            bricks--;
            points += 1;
            updateScoreboard(window, label, points);
            removeGWindow(window, contact);
        }
| improve this answer | |
  • thank you for the solution you suggest. I have the same problem in my breakout implementation and i was looking for a solution. Could you please explain more about the collisionSide function? Is it the same as detectCollision already included in the breakout program? Thanks a lot! – user10040 Oct 29 '15 at 13:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .