diff --git a/src/Globals.h b/src/Globals.h index 179faf2..db09c08 100644 --- a/src/Globals.h +++ b/src/Globals.h @@ -1,6 +1,7 @@ #pragma once #include "math_types.h" +#include "Utils.h" struct Timer; extern Timer* gTimer; diff --git a/src/Utils.h b/src/Utils.h new file mode 100644 index 0000000..16f6c59 --- /dev/null +++ b/src/Utils.h @@ -0,0 +1,41 @@ +#pragma once + +#include +#include +#include +#include + +inline void gGetFileModTime (const char* filename, int *sec, int *nsec) { + struct stat attr; + + bool stat_result = stat(filename, &attr); + if (!stat_result) { + *sec = -1; + *nsec = -1; + } + + *sec = attr.st_mtime; + *nsec = attr.st_mtim.tv_nsec; +} + +inline double gGetCurrentTime () { + struct timespec spec; + clock_gettime(CLOCK_REALTIME, &spec); + + return static_cast(spec.tv_sec) + spec.tv_nsec * 1.0e-9; +} + +extern double gTimeAtStart; + +inline double gGetTimeSinceStart () { + return gGetCurrentTime() - gTimeAtStart; +} + +inline void gLog (const char* format, ...) { + fprintf (stdout, "%11.6f: ", gGetTimeSinceStart()); + va_list argptr; + va_start(argptr, format); + vfprintf(stdout, format, argptr); + va_end(argptr); + fprintf (stdout, "\n"); +} diff --git a/src/main.cc b/src/main.cc index 5839020..951856c 100644 --- a/src/main.cc +++ b/src/main.cc @@ -25,6 +25,7 @@ GLFWwindow* gWindow = nullptr; RuntimeModuleManager* gModuleManager = nullptr; WriteSerializer* gWriteSerializer = nullptr; ReadSerializer* gReadSerializer = nullptr; +double gTimeAtStart = 0; using namespace std; @@ -64,6 +65,9 @@ static void key_callback(GLFWwindow* window, int key, int scancode, int action, int main(void) { + gTimeAtStart = gGetCurrentTime(); + std::cout << "Time at start: " << gTimeAtStart << std::endl; + WriteSerializer out_serializer; ReadSerializer in_serializer; diff --git a/src/modules/RenderModule.cc b/src/modules/RenderModule.cc index c0c5474..987c424 100644 --- a/src/modules/RenderModule.cc +++ b/src/modules/RenderModule.cc @@ -49,7 +49,7 @@ struct module_state { }; static struct module_state *module_init() { - std::cout << "RenderModule init called" << std::endl; + gLog ("RenderModule init called."); assert (gWindow != nullptr && "Cannot initialize renderer module without gWindow!"); module_state *state = (module_state*) malloc(sizeof(*state)); @@ -75,7 +75,7 @@ static void module_serialize ( } static void module_finalize(struct module_state *state) { - std::cout << "RenderModule finalize called" << std::endl; + gLog ("RenderModule finalize called"); assert (state->renderer != nullptr); delete state->renderer; @@ -84,12 +84,12 @@ static void module_finalize(struct module_state *state) { } static void module_reload(struct module_state *state, void *read_serializer) { - std::cout << "RenderModule reload called" << std::endl; + gLog ("RenderModule reload called"); assert (gWindow != nullptr); int width, height; glfwGetWindowSize(gWindow, &width, &height); - std::cout << "renderer initialize" << std::endl; + gLog ("Renderer initialize"); assert (state != nullptr); state->renderer->initialize(width, height); gRenderer = state->renderer; @@ -115,7 +115,7 @@ static void module_unload(struct module_state *state, void* write_serializer) { gRenderer = nullptr; state->renderer->shutdown(); - std::cout << "RenderModule unload called" << std::endl; + gLog ("RenderModule unload called"); } static bool module_step(struct module_state *state, float dt) { @@ -385,12 +385,14 @@ bool RenderProgram::reload() { bgfx::destroyProgram(program); } - cout << "Reload of shaders " << vertexShaderFileName << " and " << fragmentShaderFileName << " success!" << endl; + gLog ("Reload of shaders %s and %s success!", + vertexShaderFileName.c_str(), fragmentShaderFileName.c_str()); program = new_handle; return true; } else { - cout << "Reload of shaders " << vertexShaderFileName << " and " << fragmentShaderFileName << " failed!" << endl; + gLog ("Reload of shaders %s and %s failed!", + vertexShaderFileName.c_str(), fragmentShaderFileName.c_str()); } return false; @@ -996,7 +998,7 @@ void Renderer::setupShaders() { m_timeOffset = bx::getHPCounter(); // Initialize light - std::cout << "Creating light uniforms..." << std::endl; + gLog ("Creating light uniforms..."); lights[0].u_shadowMap = bgfx::createUniform("u_shadowMap", bgfx::UniformType::Int1); lights[0].u_shadowMapParams = bgfx::createUniform("u_shadowMapParams", bgfx::UniformType::Vec4); lights[0].u_lightPos = bgfx::createUniform("u_lightPos", bgfx::UniformType::Vec4); @@ -1188,7 +1190,7 @@ void Renderer::initialize(int width, int height) { bgfx::setDebug(debug); - std::cout << "Creating Cameras" << std::endl; + gLog ("Creating Cameras"); cameras.push_back (Camera()); activeCameraIndex = 0; lights.push_back (Light()); @@ -1257,7 +1259,7 @@ void Renderer::shutdown() { } for (size_t i = 0; i < lights.size(); i++) { - std::cout << "Destroying light uniforms for light " << i << std::endl; + gLog ("Destroying light uniforms for light %d", i); bgfx::destroyFrameBuffer(lights[i].shadowMapFB); bgfx::destroyUniform(lights[i].u_shadowMap); @@ -1648,7 +1650,7 @@ void Renderer::paintGL() { line.UpdateBuffers(); } - float thickness = 0.05f; + float thickness = 0.143f; float miter = 0.0f; float aspect = static_cast(width) / height; diff --git a/src/modules/RenderUtils.cc b/src/modules/RenderUtils.cc index bbe761b..8430153 100644 --- a/src/modules/RenderUtils.cc +++ b/src/modules/RenderUtils.cc @@ -22,8 +22,7 @@ namespace stl = tinystl; #include "RenderModule.h" #include "RenderUtils.h" -//#include "MeshVBO.h" -#include "SimpleMath/SimpleMath.h" +#include "Globals.h" using namespace SimpleMath; @@ -214,7 +213,7 @@ bgfx::ProgramHandle loadProgram(const char* _vsName, const char* _fsName) } bgfx::ProgramHandle loadProgramFromFiles(const char *_vsFileName, const char *_fsFileName) { - std::cout << "Loading shader " << _vsFileName << std::endl; + gLog ("Loading shader %s", _vsFileName); const char* argv[10]; argv[0] = "--type"; argv[1] = "vertex"; @@ -306,7 +305,7 @@ bgfx::TextureHandle loadTexture(bx::FileReaderI* _reader, const char* _name, uin strcat(filePath, _name); - std::cout << "Loading texture " << filePath << std::endl; + gLog ("Loading texture %s", filePath); if (NULL != bx::stristr(_name, ".dds") || NULL != bx::stristr(_name, ".pvr")