Im incrementing a value in a while loop, but its only incrementing when my mouse is moving.

Any ideas?

while (isAlwaysTrue)
    {
        value += 1;
    }

Thats basically what Im doing.

Recommended Answers

All 12 Replies

post more code

Your asking for private code. I told you what I was doing.

Im also using OpenGL.

I have a while statement that is always true, inside that while I have a float that increases by 0.4 each frame. The problem is that it only increases while my mouse is moving.

while (thatIsAlwaysTrue)
{
    value += 0.4;
}

I dont know what else you need.

Its not very accomadating you not sharing code back to us, but here's some help anyways...
Your value is being increased every frame as it is in the the same code block as your mouse detection code (guessing). Without you taking a risk and showing the code we cant help you. What about converting it to psuedo-code...?

There is no mouse detection code. It just increases when the mouse is moving. I'm not sure if that's just how GL and SDL draw frames...

Your asking for private code. I told you what I was doing.

Welp for one calm that attitude down. Second good luck with your problem then.

no attiude at all.

anyway, does anyone know?

You haven't supplied enough information to make it possible for anyone to know. Whatever the problem is, it is not in the code you have shown us. Normally these sorts of problems can only be solved by spotting the problem in code that is supplied. The only other chance is that someone else has experienced the same problem and recognizes it from your description, but you shouldn't hope for that.

So, you will probably need to find the solution yourself, and one way to do that is to create a tiny new program designed to do nothing but demonstrate your problem. Using that you can more easily conduct experiments into exactly what conditions cause the problem. If your first attempt to demonstrate the problem fails to demonstrate it then you can add features to your program one at a time until the problem appears, and then you'll have a very good idea of where the problem comes from. If your first attempt demonstrates the problem then you will have the advantage of a tiny program to study in search of the problem.

//Includes all headers needed
#include "SDL.h"
#include "SDL_opengl.h"
#include "glut.h"
#include <iostream>
#include <stdio.h>

void glutBitmapString(float x, float y, char *string) { 
    int len;
    glRasterPos2i(x,y);
    glDisable(GL_TEXTURE);
    glDisable(GL_TEXTURE_2D);

    for (int i = 0, len = strlen(string);i<len;i++) {
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, (int)string[i]);
    }
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_TEXTURE);
}

