Files
panopainter/engine/app.cpp
2017-11-05 22:54:04 +00:00

291 lines
7.8 KiB
C++

#include "pch.h"
#include "log.h"
#include "app.h"
#include "node_icon.h"
#include "node_dialog_open.h"
#ifdef __APPLE__
#include <Foundation/Foundation.h>
#endif
#ifdef __ANDROID__
void android_async_lock(struct engine* engine);
void android_async_swap(struct engine* engine);
void android_async_unlock(struct engine* engine);
#elif _WIN32
void async_lock();
void async_swap();
void async_unlock();
#endif
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()
{
#if defined(__IOS__) || defined(__OSX__)
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* docpath = [paths objectAtIndex:0];
data_path = [docpath cStringUsingEncoding:NSASCIIStringEncoding];
#endif
LogRemote::I.start();
LogRemote::I.file_init();
}
void App::upload(std::string filename)
{
CURL *curl;
struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
//curl_global_init(CURL_GLOBAL_ALL);
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "fileToUpload",
CURLFORM_FILE, filename.c_str(),
CURLFORM_END);
curl = curl_easy_init();
std::string res;
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://omigamedev.ddns.net:8080/panoview/upl.php");
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &res);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_data_handler);
auto err = curl_easy_perform(curl);
std::cout << "\n\nUPLOAD RESULT\n" << res << "\n\n\n";
curl_easy_cleanup(curl);
}
}
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<GLenum, int> 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_HIGH || severity == GL_DEBUG_SEVERITY_MEDIUM || 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 / 2000.f);
//zoom = 2;
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);
GLint fb0;
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &fb0);
LOG("Default Framebuffer %d", fb0);
}
void App::async_start()
{
#if __OSX__
[osx_view async_lock];
#elif __IOS__
[ios_view async_lock];
#elif __ANDROID__
android_async_lock(and_engine);
#elif _WIN32
async_lock();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
#endif
}
void App::async_update()
{
#if __IOS__
[ios_view->glview bindDrawable];
#elif _WIN32
glBindFramebuffer(GL_FRAMEBUFFER, 0);
#endif
redraw = true;
clear();
update(0);
#if __OSX__
[osx_view async_swap];
#elif __IOS__
[ios_view->glview bindDrawable];
[ios_view async_swap];
#elif __ANDROID__
android_async_swap(and_engine);
#elif _WIN32
async_swap();
#endif
}
void App::async_end()
{
#if __OSX__
[osx_view async_unlock];
#elif __IOS__
[ios_view async_unlock];
#elif __ANDROID__
android_async_unlock(and_engine);
#elif _WIN32
async_unlock();
#endif
}
void App::update(float dt)
{
static std::mutex m;
std::lock_guard<std::mutex> _lock(m);
// update offscreen stuff
if (canvas && canvas->m_canvas)
canvas->m_canvas->stroke_draw();
if (!(redraw || animate))
return;
//glClearColor(.1f, .1f, .1f, 1.f);
//glViewport(0, 0, (GLsizei)width, (GLsizei)height);
//glClear(GL_COLOR_BUFFER_BIT);
#ifdef _WIN32
layout.reload();
#endif
if (auto* main = layout[main_id])
main->update(width, height, zoom);
static glm::vec4 color_button_normal{.1, .1, .1, 1};
static glm::vec4 color_button_hlight{ 1, .0, .0, 1};
CanvasModePen* mode = (CanvasModePen*)canvas->m_canvas->modes[(int)Canvas::kCanvasMode::Draw][0];
layout[main_id]->find<NodeButton>("btn-pick")->set_color(
mode->m_picking ? color_button_hlight : color_button_normal);
layout[main_id]->find<NodeButton>("btn-touchlock")->set_color(
canvas->m_canvas->m_touch_lock ? color_button_hlight : color_button_normal);
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;
glm::ivec4 c = glm::vec4((int)box.x, (int)(height / zoom - box.y - box.w), (int)box.z, (int)box.w) * zoom;
glScissor(c.x, c.y, c.z, c.w);
n->draw();
}
};
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
#if __IOS__
[ios_view->glview bindDrawable];
#else
glBindFramebuffer(GL_FRAMEBUFFER, 0);
#endif
glEnable(GL_SCISSOR_TEST);
if (auto* main = layout[main_id])
main->watch(observer);
//msgbox->watch(observer);
glDisable(GL_SCISSOR_TEST);
redraw = false;
}
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<NodeText>("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();
}
}