add remote logging system using curl, normalize log messages removing \n, fix fra shaders precision issue, enable core and forward comp. in WGL, disable multisampling,

This commit is contained in:
2017-03-31 15:39:51 +01:00
parent ade95724e8
commit b1a3cb0309
18 changed files with 352 additions and 129 deletions

View File

@@ -39,6 +39,7 @@ add_library(
../engine/app.cpp
../engine/brush.cpp
../engine/canvas.cpp
../engine/log.cpp
)
target_include_directories(native-lib PRIVATE

View File

@@ -138,7 +138,7 @@ static int engine_init_display(struct engine* engine) {
engine->state.angle = 0;
// Check openGL on the system
auto opengl_info = {GL_VENDOR, GL_RENDERER, GL_VERSION, GL_EXTENSIONS};
auto opengl_info = {GL_VENDOR, GL_RENDERER, GL_VERSION/*, GL_EXTENSIONS*/};
for (auto name : opengl_info) {
auto info = glGetString(name);
LOG("OpenGL Info: %s", info);
@@ -294,46 +294,12 @@ static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
* android_native_app_glue. It runs in its own thread, with its own
* event loop for receiving input events and doing other things.
*/
#include <curl/curl.h>
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
LOG("READ SOMETHIND");
return size * nmemb;
}
int curl_test()
{
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
LOG("READ: %s", readBuffer.c_str());
}
else
{
LOG("READ FAILED");
}
return 0;
}
void android_main(struct android_app* state) {
struct engine engine;
// Make sure glue isn't stripped.
app_dummy();
LOG("NETWORK TESTING...");
//curl_test();
LOG("NETWORK TESTED");
memset(&engine, 0, sizeof(engine));
state->userData = &engine;
state->onAppCmd = engine_handle_cmd;

View File

@@ -71,23 +71,23 @@
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>libs\glm;libs\glew-2.0.0\include;libs\stb;libs\tinyxml2;libs\yoga;$(IncludePath)</IncludePath>
<LibraryPath>libs\glew-2.0.0\lib\Release\$(Platform);$(LibraryPath)</LibraryPath>
<IncludePath>libs\glm;libs\glew-2.0.0\include;libs\stb;libs\tinyxml2;libs\yoga;libs\curl-win\include;$(IncludePath)</IncludePath>
<LibraryPath>libs\curl-win\lib\dll-$(Configuration)-$(PlatformShortName);libs\glew-2.0.0\lib\Release\$(Platform);$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>libs\glm;libs\glew-2.0.0\include;libs\stb;libs\tinyxml2;libs\yoga;$(IncludePath)</IncludePath>
<LibraryPath>libs\glew-2.0.0\lib\Release\$(Platform);$(LibraryPath)</LibraryPath>
<IncludePath>libs\glm;libs\glew-2.0.0\include;libs\stb;libs\tinyxml2;libs\yoga;libs\curl-win\include;$(IncludePath)</IncludePath>
<LibraryPath>libs\curl-win\lib\dll-$(Configuration)-$(PlatformShortName);libs\glew-2.0.0\lib\Release\$(Platform);$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>libs\glm;libs\glew-2.0.0\include;libs\stb;libs\tinyxml2;libs\yoga;$(IncludePath)</IncludePath>
<LibraryPath>libs\glew-2.0.0\lib\Release\$(Platform);$(LibraryPath)</LibraryPath>
<IncludePath>libs\glm;libs\glew-2.0.0\include;libs\stb;libs\tinyxml2;libs\yoga;libs\curl-win\include;$(IncludePath)</IncludePath>
<LibraryPath>libs\curl-win\lib\dll-$(Configuration)-$(PlatformShortName);libs\glew-2.0.0\lib\Release\$(Platform);$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>libs\glm;libs\glew-2.0.0\include;libs\stb;libs\tinyxml2;libs\yoga;$(IncludePath)</IncludePath>
<LibraryPath>libs\glew-2.0.0\lib\Release\$(Platform);$(LibraryPath)</LibraryPath>
<IncludePath>libs\glm;libs\glew-2.0.0\include;libs\stb;libs\tinyxml2;libs\yoga;libs\curl-win\include;$(IncludePath)</IncludePath>
<LibraryPath>libs\curl-win\lib\dll-$(Configuration)-$(PlatformShortName);libs\glew-2.0.0\lib\Release\$(Platform);$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
@@ -158,6 +158,7 @@
<ClCompile Include="engine\font.cpp" />
<ClCompile Include="engine\image.cpp" />
<ClCompile Include="engine\layout.cpp" />
<ClCompile Include="engine\log.cpp" />
<ClCompile Include="engine\main.cpp" />
<ClCompile Include="engine\pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
@@ -198,6 +199,7 @@
<ClInclude Include="engine\font.h" />
<ClInclude Include="engine\image.h" />
<ClInclude Include="engine\layout.h" />
<ClInclude Include="engine\log.h" />
<ClInclude Include="engine\pch.h" />
<ClInclude Include="engine\rtt.h" />
<ClInclude Include="engine\shader.h" />

View File

@@ -69,6 +69,9 @@
<ClCompile Include="engine\brush.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="engine\log.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="engine\app.h">
@@ -113,5 +116,8 @@
<ClInclude Include="engine\brush.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="engine\log.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -33,8 +33,8 @@ void App::initShaders()
static const char* shader_f =
SHADER_VERSION
"uniform sampler2D tex;"
"in vec3 uv;"
"out vec4 frag;"
"in mediump vec3 uv;"
"out mediump vec4 frag;"
"void main(){"
//" frag = texture(tex, uv.xy/uv.z);"
" frag = texture(tex, uv.xy);"
@@ -42,8 +42,8 @@ void App::initShaders()
static const char* shader_uv_f =
SHADER_VERSION
"uniform sampler2D tex;"
"in vec3 uv;"
"out vec4 frag;"
"in mediump vec3 uv;"
"out mediump vec4 frag;"
"void main(){"
" frag = vec4(uv.xy, 0.0, 1.0);"
"}";
@@ -64,8 +64,8 @@ void App::initShaders()
static const char* shader_atlas_f =
SHADER_VERSION
"uniform sampler2D tex;"
"in vec2 uv;"
"out vec4 frag;"
"in mediump vec2 uv;"
"out mediump vec4 frag;"
"void main(){"
" frag = texture(tex, uv);"
"}";
@@ -80,8 +80,8 @@ void App::initShaders()
"}";
static const char* shader_color_f =
SHADER_VERSION
"uniform vec4 col;"
"out vec4 frag;"
"uniform mediump vec4 col;"
"out mediump vec4 frag;"
"void main(){"
" frag = col;"
"}";
@@ -99,11 +99,11 @@ void App::initShaders()
"}";
static const char* shader_color_quad_f =
SHADER_VERSION
"uniform vec4 col;"
"in vec3 uv;"
"out vec4 frag;"
"uniform mediump vec4 col;"
"in mediump vec3 uv;"
"out mediump vec4 frag;"
"void main(){"
" vec4 gradient_x = mix(col, vec4(1.0, 1.0, 1.0, 1.0), uv.x);"
" mediump vec4 gradient_x = mix(col, vec4(1.0, 1.0, 1.0, 1.0), uv.x);"
" frag = mix(gradient_x, vec4(0.0, 0.0, 0.0, 1.0), uv.y);"
"}";
@@ -120,12 +120,12 @@ void App::initShaders()
"}";
static const char* shader_color_hue_f =
SHADER_VERSION
"uniform vec4 col;"
"in vec3 uv;"
"out vec4 frag;"
"vec3 hsv2rgb(vec3 c) {"
" vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);"
" vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);"
"uniform mediump vec4 col;"
"in mediump vec3 uv;"
"out mediump vec4 frag;"
"mediump vec3 hsv2rgb(mediump vec3 c) {"
" mediump vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);"
" mediump vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);"
" return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);"
"}"
"void main(){"
@@ -145,12 +145,12 @@ void App::initShaders()
"}";
static const char* shader_font_f =
SHADER_VERSION
"uniform sampler2D tex;"
"uniform vec4 col;"
"in vec2 uv;"
"out vec4 frag;"
"uniform mediump sampler2D tex;"
"uniform mediump vec4 col;"
"in mediump vec2 uv;"
"out mediump vec4 frag;"
"void main(){"
" float a = texture(tex, uv).r;"
" mediump float a = texture(tex, uv).r;"
" frag = vec4(col.rgb, a);"
"}";
@@ -167,13 +167,13 @@ void App::initShaders()
"}";
static const char* shader_stroke_f =
SHADER_VERSION
"uniform sampler2D tex;"
"uniform vec4 col;"
"uniform float alpha;"
"in vec3 uv;"
"out vec4 frag;"
"uniform mediump sampler2D tex;"
"uniform mediump vec4 col;"
"uniform mediump float alpha;"
"in mediump vec3 uv;"
"out mediump vec4 frag;"
"void main(){"
" float a = (1.0 - texture(tex, uv.xy).r) * alpha;"
" mediump float a = (1.0 - texture(tex, uv.xy).r) * alpha;"
" frag = vec4(col.rgb, a);"
"}";
@@ -209,7 +209,7 @@ void App::initAssets()
sampler.create(GL_NEAREST);
LOG("initializing assets load uvs texture");
if (!tex.load("data/uvs.jpg"))
LOG("error loading image\n");
LOG("error loading image");
LOG("initializing assets completed");
}
@@ -444,6 +444,8 @@ void App::initLayout()
void App::init()
{
LogRemote::I.start();
#ifdef _WIN32
static CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
@@ -457,15 +459,33 @@ void App::init()
{ GL_DEBUG_SEVERITY_MEDIUM, FOREGROUND_GREEN | FOREGROUND_INTENSITY },
{ GL_DEBUG_SEVERITY_HIGH, FOREGROUND_RED | FOREGROUND_INTENSITY },
};
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors[severity]);
LOG("%.*s\n", length, message);
FlushConsoleInputBuffer(GetStdHandle(STD_OUTPUT_HANDLE));
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), info.wAttributes);
if (severity == GL_DEBUG_SEVERITY_MEDIUM || severity == GL_DEBUG_SEVERITY_HIGH)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors[severity]);
LOG("%.*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
glEnable(GL_TEXTURE_2D);
//int n;
//glGetIntegerv(GL_NUM_EXTENSIONS, &n);
//for (int i = 0; i < n; i++)
//{
// const unsigned char* s = glGetStringi(GL_EXTENSIONS, i);
// LOG("GL ext %03d: %s\n", i, s);
//}
LOG("GL version: %s", glGetString(GL_VERSION));
LOG("GL vendor: %s", glGetString(GL_VENDOR));
LOG("GL renderer: %s", glGetString(GL_RENDERER));
LOG("Screen Resolution: %dx%d", (int)width, (int)height);
zoom = ceilf(width / 1000.f);
//glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@@ -475,20 +495,10 @@ void App::init()
initAssets();
initLayout();
//int n;
//glGetIntegerv(GL_NUM_EXTENSIONS, &n);
//for (int i = 0; i < n; i++)
//{
// const unsigned char* s = glGetStringi(GL_EXTENSIONS, i);
// LOG("GL ext %03d: %s\n", i, s);
//}
LOG("GL version: %s\n", glGetString(GL_VERSION));
LOG("GL vendor: %s\n", glGetString(GL_VENDOR));
LOG("GL renderer: %s\n", glGetString(GL_RENDERER));
GLfloat width_range[2];
glGetFloatv(GL_ALIASED_LINE_WIDTH_RANGE, width_range);
LOG("GL line range: %f - %f\n", width_range[0], width_range[1]);
LOG("GL line range: %f - %f", width_range[0], width_range[1]);
LOG("Screen Size: %f %f", width, height);
}
@@ -540,7 +550,7 @@ bool App::mouse_down(int button, float x, float y)
e.m_type = button ? kEventType::MouseDownR : kEventType::MouseDownL;
e.m_pos = { x / zoom, y / zoom };
auto ret = layout[main_id]->on_event(&e);
LOG("mouse click button%d pos %f %f\n", button, x, y);
LOG("mouse click button%d pos %f %f", button, x, y);
// if (popup)
// {
@@ -574,7 +584,7 @@ bool App::mouse_up(int button, float x, float y)
e.m_pos = { x / zoom, y / zoom };
auto ret = layout[main_id]->on_event(&e);
layout[main_id]->update();
LOG("mouse up button%d pos %f %f\n", button, x, y);
LOG("mouse up button%d pos %f %f", button, x, y);
return ret == kEventResult::Consumed;
}
bool App::key_down(int key)

