#include "pch.h" #include "log.h" #include "app.h" #include "node_icon.h" #include "node_dialog_open.h" using namespace ui; App App::I; // singleton void App::create() { width = 800; height = 500; } void App::clear() { glClearColor(.1f, .1f, .1f, 1.f); glViewport(0, 0, (GLsizei)width, (GLsizei)height); glClear(GL_COLOR_BUFFER_BIT); } void App::initAssets() { LOG("initializing assets"); FontManager::init(); LOG("initializing assets loading fonts"); FontManager::load(kFont::Arial_11, "data/arial.ttf", 15); FontManager::load(kFont::Arial_30, "data/arial.ttf", 30); LOG("initializing assets create sampler"); sampler.create(GL_NEAREST); LOG("initializing assets load uvs texture"); if (!tex.load("data/uvs.jpg")) LOG("error loading image"); LOG("initializing assets completed"); } void App::initLog() { LogRemote::I.start(); } void App::init() { #ifdef _WIN32 static CONSOLE_SCREEN_BUFFER_INFO info; GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info); // colors: http://stackoverflow.com/questions/4053837/colorizing-text-in-the-console-with-c glDebugMessageCallback([](GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam) { static std::map colors = { { GL_DEBUG_SEVERITY_NOTIFICATION, 8 }, { GL_DEBUG_SEVERITY_LOW, 8 }, { GL_DEBUG_SEVERITY_MEDIUM, FOREGROUND_GREEN | FOREGROUND_INTENSITY }, { GL_DEBUG_SEVERITY_HIGH, FOREGROUND_RED | FOREGROUND_INTENSITY }, }; if (severity == GL_DEBUG_SEVERITY_MEDIUM || severity == GL_DEBUG_SEVERITY_HIGH || severity == GL_DEBUG_SEVERITY_LOW) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors[severity]); LOG("OPENGL: %.*s", length, message); FlushConsoleInputBuffer(GetStdHandle(STD_OUTPUT_HANDLE)); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), info.wAttributes); //__debugbreak(); } }, nullptr); glEnable(GL_DEBUG_OUTPUT); glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); #endif LOG("GL version: %s", glGetString(GL_VERSION)); LOG("GL vendor: %s", glGetString(GL_VENDOR)); LOG("GL renderer: %s", glGetString(GL_RENDERER)); // GLint n_exts; // glGetIntegerv(GL_NUM_EXTENSIONS, &n_exts); // for (int i = 0; i < n_exts; i++) // { // LOG("%s", glGetStringi(GL_EXTENSIONS, i)); // } LOG("Screen Resolution: %dx%d", (int)width, (int)height); zoom = ceilf(width / 1000.f); glDisable(GL_DEPTH_TEST); #if defined(_WIN32) || defined(__OSX__) glEnable(GL_PROGRAM_POINT_SIZE); #endif glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendEquation(GL_FUNC_ADD); initShaders(); initAssets(); initLayout(); GLfloat width_range[2]; glGetFloatv(GL_ALIASED_LINE_WIDTH_RANGE, width_range); LOG("GL line range: %f - %f", width_range[0], width_range[1]); LOG("Screen Size: %f %f", width, height); } void App::update(float dt) { // update offscreen stuff if (canvas && canvas->m_canvas) canvas->m_canvas->stroke_draw(); //glClearColor(.1f, .1f, .1f, 1.f); //glViewport(0, 0, (GLsizei)width, (GLsizei)height); //glClear(GL_COLOR_BUFFER_BIT); #ifndef __ANDROID__ layout.reload(); #endif if (auto* main = layout[main_id]) main->update(width, height, zoom); auto observer = [this](Node* n) { if (n && n->m_display) { auto box = n->m_clip; glm::ivec4 c = glm::vec4((int)box.x - 1, (int)(height / zoom - box.y - box.w) - 1, (int)box.z + 2, (int)box.w + 2) * zoom; glScissor(c.x, c.y, c.z, c.w); n->draw(); } }; //glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glEnable(GL_SCISSOR_TEST); if (auto* main = layout[main_id]) main->watch(observer); //msgbox->watch(observer); glDisable(GL_SCISSOR_TEST); } void App::terminate() { LOG("App::terminate"); TextureManager::invalidate(); ShaderManager::invalidate(); layout.clear_context(); brushes->clear_context(); layers->clear_context(); color->clear_context(); stroke->clear_context(); } void App::update_memory_usage(size_t bytes) { if (auto txt = layout[main_id]->find("txt-memory")) { static char buffer[128]; sprintf(buffer, "History memory: %.2f Mb", bytes / 1024.f / 1024.f); txt->set_text(buffer); layout[main_id]->update(); } }