added asset loading class, zoom factor, vbo switch, shader version
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include "util.h"
|
||||
#include "shader.h"
|
||||
#include "font.h"
|
||||
#include "asset.h"
|
||||
|
||||
enum class kAttribute : uint16_t
|
||||
{
|
||||
@@ -126,6 +127,7 @@ public:
|
||||
bool m_flood_events = false;
|
||||
bool m_destroyed = false;
|
||||
|
||||
float m_zoom = 1.f;
|
||||
glm::vec2 m_pos;
|
||||
glm::vec2 m_size;
|
||||
glm::vec4 m_clip;
|
||||
@@ -148,6 +150,7 @@ public:
|
||||
m_pos = o.m_pos;
|
||||
m_size = o.m_size;
|
||||
m_clip = o.m_clip;
|
||||
m_zoom = o.m_zoom;
|
||||
o.y_node = nullptr;
|
||||
o.parent = nullptr;
|
||||
}
|
||||
@@ -203,7 +206,7 @@ public:
|
||||
return o;
|
||||
}
|
||||
|
||||
void update(float width, float height);
|
||||
void update(float width, float height, float zoom);
|
||||
void update();
|
||||
void update_internal(const glm::vec2& origin, const glm::mat4& proj);
|
||||
virtual void parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr);
|
||||
@@ -220,6 +223,13 @@ public:
|
||||
c->watch(observer);
|
||||
}
|
||||
void destroy() { m_destroyed = true; }
|
||||
Node* root()
|
||||
{
|
||||
Node* ret = this;
|
||||
while (ret->parent)
|
||||
ret = ret->parent;
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<class T = Node> T* find(const char* ids)
|
||||
{
|
||||
@@ -726,14 +736,22 @@ public:
|
||||
// icons: http://www.famfamfam.com/lab/icons/silk/
|
||||
// regex css -> spritesheet.txt: \.([^{]+) {\s+width: (\d+)px;\s+height: (\d+)px;\s+.*: -(\d+)px -(\d+)px;\s+}\s+
|
||||
// to: "\1",\2,\3,\4,\5\n
|
||||
static char str[256];
|
||||
Asset file;
|
||||
if (!(file.open("data/spritesheet.txt") && file.read_all()))
|
||||
return;
|
||||
char* data = (char*)file.m_data;
|
||||
int size = file.m_len;
|
||||
static char name[256];
|
||||
int x, y, w, h;
|
||||
FILE* f = fopen("data/spritesheet.txt", "r");
|
||||
while (!feof(f) && fscanf(f, "%s %d %d %d %d", str, &w, &h, &x, &y) == 5)
|
||||
char* s = strtok(data, "\n");
|
||||
int i = strlen(s) + 1;
|
||||
while (i < size && sscanf(s, "%s %d %d %d %d", name, &w, &h, &x, &y) == 5)
|
||||
{
|
||||
m_icons[str] = glm::vec4(x, y, x+w, y+h);
|
||||
m_icons[name] = glm::vec4(x, y, x + w, y + h);
|
||||
s = strtok(nullptr, "\n");
|
||||
i += strlen(s) + 1;
|
||||
}
|
||||
fclose(f);
|
||||
file.close();
|
||||
}
|
||||
virtual Node* clone_instantiate() const override { return new NodeIcon(); }
|
||||
virtual void clone_copy(Node* dest) const override
|
||||
@@ -772,11 +790,14 @@ public:
|
||||
std::unique_ptr<Plane> m_faces;
|
||||
std::unique_ptr<Sampler> m_sampler;
|
||||
uint16_t m_tex_id;
|
||||
glm::vec2 drag_start;
|
||||
glm::vec2 drag_end;
|
||||
bool dragging = false;
|
||||
float angle = 0.0f;
|
||||
float angle_old;
|
||||
|
||||
virtual void draw() override
|
||||
{
|
||||
static float angle = 0;
|
||||
angle += 0.05;
|
||||
glm::mat4 cam = glm::lookAt(glm::vec3(sinf(angle)*10, 0, -10), glm::vec3(0, 0, 0), glm::vec3(0, -1, 0));
|
||||
glm::mat4 proj = glm::perspective<float>(glm::radians(45.f), m_clip.z / m_clip.w, .1f, 100);
|
||||
|
||||
@@ -787,8 +808,9 @@ public:
|
||||
|
||||
glClearColor(1, 0, 0, 1);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
auto box = m_clip;
|
||||
glViewport(box.x, (int)(vp[3] - box.y - box.w), box.z, box.w);
|
||||
auto box = m_clip * root()->m_zoom;
|
||||
auto c = glm::vec4(box.x, (int)(vp[3] - box.y - box.w), box.z, box.w);
|
||||
glViewport(c.x, c.y, c.z, c.w);
|
||||
TextureManager::get(m_tex_id).bind();
|
||||
m_sampler->bind(0);
|
||||
glEnable(GL_BLEND);
|
||||
@@ -816,4 +838,29 @@ public:
|
||||
TextureManager::load("data/uvs.jpg");
|
||||
m_tex_id = const_hash("data/uvs.jpg");
|
||||
}
|
||||
virtual kEventResult handle_event(Event* e) override
|
||||
{
|
||||
Node::handle_event(e);
|
||||
switch (e->m_type)
|
||||
{
|
||||
case kEventType::MouseDownL:
|
||||
dragging = true;
|
||||
drag_end = drag_start = ((MouseEvent*)e)->m_pos;
|
||||
angle_old = angle;
|
||||
break;
|
||||
case kEventType::MouseUpL:
|
||||
dragging = false;
|
||||
break;
|
||||
case kEventType::MouseMove:
|
||||
if (dragging)
|
||||
{
|
||||
drag_end = ((MouseEvent*)e)->m_pos;
|
||||
angle = angle_old + (drag_end - drag_start).x * .01f;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return kEventResult::Consumed;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user