added asset loading class, zoom factor, vbo switch, shader version

This commit is contained in:
2017-03-13 01:16:20 +00:00
parent a2a221b17a
commit ee6d352fc6
25 changed files with 345 additions and 108 deletions

View File

@@ -3,6 +3,15 @@
App App::I; // singleton
#ifdef __APPLE__
#define SHADER_VERSION "#version 300 es\n"
#elif __ANDROID__
#define SHADER_VERSION "#version 300 es\n"
#elif _WIN32
#define SHADER_VERSION "#version 150\n"
#endif
void App::create()
{
width = 800;
@@ -12,7 +21,7 @@ void App::create()
void App::initShaders()
{
static const char* shader_v =
"#version 300 es\n"
SHADER_VERSION
"uniform mat4 mvp;"
"in vec4 pos;"
"in vec2 uvs;"
@@ -22,7 +31,7 @@ void App::initShaders()
" gl_Position = mvp * vec4(pos.xyz, 1.f);"
"}";
static const char* shader_f =
"#version 300 es\n"
SHADER_VERSION
"uniform sampler2D tex;"
"in vec3 uv;"
"out vec4 frag;"
@@ -31,7 +40,7 @@ void App::initShaders()
" frag = texture(tex, uv.xy);"
"}";
static const char* shader_uv_f =
"#version 300 es\n"
SHADER_VERSION
"uniform sampler2D tex;"
"in vec3 uv;"
"out vec4 frag;"
@@ -39,7 +48,7 @@ void App::initShaders()
" frag = vec4(uv.xy,0,1);"
"}";
static const char* shader_atlas_v =
"#version 300 es\n"
SHADER_VERSION
"uniform mat4 mvp;"
"uniform vec2 tof;"
"uniform vec2 tsz;"
@@ -51,7 +60,7 @@ void App::initShaders()
" gl_Position = mvp * vec4(pos, 0, 1);"
"}";
static const char* shader_atlas_f =
"#version 300 es\n"
SHADER_VERSION
"uniform sampler2D tex;"
"in vec2 uv;"
"out vec4 frag;"
@@ -59,21 +68,21 @@ void App::initShaders()
" frag = texture(tex, uv);"
"}";
static const char* shader_color_v =
"#version 300 es\n"
SHADER_VERSION
"uniform mat4 mvp;"
"in vec4 pos;"
"void main(){"
" gl_Position = mvp * pos;"
"}";
static const char* shader_color_f =
"#version 300 es\n"
SHADER_VERSION
"uniform vec4 col;"
"out vec4 frag;"
"void main(){"
" frag = col;"
"}";
static const char* shader_font_v =
"#version 300 es\n"
SHADER_VERSION
"uniform mat4 mvp;"
"in vec2 pos;"
"in vec2 uvs;"
@@ -83,7 +92,7 @@ void App::initShaders()
" gl_Position = mvp * vec4(pos, 0, 1);"
"}";
static const char* shader_font_f =
"#version 300 es\n"
SHADER_VERSION
"uniform sampler2D tex;"
"uniform vec4 col;"
"in vec2 uv;"
@@ -93,6 +102,7 @@ void App::initShaders()
" frag = vec4(col.rgb, a);"
"}";
LOG("initializing shaders");
if (!ShaderManager::create(kShader::Texture, shader_v, shader_f))
LOG("Failed to create shader Texture");
if (!ShaderManager::create(kShader::Color, shader_color_v, shader_color_f))
@@ -103,32 +113,36 @@ void App::initShaders()
LOG("Failed to create shader Font");
if (!ShaderManager::create(kShader::Atlas, shader_atlas_v, shader_atlas_f))
LOG("Failed to create shader Atlas");
LOG("shaders initialized");
}
void App::initAssets()
{
#if _WIN32
const char* ttf = "C:\\Windows\\Fonts\\arial.ttf";
#else
const char* ttf = "/Library/Fonts/Arial.ttf";
#endif
LOG("initializing assets");
FontManager::init();
FontManager::load(kFont::Arial_11, ttf, 15);
FontManager::load(kFont::Arial_30, ttf, 30);
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\n");
LOG("initializing assets completed");
}
void App::initLayout()
{
LOG("initializing layout statics");
NodeBorder::static_init();
NodeImage::static_init();
NodeIcon::static_init();
layout.on_loaded = [&] {
layout[main_id]->update(width, height);
LOG("initializing layout updating after load");
layout[main_id]->update(width, height, zoom);
LOG("initializing layout components");
if (auto* button = layout[main_id]->find<NodeButton>("btn-close"))
{
button->on_click = [] { exit(0); };
@@ -191,7 +205,9 @@ void App::initLayout()
toolbar->m_flood_events = true;
}
};
layout.load("data/layout2.xml");
LOG("initializing layout xml");
layout.load("data/layout.xml");
LOG("initializing layout completed");
}
void App::init()
@@ -219,7 +235,7 @@ void App::init()
initShaders();
initAssets();
initLayout();
initLayout();
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
@@ -243,6 +259,7 @@ void App::init()
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("Screen Size: %f %f", width, height);
}
void App::update(float dt)
@@ -251,14 +268,17 @@ void App::update(float dt)
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glClear(GL_COLOR_BUFFER_BIT);
layout.reload();
//layout.reload();
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;
glScissor((int)box.x-1, (int)(height - box.y - box.w)-1, (int)box.z+2, (int)box.w+2);
glm::vec4 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();
}
};
@@ -275,16 +295,16 @@ void App::resize(float w, float h)
width = w;
height = h;
if (auto* main = layout[main_id])
main->update(w, h);
main->update(w, h, zoom);
}
void App::mouse_down(int button, float x, float y)
{
MouseEvent e;
e.m_type = button ? kEventType::MouseDownR : kEventType::MouseDownL;
e.m_pos = { x, y };
e.m_pos = { x / zoom, y / zoom };
layout[main_id]->on_event(&e);
LOG("mouse click %f %f\n", x, y);
LOG("mouse click button%d pos %f %f\n", button, x, y);
if (popup)
{
@@ -295,7 +315,7 @@ void App::mouse_down(int button, float x, float y)
{
popup = (NodePopupMenu*)layout[const_hash("popup-menu")]->m_children[0]->clone();
popup->SetPositioning(YGPositionTypeAbsolute);
popup->SetPosition(x, y);
popup->SetPosition(x / zoom, y / zoom);
layout[main_id]->add_child(popup);
}
layout[main_id]->update();
@@ -304,7 +324,7 @@ void App::mouse_move(float x, float y)
{
MouseEvent e;
e.m_type = kEventType::MouseMove;
e.m_pos = { x, y };
e.m_pos = { x / zoom, y / zoom };
if (auto* main = layout[main_id])
main->on_event(&e);
}
@@ -312,7 +332,7 @@ void App::mouse_up(int button, float x, float y)
{
MouseEvent e;
e.m_type = button ? kEventType::MouseUpR : kEventType::MouseUpL;
e.m_pos = { x, y };
e.m_pos = { x / zoom, y / zoom };
layout[main_id]->on_event(&e);
layout[main_id]->update();
}