Refreshing, delay & timing issue.

Jones
Unregistered
 
Post: #1
As my first simple game, I'm making an OpenGL and GLUT program where the player uses the arrow keys to move a multi-colored block around the screen avoiding another (AI) block. The bot is simple, and will increase or decrease his own x & y position in an attempt to match yours.

Basically, the necessities (like the main loop with the GLUT stuff), most of the display stuff, the screen collision and the input are done. The AI also works. Too well, that's the problem.

First, the yellow square would only move when you moved your square. I quickly realized this was because I had only placed a glutPostRedisplay(); command in my input loop and not the AI one. So I added one after all the bot if statements. The bot square simply didn't appear, so instead I put it at the end of ALL the possible bot calculations, or after the AI call in the display loop. The bot now moves much too fast because he can do so every cycle.

I could just say it's "turn based" and he only moves after you do, but that seems like the lazy man's way out. Smile

I just need some kind of timer, or perhaps I'm putting my sync/refresh commands in the wrong places?

Code:
#include <stdlib.h>
#include <GLUT/glut.h>

int boxX = 20;
int boxY = 20;

int botX = 220;
int botY = 220;

void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_SMOOTH);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    
    collision();
    
    glBegin(GL_POLYGON);
        glColor3f(1.0, 0.0, 0.0);
        glVertex3f(boxX, boxY, 0.0);
        glColor3f(0.0, 1.0, 0.0);
        glVertex3f(boxX, boxY + 20, 0.0);
        glColor3f(0.0, 0.0, 1.0);
        glVertex3f(boxX + 20, boxY + 20, 0.0);
        glVertex3f(boxX + 20, boxY, 0.0);
    glEnd();
    
    AI();
    // I COULD put the glutPostRedisplay here, or in the AI loop, but the bot goes WAY to fast.
    
    glBegin(GL_POLYGON);
        glColor3f(1.0, 1.0, 0.0);
        glVertex3f(botX, botY, 0.0);
        glVertex3f(botX, botY + 20, 0.0);
        glVertex3f(botX + 20, botY + 20, 0.0);
        glVertex3f(botX + 20, botY, 0.0);
    glEnd();
    
    glutSwapBuffers();
}


void reshape(int width, int height)
{
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, width, 0.0, height);
}

void keys(int key, int state, int x, int y)
{
    switch (key) {
        case GLUT_KEY_UP:
                boxY = boxY + 1;
                glutPostRedisplay();
            break;
        case GLUT_KEY_DOWN:
                boxY = boxY - 1;
                glutPostRedisplay();
            break;
        case GLUT_KEY_LEFT:
                boxX = boxX - 1;
                glutPostRedisplay();
            break;
        case GLUT_KEY_RIGHT:
                boxX = boxX + 1;
                glutPostRedisplay();
            break;
        default:
            break;
    }
}

void mouse(int button, int state, int x, int y)
{
    switch (button) {
        case GLUT_LEFT_BUTTON:
            if (state == GLUT_DOWN) {
            }
            break;
        case GLUT_MIDDLE_BUTTON:
            if (state == GLUT_DOWN) {
            }
            break;
        default:
            break;
    }
}

void collision()
{
    if (boxX + 20 == 250) {
        boxX = boxX - 1;
    }
    if (boxX == 0) {
        boxX = boxX + 1;
    }
    if (boxY + 20 == 250) {
        boxY = boxY - 1;
    }
    if (boxY == 0) {
        boxY = boxY + 1;
    }
}

void AI()
{
    if (botX < boxX) {
        botX = botX + 1;
    }
    if (botX > boxX) {
        botX = botX - 1;
    }
    if (botY < boxY) {
        botY = botY + 1;
    }
    if (botY > boxY) {
        botY = botY - 1;
    }
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(250, 250);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("GLUT & OpenGL program");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMouseFunc(mouse);
    glutSpecialFunc(keys);
    glutMainLoop();
    return 0;
}

EDIT: I just noticed the state aspect of the key loop, ignore it.
Quote this message in a reply
Post Reply