Browse Source

Fixed all warnings in the project

pull/1/head
gyroninja 3 years ago
parent
commit
99611cc6c4

+ 2
- 1
Makefile View File

@@ -1,5 +1,6 @@
CXX = g++
CXXFLAGS = -O3 -flto=8
WARNINGS = -Werror -pedantic -Wall -Wextra -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2 -Winit-self -Wlogical-op -Wmissing-declarations -Wmissing-include-dirs -Wnoexcept -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-null-sentinel -Wstrict-overflow=5 -Wswitch-default -Wundef -Wno-unused -Wzero-as-null-pointer-constant -Wuseless-cast
EXECUTABLE = netrunner
INCPATH = -I /usr/include/freetype2 -I /usr/include/harfbuzz
LINK = g++
@@ -36,7 +37,7 @@ endif
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp | shaders
@mkdir -p $(@D)
@mkdir -p $(subst gen,d,$(@D))
$(CXX) -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td $(CXXFLAGS) $(INCPATH) -c -o $@ $<
$(CXX) -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td $(CXXFLAGS) $(INCPATH) $(WARNINGS) -c -o $@ $<
@mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d && touch $@

$(DEPDIR)/%d: ;

+ 1
- 0
README.md View File

@@ -42,3 +42,4 @@ In the face of recent changes in Firefox, some anons were asking for a /g/'s per
- Make text components more efficient (no longer have seperate vao, vbo, and texture for every character)
- Handle more HTTP status codes
- Add address field
- Support inlining nodes

+ 13
- 11
src/graphics/opengl/Window.cpp View File

