From a4ac73529ed7311de94c97e6fe1ef46f984ea21e Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Sun, 15 Jan 2023 20:45:52 +0100 Subject: [PATCH] some dom stuff --- .clangd | 1 + meson.build | 4 +- src/dom.cpp | 30 +++++++++++++ src/dom.hpp | 92 ++++++++++++++++++++++++++++++++++++++ src/main.cpp | 13 +----- src/{all.hpp => utils.hpp} | 0 6 files changed, 126 insertions(+), 14 deletions(-) create mode 100644 src/dom.cpp create mode 100644 src/dom.hpp rename src/{all.hpp => utils.hpp} (100%) diff --git a/.clangd b/.clangd index 2d53df1..7c6ddb1 100644 --- a/.clangd +++ b/.clangd @@ -19,6 +19,7 @@ Diagnostics: CheckOptions: UnusedIncludes: Strict + cppcoreguidelines-special-member-functions.AllowSoleDefaultDtor: true # typecase rules readability-identifier-naming.ClassCase: CamelCase diff --git a/meson.build b/meson.build index 420d375..2941d51 100644 --- a/meson.build +++ b/meson.build @@ -17,10 +17,10 @@ add_project_arguments( ) sources = [] -subdir('browser') +subdir('src') executable( - 'web-browser', + 'browser', sources, win_subsystem: 'console', include_directories: [], diff --git a/src/dom.cpp b/src/dom.cpp new file mode 100644 index 0000000..c31896d --- /dev/null +++ b/src/dom.cpp @@ -0,0 +1,30 @@ +#include "dom.hpp" + +namespace dom { + +auto Box::size() const noexcept -> Size +{ + auto new_size = Size {}; + for (const auto& child : children) + new_size += child->size() + Size { 5, 5 }; + new_size += { 5, 5 }; + return new_size; +} + +auto Box::render(Renderer& renderer, Position position) const noexcept -> void +{ + renderer.draw_rectangle(position, size(), color); + auto child_position = position + Position { 5, 5 }; + for (const auto& child : children) { + child->render(renderer, child_position); + child_position.y += child->size().height + 5; + } +} + +auto Rectangle::render(Renderer& renderer, Position position) const noexcept + -> void +{ + renderer.draw_rectangle(position, size(), color); +} + +} diff --git a/src/dom.hpp b/src/dom.hpp new file mode 100644 index 0000000..0f4c533 --- /dev/null +++ b/src/dom.hpp @@ -0,0 +1,92 @@ +#pragma once + +#include +#include +#include + +namespace dom { + +template using Ref = std::shared_ptr; + +struct Position { + auto operator+=(const Position& other) noexcept -> Position& + { + x += other.x; + y += other.y; + return *this; + } + int x, y; +}; + +auto operator+(const Position& a, const Position& b) noexcept -> Position +{ + return { a.x + b.x, a.y + b.y }; +} + +struct Size { + auto operator+=(const Size& other) noexcept -> Size& + { + width += other.width; + height += other.height; + return *this; + } + friend auto operator+(const Size& a, const Size& b) noexcept -> Size; + + int width, height; +}; + +auto operator+(const Size& a, const Size& b) noexcept -> Size +{ + return { a.width + b.width, a.height + b.height }; +} + +struct Color { + static const auto opaque = uint8_t { 255 }; + static const auto transparent = uint8_t { 0 }; + + uint8_t red, green, blue, alpha { opaque }; +}; + +struct Renderer { + Renderer() = default; + virtual ~Renderer() = default; + virtual auto draw_rectangle( + Position position, Size size, Color color) const noexcept -> void + = 0; +}; + +struct Element { + Element() = default; + virtual ~Element() = default; + [[nodiscard]] virtual auto size() const noexcept -> Size = 0; + virtual auto render(Renderer& renderer, Position position) const noexcept + -> void + = 0; +}; + +struct Box final : public Element { + [[nodiscard]] auto size() const noexcept -> Size override; + auto render(Renderer& renderer, Position position) const noexcept + -> void override; + auto add_child(Ref element) noexcept + { + children.push_back(element); + } + + std::vector> children; + Color color {}; +}; + +struct Rectangle final : public Element { + Rectangle(Size size) + : m_size { size } + { } + [[nodiscard]] auto size() const noexcept -> Size override { return m_size; } + auto render(Renderer& renderer, Position position) const noexcept + -> void override; + + Color color {}; + Size m_size { 50, 50 }; +}; + +} diff --git a/src/main.cpp b/src/main.cpp index 562e092..0486dce 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,8 +3,7 @@ #include "SDL_rect.h" #include "SDL_render.h" #include "SDL_video.h" -#include "scriptlang/parser.hpp" -#include "utils/all.hpp" +#include "utils.hpp" #include #include #include @@ -70,16 +69,6 @@ private: auto main() -> int { - const auto* text = "{name: \"test\", value: [true, false, 123, \"bruh\"], " - "int: 123, float: 3.14}"; - auto ast = scriptlang::Parser(text).parse_expression(true); - if (!ast) - fmt::print("parser error at {}:{}: {}\n\t\n", - ast.unwrap_error().span.from.line, - ast.unwrap_error().span.from.column, ast.unwrap_error().message); - else - fmt::print("ast = {}\n", ast.unwrap()->to_string()); - // test fmt::print("browser: hello world!\n"); auto gui = GUI::create().unwrap(); diff --git a/src/all.hpp b/src/utils.hpp similarity index 100% rename from src/all.hpp rename to src/utils.hpp