From b706100ddc1e43bcd02373ebd25195778f320ec2 Mon Sep 17 00:00:00 2001 From: MrLetsplay2003 Date: Mon, 7 Nov 2022 21:36:45 +0100 Subject: [PATCH] Restructure code --- CMakeLists.txt | 5 ++++ DEPENDENCIES.md | 1 + src/kekengine/cpp/{ => common}/defaults.cpp | 7 +++--- src/kekengine/cpp/{ => common}/engine.cpp | 8 ++++--- src/kekengine/cpp/{ => common}/input.cpp | 4 ++-- src/kekengine/cpp/{ => object}/gameobject.cpp | 0 src/kekengine/cpp/{ => object}/light.cpp | 0 src/kekengine/cpp/{ => object}/object.cpp | 0 src/kekengine/cpp/{ => object}/scene.cpp | 0 src/kekengine/cpp/physics/physics.cpp | 23 +++++++++++++++++++ src/kekengine/cpp/{ => render}/camera.cpp | 0 src/kekengine/cpp/{ => render}/fonts.cpp | 0 src/kekengine/cpp/{ => render}/mesh.cpp | 0 src/kekengine/cpp/{ => render}/shader.cpp | 0 src/kekengine/cpp/{ => render}/texture.cpp | 0 src/kekengine/cpp/{ => ui}/ui.cpp | 19 ++++++++------- src/kekengine/cpp/{ => ui}/uielements.cpp | 13 ++++++----- src/kekengine/cpp/{ => util}/errordialog.cpp | 0 src/kekengine/cpp/{ => util}/objparser.cpp | 0 src/kekengine/cpp/{ => util}/resource.cpp | 0 src/kekengine/cpp/{ => util}/unicode.cpp | 0 src/kekengine/cpp/{ => util}/utils.cpp | 0 src/kekengine/include/internal.h | 11 +++++---- src/kekengine/include/internal/physics.h | 11 +++++++++ src/kekengine/include/internal/ui.h | 11 +++++++++ src/kekengine/include/kekengine.h | 6 ++++- src/kekengine/include/physics.h | 8 +++++++ src/kekengine/include/ui.h | 10 ++++---- src/kekgame/cpp/kekgame.cpp | 2 +- 29 files changed, 106 insertions(+), 33 deletions(-) rename src/kekengine/cpp/{ => common}/defaults.cpp (95%) rename src/kekengine/cpp/{ => common}/engine.cpp (98%) rename src/kekengine/cpp/{ => common}/input.cpp (98%) rename src/kekengine/cpp/{ => object}/gameobject.cpp (100%) rename src/kekengine/cpp/{ => object}/light.cpp (100%) rename src/kekengine/cpp/{ => object}/object.cpp (100%) rename src/kekengine/cpp/{ => object}/scene.cpp (100%) create mode 100644 src/kekengine/cpp/physics/physics.cpp rename src/kekengine/cpp/{ => render}/camera.cpp (100%) rename src/kekengine/cpp/{ => render}/fonts.cpp (100%) rename src/kekengine/cpp/{ => render}/mesh.cpp (100%) rename src/kekengine/cpp/{ => render}/shader.cpp (100%) rename src/kekengine/cpp/{ => render}/texture.cpp (100%) rename src/kekengine/cpp/{ => ui}/ui.cpp (93%) rename src/kekengine/cpp/{ => ui}/uielements.cpp (89%) rename src/kekengine/cpp/{ => util}/errordialog.cpp (100%) rename src/kekengine/cpp/{ => util}/objparser.cpp (100%) rename src/kekengine/cpp/{ => util}/resource.cpp (100%) rename src/kekengine/cpp/{ => util}/unicode.cpp (100%) rename src/kekengine/cpp/{ => util}/utils.cpp (100%) create mode 100644 src/kekengine/include/internal/physics.h create mode 100644 src/kekengine/include/internal/ui.h create mode 100644 src/kekengine/include/physics.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ad2b8ee..3c0ec60 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,10 +63,15 @@ target_link_libraries(kekengine PUBLIC microtar_static) # Freetype find_package(PkgConfig REQUIRED) + pkg_check_modules(FREETYPE REQUIRED freetype2) target_link_libraries(kekengine PUBLIC ${FREETYPE_LIBRARIES}) target_include_directories(kekengine PUBLIC ${FREETYPE_INCLUDE_DIRS}) +pkg_check_modules(BULLET REQUIRED bullet) +target_link_libraries(kekengine PUBLIC ${BULLET_LIBRARIES}) +target_include_directories(kekengine PUBLIC ${BULLET_INCLUDE_DIRS}) + if(UNIX) target_link_libraries(kekengine PUBLIC glfw GLEW GL) diff --git a/DEPENDENCIES.md b/DEPENDENCIES.md index bbc287b..302b244 100644 --- a/DEPENDENCIES.md +++ b/DEPENDENCIES.md @@ -3,3 +3,4 @@ - stb_image, stb_image_write - microtar (included) - openvr (SteamVR), if compiling with KEKENGINE_VR +- bullet 3 diff --git a/src/kekengine/cpp/defaults.cpp b/src/kekengine/cpp/common/defaults.cpp similarity index 95% rename from src/kekengine/cpp/defaults.cpp rename to src/kekengine/cpp/common/defaults.cpp index 68e7fe1..82906cd 100644 --- a/src/kekengine/cpp/defaults.cpp +++ b/src/kekengine/cpp/common/defaults.cpp @@ -2,6 +2,7 @@ #include "ui.h" #include "uielements.h" #include "internal.h" +#include "internal/ui.h" namespace kek::Defaults { @@ -92,7 +93,7 @@ static void defaultMouseCallback(GLFWwindow *window, double x, double y, void *d case GLFWCursorMode::FREE: case GLFWCursorMode::HIDDEN: { - for(UIElement *element : kekData.uiElements) { + for(UIElement *element : kekData.ui->elements) { UIPoint childPos = element->getPosition(); if(element->hoverAll(UIPoint((int) x - childPos.x, (int) y - childPos.y), UIPoint((int) x, (int) y))) break; } @@ -116,7 +117,7 @@ static void defaultMouseButtonCallback(GLFWwindow* window, int button, int actio { double x, y; glfwGetCursorPos(window, &x, &y); - for(UIElement *element : kekData.uiElements) { + for(UIElement *element : kekData.ui->elements) { UIPoint childPos = element->getPosition(); if(element->clickAll(UIPoint((int) x - childPos.x, (int) y - childPos.y), UIPoint((int) x, (int) y), (GLFWMouseButton) button)) break; } @@ -142,7 +143,7 @@ void init() { Input::addMouseListener(MouseCallback(defaultMouseCallback, nullptr)); Input::addMouseButtonListener(MouseButtonCallback(defaultMouseButtonCallback, nullptr)); - options = new ButtonElement(px(0), px(100), px(100), px(50)); + options = new ButtonElement(uiPx(0), uiPx(100), uiPx(100), uiPx(50)); //UI::addElement(options); } diff --git a/src/kekengine/cpp/engine.cpp b/src/kekengine/cpp/common/engine.cpp similarity index 98% rename from src/kekengine/cpp/engine.cpp rename to src/kekengine/cpp/common/engine.cpp index 85a0f93..c7d9f7c 100644 --- a/src/kekengine/cpp/engine.cpp +++ b/src/kekengine/cpp/common/engine.cpp @@ -13,7 +13,6 @@ #include #include -#include "internal.h" #include "errordialog.h" #include "objparser.h" #include "resource.h" @@ -25,6 +24,9 @@ #include "ui.h" #include "uielements.h" +#include "internal.h" +#include "internal/ui.h" + kek::KekData kek::kekData; namespace kek::Engine { @@ -202,7 +204,7 @@ int init() { UI::init(); Defaults::init(); - fpsText = new TextElement(px(0), px(0)); + fpsText = new TextElement(uiPx(0), uiPx(0)); UI::addElement(fpsText); return KEK_SUCCESS; @@ -312,7 +314,7 @@ int start() { if(time != prevTime) fpsText->setText("FPS: " + std::to_string((int) floor(1.0f / kekData.lastFrameTime)) + " (" + std::to_string(kekData.lastFrameTime * 1000) + " ms)"); prevTime = time; - for(UIElement *uiEl : kekData.uiElements) { + for(UIElement *uiEl : kekData.ui->elements) { UIPoint pos = uiEl->getPosition(); uiEl->drawAll(pos, uiProjection); } diff --git a/src/kekengine/cpp/input.cpp b/src/kekengine/cpp/common/input.cpp similarity index 98% rename from src/kekengine/cpp/input.cpp rename to src/kekengine/cpp/common/input.cpp index edb4316..72cf4ca 100644 --- a/src/kekengine/cpp/input.cpp +++ b/src/kekengine/cpp/common/input.cpp @@ -92,11 +92,11 @@ GLFWKeyState getKeyState(KeyBinding binding) { void setCursorMode(GLFWCursorMode mode) { glfwSetInputMode(kekData.window, GLFW_CURSOR, (int) mode); - kekData.uiCursorMode = mode; + kekData.cursorMode = mode; } GLFWCursorMode getCursorMode() { - return kekData.uiCursorMode; + return kekData.cursorMode; } KeyboardCapture captureKeyboardInput(KeyCharCallback charCallback, KeyCallback keyCallback, Callable uncaptureCallback) { diff --git a/src/kekengine/cpp/gameobject.cpp b/src/kekengine/cpp/object/gameobject.cpp similarity index 100% rename from src/kekengine/cpp/gameobject.cpp rename to src/kekengine/cpp/object/gameobject.cpp diff --git a/src/kekengine/cpp/light.cpp b/src/kekengine/cpp/object/light.cpp similarity index 100% rename from src/kekengine/cpp/light.cpp rename to src/kekengine/cpp/object/light.cpp diff --git a/src/kekengine/cpp/object.cpp b/src/kekengine/cpp/object/object.cpp similarity index 100% rename from src/kekengine/cpp/object.cpp rename to src/kekengine/cpp/object/object.cpp diff --git a/src/kekengine/cpp/scene.cpp b/src/kekengine/cpp/object/scene.cpp similarity index 100% rename from src/kekengine/cpp/scene.cpp rename to src/kekengine/cpp/object/scene.cpp diff --git a/src/kekengine/cpp/physics/physics.cpp b/src/kekengine/cpp/physics/physics.cpp new file mode 100644 index 0000000..172a479 --- /dev/null +++ b/src/kekengine/cpp/physics/physics.cpp @@ -0,0 +1,23 @@ +#include "physics.h" + +#include "internal.h" +#include "internal/physics.h" + +namespace kek::Physics { + +void init() { + kekData.physics = new PhysicsData(); + + btDefaultCollisionConfiguration *collisionConf = new btDefaultCollisionConfiguration(); + btCollisionDispatcher *dispatcher = new btCollisionDispatcher(collisionConf); + btBroadphaseInterface *overlappingPairCache = new btDbvtBroadphase(); + btSequentialImpulseConstraintSolver *solver = new btSequentialImpulseConstraintSolver(); + btDiscreteDynamicsWorld *world = new btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConf); + world->setGravity(btVector3(0, -10, 0)); +} + +void destroy() { + delete kekData.physics; +} + +} diff --git a/src/kekengine/cpp/camera.cpp b/src/kekengine/cpp/render/camera.cpp similarity index 100% rename from src/kekengine/cpp/camera.cpp rename to src/kekengine/cpp/render/camera.cpp diff --git a/src/kekengine/cpp/fonts.cpp b/src/kekengine/cpp/render/fonts.cpp similarity index 100% rename from src/kekengine/cpp/fonts.cpp rename to src/kekengine/cpp/render/fonts.cpp diff --git a/src/kekengine/cpp/mesh.cpp b/src/kekengine/cpp/render/mesh.cpp similarity index 100% rename from src/kekengine/cpp/mesh.cpp rename to src/kekengine/cpp/render/mesh.cpp diff --git a/src/kekengine/cpp/shader.cpp b/src/kekengine/cpp/render/shader.cpp similarity index 100% rename from src/kekengine/cpp/shader.cpp rename to src/kekengine/cpp/render/shader.cpp diff --git a/src/kekengine/cpp/texture.cpp b/src/kekengine/cpp/render/texture.cpp similarity index 100% rename from src/kekengine/cpp/texture.cpp rename to src/kekengine/cpp/render/texture.cpp diff --git a/src/kekengine/cpp/ui.cpp b/src/kekengine/cpp/ui/ui.cpp similarity index 93% rename from src/kekengine/cpp/ui.cpp rename to src/kekengine/cpp/ui/ui.cpp index 5e40cba..25c9d7a 100644 --- a/src/kekengine/cpp/ui.cpp +++ b/src/kekengine/cpp/ui/ui.cpp @@ -3,6 +3,7 @@ #include #include "internal.h" +#include "internal/ui.h" namespace kek { @@ -258,23 +259,25 @@ UIElement *UIElement::dragEnterAll(UIPoint pos, UIPoint screenPos) { } void UI::init() { - kekData.uiDefaultFont = new Font(KEK_DEFAULT_FONT); - kekData.uiRectangleShader = new Shader("shader/rectangle/vertex.glsl", "shader/rectangle/fragment.glsl"); + kekData.ui = new UIData(); + kekData.ui->defaultFont = new Font(KEK_DEFAULT_FONT); + kekData.ui->rectangleShader = new Shader("shader/rectangle/vertex.glsl", "shader/rectangle/fragment.glsl"); } void UI::destroy() { - delete kekData.uiDefaultFont; - delete kekData.uiRectangleShader; + delete kekData.ui->defaultFont; + delete kekData.ui->rectangleShader; + delete kekData.ui; } void UI::addElement(UIElement *element) { - kekData.uiElements.push_back(element); + kekData.ui->elements.push_back(element); } void UI::removeElement(UIElement *element) { - for(auto it = kekData.uiElements.begin(); it < kekData.uiElements.end(); it++) { + for(auto it = kekData.ui->elements.begin(); it < kekData.ui->elements.end(); it++) { if(*it == element) { - kekData.uiElements.erase(it); + kekData.ui->elements.erase(it); delete element; break; } @@ -282,7 +285,7 @@ void UI::removeElement(UIElement *element) { } std::vector UI::getElements() { - return kekData.uiElements; + return kekData.ui->elements; } } diff --git a/src/kekengine/cpp/uielements.cpp b/src/kekengine/cpp/ui/uielements.cpp similarity index 89% rename from src/kekengine/cpp/uielements.cpp rename to src/kekengine/cpp/ui/uielements.cpp index 29d3109..17f3e1b 100644 --- a/src/kekengine/cpp/uielements.cpp +++ b/src/kekengine/cpp/ui/uielements.cpp @@ -3,6 +3,7 @@ #include #include "internal.h" +#include "internal/ui.h" namespace kek { @@ -14,7 +15,7 @@ TextElement::TextElement(UIValue x, UIValue y, Font *font): UIElement(x, y) { this->textBounds = TextBounds::SMALLEST; } -TextElement::TextElement(UIValue x, UIValue y): TextElement(x, y, kekData.uiDefaultFont) {} +TextElement::TextElement(UIValue x, UIValue y): TextElement(x, y, kekData.ui->defaultFont) {} TextElement::~TextElement() { delete text; @@ -139,14 +140,14 @@ UIBounds RectangleElement::getBounds() { } void RectangleElement::draw(UIPoint screenPos, glm::mat4 projection) { - kekData.uiRectangleShader->use(); + kekData.ui->rectangleShader->use(); UIBounds offset = getBounds(); - glUniformMatrix4fv(glGetUniformLocation(kekData.uiRectangleShader->id, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); + glUniformMatrix4fv(glGetUniformLocation(kekData.ui->rectangleShader->id, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); glm::vec4 bounds = glm::vec4(offset.x + screenPos.x, offset.y + screenPos.y, uiToScreen(w), uiToScreen(h)); - glUniform4fv(glGetUniformLocation(kekData.uiRectangleShader->id, "bounds"), 1, glm::value_ptr(bounds)); - glUniform4fv(glGetUniformLocation(kekData.uiRectangleShader->id, "rectColor"), 1, color.valuePointer()); + glUniform4fv(glGetUniformLocation(kekData.ui->rectangleShader->id, "bounds"), 1, glm::value_ptr(bounds)); + glUniform4fv(glGetUniformLocation(kekData.ui->rectangleShader->id, "rectColor"), 1, color.valuePointer()); glBindVertexArray(vao); glDrawArrays(GL_TRIANGLES, 0, 6); @@ -155,7 +156,7 @@ void RectangleElement::draw(UIPoint screenPos, glm::mat4 projection) { ButtonElement::ButtonElement(UIValue x, UIValue y, UIValue w, UIValue h): RectangleElement(x, y, w, h) { clickable = true; - text = new TextElement(pw(0.5), ph(0.5)); + text = new TextElement(uiPw(0.5), uiPh(0.5)); text->origin = Origin::CENTER; addChild(text); } diff --git a/src/kekengine/cpp/errordialog.cpp b/src/kekengine/cpp/util/errordialog.cpp similarity index 100% rename from src/kekengine/cpp/errordialog.cpp rename to src/kekengine/cpp/util/errordialog.cpp diff --git a/src/kekengine/cpp/objparser.cpp b/src/kekengine/cpp/util/objparser.cpp similarity index 100% rename from src/kekengine/cpp/objparser.cpp rename to src/kekengine/cpp/util/objparser.cpp diff --git a/src/kekengine/cpp/resource.cpp b/src/kekengine/cpp/util/resource.cpp similarity index 100% rename from src/kekengine/cpp/resource.cpp rename to src/kekengine/cpp/util/resource.cpp diff --git a/src/kekengine/cpp/unicode.cpp b/src/kekengine/cpp/util/unicode.cpp similarity index 100% rename from src/kekengine/cpp/unicode.cpp rename to src/kekengine/cpp/util/unicode.cpp diff --git a/src/kekengine/cpp/utils.cpp b/src/kekengine/cpp/util/utils.cpp similarity index 100% rename from src/kekengine/cpp/utils.cpp rename to src/kekengine/cpp/util/utils.cpp diff --git a/src/kekengine/include/internal.h b/src/kekengine/include/internal.h index 68d72b4..b7a3e73 100644 --- a/src/kekengine/include/internal.h +++ b/src/kekengine/include/internal.h @@ -13,6 +13,9 @@ namespace kek { +struct PhysicsData; +struct UIData; + struct ActiveKeyboardCapture { KeyboardCapture id = KEK_INVALID_ID; KeyCharCallback charCallback; @@ -43,11 +46,11 @@ struct KekData { FT_Library freetype; - std::vector uiElements; - Font *uiDefaultFont; - Shader *uiRectangleShader; - GLFWCursorMode uiCursorMode; ActiveKeyboardCapture activeKeyboardCapture; + GLFWCursorMode cursorMode; + + UIData *ui; + PhysicsData *physics; }; extern KekData kekData; diff --git a/src/kekengine/include/internal/physics.h b/src/kekengine/include/internal/physics.h new file mode 100644 index 0000000..8820cab --- /dev/null +++ b/src/kekengine/include/internal/physics.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace kek { + +struct PhysicsData { + btDynamicsWorld *world; +}; + +} diff --git a/src/kekengine/include/internal/ui.h b/src/kekengine/include/internal/ui.h new file mode 100644 index 0000000..c1bc33a --- /dev/null +++ b/src/kekengine/include/internal/ui.h @@ -0,0 +1,11 @@ +#pragma once + +namespace kek { + +struct UIData { + std::vector elements; + Font *defaultFont; + Shader *rectangleShader; +}; + +} diff --git a/src/kekengine/include/kekengine.h b/src/kekengine/include/kekengine.h index 12a5dc8..9e9ca21 100644 --- a/src/kekengine/include/kekengine.h +++ b/src/kekengine/include/kekengine.h @@ -1,19 +1,23 @@ #pragma once #include "camera.h" +#include "color.h" #include "constants.h" #include "engine.h" #include "errordialog.h" #include "fonts.h" #include "gameobject.h" +#include "input.h" +#include "light.h" #include "mesh.h" #include "object.h" #include "objparser.h" #include "resource.h" #include "scene.h" #include "shader.h" +#include "texture.h" #include "types.h" #include "ui.h" -#include "unicode.h" #include "uielements.h" +#include "unicode.h" #include "utils.h" diff --git a/src/kekengine/include/physics.h b/src/kekengine/include/physics.h new file mode 100644 index 0000000..28178de --- /dev/null +++ b/src/kekengine/include/physics.h @@ -0,0 +1,8 @@ +#pragma once + +namespace kek::Physics { + +void init(); +void destroy(); + +} diff --git a/src/kekengine/include/ui.h b/src/kekengine/include/ui.h index 6c3d734..6c32cc9 100644 --- a/src/kekengine/include/ui.h +++ b/src/kekengine/include/ui.h @@ -6,11 +6,11 @@ #include "fonts.h" #include "input.h" -#define px(val) UIValue(val, UIUnit::PIXELS) -#define pw(val) UIValue(val, UIUnit::PARENT_WIDTH) -#define ph(val) UIValue(val, UIUnit::PARENT_HEIGHT) -#define sw(val) UIValue(val, UIUnit::SCREEN_WIDTH) -#define sh(val) UIValue(val, UIUnit::SCREEN_HEIGHT) +#define uiPx(val) UIValue(val, UIUnit::PIXELS) +#define uiPw(val) UIValue(val, UIUnit::PARENT_WIDTH) +#define uiPh(val) UIValue(val, UIUnit::PARENT_HEIGHT) +#define uiSw(val) UIValue(val, UIUnit::SCREEN_WIDTH) +#define uiSh(val) UIValue(val, UIUnit::SCREEN_HEIGHT) namespace kek { diff --git a/src/kekgame/cpp/kekgame.cpp b/src/kekgame/cpp/kekgame.cpp index 6ef6b97..6186299 100644 --- a/src/kekgame/cpp/kekgame.cpp +++ b/src/kekgame/cpp/kekgame.cpp @@ -76,7 +76,7 @@ int main(int argc, char **argv) { Input::addKeyListener(KeyCallback(keyCallback, nullptr)); Input::addMouseButtonListener(MouseButtonCallback(mouseButtonCallback, nullptr)); - button = new ButtonElement(px(10), px(100), px(200), px(50)); + button = new ButtonElement(uiPx(10), uiPx(100), uiPx(200), uiPx(50)); button->text->color = Colors::BLACK; button->color = Colors::WHITE; button->hoverColor = Colors::GRAY;