//Startpoint of Program
int SDL_main( int argc, char* args[] )
{
    //Intializes SDL
    SDL_Init( SDL_INIT_EVERYTHING );

    std::cout << "SDL is RUNNING\n";

    //Allocates RAM to OpenGL
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    //Sets Window name
    SDL_WM_SetCaption("Zaboo", NULL);

    //Sets Window Size (x, y), Color Scheme, Using OpenGL
    SDL_SetVideoMode(950, 750, 32, SDL_OPENGL);

    //Sets ClearColor, and Viewport
    glClearColor(1, 1, 1, 1);
    glViewport(0, 0, 950, 750);

    //Sets Shade Model
    glShadeModel(GL_SMOOTH);

    //Sets MatrixMode, and saves setup
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    //Disables DepthTest (used for 3D)
    glDisable(GL_DEPTH_TEST);

    std::cout << "OpenGL is RUNNING\n";

    //Handles Loop Entry
    bool isRunning = true;

    //Handles Events
    SDL_Event event;

    //Declare Variables
    int health = 100;
    float rBarX = 0;
    float FPS;
    float Timer = 0;
    bool isTrue = true;

    //Main Loop
    std::cout << "Main Loop Initialized\n";
    while (isRunning)
    {
        //EVENTS
        while (SDL_PollEvent(&event))
        {
            //Close button is pressed, close
            if (event.type == SDL_QUIT)
                {
                    isRunning = false;
                }
            //Escape key is pressed, close
            if (event.type == SDL_KEYUP && event.key .keysym .sym == SDLK_ESCAPE)
                {
                    isRunning = false;
                }
            if (event.type == SDL_KEYUP && event.key .keysym .sym == SDLK_SPACE)
                {
                    rBarX = 0;
                }
            if (event.type == SDL_KEYUP && event.key .keysym .sym == SDLK_UP && health < 100)
                {
                    health += 1;
                }
            if (event.type == SDL_KEYUP && event.key .keysym .sym == SDLK_DOWN && health > 0)
                {
                    health -= 1;
                }
            while (rBarX > 150)
                {
                    rBarX = 150;
                }
            if (isTrue)
            {
                rBarX += 0.3;
                if (rBarX += 0.3)
                {
                    Timer += 0.01;
                }
            }
        }

        //LOGIC

        //RENDER
        glClear(GL_COLOR_BUFFER_BIT);

        glPushMatrix();

        glOrtho(0, 950, 650, 0, -1, 1);

        //DRAW

        //Underlay
        glColor4ub(75,75,75,255);
        glBegin(GL_QUADS);
        glVertex2f(0, 550);
        glVertex2f(0, 650);
        glVertex2f(950, 650);
        glVertex2f(950, 550);
        glEnd();
        //Underlay End

        //Underlay Line
        glColor4ub(0,0,0,255);
        glBegin(GL_LINES);
        //First
        glVertex2f(0, 551);
        glVertex2f(0, 650);
        //Second
        glVertex2f(0, 650);
        glVertex2f(950, 650);
        //Third
        glVertex2f(950, 650);
        glVertex2f(950, 551);
        //Fourth
        glVertex2f(950, 551);
        glVertex2f(-1, 551);
        glEnd();

        glBegin(GL_LINES);
        //First
        glVertex2f(1, 552);
        glVertex2f(1, 649);
        //Second
        glVertex2f(1, 649);
        glVertex2f(949, 649);
        //Third
        glVertex2f(949, 649);
        glVertex2f(949, 552);
        //Fourth
        glVertex2f(949, 552);
        glVertex2f(1, 552);
        glEnd();
        //Underlay Line End

        //Underlay Text

        //Underlay Text End

        //Power Bar
        glColor4ub(255,100,0,255);
        glBegin(GL_QUADS);
        glVertex2f(775, 610);
        glVertex2f(775, 635);
        glColor4ub(255,200,0,255);
        glVertex2f(775+rBarX, 635);
        glVertex2f(775+rBarX, 610);
        glEnd();
        //Power Bar End

        //Power Bar Line
        glColor4ub(0,0,0,255);
        glBegin(GL_LINES);
        //First
        glVertex2f(775, 610);
        glVertex2f(775, 635);
        //Second
        glVertex2f(775, 635);
        glVertex2f(925, 635);
        //Third
        glVertex2f(925, 635);
        glVertex2f(925, 610);
        //Fourth
        glVertex2f(925, 610);
        glVertex2f(775, 610);
        glEnd();

        glBegin(GL_LINES);
        //First
        glVertex2f(774, 609);
        glVertex2f(774, 637);
        //Second
        glVertex2f(774, 636);
        glVertex2f(926, 636);
        //Third
        glVertex2f(926, 636);
        glVertex2f(926, 609);
        //Fourth
        glVertex2f(926, 609);
        glVertex2f(774, 609);
        glEnd();
        //Power Bar Line End

        //Selection Bar
        glColor4ub(200,200,200,255);
        glBegin(GL_QUADS);
        glVertex2f(15, 565);
        glVertex2f(15, 635);
        glVertex2f(450, 635);
        glVertex2f(450, 565);
        glEnd();
        //Selection Bar End

        //Selection Bar Line
        glColor4ub(0,0,0,255);
        glBegin(GL_LINES);
        //First
        glVertex2f(15,565);
        glVertex2f(15,635);
        //Second
        glVertex2f(15,635);
        glVertex2f(450,635);
        //Third
        glVertex2f(450,635);
        glVertex2f(450,565);
        //Fourth
        glVertex2f(450,565);
        glVertex2f(14,565);
        glEnd();

        glBegin(GL_LINES);
        //First
        glVertex2f(14,565);
        glVertex2f(14,636);
        //Second
        glVertex2f(14,636);
        glVertex2f(451,636);
        //Third
        glVertex2f(451,636);
        glVertex2f(451,564);
        //Fourth
        glVertex2f(451,565.5);
        glVertex2f(14,565.5);
        glEnd();
        //Selection Bar Line End

        //Health Bar
        glColor4ub(0,255,0,255);
        glBegin(GL_QUADS);
        glVertex2f(725, 575);
        glVertex2f(725, 585);
        glVertex2f(health*2+725, 585);
        glVertex2f(health*2+725, 575);
        glEnd();
        //Health Bar End

        //Health Bar Line
        glColor4ub(0,0,0,255);
        glBegin(GL_LINES);
        //First
        glVertex2f(725, 575);
        glVertex2f(725, 585);
        //Second
        glVertex2f(725, 585);
        glVertex2f(925, 585);
        //Third
        glVertex2f(925, 585);
        glVertex2f(925, 575);
        //Fourth
        glVertex2f(925, 575);
        glVertex2f(725, 575);
        glEnd();

        glBegin(GL_LINES);
        //First
        glVertex2f(724, 574);
        glVertex2f(724, 585.5);
        //Second
        glVertex2f(724, 585.5);
        glVertex2f(926, 585.5);
        //Third
        glVertex2f(926, 585.5);
        glVertex2f(926, 574);
        //Fourth
        glVertex2f(926, 574);
        glVertex2f(724, 574);
        glEnd();
        //Health Bar Line End

        //Mana Bar
        glColor4ub(0,0,255,255);
        glBegin(GL_QUADS);
        glVertex2f(725, 590);
        glVertex2f(725, 600);
        glVertex2f(health*2+725, 600);
        glVertex2f(health*2+725, 590);
        glEnd();
        //Mana Bar End

        //Text
        glColor4ub(255,255,255,255);
        glutBitmapString(718,570,"Health");
        glutBitmapString(718,628,"Power");
        char buffer[256];
        sprintf(buffer,"%f", Timer);
        glColor4ub(0,0,0,255);
        glutBitmapString(15,15,buffer);
        //Text End

        //END DRAW

        glPopMatrix();

        SDL_GL_SwapBuffers();

        //Helps with frame rate
        SDL_Delay(1);
    }

    //Stops SDL
    SDL_Quit();

    //Exits Program
    return 0;    
}

Theres ALL the code.

Is Timer += 0.01 the thing you are having the problem with? Nothing in the latest code you posted looks like what you posted in the first post, but that's the closest that I can find. Of course, SDL_PollEvent(&event) is pretty far from isAlwaysTrue. You'd be in big trouble if it were always true, because then you'd never get to do your drawing since the loop would go on forever.

Naturally that loop only happens when there are events waiting, so it nicely explains why you were complaining about the value only being incremented when the mouse was moved. I have a hard time guessing what you are trying to do with this timer. It doesn't seem much like a "timer" as I understand the word.

Also, this is a very strange loop:

while (rBarX > 150)
{
    rBarX = 150;
}
if (isTrue)
    {
        rBarX += 0.3;
    }

thats where its only working when the mouse is moving, and the while loops all work fine. The timer just shows how long the program has been running.

I worry that you may not know the difference between while and if since you seem to be using them interchangeably. I just want to be sure that you realize they are very different, please don't take offense if you already know that.

Timer is oddly named because it doesn't measure time. It counts how long the program has been running by the number of events that have been handled, with each event being counted as 0.01, except in the unlikely case that rBarX == 0.0. By the way, it is enormously strange to test if(rBarX += 0.3). Perhaps you meant if(rBarX >= 0.3).

The point is that everything in the while(SDL_PollEvent(&event)) loop will happen once for each event and not happen again until there is another event. This is why these things only happen when events are being generated, such as moving the mouse. I can't imagine how that behaviour could be anything but a design choice. Otherwise, why would they be in that loop?

Maybe what you really intend is for there to be a timer event every 0.01 seconds and instead of if(isTrue) you want to test that the current event is your timer event.

No offense taken, I do.

I never even realized it was in the event handler. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.