Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++ & OpenGL] Choppy animation and slowdowns

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Fri Apr 09, 2010 9:11 pm    Post subject: [C++ & OpenGL] Choppy animation and slowdowns Reply with quote

I'm remaking a Java game I made in C++ using OpenGL and it's already annoying me. When moving the player left and right by holding the keys to move, the animation looks slow and choppy and every so often it will move a lot slower than it should.

Never really used OpenGL and C++ before so I don't know what to do to fix it.

This is what I have so far. Any ideas appreciated.

Code:
#include <GL/glut.h>
#include <vector>
#include <iostream>

// Game Object Includes
#include "StarField.h"
#include "Entity.h"

static const int WIDTH = 800, HEIGHT = 600;
static const char* windowTitle = "Geometry Invaders";

// States and stuff
bool isShooting     = false;
bool onMenuScreen   = false;
bool isPaused       = false;
bool showHelpScreen = false;
bool movingRight    = false;
bool movingLeft     = false;

// Game variables and objects
StarField starField(1500);
Entity Player1(Entity::player, 0, 0);
int playerScore = 0;

// Entity vectors
//std::vector<Shot> currentShots; // List of shots on screen
std::vector<Entity> enemies; // List of enemies on screen

// Other variables
int iter = 0; // iterator for loops
clock_t startTime, targetTime;


/* Update objects and states */
void updateScene(void) {
    startTime = clock();

    starField.draw();

    // Update player position
    if (movingRight && Player1.getX() < WIDTH-40)
        Player1.move(7, 0);
    else if (movingLeft && Player1.getX() >= 10)
        Player1.move(-7, 0);

}


/*Do all drawing here*/
void renderScene(void) {
    // Check if it's time to update
    targetTime = clock();

    if (targetTime >= startTime + (10 * CLOCKS_PER_SEC/1000)) {
        glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

        glPushMatrix();
       
        updateScene(); // Update positions and states
       
        starField.draw();
        Player1.draw();

        glPopMatrix();

        glutSwapBuffers(); // Needed!
    }
}


/* Function for handling input*/
void handleInput(unsigned char key, int x, int y) {
    switch (key) {
        case 'a':
            movingLeft = true;
        break;

        case 'd':
            movingRight = true;
        break;
    }
   
    //std::cout << Player1.getX() << std::endl;
}


/* Handle key releases */
void keyUp(unsigned char key, int x, int y) {
    switch (key) {
        case 'a':
            movingLeft = false;
        break;

        case 'd':
            movingRight = false;
        break;
    }
}


/*Main Func*/
int main(int argc, char *argv[]) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(WIDTH, HEIGHT);
    glutInitWindowPosition(50, 50);
    glutCreateWindow(windowTitle);
    glutDisplayFunc(renderScene);
    glutIdleFunc(renderScene);
    glutKeyboardFunc(handleInput);
    glutKeyboardUpFunc(keyUp);

    // Setup gl
    glViewport(0, 0, WIDTH, HEIGHT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
   
    glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glDisable(GL_DEPTH_TEST);

    // Setup game
    Player1.move(395, HEIGHT-40);   

    // Start game
    glutMainLoop();
    return 0;
}
Back to top
View user's profile Send private message MSN Messenger
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Fri Apr 09, 2010 10:36 pm    Post subject: Reply with quote

what is your framerate? is vsync on?

how are you drawing the starfield? just simple quads?
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Sat Apr 10, 2010 4:24 am    Post subject: Reply with quote

Measure the time between update calls - use this time and a velocity to move your entities.

Also, globals make baby Jesus cry.
Back to top
View user's profile Send private message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Sat Apr 10, 2010 11:37 am    Post subject: Reply with quote

slovach wrote:
what is your framerate? is vsync on?

how are you drawing the starfield? just simple quads?


Yeah, the starfield is static, just a load of quads in random areas.
I don't know if vsync is on, the ati drivers on here are a bit wierd, i'd guess not though.


Flyte wrote:
Measure the time between update calls - use this time and a velocity to move your entities.

Also, globals make baby Jesus cry.


Yeah, I've tried that. Some entities still seem to split apart and move strangely.

I'm porting this over from something I made for java 4k. The globals are only there until I get the animation working. Also, I am atheist.
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites