+ added readme

* fixed timebased movement & maxfps
This commit is contained in:
End 2016-09-14 04:14:39 +02:00
parent 39af185188
commit cecfe55ba5
6 changed files with 109 additions and 10 deletions

2
KKK/game/src/config.cpp Normal file
View file

@ -0,0 +1,2 @@
#include <include/config.h>

View file

@ -31,6 +31,7 @@ public:
int main(int argc, const char *argv[]) {
MainWindow window(500, 500);
window.setmaxfps(60);
window.loop();
window.stop();
}

View file

@ -2,6 +2,7 @@
#include <iostream>
#include <sys/time.h>
#include <unistd.h>
#define UNUSED(foo) (void)foo;
@ -73,6 +74,14 @@ endofthejedi::GLWindow::~GLWindow() {
XCloseDisplay(m_display);
}
void endofthejedi::GLWindow::handleevents() {
if (XPending(m_display) > 0) {
XEvent xev;
XNextEvent(m_display, &xev);
handle(xev);
}
}
void endofthejedi::GLWindow::handle(XEvent event) {
if (event.type == Expose) {
XWindowAttributes attribs;
@ -93,25 +102,35 @@ void endofthejedi::GLWindow::swap() { glXSwapBuffers(m_display, m_window); }
void endofthejedi::GLWindow::loop() {
m_running = true;
timespec prev;
clock_gettime(CLOCK_MONOTONIC_RAW, &prev);
timespec current;
clock_gettime(CLOCK_MONOTONIC_RAW, &prev);
clock_gettime(CLOCK_MONOTONIC_RAW, &current);
double delta = 0.0;
double sleeptime = 0.0;
while (m_running) {
if (XPending(m_display) > 0) {
XEvent xev;
XNextEvent(m_display, &xev);
handle(xev);
}
handleevents();
render(delta);
swap();
if (m_maxfps > 0) {
sleeptime = ((1000000000.0/m_maxfps) - delta) + sleeptime;
if (sleeptime > 0.0) {
usleep((unsigned int)(sleeptime/1000.0));
sleeptime = sleeptime - (unsigned int)(sleeptime/1000.0);
}
}
clock_gettime(CLOCK_MONOTONIC_RAW, &current);
delta = diff(prev, current).tv_nsec;
prev = current;
if(delta > 0.0) {
m_fps = (1000000000.0/delta);
std::cout << m_fps << "\n";
}
}
}