5
« on: January 18, 2012, 04:30:52 pm »
I've made a rotating colored cube using OpenGL and SFML. I've made it so that the size of the cube can be changed by using the up and down arrows, and the speed of rotation can be changed by using the left and right arrows. The issue I am having is that the speed of the cube rotating is faster/slower than it should be ONLY when i'm holding the keys. Can you guys figure out what's wrong?
#include <SFML/Window.hpp>
#include <iostream>
using namespace std;
void gl_init(void);
void gl_DrawCube(float size);
void CheckEvents(void);
sf::Clock Clock;
sf::Event Event;
sf::WindowSettings Settings;
int size = 10;
float speed = 1.f;
int main(int argc, char* argv[])
{
Settings.AntialiasingLevel = 4;
sf::Window App(sf::VideoMode(800, 600, 32), "SFML OpenGL", sf::Style::Close, Settings);
App.SetFramerateLimit(90);
App.UseVerticalSync(true);
gl_init();
while (App.IsOpened())
{
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
// Escape key : exit
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
// Resize event : adjust viewport
if (Event.Type == sf::Event::Resized)
glViewport(0, 0, Event.Size.Width, Event.Size.Height);
}
if (Event.Type == sf::Event::Resized)
{
glViewport(0, 0, Event.Size.Width, Event.Size.Height);
}
App.SetActive();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef( 0.f, 0.f, -200.f);
CheckEvents();
glRotatef(speed*(Clock.GetElapsedTime() * 30), 1.f, 0.f, 0.f);
glRotatef(speed*(Clock.GetElapsedTime() * 60), 0.f, 1.f, 0.f);
glRotatef(speed*(Clock.GetElapsedTime() * 90), 0.f, 0.f, 1.f);
gl_DrawCube(size);
App.Display();
}
return EXIT_SUCCESS;
}
void gl_init(void)
{
glClearDepth(1.f);
glClearColor(0.5f, 0.25f, 0.5f, 0.f);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.f, 1.f, 1.f, 500.f);
}
void gl_DrawCube(float size)
{
glBegin(GL_QUADS);
glColor3f( 1.f, 0.f, 0.f);
glVertex3f(-size, -size, -size);
glVertex3f(-size, size, -size);
glVertex3f( size, size, -size);
glVertex3f( size, -size, -size);
glColor3f( 0.f, 1.f, 0.f);
glVertex3f(-size, -size, size);
glVertex3f(-size, size, size);
glVertex3f( size, size, size);
glVertex3f( size, -size, size);
glColor3f( 0.f, 0.f, 1.f);
glVertex3f(-size, -size, -size);
glVertex3f(-size, size, -size);
glVertex3f(-size, size, size);
glVertex3f(-size, -size, size);
glColor3f( 1.f,0.5f, 0.5f);
glVertex3f(size, -size, -size);
glVertex3f(size, size, -size);
glVertex3f(size, size, size);
glVertex3f(size, -size, size);
glColor3f( 1.f, 0.5f, 0.25f);
glVertex3f(-size, -size, size);
glVertex3f(-size, -size, -size);
glVertex3f( size, -size, -size);
glVertex3f( size, -size, size);
glColor3f( 0.5f, 0.25f, 0.f);
glVertex3f(-size, size, size);
glVertex3f(-size, size, -size);
glVertex3f( size, size, -size);
glVertex3f( size, size, size);
glEnd();
}
void CheckEvents(void)
{
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Up))
{
size = size += 1;
cout << "Size is " << size << endl;
}
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Down))
{
size = size -= 1;
cout << "Size is " << size << endl;
}
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Left))
{
speed = speed -= 0.0025f;
cout << "Speed is " << speed << endl;
}
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Right))
{
speed = speed +=0.0025f;
cout << "Speed is " << speed << endl;
}
}