1
\$\begingroup\$

I want to set a time limit for scrolling text in OpenGL & GLUT. How can I stop scrolling at 250? Here is my code. I try with the if condition but it's not working. The scrolling is not stopping in 250, it scrolls infinitely.

#include<fstream>
#include<iostream>
#include<stdlib.h>
#include<glut.h>

using namespace std;
float yr = 0;
float translate =0.0f, angle = 0.0f;

//identifiers
void introscreen();
void screen();

void specialKey(int key, int x, int y) {
    switch (key) {
    case GLUT_KEY_UP:
        translate += 1.0f;
        break;
    case GLUT_KEY_DOWN:
        translate -= 1.0f;
        break;
    case GLUT_KEY_LEFT:
        angle += 1.0f;
        break;
    case GLUT_KEY_RIGHT:
        angle -= 1.0f;
        break;
    }
    glutPostRedisplay();
}
void SpeedText() {

    GLfloat y;
    GLfloat y2;
    GLfloat fSize[5];
    GLfloat fCurrSize;
    fCurrSize = fSize[2];
    for (y = 0.0f; y <= 250.0f + yr; y += 5.0f) {
        glLineWidth(fCurrSize);
        glBegin(GL_LINES);
        glVertex3f(-200.0f, y + translate, 0);
        glVertex3f(-180.0f, y + translate, 0);
        glEnd();
        fCurrSize += 1.0f;
        introscreen();  
    }
    if (y + translate >= 50) {
        y == 50;
    }
    }
void renderbitmap(float x1, float y1, void *font, char *string) {
    char *c;
    glRasterPos2f(x1, y1);
    for (c=string; *c != '\0'; c++) {
        glutBitmapCharacter(font, *c);
    }
}

void introscreen(void) {
    glColor3f(0, 1, 0);
    char buf[10] = { '\0' };
    for (int row = 0.0f; row <= 250+yr; row +=5.0f) {
        sprintf_s(buf,"%d", row);
        renderbitmap(-220, (translate +row), GLUT_BITMAP_TIMES_ROMAN_24, buf);
    }
 }
int main(int arg, char** argv) {
    glutInit(&arg, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(width, height);
    glutInitWindowPosition(50, 100);
    glutCreateWindow("HUD Lines");
    display();
    SpeedText();
    glutSpecialFunc(specialKey);
    glutMainLoop();
    return 0;
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$
if (y + translate >= 50) {
    y == 50;
}

This doesn't actually do anything. y goes out of scope right after.

Instead you want to limit the value of translate.

\$\endgroup\$
3
  • \$\begingroup\$ I try but after that it's not showing anything. \$\endgroup\$
    – Unknown
    Sep 25, 2019 at 7:00
  • \$\begingroup\$ @Unknown this should also (hopefully) be a compiler warning or error, since == is comparing for equality and not an assignment. \$\endgroup\$
    – Linaith
    Sep 25, 2019 at 12:51
  • \$\begingroup\$ Nope I did not get any error or warning \$\endgroup\$
    – Unknown
    Sep 25, 2019 at 13:53

You must log in to answer this question.

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