Hello World ! I want to move a rectangle from going one point to another point in openGl. I am reaching the position but movement is so fast when I execute the program..it already in reached state. how to add delay so that I can see moving rectangle.

You can make a timer class and have it check to see if it is past your time delay between movements. If it is then add the offset then draw otherwise just draw.

I have a timer class if you would like to see/use it (its pretty basic).

Hello World ! I want to move a rectangle from going one point to another point in openGl. I am reaching the position but movement is so fast when I execute the program..it already in reached state. how to add delay so that I can see moving rectangle.

Try moving in many small steps instead of making one movement. If you are moving x1 pixels along the x axis and y1 pixels along the y axis move x1÷30 and y1÷30 pixels 30 times.

@Sfuo - Hello ! I want to see a timer class , I am just a beginner in openGL, facing difficulty while learning openGl. can you mention timer class ?

Here is the timer class.

It is pretty easy to use. You just have to initialize a timer then set the delay using setDelay(x) (where is is in milliseconds) and then use start() to start it up. Then use checkDelay() to check to see if the timer is up or not and if it is then use restart() to reset the timer so you can delay your next motion.

There are a few things in here that you might not need to use such as the setCD() function because it was made for some game using ability cooldowns.


timer.h

#ifndef TIMER_H
#define TIMER_H

class TIMER
{
    int startTicks;
    int pausedTicks;

    bool paused;
    bool started;

    int delay;
    int timeBuffer;

    public:

    TIMER();

    void start();
    void stop();
    void pause();
    void unpause();
    void restart();

    int getTicks();

    bool isStarted();
    bool isPaused();

    int  getDelay();
    void setDelay(int delay = 1000);
    void setCD(int delay = 1000);
    int getSecondsLeft();
    bool checkDelay();
};

#endif

timer.cpp

TIMER::TIMER()
{
    startTicks = 0;
    pausedTicks = 0;
    paused = false;
    started = false;
    timeBuffer = 100;
}

void TIMER::start()
{
	if(started == false){
    	started = true;
	    paused = false;
    	startTicks = clock();
	}
}

void TIMER::stop()
{
    started = false;
    paused = false;
}

void TIMER::pause()
{
    if( ( started == true ) && ( paused == false ) )
    {
        paused = true;

        pausedTicks = clock() - startTicks;
    }
}

void TIMER::unpause()
{
    if( paused == true )
    {
        paused = false;
        startTicks = clock() - pausedTicks;
        pausedTicks = 0;
    }
}

void TIMER::restart()
{
    startTicks = clock();
}

int TIMER::getTicks()
{
    if( started == true )
    {
        if( paused == true )
            return pausedTicks;
        else
            return clock() - startTicks;
    }
    return 0;
}

bool TIMER::isStarted()
{
    return started;
}

bool TIMER::isPaused()
{
    return paused;
}

void TIMER::setDelay(int newDelay)
{
    delay = newDelay;
}

void TIMER::setCD(int newDelay)
{
    delay = newDelay;
    startTicks -= delay;
}

bool TIMER::checkDelay()
{
    if(getTicks() >= delay && started == true)
		return true;
	return false;
}

int TIMER::getDelay()
{
   return delay;
}

int TIMER::getSecondsLeft(){
	if((delay - getTicks() + 1)/1000 > 0)
		return (delay - getTicks() + 1)/1000;
	else return 0;
}
Comments
Yeah ! nice ..good idea for time based game projects

@Sfuo- Nice implementation !! someday this timer class will really work for me...
specially in game projects like sudoku, shuffle or time based puzzles.

May be the my first post sentence seems confusing, I have solved my query using glutTimerFunc(). Now I can see my rectangle going from one point to another point.

Thanks SFUO and all for ur suggestions..

This article has been dead for over six months. Start a new discussion instead.