View File

@@ -106,19 +106,19 @@ bool ui::BrushMesh::create()
"}";
static const char* shader_stroke_inst_f =
SHADER_VERSION
"uniform sampler2D tex;"
"uniform vec4 col;"
"in float alpha;"
"in vec3 uv;"
"out vec4 frag;"
"uniform mediump sampler2D tex;"
"uniform mediump vec4 col;"
"in mediump float alpha;"
"in mediump vec3 uv;"
"out mediump vec4 frag;"
"void main(){"
" float a = (1.0 - texture(tex, uv.xy).r) * alpha;"
" mediump float a = (1.0 - texture(tex, uv.xy).r) * alpha;"
" frag = vec4(col.rgb, a);"
"}";
if (!shader.create(shader_stroke_inst_v, shader_stroke_inst_f))
LOG("Failed to create shader Texture");
LOG("Failed to create shader BrushMesh Stroke");
loc_flow = shader.GetAttribLocation("a_flow");
loc_mvp = shader.GetAttribLocation("a_mvp");

View File

@@ -16,11 +16,7 @@ bool Font::load(const char* ttf, int font_size)
auto bitmap = std::make_unique<uint8_t[]>(w*h);
chars.resize(num_chars);
int ret = stbtt_BakeFontBitmap(file.m_data, 0, (float)font_size, bitmap.get(), w, h, start_char, num_chars, chars.data());
#ifdef __APPLE__
font_tex.create(w, h, GL_RED, bitmap.get());
#else
font_tex.create(w, h, GL_LUMINANCE, bitmap.get());
#endif // __APPLE__
file.close();
return true;
}

