Archive Pages Design$type=blogging

How to display Timer in OpenGL Projects

This tutorial will demonstrate how to have timer in the project of computer graphics using OpenGL. Follow the simple steps and get you time...

This tutorial will demonstrate how to have timer in the project of computer graphics using OpenGL. Follow the simple steps and get you timer in the projects.

To have timer in the program, OpenGL had provide a library function. This library function is "glutTimerFunc" which need to be called to have the timer displayed. So to have this function work we will define and declare a function called myTimer, whose code is like-

void myTimer(int value) {

if(j==0)
{
g_counter = value + 1;
}
else if(j==1)
{
g_counter = value + 0;
}
else
{
g_counter = 0;
}
glutPostRedisplay();
glutTimerFunc(GAP, myTimer, g_counter);
}

You must have g_counter variable declared before you define this function like this. The g_counter, must be declared as a static variable. So you use it like static int g_counter = 0; where it has been initialized to 0 at first to start from 0, if you wish you can set the other time. Also have another variable J, this too must be static go as static int j=0; also initialized to 0.
There are three if else statements, first it check whether the timer is at initial position if it is, then value of   g_counter would be incremented by 1. Further second if, check that timer has been paused as value of j changes to 1, then no increment in g_counter  and last one rest the g_counter value to 0.



glutTimerFunc take three parameter of which first is the value in millisecond here it determines to have per second value. Second his the function called to process the timer, third and the last the value that determine the timer's value.

Now we will desgin a keyboard function that will cause the timer to start, stop and reset. The code for that is below -


void myKey(unsigned char k, int x, int y)
{
switch (k) {
case 's': if(j==0)j=1; else j=0; break;
case 'r': j=2; break;
case 'q':
exit(0);
break;
}
}

From the code most of the things is clear. As User press the 's' key the timer will stop first and if 's' was pressed again, the timer start from where it stopped. The logic is simple, first time when key 's' is pressed the value of j changes to 1 and it changes to 0 when press again, the  value of j is checked in myTimer function and g_counter is incremented accordingly. The  'r' is used to reset the value of the timer to 0, while 'q' will quit the program.

Download the simple timer program from here.

COMMENTS

Name

3D Bi-Cycle 3D Game 3D Laptop 3D Objects 3D Projects 3D Zoo Algorithm Android Aquarium Bellman-Ford Algorithm Block Breaker Blockshooting Bucket Sort C# Chess Clock Code Blocks Colors Complex Project Complex Projects Crab Pong Cross Zero DSAV Escapa First Come First Serve Fluids Font Demo Games Gear Motion GlutDino Glutplane Graphics Editor iOS iPhone Java Java OpenGL Graphics Programming Light Torus Linked List Ludo Board Game Mancala board games Memory blocks game Memory Game Menu Mickey Mouse Minesweeper Miniature Steam Engine moth Motion Blur MoveLight moving car Moving Ship Multiplex Networking Based Project Nuclear Power Plant Olympic opengl c++ examples OpenGL ES OpenGL Tutorial Origami OS Based Projects Paint Paper Folding Particles Drop Path Finding ping pong Pong Game Project Report Projects Report Puzzles qix like Random Flowing lines Rigid Body Rings Robot sample c++ opengl code Screen Saver Screen Saver Ship Shadow Cube Shadowfun Ship Iceberg Simple Drawing Simple Haloed Lined Wireframe Simple Move Light Simple Project Simulation SNAKE XENZIA GAME Solar system Sorting Sphere Spot Light Swing Stars Taj Mahal The Edge Game Tic Tac Toe Tower of Hanoi Traffic Signal Transformation Triangular Animation Tutorial water ripple effects Whirlpool
false
ltr
item
OpenGL Projects: How to display Timer in OpenGL Projects
How to display Timer in OpenGL Projects
OpenGL Projects
http://www.openglprojects.in/2012/08/how-to-display-timer-in-opengl-projects.html
http://www.openglprojects.in/
http://www.openglprojects.in/
http://www.openglprojects.in/2012/08/how-to-display-timer-in-opengl-projects.html
true
2020194800836807725
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago