Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML cube rotation bug  (Read 1882 times)

0 Members and 2 Guests are viewing this topic.

imbetter911

  • Newbie
  • *
  • Posts: 5
    • View Profile
SFML cube rotation bug
« 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?

Code: [Select]

#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;
                }
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SFML cube rotation bug
« Reply #1 on: January 18, 2012, 04:44:08 pm »
Your CheckEvent function checks an event that contains random data. A sf::Event instance is only valid after an event has been caught (GetEvent returned true), so your function should be called inside the event loop.

Same for the resize event.
Laurent Gomila - SFML developer

imbetter911

  • Newbie
  • *
  • Posts: 5
    • View Profile
SFML cube rotation bug
« Reply #2 on: January 19, 2012, 05:37:18 pm »
I have placed removed the ambiguous resize check code outside the event loop and have placed my CheckEvents function inside the event loop, and the issue is still apparent. But thank you for the quick info.

 

anything