View File

@@ -559,10 +559,10 @@ bool LayoutManager::load(const char* path)
auto id_str = current->Attribute("id");
if (!id_str)
{
LOG("Layout node without id\n");
LOG("Layout node without id");
return false;
}
LOG("Parsing layout: %s\n", id_str);
LOG("Parsing layout: %s", id_str);
uint16_t id = const_hash(id_str);
auto p = m_layouts.find(id);
if (p == m_layouts.end())
@@ -589,7 +589,7 @@ bool LayoutManager::load(const char* path)
}
else
{
LOG("Layout id \"%s\" duplicated\n", id_str);
LOG("Layout id \"%s\" duplicated", id_str);
}
current = current->NextSiblingElement("layout");
}

View File

@@ -1869,7 +1869,7 @@ public:
glGetIntegerv(GL_VIEWPORT, vp);
glGetFloatv(GL_COLOR_CLEAR_VALUE, cc);
glClearColor(1, 0, 0, 1);
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
float zoom = root()->m_zoom;
auto box = m_clip * zoom;

71
engine/log.cpp Normal file
View File

@@ -0,0 +1,71 @@
#include "pch.h"
#include "log.h"
LogRemote LogRemote::I;
static size_t data_handler(void *contents, size_t size, size_t nmemb, void *userp)
{
auto buffer = reinterpret_cast<std::string*>(userp);
buffer->append((char*)contents, size * nmemb);
return size * nmemb;
}
void LogRemote::start()
{
m_running = true;
m_thread = std::thread([&] {
net_init();
auto session_string = net_request("/start");
m_session = atoi(session_string.c_str());
while (m_running)
{
auto m = m_mq.Get();
auto escaped = curl_easy_escape(curl, m.c_str(), (int)m.size());
auto data = std::make_unique<char[]>(m.size() + 64);
int sz = snprintf(data.get(), m.size() + 64, "session=%d&m=%s", m_session, escaped);
curl_free(escaped);
net_request("/log", std::string(data.get(), sz));
}
net_close();
});
}
void LogRemote::net_init()
{
if (!(curl = curl_easy_init()))
return;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &this->readBuffer);
//curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, data_handler);
}
std::string LogRemote::net_request(std::string cmd, std::string data /*= ""*/)
{
readBuffer.clear();
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
auto url = m_url + cmd;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
res = curl_easy_perform(curl);
return readBuffer;
}
void LogRemote::net_close()
{
if (curl)
curl_easy_cleanup(curl);
curl = nullptr;
}
void LogRemote::log(const char* format, ...)
{
static char buffer[4096];
va_list arglist;
va_start(arglist, format);
int n = vsnprintf(buffer, sizeof(buffer), format, arglist);
va_end(arglist);
m_mq.Post(std::string(buffer, n));
}
LogRemote::~LogRemote()
{
m_running = false;
m_mq.UnlockGetters();
if (m_thread.joinable())
m_thread.join();
}

