Hi,
I am trying to test this timer movement and I just don't understand why the rectanglee is not moving right and down. Here is a simple test. Using GLFW.
#include <GL/glfw.h>
#include <stdlib.h>
void Init()
{
//! Create the viewport and projection
glClearColor(0.4f,0.4f,0.4f,1.0f);
glViewport(0, 0, 800, 600);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,800, 600,0,0,5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void CreateRect(int x, int y)
{
glBegin(GL_QUADS);
glVertex3f(x, y-32, 0);
glVertex3f(x+32, y-32, 0);
glVertex3f(x+32, y, 0);
glVertex3f(x, y, 0);
glEnd();
}
int main( void )
{
//! Initialize GLFW
if( !glfwInit() )
{
exit( EXIT_FAILURE );
}
//! Open an OpenGL window
glfwOpenWindowHint( GLFW_WINDOW_NO_RESIZE, GL_TRUE );
if( !glfwOpenWindow( 800,600, 0,0,0,0,0,0, GLFW_WINDOW ) )
{
glfwTerminate();
exit( EXIT_FAILURE );
}
Init();
int X_Pos = 256;
int Y_Pos = 256;
double StartTime = glfwGetTime();
int MOVEMENT_SPEED = 2;
int running = GL_TRUE;
//! Main loop
while( running )
{
//! OpenGL rendering goes here...
//! OpenGL rendering goes here...
glClear( GL_COLOR_BUFFER_BIT );
//! Swap front and back rendering buffers
CreateRect(X_Pos, Y_Pos);
glfwSwapBuffers();
//! Timer movement
double NewTime = glfwGetTime();
const float ElapsedTime = (float)(NewTime - StartTime) / 1000.f;
StartTime = NewTime;
int New_X = X_Pos;
int New_Y = Y_Pos;
//!Up
if(glfwGetKey('W'))
{
New_Y -= MOVEMENT_SPEED * ElapsedTime;
}
//!Down - not moving
if(glfwGetKey('S'))
{
New_Y += MOVEMENT_SPEED * ElapsedTime;
}
//!Left
if(glfwGetKey('A'))
{
New_X -= MOVEMENT_SPEED * ElapsedTime;
}
//!Right - not moving
if(glfwGetKey('D'))
{
New_X += MOVEMENT_SPEED * ElapsedTime;
}
X_Pos = New_X;
Y_Pos = New_Y;
//! Check if ESC key was pressed or window was closed
running = !glfwGetKey( GLFW_KEY_ESC ) && glfwGetWindowParam( GLFW_OPENED );
}
//! Close window and terminate GLFW
glfwTerminate();
//! Exit program
exit( EXIT_SUCCESS );
}
Thanks in advance,
Regards.