delay function

well I tried the glutPostRedisplay but it made the buttons to not blink on and off. they just stayed static.

If you want the button state to change over time, you need to start a timer, and have the timer callback change the state and call glutPostRedisplay().

I finally solved my problem, thanks for all the help.

now I have to work on the AI for my game. I have to develop the computer player for my simon game. can I get a hint on how to proceed further.

well I am now working on the computer player in my game. right now one of my squares is dimmed, I want it to turn on and off and on again.


void draw_button()
{
	if (board[0][0] == 1)
	{
		glColor3f(0.5f, 0.5f, 0.0f);
		glRectf(-90.0f, 90.0f, -10.0f, 10.0f);
		glutTimerFunc(1000, delay_one, 0);
		board[0][0] = 0;
	}
	if (board[0][1] == 1)
	{
		glColor3f(0.5f, 0.0f, 0.0f);
		glRectf(10.0f, 90.0f, 90.0f, 10.0f);
		glutTimerFunc(1000, delay_one, 0);
		board[0][1] = 0;
	}
	if (board[1][0] == 1)
	{
		glColor3f(0.0f, 0.5f, 0.0f);
		glRectf(-90.0f, -10.0f, -10.0f, -90.0f);
		glutTimerFunc(1000, delay_one, 0);
		board[1][0] = 0;
	}
	if (board[1][1] == 1)
	{
		glColor3f(0.0f, 0.0f, 0.5f);
		glRectf(10.0f, -10.0f, 90.0f, -90.0f);
		glutTimerFunc(1000, delay_one, 0);
		board[1][1] = 0;
	}
}

void comp_buttons()
{
	srand(time(NULL));

//	int num = rand() % 4 + 1;
	
	int num = 1;

	if (num == 1)
	{
		board[0][0] = 1;
	}

I will work on my game some more. I have resolved my previous problem so I will eventually resolve this problem as well.


	int num = 2;

	if (num == 1)
	{
		board[0][0] = 1;
	}
	if (num == 2)
	{
		board[0][1] = 1;
		glColor3f(0.5f, 0.0f, 0.0f);
		glRectf(10.0f, 90.0f, 90.0f, 10.0f);
		glutTimerFunc(1000, delay_one, 0);
		glColor3f(1.0f, 0.0f, 0.0f);
		glRectf(10.0f, 90.0f, 90.0f, 10.0f);
		board[0][1] = 0;
	}

I am still trying to get a rectangle to dim and then undim its color.I am using glutTimerFunc to wait a small period of time. I am also thinking about using glutIdleFunc to wait a period of time.

[QUOTE=pbivens;1290312]


	if (num == 2)
	{
		board[0][1] = 1;
		glColor3f(0.5f, 0.0f, 0.0f);
		glRectf(10.0f, 90.0f, 90.0f, 10.0f);
		glutTimerFunc(1000, delay_one, 0);
		glColor3f(1.0f, 0.0f, 0.0f);
		glRectf(10.0f, 90.0f, 90.0f, 10.0f);
		board[0][1] = 0;
	}

I am still trying to get a rectangle to dim and then undim its color.I am using glutTimerFunc to wait a small period of time. I am also thinking about using glutIdleFunc to wait a period of time.[/QUOTE]
glutTimerFunc() isn’t a “wait” function. It registers a callback which will be executed at a given point in the future, then returns immediately.

Your display() function should be doing something like:


	if (board[0][1] = 0)
		glColor3f(0.5f, 0.0f, 0.0f); // dim
        else
		glColor3f(1.0f, 0.0f, 0.0f); // bright
	glRectf(10.0f, 90.0f, 90.0f, 10.0f);

        // draw everything else

        glutSwapBuffers();

The mouse/keyboard callback does e.g.


		board[0][1] = 1;
		glutTimerFunc(1000, timer, 0);
		glutPostRedisplay();

The timer() function does:


		board[0][1] = 0;
		glutPostRedisplay();

To summarise:

The input event changes the button state, starts a timer, and requests a redraw (which, because of the change to the button state, will draw the button in the pressed state). When the timer expires, the timer callback will revert the button state and request a redraw (which will draw the button in the default state).

This isn’t specific to OpenGL; this is how GUI programming works in general. In particular, you cannot wait in the middle of handling an event. You have to arrange for a timer event to be generated in the future, return to allow event processing to continue, then handle the timer event when it occurs.

is there I can use as a wait function?

I have almost solved my problem. I got a rectangle to turn off by itself. I just want it turn on by itself as well.


void delay(int v)
{
	board[0][0] = 1;
	glutPostRedisplay();
	glutTimerFunc(1000, delay, 1);
}

void draw_button()
{
	if (board[0][0] == 1)
	{
		glColor3f(0.5f, 0.5f, 0.0f);
		glRectf(-90.0f, 90.0f, -10.0f, 10.0f);
		board[0][0] = 0;
	}
	if (board[0][1] == 1)
	{
		glColor3f(0.5f, 0.0f, 0.0f);
		glRectf(10.0f, 90.0f, 90.0f, 10.0f);
		board[0][1] = 0;
	}
	if (board[1][0] == 1)
	{
		glColor3f(0.0f, 0.5f, 0.0f);
		glRectf(-90.0f, -10.0f, -10.0f, -90.0f);
		board[1][0] = 0;
	}
	if (board[1][1] == 1)
	{
		glColor3f(0.0f, 0.0f, 0.5f);
		glRectf(10.0f, -10.0f, 90.0f, -90.0f);
		board[1][1] = 0;
	}
}

the problem is in the delay function.

can I please get some more help, I have almost solved my problem.

[QUOTE=pbivens;1290327]I have almost solved my problem. I got a rectangle to turn off by itself. I just want it turn on by itself as well.


void delay(int v)
{
	board[0][0] = 1;
	glutPostRedisplay();
	glutTimerFunc(1000, delay, 1);
}

[/QUOTE]
The delay() function shouldn’t be having the timer re-execute it; at least, not in its current form.

You can either have multiple functions where each sets a specific state then triggers the next function via glutTimerFunc(), or a single function which sets the next state each time it’s called. In the latter case, you need something like:


#define NUM_STATES 10 /* or whatever */
int state = 0;
void delay(int v)
{
	state++;
	glutPostRedisplay();
	if (state < NUM_STATES);
		glutTimerFunc(1000, delay, 1);
}

Then the display() function would use the value of [var]state[/var] to determine the button colours at any given time. E.g. if the intent is to light the buttons in a pre-determined sequence, one approach would be to have an array:


#define NUM_STATES 8
static const int buttons[NUM_STATES] = {1,0,2,0,3,0,4,0};
void display(void)
{
	int button = state<NUM_STATES ? buttons[state] : 0;

	if (button == 1)
		glColor3f(1.0,0,0);
	else
		glColor3f(0.5,0,0);
	glRectf( ... ); /* draw button 1 */

	if (button == 2)
	/* and so on */
}

The timer callback sets the variables to record the current state, requests a redraw, and if the sequence hasn’t finished starts a timer for the next point in the sequence. The display callback draws the “board” for the current state. The details depend upon exactly what you’re trying to achieve.

Really, most of this isn’t anything to do with OpenGL, it’s general programming concepts.

thanks for the help, I will work on my code more. is there any tutorials I can look at that will help?

well I got my code to blink off and on one of my rectangles, I want it to blink on and off only once.

well I finally solved my square problems. thanks for all the help

well I am working on my simon game, I am making the AI for my game. this problem is quite difficult, any input on how I should proceed?