31
engine/log.h Normal file
View File

@@ -0,0 +1,31 @@
#pragma once
#include "util.h"
#ifdef __APPLE__
#define LOG(M,...) { printf(M"\n", ##__VA_ARGS__); LogRemote::I.log(M, ##__VA_ARGS__); }
#elif __ANDROID__
#define LOG(...) { ((void)__android_log_print(ANDROID_LOG_INFO, "native-engine", __VA_ARGS__)); LogRemote::I.log(__VA_ARGS__); }
#elif _WIN32
#define LOG(M,...) { printf(M"\n", ##__VA_ARGS__); LogRemote::I.log(M, ##__VA_ARGS__); }
#endif
class LogRemote
{
public:
static LogRemote I;
bool m_running;
std::thread m_thread;
ui::BlockingQueue<std::string> m_mq;
CURL *curl = nullptr;
CURLcode res;
std::string readBuffer;
std::string m_url = "http://omigamedev.ddns.net:8083";
int m_session;
void start();
void net_init();
std::string net_request(std::string cmd, std::string data = "");
void net_close();
void log(const char* format, ...);
~LogRemote();
};

View File

@@ -367,9 +367,9 @@ int main()
if (glewInit() != GLEW_OK)
return 0;
LOG("GL version: %s\n", glGetString(GL_VERSION));
LOG("GL vendor: %s\n", glGetString(GL_VENDOR));
LOG("GL renderer: %s\n", glGetString(GL_RENDERER));
LOG("GL version: %s", glGetString(GL_VERSION));
LOG("GL vendor: %s", glGetString(GL_VENDOR));
LOG("GL renderer: %s", glGetString(GL_RENDERER));
// If supported create a 3.1 context
if (wglewIsSupported("WGL_ARB_create_context"))
@@ -378,7 +378,8 @@ int main()
{
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
WGL_CONTEXT_MINOR_VERSION_ARB, 1,
WGL_CONTEXT_FLAGS_ARB, 0,
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
0
};
int pixel_attribs[] =
@@ -391,8 +392,8 @@ int main()
WGL_COLOR_BITS_ARB, 32,
WGL_DEPTH_BITS_ARB, 24,
WGL_STENCIL_BITS_ARB, 8,
WGL_SAMPLE_BUFFERS_ARB, 1, // Number of buffers (must be 1 at time of writing)
WGL_SAMPLES_ARB, 4, // Number of samples
// WGL_SAMPLE_BUFFERS_ARB, 1, // Number of buffers (must be 1 at time of writing)
// WGL_SAMPLES_ARB, 4, // Number of samples
0
};
UINT numFormat;

