Limit gameplay speed on different computers

I am creating a 2D game using OpenGL and C ++.

I want the game to work at the same speed on different computers. At the moment, my game runs faster on my desktop than my laptop (i.e. my player moves faster on my desktop)

I was told about QueryPerformanceCounter (), but I don't know how to use this.

how can i use it or is there a better / easier way?

My display function

void display()                                  
{
static long timestamp = clock();
// First frame will have zero delta, but this is just an example.
float delta = (float)(clock() - timestamp) / CLOCKS_PER_SEC;

glClear(GL_COLOR_BUFFER_BIT);

glLoadIdentity();

createBackground();

int curSpeed = (player.getVelocity()/player.getMaxSpeed())*100;

glColor3f(1.0,0.0,0.0);
glRasterPos2i(-screenWidth+20,screenHeight-50);
glPrint("Speed: %i",curSpeed);

glRasterPos2i(screenWidth-200,screenHeight-50);
glPrint("Lives: %i",lives);

glRasterPos2i(screenWidth-800,screenHeight-50);
glPrint("Heading: %f",player.getHeading());

for(int i = 0;i<90;i++){
    if (numBullets[i].fireStatus == true){
        numBullets[i].updatePosition(player);
        if (numBullets[i].getXPos() > screenWidth || numBullets[i].getXPos() < -screenWidth || numBullets[i].getYPos() > screenHeight || numBullets[i].getYPos() < -screenHeight ){
            numBullets[i].fireStatus = false;
            numBullets[i].reset(player);
            numBullets[i].~Bullet();
        }
    }
}

player.updatePosition(playerTex,delta);

glFlush();

timestamp = clock();

}

My positiom method for updating

void Player::updatePosition(GLuint playerTex, float factor){
//draw triangle
glPushMatrix();

glEnable(GL_TEXTURE_2D);    
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, playerTex);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

glTranslatef(factor*XPos, factor*YPos, 0.0);
glRotatef(heading, 0,0,1);
glColor3f(1.0,0.0,0.0);
    glBegin(GL_POLYGON);
        glTexCoord2f(0.0, 1.0); glVertex2f(-40,40);
        glTexCoord2f(0.0, 0.0); glVertex2f(-40,-40);
        glTexCoord2f(1.0, 0.0); glVertex2f(40,-40);
        glTexCoord2f(1.0, 1.0); glVertex2f(40,40);
    glEnd();

glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glPopMatrix();

XPos += speed*cos((90+heading)*(PI/180.0f));
YPos += speed*sin((90+heading)*(PI/180.0f));
}
+3
source share
3 answers

, , , . . , clock() ( <ctime>), .

:

void main_loop() {
   static long timestamp = clock();
   // First frame will have zero delta, but this is just an example.
   float delta = (float)(clock() - timestamp) / CLOCKS_PER_SEC;

   calculate_physics(delta);
   render();

   timestamp = clock();
}

void calculate_physics(float delta) {
   // Calculate expected displacement per second.
   applyDisplacement(displacement * delta);
}

void render() {
   // Do rendering.
}

EDIT: , . Windows QueryPerformanceCounter().

#include <windows.h>

void main_loop(double delta) {
  // ...
}

int main() {
  LARGE_INTEGER freq, last, current;
  double delta;
  QueryPerformanceFrequency(&freq);

  QueryPerformanceCounter(&last);
  while (1) {
    QueryPerformanceCounter(&current);
    delta = (double)(current.QuadPart - last.QuadPart) / (double)freq.QuadPart;

    main_loop(delta);

    last = current;
  }
}
+9

. , , , (1/fps). , , FPS.

.

+4

10 ( - ), . " ", . ( ).

Basically (in psudoe code), what you would do is something like:

accumulatedTimeSinceLastUpdate = 0;
while(gameIsRunning)
{
  accumulatedTimeSinceLastUpdate += timeSinceLastFrame();
  while(accumulatedTimeSinceLastUpdate >= 10) // or another value
  {
    updatePositions();
    accumulatedTimeSinceLastUpdate -= 10;
  }
  display();
}

This means that if a super-duper runs on your computer, then a quick scan () will be called many times and from time to time updatePositions (). If your computer is very slow, updatePositions can be called several times for each time display ().

Here's another good reading (in addition to Mason Blair):

0
source

Source: https://habr.com/ru/post/1733689/


All Articles