diff --git a/src/DarkRP2D.cpp b/src/DarkRP2D.cpp index 5727a14..43b8cbd 100644 --- a/src/DarkRP2D.cpp +++ b/src/DarkRP2D.cpp @@ -8,6 +8,7 @@ #include #include #include "screens/GameScreen.h" +#include "Utility.h" DarkRP2D::~DarkRP2D() { std::cout << "Game got destroyed!" << std::endl; @@ -76,12 +77,6 @@ void DarkRP2D::update(unsigned int delta) { gameScreen->update(); } -#ifndef NDEBUG -bool show_demo_window = true; -bool show_another_window = false; -ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); -#endif - void DarkRP2D::render(unsigned int delta) { renderer->clear(); #ifndef NDEBUG @@ -97,18 +92,21 @@ void DarkRP2D::render(unsigned int delta) { #ifndef NDEBUG // 1. Show a simple window. // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets automatically appears in a window called "Debug". + static bool ShowOpenGLLogger = false; { - static float f = 0.0f; - static int counter = 0; - ImGui::Text("-------------------OpenGL Info-------------------------"); + ImGui::Separator(); + ImGui::Text("\t\t\t\t\tOpenGL Info"); ImGui::Text("GLEW Version: %s\n", glewGetString(GLEW_VERSION)); ImGui::Text(" Version: %s\n", glGetString(GL_VERSION)); ImGui::Text(" Vendor: %s\n", glGetString(GL_VENDOR)); ImGui::Text(" Renderer: %s\n", glGetString(GL_RENDERER)); ImGui::Text(" Shading: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION)); - ImGui::Text("----------------------------------------------------------"); + ImGui::Separator(); ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); + ImGui::Checkbox("Show OpenGL Logger", &ShowOpenGLLogger); } + if(ShowOpenGLLogger) + Utility::Debug::openGLLogger.Draw("OpenGL Logger"); ImGui::Render(); ImGui_ImplSdlGL3_RenderDrawData(ImGui::GetDrawData()); diff --git a/src/DesktopLauncher.cpp b/src/DesktopLauncher.cpp index aa693b8..d08be64 100755 --- a/src/DesktopLauncher.cpp +++ b/src/DesktopLauncher.cpp @@ -11,8 +11,6 @@ using namespace std; -const int SCREEN_WIDTH = 800; -const int SCREEN_HEIGHT = 600; static SDL_Window* gWindow = NULL; static SDL_GLContext glcontext; static bool gameQuit = false; @@ -46,6 +44,10 @@ void Game::exit() { gameQuit = true; } +namespace Game { + float DDPI, HDPI,VDPI; +} + bool initSDL() { //Init SDL if(SDL_Init(SDL_INIT_VIDEO) < 0 ) { @@ -53,16 +55,20 @@ bool initSDL() { return false; } //Init Window - gWindow = SDL_CreateWindow( "SDL TEST", SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL); + uint32_t WindowFlags = SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_OPENGL; + gWindow = SDL_CreateWindow( "SDL TEST", SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED, Game::SCREEN_WIDTH, Game::SCREEN_HEIGHT, WindowFlags); if( gWindow == NULL) { cout << "Window could not be created! SDL_Error: " << SDL_GetError() << endl; return false; } + //Read in Display DPI + SDL_GetDisplayDPI(SDL_GetWindowDisplayIndex(gWindow),&Game::DDPI,&Game::HDPI,&Game::VDPI); + cout << "DPI: DDPI: " << Game::DDPI << " HDPI: " << Game::HDPI << " VPDI: " << Game::VDPI << endl; //Mask deprectated functions SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); -#ifndef NDEBUG - SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); -#endif + #ifndef NDEBUG + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); + #endif //Init OpenGL Renderer SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); glcontext = SDL_GL_CreateContext(gWindow); @@ -78,7 +84,7 @@ bool initSDL() { return false; } //Init debug callback function -#ifndef NDEBUG + #ifndef NDEBUG if (GLEW_ARB_debug_output) { Utility::printInfo(Utility::GLEW, "Supporting ARB debug output!"); } @@ -104,7 +110,7 @@ bool initSDL() { } else { Utility::printWarning(Utility::OpenGL, "glDebugMessageCallback not available"); } -#endif + #endif return true; } diff --git a/src/DesktopLauncher.h b/src/DesktopLauncher.h index 80e6fb1..10b72f1 100644 --- a/src/DesktopLauncher.h +++ b/src/DesktopLauncher.h @@ -5,8 +5,16 @@ namespace Game { -#define WINDOW_WIDTH = 800; -#define WINDOW_HEIGHT = 600; +const int SCREEN_WIDTH = 800; +const int SCREEN_HEIGHT = 600; + +//Diagonal DPI of the display used while starting +extern float DDPI; +//horizontal DPI of the display used while starting +extern float HDPI; +//vertical DPI of the display used while starting +extern float VDPI; + void exit(); diff --git a/src/Texture.cpp b/src/Texture.cpp index 26066ff..53ba262 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -4,7 +4,7 @@ Texture2D::Texture2D() - : Width(0), Height(0), Internal_Format(GL_RGB), Image_Format(GL_RGB), Wrap_S(GL_REPEAT), Wrap_T(GL_REPEAT), Filter_Min(GL_LINEAR), Filter_Max(GL_LINEAR) + : Width(0), Height(0), Internal_Format(GL_RGB), Image_Format(GL_RGB), Wrap_S(GL_REPEAT), Wrap_T(GL_REPEAT), Filter_Min(GL_NEAREST_MIPMAP_NEAREST), Filter_Max(GL_NEAREST) { glGenTextures(1, &this->ID); } @@ -16,7 +16,12 @@ void Texture2D::Generate(GLuint width, GLuint height, unsigned char* data) { glBindTexture(GL_TEXTURE_2D, this->ID); glTexImage2D(GL_TEXTURE_2D, 0, this->Internal_Format, width, height, 0, this->Image_Format, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + + //Anistropic filtering + //float aniso = 0.0f; + //glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &aniso); + //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso); + // Set Texture wrap and filter modes glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, this->Wrap_S); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, this->Wrap_T); diff --git a/src/Utility.cpp b/src/Utility.cpp index 8875b07..76cd5b1 100644 --- a/src/Utility.cpp +++ b/src/Utility.cpp @@ -1,5 +1,77 @@ #include "Utility.h" +#ifndef NDEBUG + + namespace Utility{ + namespace Debug { + ImGuiLogger openGLLogger; + } + } + + void Utility::Debug::ImGuiLogger::Clear() { + Buf.clear(); + LineOffsets.clear(); + } + + void Utility::Debug::ImGuiLogger::AddLog(const char* fmt, ...) IM_FMTARGS(2) { + int old_size = Buf.size(); + va_list args; + va_start(args, fmt); + Buf.appendfv(fmt, args); + va_end(args); + for (int new_size = Buf.size(); old_size < new_size; old_size++) + if (Buf[old_size] == '\n') + LineOffsets.push_back(old_size); + ScrollToBottom = true; + } + + void Utility::Debug::ImGuiLogger::Draw(const char* title, bool* p_open) { + ImGui::SetNextWindowSize(ImVec2(500,400), ImGuiCond_FirstUseEver); + ImGui::Begin(title, p_open); + if (ImGui::Button("Clear")) Clear(); + ImGui::SameLine(); + bool copy = ImGui::Button("Copy"); + ImGui::SameLine(); + ImGui::Checkbox("Errors", &ShowError); ImGui::SameLine(); + ImGui::Checkbox("Deprecated Behavior", &ShowDeprecatedBehavior); ImGui::SameLine(); + ImGui::Checkbox("Undefined Behavior", &ShowUndefinedBehavior); ImGui::SameLine(); + ImGui::Checkbox("Portability", &ShowPortability); + ImGui::Checkbox("Performance", &ShowPerformance); ImGui::SameLine(); + ImGui::Checkbox("Marker", &ShowMarker); ImGui::SameLine(); + ImGui::Checkbox("Push Group", &ShowPushGroup); ImGui::SameLine(); + ImGui::Checkbox("Pop Group", &ShowPopGroup); ImGui::SameLine(); + ImGui::Checkbox("Other", &ShowOther); + + Filter.Draw("Filter", -100.0f); + ImGui::Separator(); + ImGui::BeginChild("scrolling", ImVec2(0,0), false, ImGuiWindowFlags_HorizontalScrollbar); + if (copy) ImGui::LogToClipboard(); + + if (Filter.IsActive()) + { + const char* buf_begin = Buf.begin(); + const char* line = buf_begin; + for (int line_no = 0; line != NULL; line_no++) + { + const char* line_end = (line_no < LineOffsets.Size) ? buf_begin + LineOffsets[line_no] : NULL; + if (Filter.PassFilter(line, line_end)) + ImGui::TextUnformatted(line, line_end); + line = line_end && line_end[1] ? line_end + 1 : NULL; + } + } + else + { + ImGui::TextUnformatted(Buf.begin()); + } + + if (ScrollToBottom) + ImGui::SetScrollHere(1.0f); + ScrollToBottom = false; + ImGui::EndChild(); + ImGui::End(); + } + +#endif //NDEBUG void APIENTRY Utility::openglCallbackFunction(GLenum source, GLenum type, @@ -8,47 +80,57 @@ void APIENTRY Utility::openglCallbackFunction(GLenum source, GLsizei length, const GLchar* message, const void* userParam){ - std::cout << "---------------------opengl-callback-start------------" << std::endl; - std::cout << "message: "<< message << std::endl; - std::cout << "type: "; - switch (type) { - case GL_DEBUG_TYPE_ERROR: - std::cout << "ERROR"; - break; - case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: - std::cout << "DEPRECATED_BEHAVIOR"; - break; - case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: - std::cout << "UNDEFINED_BEHAVIOR"; - break; - case GL_DEBUG_TYPE_PORTABILITY: - std::cout << "PORTABILITY"; - break; - case GL_DEBUG_TYPE_PERFORMANCE: - std::cout << "PERFORMANCE"; - break; - case GL_DEBUG_TYPE_OTHER: - std::cout << "OTHER"; - break; - } - std::cout << std::endl; - - std::cout << "id: " << id << std::endl; - std::cout << "severity: "; + + std::string _severity; switch (severity){ case GL_DEBUG_SEVERITY_LOW: - std::cout << "LOW"; + _severity = "LOW"; break; case GL_DEBUG_SEVERITY_MEDIUM: - std::cout << "MEDIUM"; + _severity = "MEDIUM"; break; case GL_DEBUG_SEVERITY_HIGH: - std::cout << "HIGH"; + _severity = "HIGH"; + break; + default: + _severity = "NOTIFICATION"; + break; + } + + switch (type) { + case GL_DEBUG_TYPE_ERROR: + if(Utility::Debug::openGLLogger.ShowError) { + Debug::openGLLogger.AddLog("ERROR: %s ID: %d SEVERITY: %s\n", message, id, _severity.c_str()); + } + break; + case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: + if(Utility::Debug::openGLLogger.ShowDeprecatedBehavior) { + Debug::openGLLogger.AddLog("DEPRECATED_BEHAVIOR: %s ID: %d SEVERITY: %s\n", message, id, _severity.c_str()); + } + break; + case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: + if(Utility::Debug::openGLLogger.ShowUndefinedBehavior) { + Debug::openGLLogger.AddLog("UNDEFINED_BEHAVIOR: %s ID: %d SEVERITY: %s\n", message, id, _severity.c_str()); + } + break; + case GL_DEBUG_TYPE_PORTABILITY: + if(Utility::Debug::openGLLogger.ShowPortability) { + Debug::openGLLogger.AddLog("PORTATBILITY: %s ID: %d SEVERITY: %s\n", message, id, _severity.c_str()); + } + break; + case GL_DEBUG_TYPE_PERFORMANCE: + if(Utility::Debug::openGLLogger.ShowPerformance) { + Debug::openGLLogger.AddLog("PERFORMANCE: %s ID: %d SEVERITY: %s\n", message, id, _severity.c_str()); + } + break; + case GL_DEBUG_TYPE_OTHER: + if(Utility::Debug::openGLLogger.ShowOther) { + Debug::openGLLogger.AddLog("OTHER: %s ID: %d SEVERITY: %s\n", message, id, _severity.c_str()); + } break; } - std::cout << std::endl; - std::cout << "---------------------opengl-callback-end--------------" << std::endl; } + static const char* Errornames[] = { "OpenGL", "SDL","GLEW", "Other" }; void Utility::printWarning(ERROR_TYPE type, std::string msg) { std::cout << "Warning[" << Errornames[type] << "]: " << msg << std::endl; diff --git a/src/Utility.h b/src/Utility.h index 1c4def5..390fb8c 100644 --- a/src/Utility.h +++ b/src/Utility.h @@ -6,9 +6,48 @@ #include #include +#ifndef NDEBUG + #include "imgui.h" +#endif + namespace Utility{ -enum ERROR_TYPE { OpenGL = 0, SDL,GLEW, Other}; +#ifndef NDEBUG +namespace Debug { + +// Usage: +// static ExampleAppLog my_log; +// my_log.AddLog("Hello %d world\n", 123); +// my_log.Draw("title"); +struct ImGuiLogger +{ + ImGuiTextBuffer Buf; + ImGuiTextFilter Filter; + ImVector LineOffsets; // Index to lines offset + bool ShowError; + bool ShowDeprecatedBehavior; + bool ShowUndefinedBehavior; + bool ShowPortability; + bool ShowPerformance; + bool ShowMarker; + bool ShowPushGroup; + bool ShowPopGroup; + bool ShowOther; + bool ScrollToBottom; + + ImGuiLogger() : ShowError(true) {} + + void Clear(); + void AddLog(const char* fmt, ...) IM_FMTARGS(2); + void Draw(const char* title, bool* p_open = NULL); +}; + +extern ImGuiLogger openGLLogger; + +} +#endif //NDEBUG + +enum ERROR_TYPE { OpenGL = 0, SDL, GLEW, Other}; void APIENTRY openglCallbackFunction(GLenum source, GLenum type, @@ -27,4 +66,4 @@ void printError(ERROR_TYPE type, std::string msg); void printInfo(ERROR_TYPE type, std::string msg); } -#endif +#endif // UTILITY_H