@@ -39,7 +39,7 @@ bool Window::initGLFW() {
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

window = glfwCreateWindow(640, 480, "NetRunner", NULL, NULL);
window = glfwCreateWindow(640, 480, "NetRunner", nullptr, nullptr);
if (!window) {
glfwTerminate();
std::cout << "Could not create window" << std::endl;
@@ -47,9 +47,9 @@ bool Window::initGLFW() {
}
glfwSetWindowUserPointer(window, this);
glfwGetFramebufferSize(window, &windowWidth, &windowHeight);
glfwSetFramebufferSizeCallback(window, [](GLFWwindow *window, int width, int height) {
glfwSetFramebufferSizeCallback(window, [](GLFWwindow *win, int width, int height) {
glViewport(0, 0, width, height);
Window *thiz = reinterpret_cast<Window*>(glfwGetWindowUserPointer(window));
Window *thiz = reinterpret_cast<Window*>(glfwGetWindowUserPointer(win));
thiz->windowWidth = width;
thiz->windowHeight = height;
for (const std::unique_ptr<BoxComponent> &boxComponent : thiz->boxComponents) {
@@ -62,8 +62,8 @@ bool Window::initGLFW() {
thiz->y -= textComponent->height;
}
});
glfwSetScrollCallback(window, [](GLFWwindow *window, double xOffset, double yOffset) {
Window *thiz = reinterpret_cast<Window*>(glfwGetWindowUserPointer(window));
glfwSetScrollCallback(window, [](GLFWwindow *win, double xOffset, double yOffset) {
Window *thiz = reinterpret_cast<Window*>(glfwGetWindowUserPointer(win));
thiz->transformMatrix[13] += -yOffset * 0.1;
thiz->transformMatrixDirty = true;
});
@@ -105,25 +105,27 @@ bool Window::initGL() {
textureProgram = compileProgram(textureVertexShader, textureFragmentShader);
glDeleteShader(textureVertexShader);
glDeleteShader(textureFragmentShader);

return true;
}

const GLuint Window::compileShader(const GLenum shaderType, const char *shaderSource) const {
GLuint Window::compileShader(const GLenum shaderType, const char *shaderSource) const {
const GLuint shader = glCreateShader(shaderType);
glShaderSource(shader, 1, &shaderSource, NULL);
glShaderSource(shader, 1, &shaderSource, nullptr);
glCompileShader(shader);

GLint success;
GLchar infoLog[1024];
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
glGetShaderInfoLog(shader, 1024, nullptr, infoLog);
std::cout << "Could not compile shader\n" << infoLog << std::endl;
}

return shader;
}

const GLuint Window::compileProgram(const GLuint vertexShader, const GLuint fragmentShader) const {
GLuint Window::compileProgram(const GLuint vertexShader, const GLuint fragmentShader) const {
const GLuint program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
@@ -133,7 +135,7 @@ const GLuint Window::compileProgram(const GLuint vertexShader, const GLuint frag
GLchar infoLog[1024];
glGetProgramiv(program, GL_COMPILE_STATUS, &success);
if (!success) {
glGetProgramInfoLog(program, 1024, NULL, infoLog);
glGetProgramInfoLog(program, 1024, nullptr, infoLog);
std::cout << "Could not compile program\n" << infoLog << std::endl;
}

@@ -153,7 +155,7 @@ void Window::render() {
}
glUseProgram(fontProgram);
if (transformMatrixDirty) {
GLuint transformLocation = glGetUniformLocation(fontProgram, "transform");
GLint transformLocation = glGetUniformLocation(fontProgram, "transform");
glUniformMatrix4fv(transformLocation, 1, GL_FALSE, transformMatrix);
transformMatrixDirty = false;
}

+ 2
- 2
src/graphics/opengl/Window.h View File

@@ -20,8 +20,8 @@ public:
bool initGLFW();
bool initGLEW() const;
bool initGL();
const GLuint compileShader(const GLenum shaderType, const char *shaderSource) const;
const GLuint compileProgram(const GLuint vertexShader, const GLuint fragmentShader) const;
GLuint compileShader(const GLenum shaderType, const char *shaderSource) const;
GLuint compileProgram(const GLuint vertexShader, const GLuint fragmentShader) const;
void render();
void setDOM(const std::shared_ptr<Node> rootNode);
void drawNode(const std::shared_ptr<Node> rootNode);

+ 37
- 37
src/graphics/opengl/components/BoxComponent.cpp View File

@@ -2,36 +2,36 @@
#include "../../../../anime.h"
#include <cmath>

BoxComponent::BoxComponent(const float x, const float y, const float width, const float height, const int windowWidth, const int windowHeight) {
this->x = x;
this->y = y;
this->width = width;
this->height = height;
BoxComponent::BoxComponent(const float rawX, const float rawY, const float rawWidth, const float rawHeight, const int windowWidth, const int windowHeight) {
x = rawX;
y = rawY;
width = rawWidth;
height = rawHeight;

if (width == 512) {
for (int y = 0; y < 1024; y++) {
for (int x = 0; x < 1024; x++) {
for (int py = 0; py < 1024; py++) {
for (int px = 0; px < 1024; px++) {
for (int i = 0; i < 4; i++) {
data[1023 - y][x][i] = anime.pixel_data[((x * 4) + (y * 4 * 1024)) + i];
data[1023 - py][px][i] = anime.pixel_data[((px * 4) + (py * 4 * 1024)) + i];
}
}
}
}
else {
for (int y = 0; y < 1024; y++) {
for (int x = 0; x < 1024; x++) {
for (int py = 0; py < 1024; py++) {
for (int px = 0; px < 1024; px++) {
for (int i = 0; i < 3; i++) {
data[1023 - y][x][i] = 0x88;
data[1023 - py][px][i] = 0x88;
}
data[1023 - y][x][3] = 0xff;
data[1023 - py][px][3] = 0xff;
}
}
}

float vx = x;
float vy = y;
float vWidth = width;
float vHeight = height;
float vx = rawX;
float vy = rawY;
float vWidth = rawWidth;
float vHeight = rawHeight;
pointToViewport(vx, vy, windowWidth, windowHeight);
distanceToViewport(vWidth, vHeight, windowWidth, windowHeight);

@@ -56,10 +56,10 @@ BoxComponent::BoxComponent(const float x, const float y, const float width, cons
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObject);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*) (0 * sizeof(float)));
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), nullptr);
glEnableVertexAttribArray(0);

glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*) (3 * sizeof(float)));
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), reinterpret_cast<void*>(3 * sizeof(float)));
glEnableVertexAttribArray(1);

glGenTextures(1, &texture);
@@ -84,7 +84,7 @@ void BoxComponent::render() {
}
glBindVertexArray(vertexArrayObject);
glBindTexture(GL_TEXTURE_2D, texture);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
}

void BoxComponent::resize(const int windowWidth, const int windowHeight) {
@@ -107,30 +107,30 @@ void BoxComponent::resize(const int windowWidth, const int windowHeight) {
verticesDirty = true;
}

void BoxComponent::pointToViewport(float &x, float &y, const int windowWidth, const int windowHeight) const {
if (x < 0) {
x += windowWidth;
void BoxComponent::pointToViewport(float &rawX, float &rawY, const int windowWidth, const int windowHeight) const {
if (rawX < 0) {
rawX += windowWidth;
}
if (y < 0) {
y += windowHeight;
if (rawY < 0) {
rawY += windowHeight;
}
if (x > 1) {
x /= windowWidth;
if (rawX > 1) {
rawX /= windowWidth;
}
if (y > 1) {
y /= windowHeight;
if (rawY > 1) {
rawY /= windowHeight;
}
x = (x * 2) - 1;
y = (y * 2) - 1;
rawX = (rawX * 2) - 1;
rawY = (rawY * 2) - 1;
}

void BoxComponent::distanceToViewport(float &x, float &y, const int windowWidth, const int windowHeight) const {
if (std::abs(x) > 1) {
x /= windowWidth;
void BoxComponent::distanceToViewport(float &rawX, float &rawY, const int windowWidth, const int windowHeight) const {
if (std::abs(rawX) > 1) {
rawX /= windowWidth;
}
if (std::abs(y) > 1) {
y /= windowHeight;
if (std::abs(rawY) > 1) {
rawY /= windowHeight;
}
x *= 2;
y *= 2;
rawX *= 2;
rawY *= 2;
}

+ 3
- 3
src/graphics/opengl/components/BoxComponent.h View File

@@ -26,12 +26,12 @@ private:
GLuint elementBufferObject = 0;
GLuint texture = 0;
public:
BoxComponent(const float x, const float y, const float width, const float height, const int windowWidth, const int windowHeight);
BoxComponent(const float rawX, const float rawY, const float rawWidth, const float rawHeight, const int windowWidth, const int windowHeight);
~BoxComponent();
void render();
void resize(const int width, const int height);
void pointToViewport(float &x, float &y, const int windowWidth, const int windowHeight) const ;
void distanceToViewport(float &x, float &y, const int windowWidth, const int windowHeight) const ;
void pointToViewport(float &rawX, float &rawY, const int windowWidth, const int windowHeight) const ;
void distanceToViewport(float &rawX, float &rawY, const int windowWidth, const int windowHeight) const ;
};

#endif

+ 20
- 17
src/graphics/text/TextRasterizer.cpp View File

@@ -3,8 +3,8 @@
#include <limits>
#include <iostream>

TextRasterizer::TextRasterizer(const std::string &fontPath, const int fontSize, const int resolution, const bool bold) {
this->fontSize = fontSize;
TextRasterizer::TextRasterizer(const std::string &fontPath, const int size, const unsigned int resolution, const bool bold) {
fontSize = size;
FT_Init_FreeType(&lib);

face = std::make_unique<FT_Face>();
@@ -21,7 +21,7 @@ TextRasterizer::TextRasterizer(const std::string &fontPath, const int fontSize,
return;
}

font = hb_ft_font_create(*face, NULL);
font = hb_ft_font_create(*face, nullptr);

buffer = hb_buffer_create();
if (!hb_buffer_allocation_successful(buffer)) {
@@ -43,7 +43,10 @@ std::unique_ptr<const Glyph[]> TextRasterizer::rasterize(const std::string &text

hb_buffer_add_utf8(buffer, text.c_str(), text.length(), 0, text.length());

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
const hb_feature_t KerningOn = { HB_TAG('k', 'e', 'r', 'n'), 1, 0, std::numeric_limits<unsigned int>::max() };
#pragma GCC diagnostic pop
hb_shape(font, buffer, &KerningOn, 1);

const hb_glyph_info_t *glyphInfo = hb_buffer_get_glyph_infos(buffer, &glyphCount);
@@ -54,7 +57,7 @@ std::unique_ptr<const Glyph[]> TextRasterizer::rasterize(const std::string &text
int cx = x;
int cy = y;

for (int i = 0; i < glyphCount; i++) {
for (unsigned int i = 0; i < glyphCount; i++) {
if (FT_Load_Glyph(*face, glyphInfo[i].codepoint, FT_LOAD_DEFAULT)) {
std::cout << "Could not load glyph" << std::endl;
return nullptr;
@@ -71,15 +74,15 @@ std::unique_ptr<const Glyph[]> TextRasterizer::rasterize(const std::string &text

glyphs[i].textureWidth = pow(2, ceil(log(ftBitmap.width) / log(2)));
glyphs[i].textureHeight = pow(2, ceil(log(ftBitmap.rows) / log(2)));
glyphs[i].textureData = std::make_unique<unsigned char[]>(glyphs[i].textureWidth * glyphs[i].textureHeight);
for (int iy = 0; iy < ftBitmap.rows; iy++) {
memcpy(glyphs[i].textureData.get() + iy * glyphs[i].textureWidth, ftBitmap.buffer + iy * ftBitmap.width, ftBitmap.width);
glyphs[i].textureData = std::make_unique<unsigned char[]>(static_cast<size_t>(glyphs[i].textureWidth * glyphs[i].textureHeight));
for (unsigned int iy = 0; iy < ftBitmap.rows; iy++) {
memcpy(glyphs[i].textureData.get() + iy * static_cast<unsigned int>(glyphs[i].textureWidth), ftBitmap.buffer + iy * static_cast<unsigned int>(ftBitmap.width), ftBitmap.width);
}

const float xa = (float) glyphPos[i].x_advance / 64;
const float ya = (float) glyphPos[i].y_advance / 64;
const float xo = (float) glyphPos[i].x_offset / 64;
const float yo = (float) glyphPos[i].y_offset / 64;
const float xa = static_cast<float>(glyphPos[i].x_advance) / 64;
const float ya = static_cast<float>(glyphPos[i].y_advance) / 64;
const float xo = static_cast<float>(glyphPos[i].x_offset) / 64;
const float yo = static_cast<float>(glyphPos[i].y_offset) / 64;

glyphs[i].x0 = cx + xo + slot->bitmap_left;
glyphs[i].y0 = floor(cy + yo + slot->bitmap_top);
@@ -88,8 +91,8 @@ std::unique_ptr<const Glyph[]> TextRasterizer::rasterize(const std::string &text

glyphs[i].s0 = 0.0f;
glyphs[i].t0 = 0.0f;
glyphs[i].s1 = (float) ftBitmap.width / glyphs[i].textureWidth;
glyphs[i].t1 = (float) ftBitmap.rows / glyphs[i].textureHeight;
glyphs[i].s1 = static_cast<float>(ftBitmap.width) / glyphs[i].textureWidth;
glyphs[i].t1 = static_cast<float>(ftBitmap.rows) / glyphs[i].textureHeight;

cx += xa;
cy += ya;
@@ -110,10 +113,10 @@ std::unique_ptr<const Glyph[]> TextRasterizer::rasterize(const std::string &text
return glyphs;
}

const bool TextRasterizer::isUnicodeBMP(const FT_Face &face) const {
for (int i = 0; i < face->num_charmaps; i++) {
if (((face->charmaps[i]->platform_id == 0) && (face->charmaps[i]->encoding_id == 3)) || ((face->charmaps[i]->platform_id == 3) && (face->charmaps[i]->encoding_id == 1))) {
FT_Set_Charmap(face, face->charmaps[i]);
bool TextRasterizer::isUnicodeBMP(const FT_Face &ftFace) const {
for (int i = 0; i < ftFace->num_charmaps; i++) {
if (((ftFace->charmaps[i]->platform_id == 0) && (ftFace->charmaps[i]->encoding_id == 3)) || ((ftFace->charmaps[i]->platform_id == 3) && (ftFace->charmaps[i]->encoding_id == 1))) {
FT_Set_Charmap(ftFace, ftFace->charmaps[i]);
return true;
}
}

+ 2
- 2
src/graphics/text/TextRasterizer.h View File

@@ -24,10 +24,10 @@ class TextRasterizer {
private:
int fontSize;
public:
TextRasterizer(const std::string &fontPath, const int size, const int resolution, const bool bold);
TextRasterizer(const std::string &fontPath, const int size, const unsigned int resolution, const bool bold);
~TextRasterizer();
std::unique_ptr<const Glyph[]> rasterize(const std::string &text, const int x, const int y, const int windowWidth, const int windowHeight, float &height, unsigned int &glyphCount) const;
const bool isUnicodeBMP(const FT_Face &face) const;
bool isUnicodeBMP(const FT_Face &face) const;
FT_Library lib;
hb_font_t *font;
hb_buffer_t *buffer;

+ 11
- 10
src/html/HTMLParser.cpp View File

@@ -1,8 +1,11 @@
#include "HTMLParser.h"
#include "TextNode.h"
#include <algorithm>
#include <iostream>
#include <memory>

void printNode(const std::shared_ptr<Node> node, const int indent);

void printNode(const std::shared_ptr<Node> node, const int indent) {
for (int i = 0; i < indent; i++) {
std::cout << '\t';
@@ -32,9 +35,8 @@ void printNode(const std::shared_ptr<Node> node, const int indent) {
std::shared_ptr<Node> HTMLParser::parse(const std::string &html) const {
std::shared_ptr<Node> rootNode = std::make_shared<Node>(NodeType::ROOT);
std::shared_ptr<Node> currentNode = rootNode;
std::vector<int> starts;
int cursor;
int start = 0;
std::vector<unsigned int> starts;
unsigned int cursor;
int state = 0;
for (cursor = 0; cursor < html.length(); cursor++) { // TODO handle trying to look ahead past string
if (state == 0) { // Neutral
@@ -86,37 +88,36 @@ std::shared_ptr<Node> HTMLParser::parse(const std::string &html) const {
}
else if (state == 2) { // Tag
if (html[cursor] == '>') {
const int start = starts.back();
std::string element = html.substr(starts.back(), cursor - starts.back() + 1);
starts.pop_back();
std::string element = html.substr(start, cursor - start + 1);
parseTag(element, *dynamic_cast<TagNode*>(currentNode.get()));
state = 0;
}
}
else if (state == 3) { // Text
if (html[cursor + 1] == '<') {
const int start = starts.back();
dynamic_cast<TextNode*>(currentNode.get())->text = html.substr(starts.back(), cursor - starts.back() + 1);
starts.pop_back();
dynamic_cast<TextNode*>(currentNode.get())->text = html.substr(start, cursor - start + 1);
currentNode = currentNode->parent;
state = 0;
}
}
}

// printNode(rootNode, 0);
printNode(rootNode, 0);
return rootNode;
}

void HTMLParser::parseTag(const std::string &element, TagNode &tagNode) const {
int cursor;
int start = 1; // skip first <
unsigned int cursor;
unsigned int start = 1; // skip first <
int state = 0;
std::string propertyKey;
for (cursor = 0; cursor < element.length(); cursor++) {
if (state == 0) {
if (element[cursor] == ' ' || element[cursor] == '>') {
tagNode.tag = element.substr(start, cursor - start);
std::transform(tagNode.tag.begin(), tagNode.tag.end(), tagNode.tag.begin(), tolower);
start = cursor + 1;
state = 1;
}

+ 2
- 2
src/html/Node.cpp View File

@@ -1,7 +1,7 @@
#include "Node.h"

Node::Node(NodeType nodeType) {
this->nodeType = nodeType;
Node::Node(NodeType type) {
nodeType = type;
}

Node::~Node() {

+ 1
- 1
src/html/Node.h View File

@@ -13,7 +13,7 @@ enum class NodeType {

class Node {
public:
Node(NodeType nodeType);
Node(NodeType type);
virtual ~Node();
NodeType nodeType;
std::shared_ptr<Node> parent;

+ 1
- 0
src/html/TagNode.cpp View File

@@ -1,6 +1,7 @@
#include "TagNode.h"

const Element TagNode::elements[] = {
{"", nullptr}
};

TagNode::TagNode() : Node(NodeType::TAG) {

+ 1
- 1
src/html/elements/BLOCKQUOTEElement.cpp View File

@@ -2,5 +2,5 @@
#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, windowWidth, windowHeight);
return std::make_unique<TextComponent>(static_cast<const TextNode&>(node).text, 0, y, 12, false, 0x000000FF, windowWidth, windowHeight);
}

+ 1
- 1
src/html/elements/H1Element.cpp View File

@@ -1,5 +1,5 @@
#include "H1Element.h"

std::unique_ptr<Component> H1Element::render(const Node &node, int y, int windowWidth, int windowHeight) {
return std::make_unique<TextComponent>(static_cast<const TextNode&>(node).text, 0, y, 24, true, windowWidth, windowHeight);
return std::make_unique<TextComponent>(static_cast<const TextNode&>(node).text, 0, y, 24, true, 0x000000FF, windowWidth, windowHeight);
}

+ 1
- 1
src/html/elements/H2Element.cpp View File

@@ -1,5 +1,5 @@
#include "H2Element.h"

std::unique_ptr<Component> H2Element::render(const Node &node, int y, int windowWidth, int windowHeight) {
return std::make_unique<TextComponent>(static_cast<const TextNode&>(node).text, 0, y, 18, true, windowWidth, windowHeight);
return std::make_unique<TextComponent>(static_cast<const TextNode&>(node).text, 0, y, 18, true, 0x000000FF, windowWidth, windowHeight);
}

+ 1
- 1
src/html/elements/H3Element.cpp View File

@@ -1,5 +1,5 @@
#include "H3Element.h"

std::unique_ptr<Component> H3Element::render(const Node &node, int y, int windowWidth, int windowHeight) {
return std::make_unique<TextComponent>(static_cast<const TextNode&>(node).text, 0, y, 14, true, windowWidth, windowHeight); // Should be 14.04pt
return std::make_unique<TextComponent>(static_cast<const TextNode&>(node).text, 0, y, 14, true, 0x000000FF, windowWidth, windowHeight); // Should be 14.04pt
}

+ 1
- 1
src/html/elements/LIElement.cpp View File

@@ -1,5 +1,5 @@
#include "LIElement.h"

std::unique_ptr<Component> LIElement::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, windowWidth, windowHeight);
return std::make_unique<TextComponent>("• " + static_cast<const TextNode&>(node).text, 0, y, 12, false, 0x000000FF, windowWidth, windowHeight);
}

+ 1
- 1
src/html/elements/PElement.cpp View File

@@ -1,5 +1,5 @@
#include "PElement.h"

std::unique_ptr<Component> PElement::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, windowWidth, windowHeight);
return std::make_unique<TextComponent>(static_cast<const TextNode&>(node).text, 0, y, 12, false, 0x000000FF, windowWidth, windowHeight);
}

+ 1
- 1
src/html/elements/SPANElement.cpp View File

@@ -2,7 +2,7 @@

std::unique_ptr<Component> SPANElement::render(const Node &node, int y, int windowWidth, int windowHeight) {
if (node.parent->children.size() == 1) {
return std::make_unique<TextComponent>(static_cast<const TextNode&>(node).text, 0, y, 12, false, windowWidth, windowHeight);
return std::make_unique<TextComponent>(static_cast<const TextNode&>(node).text, 0, y, 12, false, 0x000000FF, windowWidth, windowHeight);
}
return nullptr;
}

+ 11
- 5
src/main.cpp View File

@@ -8,11 +8,15 @@
#include <iostream>
#include <memory>

const std::string getDocumentFromURL(const std::string &url);
const std::string getHostFromURL(const std::string &url);
void handleRequest(const HTTPResponse &response);

const std::unique_ptr<Window> window = std::make_unique<Window>();

const std::string getDocumentFromURL(const std::string &url) {
int slashes = 0;
for (int i = 0; i < url.length(); i++) {
for (unsigned int i = 0; i < url.length(); i++) {
if (url[i] == '/') {
slashes++;
if (slashes == 3) {
@@ -20,12 +24,13 @@ const std::string getDocumentFromURL(const std::string &url) {
}
}
}
return "";
}

const std::string getHostFromURL(const std::string &url) {
int slashes = 0;
int start = 0;
for (int i = 0; i < url.length(); i++) {
unsigned int start = 0;
for (unsigned int i = 0; i < url.length(); i++) {
if (url[i] == '/') {
if (slashes == 2) {
return url.substr(start, i - start);
@@ -34,6 +39,7 @@ const std::string getHostFromURL(const std::string &url) {
start = i + 1;
}
}
return "";
}

void handleRequest(const HTTPResponse &response) {
@@ -42,7 +48,7 @@ void handleRequest(const HTTPResponse &response) {
const std::clock_t begin = clock();
std::shared_ptr<Node> rootNode = parser->parse(response.body);
const std::clock_t end = clock();
std::cout << "Parsed document in: " << std::fixed << (((double) (end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
std::cout << "Parsed document in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
window->setDOM(rootNode);
}
else if (response.statusCode == 301) {
@@ -69,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 << ((((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;
}

+ 18
- 16
src/networking/HTTPRequest.cpp View File

@@ -6,15 +6,15 @@
#include <sys/types.h>
#include <sys/socket.h>

HTTPRequest::HTTPRequest(const std::string &host, const std::string &document) {
this->document = document;
this->version = Version::HTTP10;
this->method = Method::GET;
this->host = host;
this->userAgent = "NetRunner";
HTTPRequest::HTTPRequest(const std::string &hostName, const std::string &doc) {
document = doc;
version = Version::HTTP10;
method = Method::GET;
host = hostName;
userAgent = "NetRunner";
}

const bool HTTPRequest::sendRequest(std::function<void(const HTTPResponse&)> responseCallback) const {
bool HTTPRequest::sendRequest(std::function<void(const HTTPResponse&)> responseCallback) const {
struct addrinfo hints;
struct addrinfo *serverInfo;
memset(&hints, 0, sizeof(hints));
@@ -41,7 +41,7 @@ const bool HTTPRequest::sendRequest(std::function<void(const HTTPResponse&)> res
}

const std::string request = methodToString(method) + std::string(" ") + document + std::string(" ") + versionToString(version) + std::string("\r\nHost: ") + host + std::string("\r\nUser-Agent: ") + userAgent + std::string("\r\n\r\n");
const size_t sent = send(sock, request.c_str(), request.length(), 0);
const ssize_t sent = send(sock, request.c_str(), request.length(), 0);
if (sent == -1) {
std::cout << "Could not send \"" << request << "\": " << errno << std::endl;
freeaddrinfo(serverInfo);
@@ -50,9 +50,9 @@ const bool HTTPRequest::sendRequest(std::function<void(const HTTPResponse&)> res

std::string response;
char buffer[512];
size_t received;
ssize_t received;
while ((received = recv(sock, buffer, sizeof(buffer), 0)) != 0) {
response += std::string(buffer, received);
response += std::string(buffer, static_cast<unsigned int>(received));
}

responseCallback(HTTPResponse(response));
@@ -61,20 +61,22 @@ const bool HTTPRequest::sendRequest(std::function<void(const HTTPResponse&)> res
return true;
}

const std::string HTTPRequest::versionToString(const Version version) const {
switch (version) {
const std::string HTTPRequest::versionToString(const Version ver) const {
switch (ver) {
case Version::HTTP10:
return "HTTP/1.0";
default:
return "ERROR";
}
return "ERROR";
}

const std::string HTTPRequest::methodToString(const Method method) const {
switch (method) {
const std::string HTTPRequest::methodToString(const Method meth) const {
switch (meth) {
case Method::GET:
return "GET";
case Method::POST:
return "POST";
default:
return "ERROR";
}
return "ERROR";
}

+ 2
- 2
src/networking/HTTPRequest.h View File

@@ -16,8 +16,8 @@ enum class Method {

class HTTPRequest {
public:
HTTPRequest(const std::string &host, const std::string &document);
const bool sendRequest(std::function<void(const HTTPResponse&)> responseCallback) const;
HTTPRequest(const std::string &hostName, const std::string &doc);
bool sendRequest(std::function<void(const HTTPResponse&)> responseCallback) const;
const std::string versionToString(const Version version) const;
const std::string methodToString(const Method method) const;
private:

+ 2
- 2
src/networking/HTTPResponse.cpp View File

@@ -2,8 +2,8 @@

HTTPResponse::HTTPResponse(const std::string &rawResponse) {
int state = 0;
int cursor = 0;
int start = 0;
unsigned int cursor = 0;
unsigned int start = 0;
std::string propertyKey;
for (cursor = 0; cursor < rawResponse.length(); cursor++) {
if (state % 2 == 0 && rawResponse[cursor] == ' ') {

Loading…
Cancel
Save