Restructure code

This commit is contained in:
MrLetsplay 2022-11-07 21:36:45 +01:00
parent 6bf2e12b08
commit b706100ddc
29 changed files with 106 additions and 33 deletions

View File

@ -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)

View File

@ -3,3 +3,4 @@
- stb_image, stb_image_write
- microtar (included)
- openvr (SteamVR), if compiling with KEKENGINE_VR
- bullet 3

View File

@ -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);
}

View File

@ -13,7 +13,6 @@
#include <stb/stb_image.h>
#include <stb/stb_image_write.h>
#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);
}

View File

@ -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) {

View File

@ -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;
}
}

View File

@ -3,6 +3,7 @@
#include <bits/stdc++.h>
#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<UIElement *> UI::getElements() {
return kekData.uiElements;
return kekData.ui->elements;
}
}

View File

@ -3,6 +3,7 @@
#include <glm/gtc/type_ptr.hpp>
#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);
}

View File

@ -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<UIElement *> uiElements;
Font *uiDefaultFont;
Shader *uiRectangleShader;
GLFWCursorMode uiCursorMode;
ActiveKeyboardCapture activeKeyboardCapture;
GLFWCursorMode cursorMode;
UIData *ui;
PhysicsData *physics;
};
extern KekData kekData;

View File

@ -0,0 +1,11 @@
#pragma once
#include <btBulletDynamicsCommon.h>
namespace kek {
struct PhysicsData {
btDynamicsWorld *world;
};
}

View File

@ -0,0 +1,11 @@
#pragma once
namespace kek {
struct UIData {
std::vector<UIElement *> elements;
Font *defaultFont;
Shader *rectangleShader;
};
}

View File

@ -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"

View File

@ -0,0 +1,8 @@
#pragma once
namespace kek::Physics {
void init();
void destroy();
}

View File

@ -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 {

View File

@ -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;