From cecfe55ba5a289a1cf71584994dbfee9ec7227c2 Mon Sep 17 00:00:00 2001 From: End Date: Wed, 14 Sep 2016 04:14:39 +0200 Subject: [PATCH] + added readme * fixed timebased movement & maxfps --- KKK/README.md | 4 ++++ KKK/game/include/config.h | 36 ++++++++++++++++++++++++++++++++++ KKK/game/include/opengl.h | 41 +++++++++++++++++++++++++++++++++++++-- KKK/game/src/config.cpp | 2 ++ KKK/game/src/main.cpp | 1 + KKK/game/src/opengl.cpp | 35 +++++++++++++++++++++++++-------- 6 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 KKK/README.md create mode 100644 KKK/game/include/config.h create mode 100644 KKK/game/src/config.cpp diff --git a/KKK/README.md b/KKK/README.md new file mode 100644 index 0000000..1b13b96 --- /dev/null +++ b/KKK/README.md @@ -0,0 +1,4 @@ +Klassische Kepler Kriege +=== + +Game based on NewtonWars (https://github.com/Draradech/NewtonWars), rewritten in C++ with some more fancy OpenGL shitz. diff --git a/KKK/game/include/config.h b/KKK/game/include/config.h new file mode 100644 index 0000000..e1ea4ba --- /dev/null +++ b/KKK/game/include/config.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include + +struct Config { + int maxPlayers; + int numPlanets; + int maxSegments; + int segmentSteps; + int numShots; + int fastmode; + int fullscreen; + int timeout; + int margintop; + int marginleft; + int marginright; + int marginbottom; + double playerSize; + int energy; + int realtime; + int debug; + double battlefieldW; + double battlefieldH; + int throttle; + char* ip; + char* message; + int pot; + int area; +}; + +class ConfigParser{ + private: + protected: + public: +} diff --git a/KKK/game/include/opengl.h b/KKK/game/include/opengl.h index 97da407..2114682 100644 --- a/KKK/game/include/opengl.h +++ b/KKK/game/include/opengl.h @@ -11,6 +11,7 @@ namespace endofthejedi { class GLWindow { private: + //X-related stuff Display* m_display; Window m_rootwnd; GLint m_attributes[23] = @@ -35,25 +36,44 @@ class GLWindow { GLXContext m_glcontext; XWindowAttributes m_gwa; Atom m_atomWmDeleteWindow; + //End of X related stuff unsigned int m_width; unsigned int m_height; + + //mainloop condition bool m_running = false; + double m_fps; + //if maxfps = 0 there's no fps limit + double m_maxfps; + + void handleevents(); + protected: - virtual void handle(XEvent event); + //ancestors shall override these methods virtual void init(); + //called by mainloop periodically + //time shall be used for timebased movement virtual void render(double time); + //called by handle on window resize virtual void resize(); + //the ancestor should call the handle function in this class, too, + //as it handles the close calls & resizing + virtual void handle(XEvent event); public: + //initializes the X Window & creates an OpenGL context GLWindow(unsigned int width, unsigned int height); ~GLWindow(); + //mainloop does event handling & calls render/swap void loop(); - void stop(); void swap(); + //stops the mainloop by setting m_running false + void stop(); + //getters unsigned int getheight() const { return m_height; } @@ -61,6 +81,23 @@ class GLWindow { unsigned int getwidth() const { return m_width; } + + double get_maxfps() const { + return m_maxfps; + } + + double get_fps() const { + return m_fps; + } + + bool get_running() const { + return m_running; + } + + //setters + void set_maxfps(const double maxfps) { + m_maxfps = maxfps; + } }; } diff --git a/KKK/game/src/config.cpp b/KKK/game/src/config.cpp new file mode 100644 index 0000000..003f3c5 --- /dev/null +++ b/KKK/game/src/config.cpp @@ -0,0 +1,2 @@ +#include + diff --git a/KKK/game/src/main.cpp b/KKK/game/src/main.cpp index 31641a2..495c739 100644 --- a/KKK/game/src/main.cpp +++ b/KKK/game/src/main.cpp @@ -31,6 +31,7 @@ public: int main(int argc, const char *argv[]) { MainWindow window(500, 500); + window.setmaxfps(60); window.loop(); window.stop(); } diff --git a/KKK/game/src/opengl.cpp b/KKK/game/src/opengl.cpp index 97734cc..5c8e1bd 100644 --- a/KKK/game/src/opengl.cpp +++ b/KKK/game/src/opengl.cpp @@ -2,6 +2,7 @@ #include #include +#include #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, ¤t); + 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, ¤t); delta = diff(prev, current).tv_nsec; prev = current; + if(delta > 0.0) { + m_fps = (1000000000.0/delta); + std::cout << m_fps << "\n"; + } } }