 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Fri Apr 09, 2010 9:11 pm Post subject: [C++ & OpenGL] Choppy animation and slowdowns |
|
|
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 |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Fri Apr 09, 2010 10:36 pm Post subject: |
|
|
what is your framerate? is vsync on?
how are you drawing the starfield? just simple quads?
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Sat Apr 10, 2010 4:24 am Post subject: |
|
|
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 |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Sat Apr 10, 2010 11:37 am Post subject: |
|
|
| 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 |
|
 |
|
|
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
|
|