1

I'm walking in some trouble with this particular piece of code. It has either a race condition or a problem with quint8.

quint8 chunk3[CHUNK_SIZE_MULT];
memset(chunk3,0x00, sizeof(chunk3));
for (int cx = 0; cx < CHUNK_SIZE; cx++)
    for (int cz = 0; cz < CHUNK_SIZE; cz++) {
        int i = cx * CHUNK_SIZE + cz;
        float abs[2];
        abs[0] = x * CHUNK_SIZE + lastPosX + cx;
        abs[1] = z * CHUNK_SIZE + lastPosZ + cz;
        int perlin = (int)(wallsPerlin->Get(abs[0], abs[1])) % 8;
        chunk3[i] = perlin > 6 ? perlin : 0;//(int)(sin(i) + 1.0 * 32.0);
    }

if I use chunk3[i] = 0 or 30 or i % 30, it will work fine. However when I introduce a sin function, rand function or perlin noise, it will throw throw memory faults. I thought it might have been the conversion from double to char, but this now seems unlikely.

Can someone clarify whether this is related to quint8 or timing?

Is use a QTimer interval to call the above code and OpenGL related functions, would it be possible that the QTimer fires an event without finishing the previous one?

If so, how could I construct my gameloop better with Qt? Using usleep?

EDIT: I see now that is not a timing issue, since leaving perlin and printing it, but setting chunk3[i] to a fixed 30 works fine.

EDIT: casting int to quint8 does not seem to fix the problem.

3
  • 2
    one thing that pops right up is that quint8 is one byte of data, and you're inserting (int) cast values into it (4 bytes). I don't think it's your problem, but I'd fix that :). also, is CHUNK_SIZE_MULT exactly CHUNK_SIZE*CHUNK_SIZE? Mar 6, 2012 at 20:53
  • I'm casting to an int first, because I thought it might've been the problem that it was casting a double to a char. But I think that's not the problem. CHUNK_SIZE_MULT is indeed CHUNK_SIZE*CHUNK_SIZE.
    – RobotRock
    Mar 6, 2012 at 22:32
  • I see now that is not a timing issue, since leaving perlin and printing it, but setting chunk3[i] to a fixed 30 works fine.
    – RobotRock
    Mar 6, 2012 at 22:42

1 Answer 1

0
        quint8 perlin = (quint8)((int)(wallsPerlin->Get(abs[0], abs[1])) % 8 + 8);

Fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.