View File

@@ -5,3 +5,10 @@
#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h>
#ifdef DEBUG
#pragma comment (lib, "libcurl_debug.lib")
#else
#pragma comment (lib, "libcurl.lib")
#endif // DEBUG

View File

@@ -9,7 +9,6 @@
#include <sys/types.h>
#include <dirent.h>
#define LOG(M,...) printf(M"\n", ##__VA_ARGS__)
#define SHADER_VERSION "#version 150\n"
#elif __ANDROID__
@@ -21,7 +20,6 @@
#include <android/log.h>
#include <android_native_app_glue.h>
#define LOG(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-engine", __VA_ARGS__))
#define SHADER_VERSION "#version 300 es\n"
#elif _WIN32
@@ -32,8 +30,8 @@
#include <gl\wglew.h>
#include <gl\GL.h>
#define LOG(M,...) printf(M"\n", ##__VA_ARGS__)
#define SHADER_VERSION "#version 150\n"
#define SHADER_VERSION "#version 150\n"
#endif
#define NS_START namespace ui {
@@ -44,13 +42,16 @@
#include <cmath>
#include <stack>
#include <regex>
#include <mutex>
#include <memory>
#include <string>
#include <vector>
#include <random>
#include <thread>
#include <iostream>
#include <algorithm>
#include <functional>
#include <condition_variable>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_SWIZZLE
@@ -68,3 +69,6 @@
#include <yoga/Yoga.h>
#include <stb/stb_truetype.h>
#include <stb/stb_image.h>
#include <curl/curl.h>
#include "log.h"

