Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 4 of 4 FirstFirst ... 234
Results 31 to 35 of 35

Thread: delay function

  1. #31
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,926
    Quote Originally Posted by pbivens View Post
    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.
    Code :
    void delay(int v)
    {
    	board[0][0] = 1;
    	glutPostRedisplay();
    	glutTimerFunc(1000, delay, 1);
    }
    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:
    Code :
    #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 state 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:
    Code :
    #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.

  2. #32
    Junior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    148
    thanks for the help, I will work on my code more. is there any tutorials I can look at that will help?

  3. #33
    Junior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    148
    well I got my code to blink off and on one of my rectangles, I want it to blink on and off only once.

  4. #34
    Junior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    148
    well I finally solved my square problems. thanks for all the help

  5. #35
    Junior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    148
    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •