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 1 of 4 123 ... LastLast
Results 1 to 10 of 35

Thread: delay function

  1. #1
    Junior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    148

    Question delay function

    Code :
    void delay(float secs)
    {
    	float end = clock() / CLOCKS_PER_SEC + secs;
    	while ((clock() / CLOCKS_PER_SEC) < end);
    }
    void draw_button()
    {
    	if (board[0][0] == 1)
    	{
    		glColor3f(0.5f, 0.5f, 0.0f);
    		glRectf(-90.0f, 90.0f, -10.0f, 10.0f);
    		delay(1);
    		glColor3f(1.0f, 1.0f, 0.0f);
    		glRectf(-90.0f, 90.0f, -10.0f, 10.0f);
    	}
    }
    I am drawing a square then I am drawing a dimmed square then I want to wait a second and then draw a undimmed square. well this code draws a square but then it does not draw a dimmed square or wait to draw a undimmed square.

  2. #2
    Junior Member Newbie Symbroson's Avatar
    Join Date
    Jan 2018
    Location
    Germany
    Posts
    3

    render between the two squares!

    It seems like you forgot to render the screen after drawing a square - I don't know where exactly you call SwapBuffers, but before the delay there's none - so you'll never see the first square but the second one which will compleately overdraw the first.

    I also suggest to change the Vertices of the second one so that the squares just overlap in a smaller area. Then you wouldnt need to render between them and the delay too. Depends on what you need it for
    Last edited by Symbroson; 01-06-2018 at 02:28 AM.

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,925
    Quote Originally Posted by pbivens View Post
    Code :
    		glColor3f(0.5f, 0.5f, 0.0f);
    		glRectf(-90.0f, 90.0f, -10.0f, 10.0f);
    		delay(1);
    		glColor3f(1.0f, 1.0f, 0.0f);
    		glRectf(-90.0f, 90.0f, -10.0f, 10.0f);
    This won't work, for a variety of reasons.

    If you're using GLUT, you should draw the first square and use glutTimerFunc() to register a callback which will be invoked after one second. That callback should call glutPostRedisplay(), which will cause your display function to be called. That should draw the second square. Use a variable to keep track of which version of the square the display function should draw at any given time. Also, you should call glutSwapBuffers() at the end of your display function to ensure that what you draw is actually visible on screen.

  4. #4
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,475
    pbivens, with OpenGL, in most cases you're drawing to an invisible buffer (term: "double-buffering"). Not only that, the drawing commands you call in your application aren't executed immediately; instead, they're recorded for execution later.

    So sleeping in your application is not the way to force the execution of your draw commands and to display the result on-screen.

    Instead, how you request that both of these be done is to call *SwapBuffers. If you're using GLUT, you should call glutSwapBuffers().

  5. #5
    Junior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    148
    Code :
    void delay(int v)
    {
    	glColor3f(1.0f, 1.0f, 0.0f);
    	glRectf(-90.0f, 90.0f, -10.0f, 10.0f);
    	glutPostRedisplay();
    	glutTimerFunc(1000, delay, v);
    }
    void draw_button()
    {
    	if (board[0][0] == 1)
    	{
    		delay(0);
    		glColor3f(0.5f, 0.5f, 0.0f);
    		glRectf(-90.0f, 90.0f, -10.0f, 10.0f);
    		glutSwapBuffers();
    	}
    }
    well I am working on my code as per gclements I have made some changes. but it still does not work however it does flicker when I hit the yellow square. thanks for all the help. I am close to solving this problem I just need a little help.

  6. #6
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,475
    I suggest you don't draw anything in your timer callback. Draw everything in one "draw" function based on the current program state.

    Also, you need to call glutPostRedisplay anytime you want a draw to occur. I don't know, but I suspect you want that to happen more than every 1 second (the rate that you're currently calling it, based on your timer callback interval).

    For testing, just drop a glutPostRedisplay at the end of your init() and draw() functions so that it's just called as often as possible. Then after you get things working well, you can scale back and only request a redraw when it's needed if you want.

  7. #7
    Junior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    148
    Code :
    void delay(int v)
    {
    	glColor3f(1.0f, 1.0f, 0.0f);
    	glRectf(-90.0f, 90.0f, -10.0f, 10.0f);
    	glutPostRedisplay();
    	glutTimerFunc(1000, delay, v);
    	glFlush();
    }
    void draw_button()
    {
    	if (board[0][0] == 1)
    	{
    		glColor3f(0.5f, 0.5f, 0.0f);
    		glRectf(-90.0f, 90.0f, -10.0f, 10.0f);
    		glFlush();
    	}
    }
    well I have made some progress when I hit the square it blinks on and off and it continues to do so. I took the drawing routine out of the timer but it does not work at all.

  8. #8
    Junior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    148
    well I finally solved this problem. here is the code I am using.
    Code :
    void delay(int v)
    {
    	glColor3f(1.0f, 1.0f, 0.0f);
    	glRectf(-90.0f, 90.0f, -10.0f, 10.0f);
    	glutTimerFunc(1000, delay, v);
    	glFlush();
    }
    void draw_button()
    {
    	if (board[0][0] == 1)
    	{
    		glColor3f(0.5f, 0.5f, 0.0f);
    		glRectf(-90.0f, 90.0f, -10.0f, 10.0f);
    		glFlush();
    	}
    thanks for all the help [SOLVED]

  9. #9
    Junior Member Regular Contributor
    Join Date
    Jan 2013
    Posts
    148
    I have made more progress.
    Code :
    void delay_one(int v)
    {
    	glColor3f(1.0f, 1.0f, 0.0f);
    	glRectf(-90.0f, 90.0f, -10.0f, 10.0f);
    	glutTimerFunc(1000, delay_one, v);
    	glFlush();
    }
    void delay_two(int v)
    {
    	glColor3f(1.0f, 0.0f, 0.0f);
    	glRectf(10.0f, 90.0f, 90.0f, 10.0f);
    	glutTimerFunc(1000, delay_two, v);
    	glFlush();
    }
    void delay_three(int v)
    {
    	glColor3f(0.0f, 1.0f, 0.0f);
    	glRectf(-90.0f, -10.0f, -10.0f, -90.0f);
    	glutTimerFunc(1000, delay_three, v);
    	glFlush();
    }
    void delay_four(int v)
    {
    	glColor3f(0.0f, 0.0f, 1.0f);
    	glRectf(10.0f, -10.0f, 90.0f, -90.0f);
    	glutTimerFunc(1000, delay_four, v);
    	glFlush();
    }
    void draw_button()
    {
    	if (board[0][0] == 1)
    	{
    		glColor3f(0.5f, 0.5f, 0.0f);
    		glRectf(-90.0f, 90.0f, -10.0f, 10.0f);
    		glFlush();
    	}
    	if (board[0][1] == 1)
    	{
    		glColor3f(0.5f, 0.0f, 0.0f);
    		glRectf(10.0f, 90.0f, 90.0f, 10.0f);
    		glFlush();
    	}
    	if (board[1][0] == 1)
    	{
    		glColor3f(0.0f, 0.5f, 0.0f);
    		glRectf(-90.0f, -10.0f, -10.0f, -90.0f);
    		glFlush();
    	}
    	if (board[1][1] == 1)
    	{
    		glColor3f(0.0f, 0.0f, 0.5f);
    		glRectf(10.0f, -10.0f, 90.0f, -90.0f);
    		glFlush();
    	}
    }
     
    void onMouseButton(int button, int state, int x, int y)
    {
    	if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    	{
            if (x >= 130 && x <= 370 && y <= 270 && y >= 30)
    		{
                board[0][0] = 1;
    		}
    		if (x >= 430 && x <= 670 && y <= 270 && y >= 30)
    		{
    			board[0][1] = 1;
    		}
    		if (x >= 130 && x <= 370 && y <= 570 && y >= 330)
    		{
    			board[1][0] = 1;
    		}
    		if (x >= 430 && x <= 670 && y <= 570 && y >= 330)
    		{
    			board[1][1] = 1;
    		}
    	draw_button();
    	}
    	glFlush();
    }
    I am building a simon game. However when I press a square and another square it turns both squares off and on. I want to turn on and off one square at a time.

  10. #10
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,925
    First: the only place you should be calling OpenGL functions is during initialisation and from the display callback (whether in the display function itself or in other functions called from it). Other callbacks shouldn't call OpenGL functions, directly or indirectly. The display function needs to be able to draw the scene based upon the program state. Other functions simply modify that state.

    So to change the display in response to events (keyboard, mouse, timer), you change some variables in response to those events (and if necessary, force a display update with glutPostRedisplay()). The display function just takes those variables into account when deciding what to draw.

Posting Permissions

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