View File

@@ -57,20 +57,20 @@ bool RTT::create(int width, int height, int tex/* = -1*/)
glBindTexture(GL_TEXTURE_2D, texID);
if (tex == -1)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);
// Create a renderbuffer object to store depth info
glGenRenderbuffers(1, &rboID);
glBindRenderbuffer(GL_RENDERBUFFER, rboID);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
// glGenRenderbuffers(1, &rboID);
// glBindRenderbuffer(GL_RENDERBUFFER, rboID);
// glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
// glBindRenderbuffer(GL_RENDERBUFFER, 0);
// Create a framebuffer object
glGenFramebuffers(1, &fboID);
@@ -80,7 +80,7 @@ bool RTT::create(int width, int height, int tex/* = -1*/)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texID, 0);
// Attach the renderbuffer to depth attachment point
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboID);
//glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboID);
// Check FBO status
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);

View File

@@ -31,13 +31,13 @@ bool Texture2D::create(int width, int height, GLint format, const uint8_t* data)
m_format = format;
glGenTextures(1, &m_tex);
bind();
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, format, GL_UNSIGNED_BYTE, data);
unbind();
return true;
}
bool Texture2D::create(const ui::Image& img)
{
static GLint formats[] = { 0, 0, GL_RGB, GL_RGBA };
static GLint formats[] = { GL_RED, GL_RG, GL_RGB, GL_RGBA };
return create(img.width, img.height, formats[img.comp - 1], img.data());
}

View File

