Mam problem z wyliczaniem delty i główną pętlą gry.
Brak błędów podczas kompilacji.
Program przez kilka sekund działa w porządku, lecz po chwili w konsolce zamiast delty pojawia się NaN, czyli wynik dzielenia przez zero. Patrząc na wyjście w konsolce to się nie dzieje.
Używam bibliotek: GLFW, GLEW i OpenGL
Cały kod jest zamknięty w klasie "Core"
Oto nagłówek klasy:
#ifndef _CORE
#define _CORE
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <GL/gl.h>
class Core{
public:
Core();
~Core();
bool init(int width,int height,const char* windowTitle);
bool isRunning();
void run();
protected:
void update();
void render();
private:
GLFWwindow* window;
bool* running;
int ticks;
int frameCount;
double limitFramerate;
int desiredFPS;
double* delta;
double lastTime,nowTime,timer;
};
#endif
Oto kod pętli:
void Core::run(){
*running = true;
this->lastTime = glfwGetTime();
this->timer = lastTime;
while(*running && !glfwWindowShouldClose(window)){
this->nowTime = glfwGetTime();
*delta +=(nowTime-lastTime)/limitFramerate;
glClear(GL_COLOR_BUFFER_BIT);
while(*delta >=limitFramerate){
update();
ticks++;
*delta--;
}
render();
frameCount++;
if(glfwGetTime()-timer > 1.0){
timer++;
std::cout<<"FPS: "<<frameCount<<" Updates:"<<ticks << std::endl;
std::cout << *delta <<"="<<"("<<nowTime<<"-"<<lastTime<<")"<<"/"<<limitFramerate << std::endl;
ticks=0;frameCount=0;
lastTime=nowTime;
}
glfwSwapBuffers(window);
glfwPollEvents();
}
*running = false;
}
Także wyjście z konsoli:
Hello
FPS: 999 Updates:1
nan=(1.04363-0.0436513)/0.0166667
FPS: 1117 Updates:0
nan=(2.04386-1.04363)/0.0166667
FPS: 931 Updates:0
nan=(3.63028-2.04386)/0.0166667
FPS: 456 Updates:0
nan=(4.04367-3.63028)/0.0166667
FPS: 1086 Updates:0
nan=(5.04411-4.04367)/0.0166667
FPS: 1039 Updates:0
nan=(6.04359-5.04411)/0.0166667
FPS: 741 Updates:0
nan=(7.04381-6.04359)/0.0166667