Browse Source

Moved renderer code stuff out of the html module and into the graphics module

pull/1/head
gyroninja 5 years ago
parent
commit
c31fe92747
  1. 2
      src/graphics/components/BoxComponent.cpp
  2. 3
      src/graphics/components/BoxComponent.h
  3. 0
      src/graphics/components/Component.cpp
  4. 3
      src/graphics/components/Component.h
  5. 42
      src/graphics/components/ComponentBuilder.cpp
  6. 30
      src/graphics/components/ComponentBuilder.h
  7. 3
      src/graphics/components/TextComponent.cpp
  8. 5
      src/graphics/components/TextComponent.h
  9. 0
      src/graphics/elements/AElement.cpp
  10. 6
      src/graphics/elements/AElement.h
  11. 1
      src/graphics/elements/BLOCKQUOTEElement.cpp
  12. 6
      src/graphics/elements/BLOCKQUOTEElement.h
  13. 0
      src/graphics/elements/H1Element.cpp
  14. 6
      src/graphics/elements/H1Element.h
  15. 0
      src/graphics/elements/H2Element.cpp
  16. 6
      src/graphics/elements/H2Element.h
  17. 0
      src/graphics/elements/H3Element.cpp
  18. 6
      src/graphics/elements/H3Element.h
  19. 0
      src/graphics/elements/LIElement.cpp
  20. 6
      src/graphics/elements/LIElement.h
  21. 0
      src/graphics/elements/PElement.cpp
  22. 6
      src/graphics/elements/PElement.h
  23. 0
      src/graphics/elements/SPANElement.cpp
  24. 6
      src/graphics/elements/SPANElement.h
  25. 29
      src/graphics/opengl/Window.cpp
  26. 8
      src/graphics/opengl/Window.h
  27. 4
      src/html/HTMLParser.cpp
  28. 3
      src/html/Node.h
  29. 13
      src/html/TagNode.cpp
  30. 4
      src/html/TagNode.h
  31. 32
      src/html/TextNode.cpp
  32. 4
      src/html/TextNode.h
  33. 2
      src/main.cpp