@@ -33,3 +33,32 @@ glm::vec3 convert_rgb2hsv(const glm::vec3 c)
float e = 1.0e-10f;
return glm::vec3(fabs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
static const char* gl2str(GLenum err)
{
switch (err)
{
case GL_NO_ERROR: return "GL_NO_ERROR";
case GL_INVALID_ENUM: return "GL_INVALID_ENUM";
case GL_INVALID_VALUE: return "GL_INVALID_VALUE";
case GL_INVALID_OPERATION: return "GL_INVALID_OPERATION";
case GL_INVALID_FRAMEBUFFER_OPERATION: return "GL_INVALID_FRAMEBUFFER_OPERATION";
case GL_OUT_OF_MEMORY: return "GL_OUT_OF_MEMORY";
default: return "Unknown";
}
}
void check_OpenGLError(const char* stmt, const char* fname, int line)
{
GLenum err;
while ((err = glGetError()) != GL_NO_ERROR)
{
LOG("OpenGL error %08x (%s), at %s:%i - for %s", err, gl2str(err), fname, line, stmt);
}
}
#ifdef _DEBUG
#define GL(stmt) stmt; CheckOpenGLError(#stmt, __FILE__, __LINE__);
#else
#define GL(stmt) stmt
#endif

View File

@@ -11,3 +11,102 @@ bool point_in_rect(const glm::vec2& point, const glm::vec4& rect);
glm::vec4 rand_color();
glm::vec3 convert_hsv2rgb(const glm::vec3 c);
glm::vec3 convert_rgb2hsv(const glm::vec3 c);
//void check_OpenGLError(const char* stmt, const char* fname, int line)
//{
// GLenum err;
// while ((err = glGetError()) != GL_NO_ERROR)
// {
// //LOG("OpenGL error %08x (%s), at %s:%i - for %s", err, gluErrorString(err), fname, line, stmt);
// }
//}
template<typename T, int N> struct cbuffer
{
T m_vec[N];
int m_capacity;
int m_count;
int m_index;
cbuffer()
{
m_capacity = N;
m_index = 0;
m_count = 0;
}
T& head()
{
return m_index == 0 ? m_vec[m_count - 1] : m_vec[m_index - 1];
}
void add(const T& v)
{
m_vec[m_index] = v;
m_index = (m_index + 1) % m_capacity;
m_count = m_count < m_capacity ? m_count + 1 : m_count;
}
int count() const
{
return m_count;
}
template<typename T2 = T> T2 average() const
{
T2 tot{};
if (m_count == 0)
return tot;
for (int i = 0; i < m_count; i++)
tot += m_vec[i];
return tot / (float)m_count;
}
};
NS_START
template<typename T, int Max = 0>
class BlockingQueue
{
std::deque<T> q;
std::condition_variable post_cv;
std::condition_variable get_cv;
mutable std::mutex mutex;
public:
volatile bool unlocked = false;
BlockingQueue() = default;
BlockingQueue(const BlockingQueue& other) = delete;
BlockingQueue& operator=(const BlockingQueue& other) = delete;
BlockingQueue(BlockingQueue&& other) : q(std::move(q)) { }
BlockingQueue& operator=(BlockingQueue&& other) { q = std::move(q); return *this; }
void Post(T&& pkt)
{
std::unique_lock<std::mutex> lock(mutex);
if (Max > 0)
{
post_cv.wait(lock, [&]() { return unlocked | (q.size() < Max); });
if (q.size() >= Max) return;
}
q.push_back(std::forward<T>(pkt));
get_cv.notify_one();
}
T Get()
{
static T emptyT{};
std::unique_lock<std::mutex> lock(mutex);
get_cv.wait(lock, [&]() { return unlocked | (q.size() > 0); });
if (q.empty())
return std::move(emptyT);
T tmp = std::move(q.front());
q.pop_front();
if (Max > 0) post_cv.notify_all();
return std::move(tmp);
}
void UnlockGetters()
{
unlocked = true;
get_cv.notify_all();
if (Max > 0) post_cv.notify_all();
}
int Size() const
{
std::lock_guard<std::mutex> lock(mutex);
return (int)q.size();
}
};
NS_END