2
src/graphics/opengl/components/BoxComponent.cpp → src/graphics/components/BoxComponent.cpp

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
#include "BoxComponent.h"
#include "../../../../anime.h"
#include "../../../anime.h"
#include <cmath>
BoxComponent::BoxComponent(const float rawX, const float rawY, const float rawWidth, const float rawHeight, const int windowWidth, const int windowHeight) {

3
src/graphics/opengl/components/BoxComponent.h → src/graphics/components/BoxComponent.h

@ -6,9 +6,6 @@ @@ -6,9 +6,6 @@
class BoxComponent : public Component {
private:
float x;
float y;
float width;
float vertices[20] = {
0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f, 1.0f,

0
src/graphics/opengl/components/Component.cpp → src/graphics/components/Component.cpp

3
src/graphics/opengl/components/Component.h → src/graphics/components/Component.h

@ -4,7 +4,10 @@ @@ -4,7 +4,10 @@
class Component {
public:
virtual ~Component();
float x;
float y;
float height;
float width;
};
#endif

42
src/graphics/components/ComponentBuilder.cpp

@ -0,0 +1,42 @@ @@ -0,0 +1,42 @@
#include "ComponentBuilder.h"
#include <iostream>
const ElementRendererMap ComponentBuilder::tagRenderers {
};
const ElementRendererMap ComponentBuilder::textRenderers {
{"a", &AElement::render},
{"blockquote", &BLOCKQUOTEElement::render},
{"h1", &H1Element::render},
{"h2", &H2Element::render},
{"h3", &H3Element::render},
{"li", &LIElement::render},
{"p", &PElement::render},
{"span", &SPANElement::render}
};
std::unique_ptr<Component> ComponentBuilder::build(const std::shared_ptr<Node> node, int y, int windowWidth, int windowHeight) {
std::unique_ptr<Component> component;
if (node->nodeType == NodeType::TAG) {
TagNode *tagNode = dynamic_cast<TagNode*>(node.get());
if (tagNode) {
ElementRendererMap::const_iterator tagRenderer = tagRenderers.find(tagNode->tag);
if (tagRenderer != tagRenderers.end()) {
component = tagRenderer->second(*tagNode, y, windowWidth, windowHeight);
}
}
}
else if (node->nodeType == NodeType::TEXT) {
TagNode *tagNode = dynamic_cast<TagNode*>(node->parent.get());
if (tagNode) {
ElementRendererMap::const_iterator textRenderer = textRenderers.find(tagNode->tag);
if (textRenderer != textRenderers.end()) {
TextNode *textNode = dynamic_cast<TextNode*>(node.get());
if (textNode) {
component = textRenderer->second(*textNode, y, windowWidth, windowHeight);
}
}
}
}
return component;
}

30
src/graphics/components/ComponentBuilder.h

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
#ifndef COMPONENTBUILDER_H
#define COMPONENTBUILDER_H
#include <algorithm>
#include <memory>
#include <unordered_map>
#include "Component.h"
#include "../elements/AElement.h"
#include "../elements/BLOCKQUOTEElement.h"
#include "../elements/H1Element.h"
#include "../elements/H2Element.h"
#include "../elements/H3Element.h"
#include "../elements/LIElement.h"
#include "../elements/PElement.h"
#include "../elements/SPANElement.h"
#include "../../html/TagNode.h"
#include "../../html/TextNode.h"
#include "../../html/Node.h"
typedef std::unordered_map<std::string, std::function<std::unique_ptr<Component>(const Node &node, int y, int windowWidth, int windowHeight)>> ElementRendererMap;
class ComponentBuilder {
private:
const static ElementRendererMap tagRenderers;
const static ElementRendererMap textRenderers;
public:
std::unique_ptr<Component> build(const std::shared_ptr<Node> node, int y, int windowWidth, int windowHeight);
};
#endif

3
src/graphics/opengl/components/TextComponent.cpp → src/graphics/components/TextComponent.cpp

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
#include "TextComponent.h"
#include <iostream>
#include <ctime>
TextComponent::TextComponent(const std::string &rawText, const int rawX, const int rawY, const int size, const bool bolded, const unsigned int hexColor, const int windowWidth, const int windowHeight) {
text = rawText;
x = rawX;

5
src/graphics/opengl/components/TextComponent.h → src/graphics/components/TextComponent.h

@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
#define TEXTCOMPONENT_H
#include <GL/glew.h>
#include "../../text/TextRasterizer.h"
#include "../text/TextRasterizer.h"
#include "Component.h"
#include <array>
#include <memory>
@ -11,8 +11,6 @@ @@ -11,8 +11,6 @@
class TextComponent : public Component {
private:
std::string text;
float x;
float y;
int fontSize;
bool bold;
unsigned int color;
@ -21,7 +19,6 @@ private: @@ -21,7 +19,6 @@ private:
0, 1, 2,
0, 2, 3
};
unsigned char data[1024][1024][4];
std::unique_ptr<const Glyph[]> glyphs;
std::vector<std::unique_ptr<float[]>> glyphVertices;
std::vector<GLuint> vertexArrayObjects;

0
src/html/elements/AElement.cpp → src/graphics/elements/AElement.cpp

6
src/html/elements/AElement.h → src/graphics/elements/AElement.h

@ -1,9 +1,9 @@ @@ -1,9 +1,9 @@
#ifndef AELEMENT_H
#define AELEMENT_H
#include "../../graphics/opengl/components/Component.h"
#include "../../graphics/opengl/components/TextComponent.h"
#include "../TextNode.h"
#include "../components/Component.h"
#include "../components/TextComponent.h"
#include "../../html/TextNode.h"
class AElement {
public:

1
src/html/elements/BLOCKQUOTEElement.cpp → src/graphics/elements/BLOCKQUOTEElement.cpp

@ -1,5 +1,4 @@ @@ -1,5 +1,4 @@
#include "BLOCKQUOTEElement.h"
#include "../TextNode.h"
std::unique_ptr<Component> BLOCKQUOTEElement::render(const Node &node, int y, int windowWidth, int windowHeight) {
return std::make_unique<TextComponent>(static_cast<const TextNode&>(node).text, 0, y, 12, false, 0x000000FF, windowWidth, windowHeight);

6
src/html/elements/BLOCKQUOTEElement.h → src/graphics/elements/BLOCKQUOTEElement.h

@ -1,9 +1,9 @@ @@ -1,9 +1,9 @@
#ifndef BLOCKQUOTEELEMENT_H
#define BLOCKQUOTEELEMENT_H
#include "../../graphics/opengl/components/Component.h"
#include "../../graphics/opengl/components/TextComponent.h"
#include "../TextNode.h"
#include "../components/Component.h"
#include "../components/TextComponent.h"
#include "../../html/TextNode.h"
class BLOCKQUOTEElement {
public:

0
src/html/elements/H1Element.cpp → src/graphics/elements/H1Element.cpp

6
src/html/elements/H1Element.h → src/graphics/elements/H1Element.h

@ -1,9 +1,9 @@ @@ -1,9 +1,9 @@
#ifndef H1ELEMENT_H
#define H1ELEMENT_H
#include "../../graphics/opengl/components/Component.h"
#include "../../graphics/opengl/components/TextComponent.h"
#include "../TextNode.h"
#include "../components/Component.h"
#include "../components/TextComponent.h"
#include "../../html/TextNode.h"
class H1Element {
public:

0
src/html/elements/H2Element.cpp → src/graphics/elements/H2Element.cpp

6
src/html/elements/H2Element.h → src/graphics/elements/H2Element.h

@ -1,9 +1,9 @@ @@ -1,9 +1,9 @@
#ifndef H2ELEMENT_H
#define H2ELEMENT_H
#include "../../graphics/opengl/components/Component.h"
#include "../../graphics/opengl/components/TextComponent.h"
#include "../TextNode.h"
#include "../components/Component.h"
#include "../components/TextComponent.h"
#include "../../html/TextNode.h"
class H2Element {
public:

0
src/html/elements/H3Element.cpp → src/graphics/elements/H3Element.cpp

6
src/html/elements/H3Element.h → src/graphics/elements/H3Element.h

@ -1,9 +1,9 @@ @@ -1,9 +1,9 @@
#ifndef H3ELEMENT_H
#define H3ELEMENT_H
#include "../../graphics/opengl/components/Component.h"
#include "../../graphics/opengl/components/TextComponent.h"
#include "../TextNode.h"
#include "../components/Component.h"
#include "../components/TextComponent.h"
#include "../../html/TextNode.h"
class H3Element {
public:

0
src/html/elements/LIElement.cpp → src/graphics/elements/LIElement.cpp

6
src/html/elements/LIElement.h → src/graphics/elements/LIElement.h

@ -1,9 +1,9 @@ @@ -1,9 +1,9 @@
#ifndef LIELEMENT_H
#define LIELEMENT_H
#include "../../graphics/opengl/components/Component.h"
#include "../../graphics/opengl/components/TextComponent.h"
#include "../TextNode.h"
#include "../components/Component.h"
#include "../components/TextComponent.h"
#include "../../html/TextNode.h"
class LIElement {
public:

0
src/html/elements/PElement.cpp → src/graphics/elements/PElement.cpp

6
src/html/elements/PElement.h → src/graphics/elements/PElement.h

@ -1,9 +1,9 @@ @@ -1,9 +1,9 @@
#ifndef PELEMENT_H
#define PELEMENT_H
#include "../../graphics/opengl/components/Component.h"
#include "../../graphics/opengl/components/TextComponent.h"
#include "../TextNode.h"
#include "../components/Component.h"
#include "../components/TextComponent.h"
#include "../../html/TextNode.h"
class PElement {
public:

0
src/html/elements/SPANElement.cpp → src/graphics/elements/SPANElement.cpp

6
src/html/elements/SPANElement.h → src/graphics/elements/SPANElement.h

@ -1,9 +1,9 @@ @@ -1,9 +1,9 @@
#ifndef SPANELEMENT_H
#define SPANELEMENT_H
#include "../../graphics/opengl/components/Component.h"
#include "../../graphics/opengl/components/TextComponent.h"
#include "../TextNode.h"
#include "../components/Component.h"
#include "../components/TextComponent.h"
#include "../../html/TextNode.h"
class SPANElement {
public:

29
src/graphics/opengl/Window.cpp

@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
#include "../../html/TagNode.h"
#include "../../html/TextNode.h"
#include <cmath>
#include <ctime>
#include <iostream>
Window::~Window() {
@ -67,6 +68,17 @@ bool Window::initGLFW() { @@ -67,6 +68,17 @@ bool Window::initGLFW() {
thiz->transformMatrix[13] += -yOffset * 0.1;
thiz->transformMatrixDirty = true;
});
// glfwSetMouseButtonCallback(window, [](GLFWWindow *win, int button, int action, int mods) {
// Window *thiz = reinterpret_cast<Window*>(glfwGetWindowUserPointer(win));
// if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) {
// for (const std::unique_ptr<Component> &component : thiz->components) {
//
// TextComponent *textComponent = dynamic_cast<TextComponent*>(component.get());
// textComponent->resize(0, thiz->y, width, height);
// thiz->y -= textComponent->height;
// }
// }
// })
glfwMakeContextCurrent(window);
return true;
@ -144,7 +156,11 @@ GLuint Window::compileProgram(const GLuint vertexShader, const GLuint fragmentSh @@ -144,7 +156,11 @@ GLuint Window::compileProgram(const GLuint vertexShader, const GLuint fragmentSh
void Window::render() {
if (domDirty) {
drawNode(domRootNode);
const std::clock_t begin = clock();
drawNode(domRootNode);
const std::clock_t end = clock();
std::cout << "Parsed dom in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
domDirty = false;
}
@ -173,13 +189,10 @@ void Window::setDOM(const std::shared_ptr<Node> rootNode) { @@ -173,13 +189,10 @@ void Window::setDOM(const std::shared_ptr<Node> rootNode) {
}
void Window::drawNode(const std::shared_ptr<Node> node) {
if (node->nodeType == NodeType::TEXT) {
TextNode *textNode = dynamic_cast<TextNode*>(node.get());
std::unique_ptr<Component> component = textNode->render(*textNode, y, windowWidth, windowHeight);
if (component) {
y -= component->height;
components.push_back(std::move(component));
}
std::unique_ptr<Component> component = componentBuilder.build(node, y, windowWidth, windowHeight);
if (component) {
y -= component->height;
components.push_back(std::move(component));
}
for (std::shared_ptr<Node> child : node->children) {
drawNode(child);

8
src/graphics/opengl/Window.h

@ -1,9 +1,10 @@ @@ -1,9 +1,10 @@
#ifndef WINDOW_H
#define WINDOW_H
#include "components/BoxComponent.h"
#include "components/TextComponent.h"
#include "components/Component.h"
#include "../components/BoxComponent.h"
#include "../components/TextComponent.h"
#include "../components/Component.h"
#include "../components/ComponentBuilder.h"
#include "../../html/Node.h"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
@ -38,6 +39,7 @@ public: @@ -38,6 +39,7 @@ public:
int windowHeight;
std::shared_ptr<Node> domRootNode;
bool domDirty = false;
ComponentBuilder componentBuilder;
std::vector<std::unique_ptr<BoxComponent>> boxComponents;
std::vector<std::unique_ptr<Component>> components;
int y = 950;

4
src/html/HTMLParser.cpp

@ -12,7 +12,7 @@ void printNode(const std::shared_ptr<Node> node, const int indent) { @@ -12,7 +12,7 @@ void printNode(const std::shared_ptr<Node> node, const int indent) {
}
if (node->nodeType == NodeType::ROOT) {
std::cout << "ROOT" << std::endl;
std::cout << "ROOT\n" << std::endl;
}
else if (node->nodeType == NodeType::TAG) {
std::cout << "TAG: " << dynamic_cast<TagNode*>(node.get())->tag << std::endl;
@ -104,7 +104,7 @@ std::shared_ptr<Node> HTMLParser::parse(const std::string &html) const { @@ -104,7 +104,7 @@ std::shared_ptr<Node> HTMLParser::parse(const std::string &html) const {
}
}
printNode(rootNode, 0);
// printNode(rootNode, 0);
return rootNode;
}

3
src/html/Node.h

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
#ifndef NODE_H
#define NODE_H
#include "../graphics/opengl/components/Component.h"
#include "../graphics/components/Component.h"
#include <memory>
#include <vector>
@ -18,6 +18,7 @@ public: @@ -18,6 +18,7 @@ public:
NodeType nodeType;
std::shared_ptr<Node> parent;
std::vector<std::shared_ptr<Node>> children;
std::shared_ptr<Component> component;
};
struct Element {

13
src/html/TagNode.cpp

@ -1,17 +1,4 @@ @@ -1,17 +1,4 @@
#include "TagNode.h"
const Element TagNode::elements[] = {
{"", nullptr}
};
TagNode::TagNode() : Node(NodeType::TAG) {
}
std::unique_ptr<Component> TagNode::render(const Node &node, int y, int windowWidth, int windowHeight) {
for (Element element : elements) {
if (element.tag == tag) {
return element.render(node, y, windowWidth, windowHeight);
}
}
return nullptr;
}

4
src/html/TagNode.h

@ -2,15 +2,11 @@ @@ -2,15 +2,11 @@
#define TAGNODE_H
#include "Node.h"
#include "../graphics/opengl/components/Component.h"
#include <map>
class TagNode : public Node {
private:
static const Element elements[];
public:
TagNode();
std::unique_ptr<Component> render(const Node &node, int y, int windowWidth, int windowHeight);
std::string tag;
std::map<std::string, std::string> properties;
};

32
src/html/TextNode.cpp

@ -1,36 +1,4 @@ @@ -1,36 +1,4 @@
#include "TextNode.h"
#include "TagNode.h"
#include "elements/AElement.h"
#include "elements/BLOCKQUOTEElement.h"
#include "elements/H1Element.h"
#include "elements/H2Element.h"
#include "elements/H3Element.h"
#include "elements/LIElement.h"
#include "elements/PElement.h"
#include "elements/SPANElement.h"
const Element TextNode::elements[] = {
{"a", &AElement::render},
{"blockquote", &BLOCKQUOTEElement::render},
{"h1", &H1Element::render},
{"h2", &H2Element::render},
{"h3", &H3Element::render},
{"li", &LIElement::render},
{"p", &PElement::render},
{"span", &SPANElement::render}
};
TextNode::TextNode() : Node(NodeType::TEXT) {
}
#include <iostream>
std::unique_ptr<Component> TextNode::render(const Node &node, int y, int windowWidth, int windowHeight) {
TagNode *tagNode = dynamic_cast<TagNode*>(node.parent.get());
if (tagNode) {
for (Element element : elements) {
if (element.tag == tagNode->tag) {
return element.render(node, y, windowWidth, windowHeight);
}
}
}
return nullptr;
}

4
src/html/TextNode.h

@ -2,14 +2,10 @@ @@ -2,14 +2,10 @@
#define TEXTNODE_H
#include "Node.h"
#include <map>
class TextNode : public Node {
private:
static const Element elements[];
public:
TextNode();
std::unique_ptr<Component> render(const Node &node, int y, int windowWidth, int windowHeight);
std::string text;
};

2
src/main.cpp

@ -75,7 +75,7 @@ int main(int argc, char *argv[]) { @@ -75,7 +75,7 @@ int main(int argc, char *argv[]) {
const std::clock_t begin = clock();
window->render();
const std::clock_t end = clock();
std::cout << '\r' << std::fixed << (((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) * 1000) << std::scientific << " ms/f " << std::flush;
// std::cout << '\r' << std::fixed << (((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) * 1000) << std::scientific << " ms/f " << std::flush;
}
return 0;
}

Loading…
Cancel
Save