From 471679df0249d1a390163115ae0475131cf1a231 Mon Sep 17 00:00:00 2001 From: Ben McAvoy Date: Wed, 9 Oct 2024 12:33:23 +0100 Subject: [PATCH] style: run clang formatter --- README.md | 1 - engine/CMakeLists.txt | 10 +- engine/include/jenjin/camera.h | 50 +- engine/include/jenjin/editor/editor.h | 48 +- engine/include/jenjin/editor/state.h | 6 +- engine/include/jenjin/editor/utils.h | 6 +- engine/include/jenjin/editor/widgets.h | 4 +- engine/include/jenjin/engine.h | 22 +- engine/include/jenjin/framebuffer.h | 18 +- engine/include/jenjin/gameobject.h | 78 +- engine/include/jenjin/helpers.h | 12 +- engine/include/jenjin/mesh.h | 18 +- engine/include/jenjin/scene.h | 63 +- engine/include/jenjin/shader.h | 30 +- engine/include/jenjin/target.h | 21 +- engine/include/jenjin/targets/default.h | 14 +- engine/include/jenjin/targets/editor.h | 26 +- engine/include/jenjin/texture.h | 11 +- engine/src/camera.cpp | 66 +- engine/src/editor/editor.cpp | 1096 ++++++++++++----------- engine/src/editor/utils.cpp | 24 +- engine/src/editor/widgets.cpp | 136 +-- engine/src/engine.cpp | 134 +-- engine/src/framebuffer.cpp | 80 +- engine/src/helpers.cpp | 311 ++++--- engine/src/scene.cpp | 286 +++--- engine/src/shader.cpp | 225 ++--- engine/src/targets/default.cpp | 42 +- engine/src/targets/editor.cpp | 135 ++- engine/src/texture.cpp | 84 +- jenjin/src/main.cpp | 80 +- windows.sh | 14 +- 32 files changed, 1669 insertions(+), 1482 deletions(-) diff --git a/README.md b/README.md index c081e57..e0b087d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ # Jenjin (Engine/Editor) - A source available lightweight game engine built for beginners to teach programming for education purposes. ## Usage diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index b4454fe..809b491 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -4,9 +4,9 @@ file (GLOB_RECURSE SOURCES src/*.cpp) add_library (JenjinEngine ${SOURCES}) target_include_directories(JenjinEngine - PUBLIC - $ - $ + PUBLIC + $ + $ ${CMAKE_SOURCE_DIR}/megasource/libs/glfw/include/ ${CMAKE_SOURCE_DIR}/megasource/libs/glad/include/ ${CMAKE_SOURCE_DIR}/megasource/libs/glm/ @@ -16,8 +16,8 @@ target_include_directories(JenjinEngine ${CMAKE_SOURCE_DIR}/megasource/libs/lua/include/ ${CMAKE_SOURCE_DIR}/megasource/libs/sol2/include/ ${CMAKE_SOURCE_DIR}/megasource/libs/ - PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/src + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/src ) target_link_libraries(JenjinEngine PRIVATE glfw glad spdlog glm stb imgui lua54 sol2) diff --git a/engine/include/jenjin/camera.h b/engine/include/jenjin/camera.h index 2fd7ff4..dda9be5 100644 --- a/engine/include/jenjin/camera.h +++ b/engine/include/jenjin/camera.h @@ -2,47 +2,47 @@ #include "jenjin/shader.h" -#include #include +#include namespace Jenjin { // 2D Orthographic Camera // NOTE: one day hybrid 2D/3D camera? class Camera { public: - Camera(Shader* shader, glm::vec2 size); + Camera(Shader *shader, glm::vec2 size); - void SetShader(Shader* shader); + void SetShader(Shader *shader); - void SetPosition(const glm::vec3& position); - void SetRotation(float rotation); - void SetZoom(float zoom); + void SetPosition(const glm::vec3 &position); + void SetRotation(float rotation); + void SetZoom(float zoom); - void Translate(const glm::vec3& translation); - void Rotate(float rotation); - void Zoom(float zoom); + void Translate(const glm::vec3 &translation); + void Rotate(float rotation); + void Zoom(float zoom); - void Resize(glm::vec2 size); + void Resize(glm::vec2 size); - void Update(); - void Use(); + void Update(); + void Use(); - const glm::vec3& GetPosition() const { return position; } - const float GetRotation() const { return rotation; } - const glm::mat4 GetViewProjection() const { return projection * view; } + const glm::vec3 &GetPosition() const { return position; } + const float GetRotation() const { return rotation; } + const glm::mat4 GetViewProjection() const { return projection * view; } - glm::vec3* GetPositionPointer() { return &position; } - float* GetZoomPointer() { return &zoom; } - float* GetRotationPointer() { return &rotation; } + glm::vec3 *GetPositionPointer() { return &position; } + float *GetZoomPointer() { return &zoom; } + float *GetRotationPointer() { return &rotation; } private: - Shader* shader; + Shader *shader; - glm::vec3 position; - float rotation; - float zoom; + glm::vec3 position; + float rotation; + float zoom; - glm::mat4 projection; - glm::mat4 view; + glm::mat4 projection; + glm::mat4 view; }; -} +} // namespace Jenjin diff --git a/engine/include/jenjin/editor/editor.h b/engine/include/jenjin/editor/editor.h index 3739e10..d8455aa 100644 --- a/engine/include/jenjin/editor/editor.h +++ b/engine/include/jenjin/editor/editor.h @@ -9,37 +9,37 @@ namespace Jenjin { namespace Editor { class Manager { public: - Manager(); + Manager(); - void menu(); - void welcome(); + void menu(); + void welcome(); - void dockspace(); + void dockspace(); - void controls(Jenjin::Scene* scene); - void hierarchy(Jenjin::Scene* scene); - void inspector(Jenjin::Scene* scene); - void scene(Jenjin::Scene* scene); - void viewport(Jenjin::Scene* scene); - void explorer(Jenjin::Scene* scene); - void code(Jenjin::Scene* scene); + void controls(Jenjin::Scene *scene); + void hierarchy(Jenjin::Scene *scene); + void inspector(Jenjin::Scene *scene); + void scene(Jenjin::Scene *scene); + void viewport(Jenjin::Scene *scene); + void explorer(Jenjin::Scene *scene); + void code(Jenjin::Scene *scene); - void backup_prompts(Jenjin::Scene* scene); + void backup_prompts(Jenjin::Scene *scene); - void show_all(Jenjin::Scene* scene); + void show_all(Jenjin::Scene *scene); - struct { - std::string projectPath; - std::string openScenePath; - std::string liveScenePath; - std::string scriptsPath; - } paths; + struct { + std::string projectPath; + std::string openScenePath; + std::string liveScenePath; + std::string scriptsPath; + } paths; private: - Jenjin::GameObject* selectedObject = nullptr; - bool selectedCamera = false; + Jenjin::GameObject *selectedObject = nullptr; + bool selectedCamera = false; - char renameGameObjectBuffer[256] = { 0 }; + char renameGameObjectBuffer[256] = {0}; }; -} -} +} // namespace Editor +} // namespace Jenjin diff --git a/engine/include/jenjin/editor/state.h b/engine/include/jenjin/editor/state.h index 2ae499a..e6dd671 100644 --- a/engine/include/jenjin/editor/state.h +++ b/engine/include/jenjin/editor/state.h @@ -6,7 +6,7 @@ namespace Jenjin { namespace Editor { class State_t { public: - GLuint renderTexture; + GLuint renderTexture; } extern State; -} -} +} // namespace Editor +} // namespace Jenjin diff --git a/engine/include/jenjin/editor/utils.h b/engine/include/jenjin/editor/utils.h index aa217a0..3f62588 100644 --- a/engine/include/jenjin/editor/utils.h +++ b/engine/include/jenjin/editor/utils.h @@ -1,7 +1,7 @@ #pragma once -#include #include +#include #include #include @@ -13,5 +13,5 @@ namespace Jenjin { namespace Editor { void ensure_dir(std::string path); std::string get_jendir(); -} -} +} // namespace Editor +} // namespace Jenjin diff --git a/engine/include/jenjin/editor/widgets.h b/engine/include/jenjin/editor/widgets.h index 983e6f7..c538d28 100644 --- a/engine/include/jenjin/editor/widgets.h +++ b/engine/include/jenjin/editor/widgets.h @@ -2,5 +2,7 @@ #include "jenjin/gameobject.h" namespace Jenjin::Editor::Widgets { -bool transformWidget(Jenjin::GameObject::Transform* transform); // Returns true if the transform was changed +bool transformWidget( + Jenjin::GameObject::Transform + *transform); // Returns true if the transform was changed }; diff --git a/engine/include/jenjin/engine.h b/engine/include/jenjin/engine.h index 8b9f269..166d8a8 100644 --- a/engine/include/jenjin/engine.h +++ b/engine/include/jenjin/engine.h @@ -3,27 +3,27 @@ #include "jenjin/scene.h" #include "jenjin/target.h" -#include #include +#include namespace Jenjin { class Engine { public: - Engine(GLFWwindow* window); - ~Engine(); + Engine(GLFWwindow *window); + ~Engine(); - void AddScene(std::shared_ptr scene, bool defaultScene = false); + void AddScene(std::shared_ptr scene, bool defaultScene = false); - void Render(Target* target); // Render directly to the target + void Render(Target *target); // Render directly to the target - GLFWwindow* glfwContext; + GLFWwindow *glfwContext; - Scene* GetCurrentScene() { return currentScene; } + Scene *GetCurrentScene() { return currentScene; } private: - std::vector> scenes = {}; - Scene* currentScene = nullptr; + std::vector> scenes = {}; + Scene *currentScene = nullptr; }; -extern Engine* EngineRef; -} +extern Engine *EngineRef; +} // namespace Jenjin diff --git a/engine/include/jenjin/framebuffer.h b/engine/include/jenjin/framebuffer.h index c6c8192..37308c2 100644 --- a/engine/include/jenjin/framebuffer.h +++ b/engine/include/jenjin/framebuffer.h @@ -5,18 +5,18 @@ namespace Jenjin { class Framebuffer { public: - Framebuffer(); - ~Framebuffer(); + Framebuffer(); + ~Framebuffer(); - void Bind(); - void Unbind(); + void Bind(); + void Unbind(); - void Resize(int width, int height); + void Resize(int width, int height); - GLuint texture; + GLuint texture; private: - GLuint fbo; - GLuint rbo; + GLuint fbo; + GLuint rbo; }; -} +} // namespace Jenjin diff --git a/engine/include/jenjin/gameobject.h b/engine/include/jenjin/gameobject.h index 9ab04e4..4c6d293 100644 --- a/engine/include/jenjin/gameobject.h +++ b/engine/include/jenjin/gameobject.h @@ -2,63 +2,63 @@ #include "jenjin/mesh.h" -#include #include +#include #include namespace Jenjin { class GameObject { public: - GameObject(std::string name, Mesh mesh) : mesh(mesh), name(name) {} - ~GameObject() { spdlog::trace("GameObject::~GameObject(\"{}\")", name); } + GameObject(std::string name, Mesh mesh) : mesh(mesh), name(name) {} + ~GameObject() { spdlog::trace("GameObject::~GameObject(\"{}\")", name); } - std::string name; + std::string name; - // Transform - struct Transform { - glm::vec2 position; - glm::vec2 scale; - float rotation; - } transform = { glm::vec2(0.0f), glm::vec2(1.0f), 0.0f }; + // Transform + struct Transform { + glm::vec2 position; + glm::vec2 scale; + float rotation; + } transform = {glm::vec2(0.0f), glm::vec2(1.0f), 0.0f}; - // Appearance - glm::vec3 color = glm::vec3(1.0f); + // Appearance + glm::vec3 color = glm::vec3(1.0f); - Mesh mesh; - std::string texturePath = ""; + Mesh mesh; + std::string texturePath = ""; - int meshReferenceID = -1; + int meshReferenceID = -1; - bool mixColor = false; + bool mixColor = false; - // TODO: hierarchy + // TODO: hierarchy #ifdef false - GameObject* parent = nullptr; - std::vector children = {}; + GameObject *parent = nullptr; + std::vector children = {}; #endif - // Getters - std::string GetName() { return name; } - glm::vec2 GetPosition() { return transform.position; } - glm::vec2 GetScale() { return transform.scale; } - float GetRotation() { return transform.rotation; } + // Getters + std::string GetName() { return name; } + glm::vec2 GetPosition() { return transform.position; } + glm::vec2 GetScale() { return transform.scale; } + float GetRotation() { return transform.rotation; } - // Setters - void SetName(std::string name) { this->name = name; } - void SetPosition(glm::vec2 position) { transform.position = position; } - void SetScale(glm::vec2 scale) { transform.scale = scale; } - void SetRotation(float rotation) { transform.rotation = rotation; } + // Setters + void SetName(std::string name) { this->name = name; } + void SetPosition(glm::vec2 position) { transform.position = position; } + void SetScale(glm::vec2 scale) { transform.scale = scale; } + void SetRotation(float rotation) { transform.rotation = rotation; } - // Pointer getters - std::string* GetNamePointer() { return &name; } - glm::vec2* GetPositionPointer() { return &transform.position; } - glm::vec2* GetScalePointer() { return &transform.scale; } - float* GetRotationPointer() { return &transform.rotation; } + // Pointer getters + std::string *GetNamePointer() { return &name; } + glm::vec2 *GetPositionPointer() { return &transform.position; } + glm::vec2 *GetScalePointer() { return &transform.scale; } + float *GetRotationPointer() { return &transform.rotation; } - // Modifiers - void Translate(glm::vec2 translation) { transform.position += translation; } - void Scale(glm::vec2 scale) { transform.scale *= scale; } - void Rotate(float rotation) { transform.rotation += rotation; } + // Modifiers + void Translate(glm::vec2 translation) { transform.position += translation; } + void Scale(glm::vec2 scale) { transform.scale *= scale; } + void Rotate(float rotation) { transform.rotation += rotation; } }; -} +} // namespace Jenjin diff --git a/engine/include/jenjin/helpers.h b/engine/include/jenjin/helpers.h index 26dff81..3f42737 100644 --- a/engine/include/jenjin/helpers.h +++ b/engine/include/jenjin/helpers.h @@ -2,15 +2,15 @@ #include "jenjin/mesh.h" -#include #include +#include namespace Jenjin { namespace Helpers { -GLFWwindow* CreateWindow(int width, int height, const char* title); -void CheckWindow(GLFWwindow* window); -void InitiateImGui(GLFWwindow* window); +GLFWwindow *CreateWindow(int width, int height, const char *title); +void CheckWindow(GLFWwindow *window); +void InitiateImGui(GLFWwindow *window); Mesh CreateQuad(int width, int height); -} -} +} // namespace Helpers +} // namespace Jenjin diff --git a/engine/include/jenjin/mesh.h b/engine/include/jenjin/mesh.h index da4add6..8a24fad 100644 --- a/engine/include/jenjin/mesh.h +++ b/engine/include/jenjin/mesh.h @@ -7,19 +7,19 @@ namespace Jenjin { struct MeshReference { - int baseVertex; - int baseIndex; - int indexCount; - int vertexCount; + int baseVertex; + int baseIndex; + int indexCount; + int vertexCount; }; struct Vertex { - glm::vec3 position; - glm::vec2 texCoord; + glm::vec3 position; + glm::vec2 texCoord; }; struct Mesh { - std::vector vertices; - std::vector indices; + std::vector vertices; + std::vector indices; }; -} +} // namespace Jenjin diff --git a/engine/include/jenjin/scene.h b/engine/include/jenjin/scene.h index 54bec94..8fb9921 100644 --- a/engine/include/jenjin/scene.h +++ b/engine/include/jenjin/scene.h @@ -1,14 +1,14 @@ #pragma once -#include "jenjin/gameobject.h" #include "jenjin/camera.h" +#include "jenjin/gameobject.h" #include "jenjin/shader.h" #include "jenjin/target.h" #include "jenjin/texture.h" -#include -#include #include +#include +#include #include #include @@ -17,43 +17,48 @@ namespace Jenjin { class Scene { public: - Scene(); - ~Scene() { spdlog::trace("Scene::~Scene()"); } + Scene(); + ~Scene() { spdlog::trace("Scene::~Scene()"); } - void SetTarget(Target* target); + void SetTarget(Target *target); - void AddGameObject(std::shared_ptr gameObject); - void RemoveGameObject(GameObject* gameObject); - void RemoveGameObject(std::shared_ptr gameObject); + void AddGameObject(std::shared_ptr gameObject); + void RemoveGameObject(GameObject *gameObject); + void RemoveGameObject(std::shared_ptr gameObject); - void SetGameObjectTexture(GameObject* gameObject, const std::string& texturePath); - void SetGameObjectTexture(std::shared_ptr gameObject, const std::string& texturePath); + void SetGameObjectTexture(GameObject *gameObject, + const std::string &texturePath); + void SetGameObjectTexture(std::shared_ptr gameObject, + const std::string &texturePath); - void Build(); + void Build(); - void Update(); - void Render(); + void Update(); + void Render(); - Camera* GetCamera() { return &camera; } - std::vector>* GetGameObjects() { return &gameObjects; } - Target* GetTarget() { return target; } + Camera *GetCamera() { return &camera; } + std::vector> *GetGameObjects() { + return &gameObjects; + } + Target *GetTarget() { return target; } - void Save(const std::string& path); - void Save(std::ofstream& file); - void Load(const std::string& path); - void Load(std::ifstream& file); + void Save(const std::string &path); + void Save(std::ofstream &file); + void Load(const std::string &path); + void Load(std::ifstream &file); private: - GLuint vao, vbo, ebo = 0; + GLuint vao, vbo, ebo = 0; - std::vector> gameObjects = {}; + std::vector> gameObjects = {}; - std::vector meshReferences = {}; - std::unordered_map> textures; + std::vector meshReferences = {}; + std::unordered_map> textures; - Shader shader = Shader("resources/shaders/default_vert.glsl", "resources/shaders/default_frag.glsl"); - Target* target = nullptr; + Shader shader = Shader("resources/shaders/default_vert.glsl", + "resources/shaders/default_frag.glsl"); + Target *target = nullptr; - Camera camera = Camera(&shader, glm::vec2(800, 600)); + Camera camera = Camera(&shader, glm::vec2(800, 600)); }; -} +} // namespace Jenjin diff --git a/engine/include/jenjin/shader.h b/engine/include/jenjin/shader.h index a6ed640..07db020 100644 --- a/engine/include/jenjin/shader.h +++ b/engine/include/jenjin/shader.h @@ -7,22 +7,22 @@ namespace Jenjin { // Shader class, helps to load and manage shaders class Shader { public: - unsigned int ID; // ID of the shader program object + unsigned int ID; // ID of the shader program object - // RAII pattern - Shader(const char* vertexPath, const char* fragmentPath); - ~Shader(); + // RAII pattern + Shader(const char *vertexPath, const char *fragmentPath); + ~Shader(); - // Apply the shader - void use(); + // Apply the shader + void use(); - // Utility uniform functions for setting values - // without using many different functions - void set(const char* name, float value); - void set(const char* name, int value); - void set(const char* name, bool value); - void set(const char* name, GLfloat *value); - void set(const char* name, glm::mat4 value); - void set(const char* name, glm::vec3 value); + // Utility uniform functions for setting values + // without using many different functions + void set(const char *name, float value); + void set(const char *name, int value); + void set(const char *name, bool value); + void set(const char *name, GLfloat *value); + void set(const char *name, glm::mat4 value); + void set(const char *name, glm::vec3 value); }; -} +} // namespace Jenjin diff --git a/engine/include/jenjin/target.h b/engine/include/jenjin/target.h index a4b88b8..17dc3f7 100644 --- a/engine/include/jenjin/target.h +++ b/engine/include/jenjin/target.h @@ -11,15 +11,20 @@ namespace Jenjin { // like a viewport. class Target { public: - virtual void PreRender() {}; // Called before rendering - virtual void Render() {}; // Called during rendering (usually not needed) - virtual void PostRender() {}; // Called after rendering + virtual void PreRender() {}; // Called before rendering + virtual void Render() {}; // Called during rendering (usually not needed) + virtual void PostRender() {}; // Called after rendering - virtual glm::vec2 GetSize() { return glm::vec2(-1, -1); }; // Get the size of the target - virtual void Resize(glm::vec2 size) {}; // Resize the target + // Get the size of the target + virtual glm::vec2 GetSize() { return glm::vec2(-1, -1); }; - virtual glm::vec2 GetMousePosition() { return glm::vec2(-1, -1); }; // Get the mouse position + // Resize the target + virtual void Resize(glm::vec2 size) {}; - virtual bool RespondsToWindowResize() { return true; }; // Does the target respond to events of the window resizing + // Get the mouse position + virtual glm::vec2 GetMousePosition() { return glm::vec2(-1, -1); }; + + // Does the target respond to events of the window resizing + virtual bool RespondsToWindowResize() { return true; }; }; -} +} // namespace Jenjin diff --git a/engine/include/jenjin/targets/default.h b/engine/include/jenjin/targets/default.h index ca6477e..de220cf 100644 --- a/engine/include/jenjin/targets/default.h +++ b/engine/include/jenjin/targets/default.h @@ -6,14 +6,14 @@ namespace Jenjin { namespace Targets { class DefaultTarget : public Jenjin::Target { public: - DefaultTarget() = default; + DefaultTarget() = default; - virtual void PreRender() override; + virtual void PreRender() override; - virtual glm::vec2 GetSize() override; - virtual void Resize(glm::vec2 size) override; + virtual glm::vec2 GetSize() override; + virtual void Resize(glm::vec2 size) override; - virtual glm::vec2 GetMousePosition() override; + virtual glm::vec2 GetMousePosition() override; }; -} -} +} // namespace Targets +} // namespace Jenjin diff --git a/engine/include/jenjin/targets/editor.h b/engine/include/jenjin/targets/editor.h index b5cb958..2761e28 100644 --- a/engine/include/jenjin/targets/editor.h +++ b/engine/include/jenjin/targets/editor.h @@ -8,23 +8,23 @@ namespace Jenjin { namespace Targets { class EditorTarget : public Jenjin::Target { public: - EditorTarget(); + EditorTarget(); - virtual void PreRender() override; - virtual void Render() override; - virtual void PostRender() override; + virtual void PreRender() override; + virtual void Render() override; + virtual void PostRender() override; - virtual glm::vec2 GetSize() override; - virtual void Resize(glm::vec2 size) override; + virtual glm::vec2 GetSize() override; + virtual void Resize(glm::vec2 size) override; - virtual glm::vec2 GetMousePosition() override; + virtual glm::vec2 GetMousePosition() override; - virtual bool RespondsToWindowResize() override; + virtual bool RespondsToWindowResize() override; - Jenjin::Framebuffer renderTexture; - int width, height; + Jenjin::Framebuffer renderTexture; + int width, height; - Jenjin::Editor::Manager editor; + Jenjin::Editor::Manager editor; }; -} -} +} // namespace Targets +} // namespace Jenjin diff --git a/engine/include/jenjin/texture.h b/engine/include/jenjin/texture.h index d18f002..cd40059 100644 --- a/engine/include/jenjin/texture.h +++ b/engine/include/jenjin/texture.h @@ -4,14 +4,13 @@ class Texture { public: - unsigned int ID; // ID of the shader program object + unsigned int ID; // ID of the shader program object - Texture(const char* imagePath, bool alpha = false); - ~Texture(); + Texture(const char *imagePath, bool alpha = false); + ~Texture(); - void bind(int id = 0); + void bind(int id = 0); private: - std::string imagePath; + std::string imagePath; }; - diff --git a/engine/src/camera.cpp b/engine/src/camera.cpp index a70ddf4..f67073b 100644 --- a/engine/src/camera.cpp +++ b/engine/src/camera.cpp @@ -1,3 +1,5 @@ +#define GLFW_INCLUDE_NONE + #include "jenjin/camera.h" #include "glm/trigonometric.hpp" @@ -8,58 +10,56 @@ using namespace Jenjin; -Camera::Camera(Shader* shader, glm::vec2 size) : shader(shader) { - float& width = size.x; float& height = size.y; +Camera::Camera(Shader *shader, glm::vec2 size) : shader(shader) { + float &width = size.x; + float &height = size.y; - rotation = 0; - zoom = 1; + rotation = 0; + zoom = 1; - position = glm::vec3(0, 0, 0); + position = glm::vec3(0, 0, 0); - // Set the camera's projection matrix - Resize(size); + // Set the camera's projection matrix + Resize(size); } void Camera::Resize(glm::vec2 size) { - float aspectRatio = size.x / size.y; - float inverseZoom = 1.0f / (zoom / 10); // Invert to make zoom zoom in and also scale everything down... - this->projection = glm::ortho(-aspectRatio * inverseZoom, aspectRatio * inverseZoom, -inverseZoom, inverseZoom, -1.0f, 1.0f); + float aspectRatio = size.x / size.y; + float inverseZoom = + 1.0f / + (zoom / + 10); // Invert to make zoom zoom in and also scale everything down... + this->projection = + glm::ortho(-aspectRatio * inverseZoom, aspectRatio * inverseZoom, + -inverseZoom, inverseZoom, -1.0f, 1.0f); } void Camera::Update() { - // include rotation - view = glm::translate(glm::mat4(1.0f), -position) * glm::rotate(glm::mat4(1.0f), glm::radians(rotation), glm::vec3(0, 0, 1)); + // include rotation + view = + glm::translate(glm::mat4(1.0f), -position) * + glm::rotate(glm::mat4(1.0f), glm::radians(rotation), glm::vec3(0, 0, 1)); } -void Camera::SetPosition(const glm::vec3& position) { - this->position = position; +void Camera::SetPosition(const glm::vec3 &position) { + this->position = position; } -void Camera::SetRotation(float rotation) { - this->rotation = rotation; -} +void Camera::SetRotation(float rotation) { this->rotation = rotation; } -void Camera::SetZoom(float zoom) { - this->zoom = zoom; -} +void Camera::SetZoom(float zoom) { this->zoom = zoom; } -void Camera::Translate(const glm::vec3& translation) { - position += translation; +void Camera::Translate(const glm::vec3 &translation) { + position += translation; } -void Camera::Rotate(float rotation) { - this->rotation += rotation; -} +void Camera::Rotate(float rotation) { this->rotation += rotation; } -void Camera::Zoom(float zoom) { - this->zoom += zoom; -} +void Camera::Zoom(float zoom) { this->zoom += zoom; } -void Camera::SetShader(Shader* shader) { - this->shader = shader; -} +void Camera::SetShader(Shader *shader) { this->shader = shader; } void Camera::Use() { - shader->use(); - shader->set("u_viewProjection", GetViewProjection()); + shader->use(); + shader->set("u_viewProjection", GetViewProjection()); } diff --git a/engine/src/editor/editor.cpp b/engine/src/editor/editor.cpp index 298de15..46919cb 100644 --- a/engine/src/editor/editor.cpp +++ b/engine/src/editor/editor.cpp @@ -1,17 +1,19 @@ +#define GLFW_INCLUDE_NONE + #include "jenjin/editor/editor.h" #include "jenjin/editor/utils.h" #include "jenjin/editor/widgets.h" +#include "jenjin/engine.h" #include "jenjin/gameobject.h" #include "jenjin/helpers.h" -#include "jenjin/engine.h" #include "jenjin/scene.h" +#include #include #include -#include -#include #include +#include #include #include @@ -24,534 +26,602 @@ using namespace Jenjin::Editor; Manager::Manager() {} void Manager::menu() { - if (ImGui::BeginMainMenuBar()) { - if (ImGui::BeginMenu("File")) { - if (!this->paths.projectPath.empty()) { - if (ImGui::MenuItem("New Scene")) { - auto scene = std::make_shared(); - Jenjin::EngineRef->AddScene(scene, true); - } - - if (ImGui::MenuItem("Open Scene")) { - Jenjin::EngineRef->GetCurrentScene()->Load(this->paths.openScenePath); - } - - if (ImGui::MenuItem("Save Scene")) { - Jenjin::EngineRef->GetCurrentScene()->Save(this->paths.openScenePath); - } - } - - if (ImGui::MenuItem("Exit")) { - glfwSetWindowShouldClose(glfwGetCurrentContext(), GLFW_TRUE); - } - - ImGui::EndMenu(); - } - - if (!this->paths.openScenePath.empty()) { - if (ImGui::BeginMenu("Scripts")) { - if (ImGui::MenuItem("Reload")) { - spdlog::warn("Unimplemented: Reload Scripts"); - } - - ImGui::EndMenu(); - } - - if (ImGui::BeginMenu("Game")) { - if (ImGui::MenuItem("Play")) { - spdlog::warn("Unimplemented: Play"); - } - - ImGui::EndMenu(); - } - } - - auto toDisplay = this->paths.openScenePath; - ImGui::SetCursorPosX(ImGui::GetWindowWidth() - ImGui::CalcTextSize(toDisplay.c_str()).x - 10); - ImGui::Text("%s", toDisplay.c_str()); - - ImGui::EndMainMenuBar(); - } + if (ImGui::BeginMainMenuBar()) { + if (ImGui::BeginMenu("File")) { + if (!this->paths.projectPath.empty()) { + if (ImGui::MenuItem("New Scene")) { + auto scene = std::make_shared(); + Jenjin::EngineRef->AddScene(scene, true); + } + + if (ImGui::MenuItem("Open Scene")) { + Jenjin::EngineRef->GetCurrentScene()->Load(this->paths.openScenePath); + } + + if (ImGui::MenuItem("Save Scene")) { + Jenjin::EngineRef->GetCurrentScene()->Save(this->paths.openScenePath); + } + } + + if (ImGui::MenuItem("Exit")) { + glfwSetWindowShouldClose(glfwGetCurrentContext(), GLFW_TRUE); + } + + ImGui::EndMenu(); + } + + if (!this->paths.openScenePath.empty()) { + if (ImGui::BeginMenu("Scripts")) { + if (ImGui::MenuItem("Reload")) { + spdlog::warn("Unimplemented: Reload Scripts"); + } + + ImGui::EndMenu(); + } + + if (ImGui::BeginMenu("Game")) { + if (ImGui::MenuItem("Play")) { + spdlog::warn("Unimplemented: Play"); + } + + ImGui::EndMenu(); + } + } + + auto toDisplay = this->paths.openScenePath; + ImGui::SetCursorPosX(ImGui::GetWindowWidth() - + ImGui::CalcTextSize(toDisplay.c_str()).x - 10); + ImGui::Text("%s", toDisplay.c_str()); + + ImGui::EndMainMenuBar(); + } } void Manager::dockspace() { - static ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_PassthruCentralNode; - static ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking; - - // Position and resize the dockspace window - ImGuiViewport* viewport = ImGui::GetMainViewport(); - ImGui::SetNextWindowPos(viewport->Pos); - ImGui::SetNextWindowSize(viewport->Size); - ImGui::SetNextWindowViewport(viewport->ID); - - // Style it so that it isn't visible, doesn't have rounding, etc - ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); - ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f); - ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f)); - window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove; - window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus; - window_flags |= ImGuiWindowFlags_NoBackground; - - // Create the actual dockspace window - ImGui::Begin("DockSpace", nullptr, window_flags); - ImGui::PopStyleVar(3); - - // Create a dockspace layout - ImGuiIO& io = ImGui::GetIO(); - ImGuiID dockspace_id = ImGui::GetID("MyDockSpace"); - ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags); - - static auto first_time = true; - if (first_time) { - first_time = false; - - // Reset layout - ImGui::DockBuilderRemoveNode(dockspace_id); - ImGui::DockBuilderAddNode(dockspace_id, dockspace_flags | ImGuiDockNodeFlags_DockSpace); - ImGui::DockBuilderSetNodeSize(dockspace_id, viewport->Size); - - // Dock the welcome window in the center - ImGui::DockBuilderDockWindow("Welcome", dockspace_id); - - auto dock_left = ImGui::DockBuilderSplitNode(dockspace_id, ImGuiDir_Left, 0.25f, nullptr, &dockspace_id); - auto dock_left_up = ImGui::DockBuilderSplitNode(dock_left, ImGuiDir_Up, 0.8f, nullptr, &dock_left); - auto dock_right = ImGui::DockBuilderSplitNode(dockspace_id, ImGuiDir_Right, 0.6f, nullptr, &dockspace_id); - auto middle = dockspace_id; - - ImGui::DockBuilderDockWindow("Hierarchy", dock_left_up); - ImGui::DockBuilderDockWindow("Explorer", dock_left); - ImGui::DockBuilderDockWindow("Inspector", dock_right); - ImGui::DockBuilderDockWindow("Viewport", middle); - - ImGui::DockBuilderFinish(dockspace_id); - } - - ImGui::End(); + static ImGuiDockNodeFlags dockspace_flags = + ImGuiDockNodeFlags_PassthruCentralNode; + static ImGuiWindowFlags window_flags = + ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking; + + // Position and resize the dockspace window + ImGuiViewport *viewport = ImGui::GetMainViewport(); + ImGui::SetNextWindowPos(viewport->Pos); + ImGui::SetNextWindowSize(viewport->Size); + ImGui::SetNextWindowViewport(viewport->ID); + + // Style it so that it isn't visible, doesn't have rounding, etc + ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); + ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f); + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f)); + window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | + ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove; + window_flags |= + ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus; + window_flags |= ImGuiWindowFlags_NoBackground; + + // Create the actual dockspace window + ImGui::Begin("DockSpace", nullptr, window_flags); + ImGui::PopStyleVar(3); + + // Create a dockspace layout + ImGuiIO &io = ImGui::GetIO(); + ImGuiID dockspace_id = ImGui::GetID("MyDockSpace"); + ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags); + + static auto first_time = true; + if (first_time) { + first_time = false; + + // Reset layout + ImGui::DockBuilderRemoveNode(dockspace_id); + ImGui::DockBuilderAddNode(dockspace_id, + dockspace_flags | ImGuiDockNodeFlags_DockSpace); + ImGui::DockBuilderSetNodeSize(dockspace_id, viewport->Size); + + // Dock the welcome window in the center + ImGui::DockBuilderDockWindow("Welcome", dockspace_id); + + auto dock_left = ImGui::DockBuilderSplitNode(dockspace_id, ImGuiDir_Left, + 0.25f, nullptr, &dockspace_id); + auto dock_left_up = ImGui::DockBuilderSplitNode(dock_left, ImGuiDir_Up, + 0.8f, nullptr, &dock_left); + auto dock_right = ImGui::DockBuilderSplitNode(dockspace_id, ImGuiDir_Right, + 0.6f, nullptr, &dockspace_id); + auto middle = dockspace_id; + + ImGui::DockBuilderDockWindow("Hierarchy", dock_left_up); + ImGui::DockBuilderDockWindow("Explorer", dock_left); + ImGui::DockBuilderDockWindow("Inspector", dock_right); + ImGui::DockBuilderDockWindow("Viewport", middle); + + ImGui::DockBuilderFinish(dockspace_id); + } + + ImGui::End(); } -void Manager::hierarchy(Jenjin::Scene* scene) { - if (scene == nullptr) { - spdlog::error("Scene is nullptr"); - return; - } - - std::vector>* gameObjects = Jenjin::EngineRef->GetCurrentScene()->GetGameObjects(); - - ImGui::Begin("Hierarchy"); - - static char name[128] = {0}; - // Press or Ctrl + Shift + A - if (ImGui::Button("+", ImVec2(40, 40)) || (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_A)) && ImGui::GetIO().KeyCtrl && ImGui::GetIO().KeyShift)) { - memset(name, 0, sizeof(name)); - ImGui::OpenPopup("Add GameObject"); - } - - ImGui::Separator(); - ImGui::Spacing(); - - ImGui::SetNextWindowSize(ImVec2(300, 200)); - if (ImGui::BeginPopupModal("Add GameObject")) { - ImGui::SetItemDefaultFocus(); - ImGui::InputText("Name", name, sizeof(name)); - - if (ImGui::Button("Create##Add") || ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Enter))) { - auto gameObject = std::make_shared(name, Jenjin::Helpers::CreateQuad(2.0f, 2.0f)); - scene->AddGameObject(gameObject); - scene->Build(); - - selectedCamera = false; - selectedObject = gameObjects->back().get(); - - memset(this->renameGameObjectBuffer, 0, sizeof(this->renameGameObjectBuffer)); - memcpy(this->renameGameObjectBuffer, selectedObject->name.c_str(), selectedObject->name.size()); - - ImGui::CloseCurrentPopup(); - } - - ImGui::SameLine(); - - if (ImGui::Button("Cancel##Add") || ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Escape))) { - ImGui::CloseCurrentPopup(); - } - - ImGui::EndPopup(); - } - - if (ImGui::Selectable("Camera", selectedCamera)) { - selectedCamera = !selectedCamera; - if (selectedCamera) selectedObject = nullptr; - } - - for (std::shared_ptr gameObject : *gameObjects) { - bool isSelected = selectedObject == gameObject.get(); - - if (ImGui::Selectable(gameObject->name.c_str(), isSelected)) { - if (isSelected) { - selectedObject = nullptr; - } else { - selectedCamera = false; - selectedObject = gameObject.get(); - memset(this->renameGameObjectBuffer, 0, sizeof(this->renameGameObjectBuffer)); - memcpy(this->renameGameObjectBuffer, selectedObject->name.c_str(), selectedObject->name.size()); - } - } - } - - ImGui::End(); +void Manager::hierarchy(Jenjin::Scene *scene) { + if (scene == nullptr) { + spdlog::error("Scene is nullptr"); + return; + } + + std::vector> *gameObjects = + Jenjin::EngineRef->GetCurrentScene()->GetGameObjects(); + + ImGui::Begin("Hierarchy"); + + static char name[128] = {0}; + // Press or Ctrl + Shift + A + if (ImGui::Button("+", ImVec2(40, 40)) || + (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_A)) && + ImGui::GetIO().KeyCtrl && ImGui::GetIO().KeyShift)) { + memset(name, 0, sizeof(name)); + ImGui::OpenPopup("Add GameObject"); + } + + ImGui::Separator(); + ImGui::Spacing(); + + ImGui::SetNextWindowSize(ImVec2(300, 200)); + if (ImGui::BeginPopupModal("Add GameObject")) { + ImGui::SetItemDefaultFocus(); + ImGui::InputText("Name", name, sizeof(name)); + + if (ImGui::Button("Create##Add") || + ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Enter))) { + auto gameObject = std::make_shared( + name, Jenjin::Helpers::CreateQuad(2.0f, 2.0f)); + scene->AddGameObject(gameObject); + scene->Build(); + + selectedCamera = false; + selectedObject = gameObjects->back().get(); + + memset(this->renameGameObjectBuffer, 0, + sizeof(this->renameGameObjectBuffer)); + memcpy(this->renameGameObjectBuffer, selectedObject->name.c_str(), + selectedObject->name.size()); + + ImGui::CloseCurrentPopup(); + } + + ImGui::SameLine(); + + if (ImGui::Button("Cancel##Add") || + ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Escape))) { + ImGui::CloseCurrentPopup(); + } + + ImGui::EndPopup(); + } + + if (ImGui::Selectable("Camera", selectedCamera)) { + selectedCamera = !selectedCamera; + if (selectedCamera) + selectedObject = nullptr; + } + + for (std::shared_ptr gameObject : *gameObjects) { + bool isSelected = selectedObject == gameObject.get(); + + if (ImGui::Selectable(gameObject->name.c_str(), isSelected)) { + if (isSelected) { + selectedObject = nullptr; + } else { + selectedCamera = false; + selectedObject = gameObject.get(); + memset(this->renameGameObjectBuffer, 0, + sizeof(this->renameGameObjectBuffer)); + memcpy(this->renameGameObjectBuffer, selectedObject->name.c_str(), + selectedObject->name.size()); + } + } + } + + ImGui::End(); } -void Manager::inspector(Jenjin::Scene* scene) { - if (!selectedObject && !selectedCamera) { - return; - } - - ImGui::Begin("Inspector"); - - if (selectedCamera) { - ImGui::Text("Camera"); - ImGui::Separator(); - ImGui::Indent(); - - ImGui::Text("Projection"); - ImGui::Separator(); - ImGui::Indent(); - - ImGui::DragFloat("Zoom", scene->GetCamera()->GetZoomPointer(), 0.01f, 0.01f, 1000.0f); - - ImGui::Unindent(); - - ImGui::Text("Position"); - ImGui::Separator(); - ImGui::Indent(); - - ImGui::DragFloat2("Position", glm::value_ptr(*scene->GetCamera()->GetPositionPointer()), 0.1f); - ImGui::DragFloat("Rotation", scene->GetCamera()->GetRotationPointer(), 0.1f); - - ImGui::Unindent(); - - ImGui::Text("Appearance"); - ImGui::Separator(); - ImGui::Indent(); - - ImGui::Text("Color here..."); - - ImGui::Unindent(); - - ImGui::Unindent(); - ImGui::End(); - return; - } - - if (selectedObject == nullptr) { - ImGui::Text("No object selected"); - ImGui::End(); - - return; - } - - if (ImGui::CollapsingHeader("Transform", ImGuiTreeNodeFlags_DefaultOpen)) { - ImGui::Indent(); - Jenjin::Editor::Widgets::transformWidget(&selectedObject->transform); - ImGui::Unindent(); - } - - ImGui::ItemSize(ImVec2(0, 10)); - - if (ImGui::CollapsingHeader("Appearance", ImGuiTreeNodeFlags_DefaultOpen)) { - ImGui::Indent(); - ImGui::ColorPicker3("Color", glm::value_ptr(selectedObject->color)); - ImGui::Unindent(); - } - - ImGui::ItemSize(ImVec2(0, 10)); - - if (ImGui::CollapsingHeader("Textures", ImGuiTreeNodeFlags_DefaultOpen)) { - ImGui::Indent(); - auto diriter = std::filesystem::directory_iterator(this->paths.projectPath + "/textures/"); - if (diriter == std::filesystem::directory_iterator()) { - ImGui::Text("No textures found"); - } - for (auto& texture : diriter) { - if (texture.is_regular_file() && texture.path().extension() == ".png" || texture.path().extension() == ".jpg") { - bool isSelected = selectedObject->texturePath == texture.path().string(); - if (ImGui::Selectable(texture.path().filename().string().c_str(), isSelected)) { - scene->SetGameObjectTexture(selectedObject, texture.path().string()); - } - } - } - - ImGui::ItemSize(ImVec2(0, 2)); - - if (!selectedObject->texturePath.empty()) { - ImGui::Spacing(); - ImGui::Checkbox("Mix Color", &selectedObject->mixColor); - } - - ImGui::Unindent(); - } - - ImGui::ItemSize(ImVec2(0, 10)); - - if (ImGui::CollapsingHeader("Manage", ImGuiTreeNodeFlags_DefaultOpen)) { - ImGui::Indent(); - ImGui::InputText("##RenameInput", renameGameObjectBuffer, sizeof(renameGameObjectBuffer)); - ImGui::SameLine(); - if (ImGui::Button("Rename")) { - selectedObject->SetName(renameGameObjectBuffer); - } - if (ImGui::Button("Delete")) { - scene->RemoveGameObject(selectedObject); - selectedObject = nullptr; - } - ImGui::Unindent(); - } - - /* ImGui::Text("Colours"); */ - /* ImGui::Separator(); */ - /* ImGui::Indent(); */ - /* ImGui::Spacing(); */ - /* ImGui::ColorEdit3("Color", glm::value_ptr(selectedObject->color)); */ - /* ImGui::Unindent(); */ - - /* ImGui::Text("Textures"); */ - /* ImGui::Separator(); */ - /* ImGui::Indent(); */ - /* ImGui::Spacing(); */ - /* auto diriter = std::filesystem::directory_iterator(this->paths.projectPath + "/textures/"); */ - - /* if (diriter == std::filesystem::directory_iterator()) { */ - /* ImGui::Text("No textures found"); */ - /* } */ - - /* for (auto& texture : diriter) { */ - /* if (texture.is_regular_file() && texture.path().extension() == ".png" || texture.path().extension() == ".jpg") { */ - /* bool isSelected = selectedObject->texturePath == texture.path().string(); */ - /* if (ImGui::Selectable(texture.path().filename().string().c_str(), isSelected)) { */ - /* scene->SetGameObjectTexture(selectedObject, texture.path().string()); */ - /* } */ - /* } */ - /* } */ - - /* if (!selectedObject->texturePath.empty()) { */ - /* ImGui::Spacing(); */ - /* ImGui::Checkbox("Mix Color", &selectedObject->mixColor); */ - /* } */ - - /* ImGui::Unindent(); */ - - /* ImGui::Spacing(); */ - - /* ImGui::Text("Manage"); */ - /* ImGui::Separator(); */ - /* ImGui::Indent(); */ - /* ImGui::Spacing(); */ - - /* ImGui::InputText("##RenameInput", renameGameObjectBuffer, sizeof(renameGameObjectBuffer)); */ - /* ImGui::SameLine(); */ - /* if (ImGui::Button("Rename")) { */ - /* selectedObject->SetName(renameGameObjectBuffer); */ - /* } */ - - /* if (ImGui::Button("Delete")) { */ - /* scene->RemoveGameObject(selectedObject); */ - /* selectedObject = nullptr; */ - /* } */ - - ImGui::Unindent(); - - ImGui::End(); +void Manager::inspector(Jenjin::Scene *scene) { + if (!selectedObject && !selectedCamera) { + return; + } + + ImGui::Begin("Inspector"); + + if (selectedCamera) { + ImGui::Text("Camera"); + ImGui::Separator(); + ImGui::Indent(); + + ImGui::Text("Projection"); + ImGui::Separator(); + ImGui::Indent(); + + ImGui::DragFloat("Zoom", scene->GetCamera()->GetZoomPointer(), 0.01f, 0.01f, + 1000.0f); + + ImGui::Unindent(); + + ImGui::Text("Position"); + ImGui::Separator(); + ImGui::Indent(); + + ImGui::DragFloat2("Position", + glm::value_ptr(*scene->GetCamera()->GetPositionPointer()), + 0.1f); + ImGui::DragFloat("Rotation", scene->GetCamera()->GetRotationPointer(), + 0.1f); + + ImGui::Unindent(); + + ImGui::Text("Appearance"); + ImGui::Separator(); + ImGui::Indent(); + + ImGui::Text("Color here..."); + + ImGui::Unindent(); + + ImGui::Unindent(); + ImGui::End(); + return; + } + + if (selectedObject == nullptr) { + ImGui::Text("No object selected"); + ImGui::End(); + + return; + } + + if (ImGui::CollapsingHeader("Transform", ImGuiTreeNodeFlags_DefaultOpen)) { + ImGui::Indent(); + Jenjin::Editor::Widgets::transformWidget(&selectedObject->transform); + ImGui::Unindent(); + } + + ImGui::ItemSize(ImVec2(0, 10)); + + if (ImGui::CollapsingHeader("Appearance", ImGuiTreeNodeFlags_DefaultOpen)) { + ImGui::Indent(); + ImGui::ColorPicker3("Color", glm::value_ptr(selectedObject->color)); + ImGui::Unindent(); + } + + ImGui::ItemSize(ImVec2(0, 10)); + + if (ImGui::CollapsingHeader("Textures", ImGuiTreeNodeFlags_DefaultOpen)) { + ImGui::Indent(); + auto diriter = std::filesystem::directory_iterator(this->paths.projectPath + + "/textures/"); + if (diriter == std::filesystem::directory_iterator()) { + ImGui::Text("No textures found"); + } + for (auto &texture : diriter) { + if (texture.is_regular_file() && texture.path().extension() == ".png" || + texture.path().extension() == ".jpg") { + bool isSelected = + selectedObject->texturePath == texture.path().string(); + if (ImGui::Selectable(texture.path().filename().string().c_str(), + isSelected)) { + scene->SetGameObjectTexture(selectedObject, texture.path().string()); + } + } + } + + ImGui::ItemSize(ImVec2(0, 2)); + + if (!selectedObject->texturePath.empty()) { + ImGui::Spacing(); + ImGui::Checkbox("Mix Color", &selectedObject->mixColor); + } + + ImGui::Unindent(); + } + + ImGui::ItemSize(ImVec2(0, 10)); + + if (ImGui::CollapsingHeader("Manage", ImGuiTreeNodeFlags_DefaultOpen)) { + ImGui::Indent(); + ImGui::InputText("##RenameInput", renameGameObjectBuffer, + sizeof(renameGameObjectBuffer)); + ImGui::SameLine(); + if (ImGui::Button("Rename")) { + selectedObject->SetName(renameGameObjectBuffer); + } + if (ImGui::Button("Delete")) { + scene->RemoveGameObject(selectedObject); + selectedObject = nullptr; + } + ImGui::Unindent(); + } + + /* ImGui::Text("Colours"); */ + /* ImGui::Separator(); */ + /* ImGui::Indent(); */ + /* ImGui::Spacing(); */ + /* ImGui::ColorEdit3("Color", glm::value_ptr(selectedObject->color)); */ + /* ImGui::Unindent(); */ + + /* ImGui::Text("Textures"); */ + /* ImGui::Separator(); */ + /* ImGui::Indent(); */ + /* ImGui::Spacing(); */ + /* auto diriter = std::filesystem::directory_iterator(this->paths.projectPath + * + "/textures/"); */ + + /* if (diriter == std::filesystem::directory_iterator()) { */ + /* ImGui::Text("No textures found"); */ + /* } */ + + /* for (auto& texture : diriter) { */ + /* if (texture.is_regular_file() && texture.path().extension() == ".png" || + * texture.path().extension() == ".jpg") { */ + /* bool isSelected = selectedObject->texturePath == + * texture.path().string(); */ + /* if + * (ImGui::Selectable(texture.path().filename().string().c_str(), isSelected)) + * { */ + /* scene->SetGameObjectTexture(selectedObject, + * texture.path().string()); */ + /* } */ + /* } */ + /* } */ + + /* if (!selectedObject->texturePath.empty()) { */ + /* ImGui::Spacing(); */ + /* ImGui::Checkbox("Mix Color", &selectedObject->mixColor); */ + /* } */ + + /* ImGui::Unindent(); */ + + /* ImGui::Spacing(); */ + + /* ImGui::Text("Manage"); */ + /* ImGui::Separator(); */ + /* ImGui::Indent(); */ + /* ImGui::Spacing(); */ + + /* ImGui::InputText("##RenameInput", renameGameObjectBuffer, + * sizeof(renameGameObjectBuffer)); */ + /* ImGui::SameLine(); */ + /* if (ImGui::Button("Rename")) { */ + /* selectedObject->SetName(renameGameObjectBuffer); */ + /* } */ + + /* if (ImGui::Button("Delete")) { */ + /* scene->RemoveGameObject(selectedObject); */ + /* selectedObject = nullptr; */ + /* } */ + + ImGui::Unindent(); + + ImGui::End(); } -void Manager::explorer(Jenjin::Scene* scene) { - ImGui::Begin("Explorer"); - - for (auto file : std::filesystem::directory_iterator(this->paths.projectPath)) { - if (file.is_directory()) { - if (ImGui::TreeNode(file.path().filename().string().c_str())) { - for (auto subfile : std::filesystem::directory_iterator(file.path())) { - if (subfile.is_regular_file()) { - ImGui::Selectable(fmt::format("{}", subfile.path().filename().string()).c_str()); - } - } - ImGui::TreePop(); - } - } - } - - ImGui::SetCursorPosY(ImGui::GetCursorPosY() + 256); - - static bool demo_tools; ImGui::Checkbox("Demo Tools", &demo_tools); if (demo_tools) ImGui::ShowDemoWindow(&demo_tools); - - ImGui::End(); +void Manager::explorer(Jenjin::Scene *scene) { + ImGui::Begin("Explorer"); + + for (auto file : + std::filesystem::directory_iterator(this->paths.projectPath)) { + if (file.is_directory()) { + if (ImGui::TreeNode(file.path().filename().string().c_str())) { + for (auto subfile : std::filesystem::directory_iterator(file.path())) { + if (subfile.is_regular_file()) { + ImGui::Selectable( + fmt::format("{}", subfile.path().filename().string()).c_str()); + } + } + ImGui::TreePop(); + } + } + } + + ImGui::SetCursorPosY(ImGui::GetCursorPosY() + 256); + + static bool demo_tools; + ImGui::Checkbox("Demo Tools", &demo_tools); + if (demo_tools) + ImGui::ShowDemoWindow(&demo_tools); + + ImGui::End(); } -void Manager::backup_prompts(Jenjin::Scene* scene) { - if (glfwWindowShouldClose(glfwGetCurrentContext())) { - if (!ImGui::IsPopupOpen("Exit")) { - glfwSetWindowShouldClose(glfwGetCurrentContext(), GLFW_FALSE); - ImGui::OpenPopup("Exit"); - } - } - - // promtp with exit, save and exit, cancel - if (ImGui::BeginPopupModal("Exit", NULL, ImGuiWindowFlags_AlwaysAutoResize)) { - ImGui::Text("Do you want to save before exiting?"); - ImGui::Separator(); - - if (ImGui::Button("Save and Exit")) { - Jenjin::EngineRef->GetCurrentScene()->Save(this->paths.openScenePath); - glfwSetWindowShouldClose(glfwGetCurrentContext(), GLFW_TRUE); - } - - ImGui::SameLine(); - - if (ImGui::Button("Exit")) { - glfwSetWindowShouldClose(glfwGetCurrentContext(), GLFW_TRUE); - } - - ImGui::SameLine(); - - if (ImGui::Button("Cancel")) { - glfwSetWindowShouldClose(glfwGetCurrentContext(), GLFW_FALSE); - ImGui::CloseCurrentPopup(); - } - ImGui::EndPopup(); - } +void Manager::backup_prompts(Jenjin::Scene *scene) { + if (glfwWindowShouldClose(glfwGetCurrentContext())) { + if (!ImGui::IsPopupOpen("Exit")) { + glfwSetWindowShouldClose(glfwGetCurrentContext(), GLFW_FALSE); + ImGui::OpenPopup("Exit"); + } + } + + // promtp with exit, save and exit, cancel + if (ImGui::BeginPopupModal("Exit", NULL, ImGuiWindowFlags_AlwaysAutoResize)) { + ImGui::Text("Do you want to save before exiting?"); + ImGui::Separator(); + + if (ImGui::Button("Save and Exit")) { + Jenjin::EngineRef->GetCurrentScene()->Save(this->paths.openScenePath); + glfwSetWindowShouldClose(glfwGetCurrentContext(), GLFW_TRUE); + } + + ImGui::SameLine(); + + if (ImGui::Button("Exit")) { + glfwSetWindowShouldClose(glfwGetCurrentContext(), GLFW_TRUE); + } + + ImGui::SameLine(); + + if (ImGui::Button("Cancel")) { + glfwSetWindowShouldClose(glfwGetCurrentContext(), GLFW_FALSE); + ImGui::CloseCurrentPopup(); + } + ImGui::EndPopup(); + } } -void Manager::code(Jenjin::Scene* scene) { -} +void Manager::code(Jenjin::Scene *scene) {} -void Manager::show_all(Jenjin::Scene* scene) { - if (this->paths.projectPath.empty()) { - menu(); - dockspace(); - welcome(); +void Manager::show_all(Jenjin::Scene *scene) { + if (this->paths.projectPath.empty()) { + menu(); + dockspace(); + welcome(); - return; - } + return; + } - menu(); - dockspace(); + menu(); + dockspace(); - hierarchy(scene); - inspector(scene); - backup_prompts(scene); - explorer(scene); + hierarchy(scene); + inspector(scene); + backup_prompts(scene); + explorer(scene); } void Manager::welcome() { - ImGui::Begin("Welcome", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoScrollbar); - - static std::string selectedPreExisting = ""; - - int pad = ImGui::GetStyle().WindowPadding.y * 2 - ImGui::GetFrameHeightWithSpacing(); - int spad = ImGui::GetStyle().WindowPadding.y * 2 + ImGui::GetStyle().ItemSpacing.y * 2; - ImGui::BeginChild("WelcomeChild", ImVec2(ImGui::GetWindowWidth() - ImGui::GetStyle().WindowPadding.x * 2, ImGui::GetWindowHeight() - pad - 64 - spad - ImGui::GetStyle().WindowPadding.y), true); - - auto open_project = [&]() { - auto file = std::string(selectedPreExisting); - auto jendir = Jenjin::Editor::get_jendir(); - this->paths.projectPath = jendir + "/Projects/" + file; - this->paths.openScenePath = jendir + "/Projects/" + file + "/main.jenscene"; - this->paths.liveScenePath = jendir + "/Projects/" + file + "/live.jenscene"; - - Jenjin::EngineRef->GetCurrentScene()->GetGameObjects()->clear(); - std::ifstream ifile(this->paths.openScenePath); - /* Jenjin::EngineRef->GetCurrentScene()->load(ifile); */ - Jenjin::EngineRef->GetCurrentScene()->Load(this->paths.openScenePath); - - // Load all the lua files - spdlog::warn("Unimplemented: Load Lua files"); - /* Jenjin::EngineRef->GetCurrentScene()->reload_lua((this->paths.projectPath + "/scripts/").c_str()); */ - }; - - for (auto file : std::filesystem::directory_iterator(Jenjin::Editor::get_jendir() + "/Projects")) { - if (file.is_directory()) { - bool isSelected = selectedPreExisting == file.path().filename().string(); - if (ImGui::Selectable(file.path().filename().string().c_str(), isSelected, ImGuiSelectableFlags_None, ImVec2(ImGui::GetWindowWidth() - ImGui::GetStyle().WindowPadding.x * 2, 0))) { - selectedPreExisting = file.path().filename().string(); - - if (isSelected) { - open_project(); - } - } - } - } - - ImGui::EndChild(); - - ImGui::BeginChild("CreateProject", ImVec2(ImGui::GetWindowWidth() - ImGui::GetStyle().WindowPadding.x * 2, 64 - spad), true); - - if (selectedPreExisting.empty()) { - ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.5f, 0.5f, 0.5f, 1.0f)); - ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.5f, 0.5f, 0.5f, 1.0f)); - ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.5f, 0.5f, 0.5f, 1.0f)); - } - - if (ImGui::Button("Open Project") && !selectedPreExisting.empty()) { - open_project(); - } - if (selectedPreExisting.empty()) ImGui::PopStyleColor(3); - - ImGui::SameLine(); - - if (ImGui::Button("New Project")) { - ImGui::OpenPopup("New Project"); - } - - ImGui::SameLine(); - - auto text = "Delete"; - auto width = ImGui::CalcTextSize(text).x; - ImGui::SetCursorPosX(ImGui::GetWindowWidth() - width - ImGui::GetStyle().WindowPadding.x - ImGui::GetStyle().ItemSpacing.x); - if (ImGui::Button(text)) { - ImGui::OpenPopup("DeleteProject"); - } - - if (ImGui::BeginPopupModal("New Project", NULL, ImGuiWindowFlags_AlwaysAutoResize)) { - static char projectName[128] = {0}; - ImGui::InputText("Project Name", projectName, sizeof(projectName)); - if (ImGui::Button("Create##NewProject") || ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Enter))) { - std::string jendir = Jenjin::Editor::get_jendir(); - std::string projectPath = jendir + "/Projects/" + projectName; - - Jenjin::Editor::ensure_dir(projectPath); - Jenjin::Editor::ensure_dir(projectPath + "/scripts"); - Jenjin::Editor::ensure_dir(projectPath + "/textures"); - - this->paths.projectPath = projectPath; - this->paths.openScenePath = projectPath + "/main.jenscene"; - this->paths.liveScenePath = projectPath + "/live.jenscene"; - Jenjin::EngineRef->GetCurrentScene()->GetGameObjects()->clear(); - Jenjin::EngineRef->GetCurrentScene()->Save(this->paths.openScenePath); - ImGui::CloseCurrentPopup(); - } - ImGui::SameLine(); - if (ImGui::Button("Cancel##NewProject") || ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Escape))) { - ImGui::CloseCurrentPopup(); - } - - ImGui::EndPopup(); - } - - if (ImGui::BeginPopupModal("DeleteProject", NULL, ImGuiWindowFlags_AlwaysAutoResize)) { - ImGui::Text("Are you sure you want to delete this project?"); - ImGui::Separator(); - if (ImGui::Button("Delete##DeleteProject")) { - std::string jendir = Jenjin::Editor::get_jendir(); - std::string projectPath = jendir + "/Projects/" + selectedPreExisting; - std::filesystem::remove_all(projectPath); - selectedPreExisting = ""; - ImGui::CloseCurrentPopup(); - } - ImGui::SameLine(); - if (ImGui::Button("Cancel##DeleteProject")) { - ImGui::CloseCurrentPopup(); - } - ImGui::EndPopup(); - } - - ImGui::EndChild(); - ImGui::End(); + ImGui::Begin("Welcome", nullptr, + ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | + ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoScrollbar); + + static std::string selectedPreExisting = ""; + + int pad = ImGui::GetStyle().WindowPadding.y * 2 - + ImGui::GetFrameHeightWithSpacing(); + int spad = ImGui::GetStyle().WindowPadding.y * 2 + + ImGui::GetStyle().ItemSpacing.y * 2; + ImGui::BeginChild( + "WelcomeChild", + ImVec2(ImGui::GetWindowWidth() - ImGui::GetStyle().WindowPadding.x * 2, + ImGui::GetWindowHeight() - pad - 64 - spad - + ImGui::GetStyle().WindowPadding.y), + true); + + auto open_project = [&]() { + auto file = std::string(selectedPreExisting); + auto jendir = Jenjin::Editor::get_jendir(); + this->paths.projectPath = jendir + "/Projects/" + file; + this->paths.openScenePath = jendir + "/Projects/" + file + "/main.jenscene"; + this->paths.liveScenePath = jendir + "/Projects/" + file + "/live.jenscene"; + + Jenjin::EngineRef->GetCurrentScene()->GetGameObjects()->clear(); + std::ifstream ifile(this->paths.openScenePath); + /* Jenjin::EngineRef->GetCurrentScene()->load(ifile); */ + Jenjin::EngineRef->GetCurrentScene()->Load(this->paths.openScenePath); + + // Load all the lua files + spdlog::warn("Unimplemented: Load Lua files"); + /* Jenjin::EngineRef->GetCurrentScene()->reload_lua((this->paths.projectPath + * + "/scripts/").c_str()); */ + }; + + for (auto file : std::filesystem::directory_iterator( + Jenjin::Editor::get_jendir() + "/Projects")) { + if (file.is_directory()) { + bool isSelected = selectedPreExisting == file.path().filename().string(); + if (ImGui::Selectable(file.path().filename().string().c_str(), isSelected, + ImGuiSelectableFlags_None, + ImVec2(ImGui::GetWindowWidth() - + ImGui::GetStyle().WindowPadding.x * 2, + 0))) { + selectedPreExisting = file.path().filename().string(); + + if (isSelected) { + open_project(); + } + } + } + } + + ImGui::EndChild(); + + ImGui::BeginChild( + "CreateProject", + ImVec2(ImGui::GetWindowWidth() - ImGui::GetStyle().WindowPadding.x * 2, + 64 - spad), + true); + + if (selectedPreExisting.empty()) { + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.5f, 0.5f, 0.5f, 1.0f)); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, + ImVec4(0.5f, 0.5f, 0.5f, 1.0f)); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, + ImVec4(0.5f, 0.5f, 0.5f, 1.0f)); + } + + if (ImGui::Button("Open Project") && !selectedPreExisting.empty()) { + open_project(); + } + if (selectedPreExisting.empty()) + ImGui::PopStyleColor(3); + + ImGui::SameLine(); + + if (ImGui::Button("New Project")) { + ImGui::OpenPopup("New Project"); + } + + ImGui::SameLine(); + + auto text = "Delete"; + auto width = ImGui::CalcTextSize(text).x; + ImGui::SetCursorPosX(ImGui::GetWindowWidth() - width - + ImGui::GetStyle().WindowPadding.x - + ImGui::GetStyle().ItemSpacing.x); + if (ImGui::Button(text)) { + ImGui::OpenPopup("DeleteProject"); + } + + if (ImGui::BeginPopupModal("New Project", NULL, + ImGuiWindowFlags_AlwaysAutoResize)) { + static char projectName[128] = {0}; + ImGui::InputText("Project Name", projectName, sizeof(projectName)); + if (ImGui::Button("Create##NewProject") || + ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Enter))) { + std::string jendir = Jenjin::Editor::get_jendir(); + std::string projectPath = jendir + "/Projects/" + projectName; + + Jenjin::Editor::ensure_dir(projectPath); + Jenjin::Editor::ensure_dir(projectPath + "/scripts"); + Jenjin::Editor::ensure_dir(projectPath + "/textures"); + + this->paths.projectPath = projectPath; + this->paths.openScenePath = projectPath + "/main.jenscene"; + this->paths.liveScenePath = projectPath + "/live.jenscene"; + Jenjin::EngineRef->GetCurrentScene()->GetGameObjects()->clear(); + Jenjin::EngineRef->GetCurrentScene()->Save(this->paths.openScenePath); + ImGui::CloseCurrentPopup(); + } + ImGui::SameLine(); + if (ImGui::Button("Cancel##NewProject") || + ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Escape))) { + ImGui::CloseCurrentPopup(); + } + + ImGui::EndPopup(); + } + + if (ImGui::BeginPopupModal("DeleteProject", NULL, + ImGuiWindowFlags_AlwaysAutoResize)) { + ImGui::Text("Are you sure you want to delete this project?"); + ImGui::Separator(); + if (ImGui::Button("Delete##DeleteProject")) { + std::string jendir = Jenjin::Editor::get_jendir(); + std::string projectPath = jendir + "/Projects/" + selectedPreExisting; + std::filesystem::remove_all(projectPath); + selectedPreExisting = ""; + ImGui::CloseCurrentPopup(); + } + ImGui::SameLine(); + if (ImGui::Button("Cancel##DeleteProject")) { + ImGui::CloseCurrentPopup(); + } + ImGui::EndPopup(); + } + + ImGui::EndChild(); + ImGui::End(); } diff --git a/engine/src/editor/utils.cpp b/engine/src/editor/utils.cpp index 69632a2..56d1ce8 100644 --- a/engine/src/editor/utils.cpp +++ b/engine/src/editor/utils.cpp @@ -1,3 +1,5 @@ +#define GLFW_INCLUDE_NONE + #include "jenjin/editor/utils.h" #include @@ -6,23 +8,23 @@ #include void Jenjin::Editor::ensure_dir(std::string path) { - if (!std::filesystem::exists(path)) { - spdlog::debug("Creating directory: {}", path); - std::filesystem::create_directories(path); - } else { - spdlog::debug("Directory exists: {}", path); - } + if (!std::filesystem::exists(path)) { + spdlog::debug("Creating directory: {}", path); + std::filesystem::create_directories(path); + } else { + spdlog::debug("Directory exists: {}", path); + } } std::string Jenjin::Editor::get_jendir() { - std::string jendir = ""; + std::string jendir = ""; #ifdef _WIN32 - jendir = std::getenv("USERPROFILE") + std::string("\\Documents\\Jenjin"); + jendir = std::getenv("USERPROFILE") + std::string("\\Documents\\Jenjin"); #elif __APPLE__ || __linux__ - jendir = std::getenv("HOME") + std::string("/Documents/Jenjin"); + jendir = std::getenv("HOME") + std::string("/Documents/Jenjin"); #else - jendir = "Jendir"; + jendir = "Jendir"; #endif - return jendir; + return jendir; } diff --git a/engine/src/editor/widgets.cpp b/engine/src/editor/widgets.cpp index fcf16cb..a3e62fd 100644 --- a/engine/src/editor/widgets.cpp +++ b/engine/src/editor/widgets.cpp @@ -1,3 +1,5 @@ +#define GLFW_INCLUDE_NONE + #include "jenjin/editor/widgets.h" #include "jenjin/gameobject.h" @@ -8,79 +10,85 @@ using namespace Jenjin::Editor; -static void drawButtonWithDrag(const std::string& buttonLabel, const std::string& dragLabel, float& value, const ImVec4& buttonColor, const ImVec4& buttonHoveredColor, const ImVec4& buttonActiveColor, const ImVec2& buttonSize, int defaultValue = 0) { - ImGui::PushStyleColor(ImGuiCol_Button, buttonColor); - ImGui::PushStyleColor(ImGuiCol_ButtonHovered, buttonHoveredColor); - ImGui::PushStyleColor(ImGuiCol_ButtonActive, buttonActiveColor); +static void drawButtonWithDrag(const std::string &buttonLabel, + const std::string &dragLabel, float &value, + const ImVec4 &buttonColor, + const ImVec4 &buttonHoveredColor, + const ImVec4 &buttonActiveColor, + const ImVec2 &buttonSize, int defaultValue = 0) { + + ImGui::PushStyleColor(ImGuiCol_Button, buttonColor); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, buttonHoveredColor); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, buttonActiveColor); - if (ImGui::Button(buttonLabel.c_str(), buttonSize)) - value = defaultValue; + if (ImGui::Button(buttonLabel.c_str(), buttonSize)) + value = defaultValue; - ImGui::PopStyleColor(3); + ImGui::PopStyleColor(3); - ImGui::SameLine(); - ImGui::DragFloat(dragLabel.c_str(), &value, 0.1f, 0.0f, 0.0f, "%.2f"); - ImGui::PopItemWidth(); - ImGui::SameLine(); + ImGui::SameLine(); + ImGui::DragFloat(dragLabel.c_str(), &value, 0.1f, 0.0f, 0.0f, "%.2f"); + ImGui::PopItemWidth(); + ImGui::SameLine(); } -static void start_widget(const std::string& label, int count = 2) { - ImGui::PushID(label.c_str()); - ImGui::Columns(2); - ImGui::SetColumnWidth(0, 100.0f); - ImGui::Text("%s", label.c_str()); - ImGui::NextColumn(); +static void start_widget(const std::string &label, int count = 2) { + ImGui::PushID(label.c_str()); + ImGui::Columns(2); + ImGui::SetColumnWidth(0, 100.0f); + ImGui::Text("%s", label.c_str()); + ImGui::NextColumn(); + + // HACK: This is a hack to make the drag float buttons all the same size... + // this needs to be revisited + auto &s = ImGui::GetStyle(); + int added = count == 1 ? s.ItemSpacing.x * 2 + s.FramePadding.x + 1 : 0; + ImGui::PushMultiItemsWidths(count, ImGui::CalcItemWidth() + added); - // HACK: This is a hack to make the drag float buttons all the same size... this needs to be revisited - ImGui::PushMultiItemsWidths(count, ImGui::CalcItemWidth() + (count == 1 ? ImGui::GetStyle().ItemSpacing.x * 2 + ImGui::GetStyle().FramePadding.x + 1 : 0)); - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2{ 0, 0 }); + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2{0, 0}); } static void end_widget() { - ImGui::PopStyleVar(); - ImGui::Columns(1); - ImGui::PopID(); + ImGui::PopStyleVar(); + ImGui::Columns(1); + ImGui::PopID(); } -bool Widgets::transformWidget(Jenjin::GameObject::Transform* transform) { - float lineHeight = GImGui->Font->FontSize + GImGui->Style.FramePadding.y * 2.0f; - ImVec2 buttonSize = { lineHeight + 3.0f, lineHeight }; - - start_widget("Position"); - drawButtonWithDrag("X", "##X", transform->position.x, - ImVec4{ 0.6f, 0.2f, 0.2f, 1.0f }, - ImVec4{ 0.7f, 0.3f, 0.3f, 1.0f }, - ImVec4{ 0.6f, 0.2f, 0.2f, 1.0f }, - buttonSize); - - drawButtonWithDrag("Y", "##Y", transform->position.y, - ImVec4{ 0.3f, 0.6f, 0.3f, 1.0f }, - ImVec4{ 0.4f, 0.7f, 0.4f, 1.0f }, - ImVec4{ 0.3f, 0.6f, 0.3f, 1.0f }, - buttonSize); - end_widget(); - - start_widget("Scale"); - drawButtonWithDrag("W", "##ScaleW", transform->scale.x, - ImVec4{ 0.6f, 0.2f, 0.2f, 1.0f }, - ImVec4{ 0.7f, 0.3f, 0.3f, 1.0f }, - ImVec4{ 0.6f, 0.2f, 0.2f, 1.0f }, - buttonSize, 1); - - drawButtonWithDrag("H", "##ScaleH", transform->scale.y, - ImVec4{ 0.3f, 0.6f, 0.3f, 1.0f }, - ImVec4{ 0.4f, 0.7f, 0.4f, 1.0f }, - ImVec4{ 0.3f, 0.6f, 0.3f, 1.0f }, - buttonSize, 1); - end_widget(); - - start_widget("Rotation", 1); - drawButtonWithDrag("Z", "##Rotation", transform->rotation, - ImVec4{ 0.3f, 0.4f, 0.7f, 1.0f }, - ImVec4{ 0.4f, 0.5f, 0.8f, 1.0f }, - ImVec4{ 0.3f, 0.4f, 0.7f, 1.0f }, - buttonSize); - end_widget(); - - return false; +bool Widgets::transformWidget(Jenjin::GameObject::Transform *transform) { + float lineHeight = + GImGui->Font->FontSize + GImGui->Style.FramePadding.y * 2.0f; + ImVec2 buttonSize = {lineHeight + 3.0f, lineHeight}; + + start_widget("Position"); + drawButtonWithDrag("X", "##X", transform->position.x, + ImVec4{0.6f, 0.2f, 0.2f, 1.0f}, + ImVec4{0.7f, 0.3f, 0.3f, 1.0f}, + ImVec4{0.6f, 0.2f, 0.2f, 1.0f}, buttonSize); + + drawButtonWithDrag("Y", "##Y", transform->position.y, + ImVec4{0.3f, 0.6f, 0.3f, 1.0f}, + ImVec4{0.4f, 0.7f, 0.4f, 1.0f}, + ImVec4{0.3f, 0.6f, 0.3f, 1.0f}, buttonSize); + end_widget(); + + start_widget("Scale"); + drawButtonWithDrag("W", "##ScaleW", transform->scale.x, + ImVec4{0.6f, 0.2f, 0.2f, 1.0f}, + ImVec4{0.7f, 0.3f, 0.3f, 1.0f}, + ImVec4{0.6f, 0.2f, 0.2f, 1.0f}, buttonSize, 1); + + drawButtonWithDrag("H", "##ScaleH", transform->scale.y, + ImVec4{0.3f, 0.6f, 0.3f, 1.0f}, + ImVec4{0.4f, 0.7f, 0.4f, 1.0f}, + ImVec4{0.3f, 0.6f, 0.3f, 1.0f}, buttonSize, 1); + end_widget(); + + start_widget("Rotation", 1); + drawButtonWithDrag("Z", "##Rotation", transform->rotation, + ImVec4{0.3f, 0.4f, 0.7f, 1.0f}, + ImVec4{0.4f, 0.5f, 0.8f, 1.0f}, + ImVec4{0.3f, 0.4f, 0.7f, 1.0f}, buttonSize); + end_widget(); + + return false; } diff --git a/engine/src/engine.cpp b/engine/src/engine.cpp index 6ada285..2201036 100644 --- a/engine/src/engine.cpp +++ b/engine/src/engine.cpp @@ -1,88 +1,92 @@ +#define GLFW_INCLUDE_NONE + #include "jenjin/engine.h" #include "jenjin/editor/state.h" -#include #include +#include #include using namespace Jenjin; -const char* VERSION = "0.0.1"; +const char *VERSION = "0.0.1"; -Engine* Jenjin::EngineRef = nullptr; +Engine *Jenjin::EngineRef = nullptr; Jenjin::Editor::State_t Jenjin::Editor::State; -Engine::Engine(GLFWwindow* window) { - EngineRef = this; - - spdlog::set_level(spdlog::level::trace); - spdlog::debug("Initializing Jenjin {}", VERSION); - - glfwSetErrorCallback([](int code, const char* error) { - spdlog::error("GLFW Error: {} ({})", error, code); - }); - - // Intialize GLFW, logging any errors - if (glfwGetKeyScancode(GLFW_KEY_ESCAPE) == GLFW_NOT_INITIALIZED) { - spdlog::error("GLFW isn't initialized, Jenjin needs GLFW externally initialized"); - exit(EXIT_FAILURE); - } - - // Check if the engine has a window - if (!window) { - spdlog::error("There isn't a glfwContext to render to, Jenjin needs a window to render to"); - exit(EXIT_FAILURE); - } - - glfwContext = window; - - // Make the glfwContext's context current - glfwMakeContextCurrent(glfwContext); - - // Load OpenGL functions using GLAD - if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { - spdlog::error("Failed to initialize GLAD"); - glfwTerminate(); - exit(EXIT_FAILURE); - } - - spdlog::debug("OpenGL Version: {}", (const char*)glGetString(GL_VERSION)); - spdlog::debug("GLSL Version: {}", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION)); - - spdlog::debug("Vendor: {}", (const char*)glGetString(GL_VENDOR)); - - glfwSetFramebufferSizeCallback(glfwContext, [](GLFWwindow* glfwContext, int width, int height) { - Target* target = EngineRef->GetCurrentScene()->GetTarget(); - if (target->RespondsToWindowResize()) { - target->Resize(glm::vec2(width, height)); - } - }); +Engine::Engine(GLFWwindow *window) { + EngineRef = this; + + spdlog::set_level(spdlog::level::trace); + spdlog::debug("Initializing Jenjin {}", VERSION); + + glfwSetErrorCallback([](int code, const char *error) { + spdlog::error("GLFW Error: {} ({})", error, code); + }); + + // Intialize GLFW, logging any errors + if (glfwGetKeyScancode(GLFW_KEY_ESCAPE) == GLFW_NOT_INITIALIZED) { + spdlog::error( + "GLFW isn't initialized, Jenjin needs GLFW externally initialized"); + exit(EXIT_FAILURE); + } + + // Check if the engine has a window + if (!window) { + spdlog::error("There isn't a glfwContext to render to, Jenjin needs a " + "window to render to"); + exit(EXIT_FAILURE); + } + + glfwContext = window; + + // Make the glfwContext's context current + glfwMakeContextCurrent(glfwContext); + + // Load OpenGL functions using GLAD + if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { + spdlog::error("Failed to initialize GLAD"); + glfwTerminate(); + exit(EXIT_FAILURE); + } + + spdlog::debug("OpenGL Version: {}", (const char *)glGetString(GL_VERSION)); + spdlog::debug("GLSL Version: {}", + (const char *)glGetString(GL_SHADING_LANGUAGE_VERSION)); + + spdlog::debug("Vendor: {}", (const char *)glGetString(GL_VENDOR)); + + glfwSetFramebufferSizeCallback( + glfwContext, [](GLFWwindow *glfwContext, int width, int height) { + Target *target = EngineRef->GetCurrentScene()->GetTarget(); + if (target->RespondsToWindowResize()) { + target->Resize(glm::vec2(width, height)); + } + }); } -Engine::~Engine() { - spdlog::trace("Engine::~Engine()"); -} +Engine::~Engine() { spdlog::trace("Engine::~Engine()"); } void Engine::AddScene(std::shared_ptr scene, bool defaultScene) { - spdlog::trace("Engine::AddScene({}, {})", (void*)scene.get(), defaultScene); - scenes.emplace_back(scene); + spdlog::trace("Engine::AddScene({}, {})", (void *)scene.get(), defaultScene); + scenes.emplace_back(scene); - if (defaultScene) { - currentScene = scene.get(); - } + if (defaultScene) { + currentScene = scene.get(); + } } -void Engine::Render(Target* target) { - target->PreRender(); +void Engine::Render(Target *target) { + target->PreRender(); - if (currentScene) { - currentScene->Render(); - } else { - spdlog::warn("No scene to render"); - } + if (currentScene) { + currentScene->Render(); + } else { + spdlog::warn("No scene to render"); + } - target->Render(); + target->Render(); - target->PostRender(); + target->PostRender(); } diff --git a/engine/src/framebuffer.cpp b/engine/src/framebuffer.cpp index 1b46769..edf3c4b 100644 --- a/engine/src/framebuffer.cpp +++ b/engine/src/framebuffer.cpp @@ -1,3 +1,5 @@ +#define GLFW_INCLUDE_NONE + #include "jenjin/framebuffer.h" #include @@ -7,49 +9,55 @@ using namespace Jenjin; Framebuffer::Framebuffer() { - glGenFramebuffers(1, &fbo); - glBindFramebuffer(GL_FRAMEBUFFER, fbo); - - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); - - glGenRenderbuffers(1, &rbo); - glBindRenderbuffer(GL_RENDERBUFFER, rbo); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 800, 600); - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo); - - if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { - const char* error; - int code = glGetError(); - spdlog::error("Framebuffer is not complete: {} ({})", error, code); - exit(EXIT_FAILURE); - } - - glBindFramebuffer(GL_FRAMEBUFFER, 0); - glBindTexture(GL_TEXTURE_2D, 0); - glBindRenderbuffer(GL_RENDERBUFFER, 0); + glGenFramebuffers(1, &fbo); + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_RGB, GL_UNSIGNED_BYTE, + nullptr); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, + texture, 0); + + glGenRenderbuffers(1, &rbo); + glBindRenderbuffer(GL_RENDERBUFFER, rbo); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 800, 600); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, + GL_RENDERBUFFER, rbo); + + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { + const char *error; + int code = glGetError(); + spdlog::error("Framebuffer is not complete: {} ({})", error, code); + exit(EXIT_FAILURE); + } + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glBindTexture(GL_TEXTURE_2D, 0); + glBindRenderbuffer(GL_RENDERBUFFER, 0); } Framebuffer::~Framebuffer() { - spdlog::trace("Framebuffer::~Framebuffer()"); + spdlog::trace("Framebuffer::~Framebuffer()"); - glDeleteFramebuffers(1, &fbo); - glDeleteTextures(1, &texture); - glDeleteRenderbuffers(1, &rbo); + glDeleteFramebuffers(1, &fbo); + glDeleteTextures(1, &texture); + glDeleteRenderbuffers(1, &rbo); } void Framebuffer::Bind() { glBindFramebuffer(GL_FRAMEBUFFER, fbo); } -void Framebuffer::Unbind() { glBindFramebuffer(GL_FRAMEBUFFER, 0); } // NOTE: This is not necessary +void Framebuffer::Unbind() { + glBindFramebuffer(GL_FRAMEBUFFER, 0); +} // NOTE: This is not necessary void Framebuffer::Resize(int width, int height) { - glBindTexture(GL_TEXTURE_2D, texture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr); - glBindTexture(GL_TEXTURE_2D, 0); - glBindRenderbuffer(GL_RENDERBUFFER, rbo); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height); - glBindRenderbuffer(GL_RENDERBUFFER, 0); + glBindTexture(GL_TEXTURE_2D, texture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, + GL_UNSIGNED_BYTE, nullptr); + glBindTexture(GL_TEXTURE_2D, 0); + glBindRenderbuffer(GL_RENDERBUFFER, rbo); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height); + glBindRenderbuffer(GL_RENDERBUFFER, 0); } diff --git a/engine/src/helpers.cpp b/engine/src/helpers.cpp index 1fac5aa..50034fc 100644 --- a/engine/src/helpers.cpp +++ b/engine/src/helpers.cpp @@ -1,3 +1,5 @@ +#define GLFW_INCLUDE_NONE + #include "jenjin/helpers.h" #include @@ -7,147 +9,194 @@ #include namespace Jenjin::Helpers { -GLFWwindow* CreateWindow(int width, int height, const char* title) { - if (!glfwInit()) { - return nullptr; - } +GLFWwindow *CreateWindow(int width, int height, const char *title) { + if (!glfwInit()) { + return nullptr; + } - GLFWwindow* window = glfwCreateWindow(width, height, title, nullptr, nullptr); + GLFWwindow *window = glfwCreateWindow(width, height, title, nullptr, nullptr); - if (!window) { - glfwTerminate(); - return nullptr; - } + if (!window) { + glfwTerminate(); + return nullptr; + } - glfwMakeContextCurrent(window); - return window; + glfwMakeContextCurrent(window); + return window; } -void CheckWindow(GLFWwindow* window) { - if (!window) { - const char* error; - int code = glfwGetError(&error); - spdlog::error("Failed to create window: {} ({})", error, code); - exit(EXIT_FAILURE); - } +void CheckWindow(GLFWwindow *window) { + if (!window) { + const char *error; + int code = glfwGetError(&error); + spdlog::error("Failed to create window: {} ({})", error, code); + exit(EXIT_FAILURE); + } } -void InitiateImGui(GLFWwindow* window) { - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); - ImGuiIO& io = ImGui::GetIO(); - io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; - io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; - io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; - - io.Fonts->AddFontFromFileTTF("resources/fonts/Roboto-Medium.ttf", 16.0f); - ImGui::StyleColorsDark(); - - // Photoshop style by Derydoca from ImThemes - auto Derydocas_PhotoshopTheme_FromImThemes = [](ImGuiStyle& style) { - style.Alpha = 1.0f; - style.DisabledAlpha = 0.6000000238418579f; - style.WindowPadding = ImVec2(8.0f, 8.0f); - style.WindowRounding = 4.0f; - style.WindowBorderSize = 1.0f; - style.WindowMinSize = ImVec2(32.0f, 32.0f); - style.WindowTitleAlign = ImVec2(0.0f, 0.5f); - style.WindowMenuButtonPosition = ImGuiDir_Left; - style.ChildRounding = 4.0f; - style.ChildBorderSize = 1.0f; - style.PopupRounding = 2.0f; - style.PopupBorderSize = 1.0f; - style.FramePadding = ImVec2(4.0f, 3.0f); - style.FrameRounding = 2.0f; - style.FrameBorderSize = 1.0f; - style.ItemSpacing = ImVec2(8.0f, 4.0f); - style.ItemInnerSpacing = ImVec2(4.0f, 4.0f); - style.CellPadding = ImVec2(10.0f, 8.0f); - style.IndentSpacing = 21.0f; - style.ColumnsMinSpacing = 6.0f; - style.ScrollbarSize = 13.0f; - style.ScrollbarRounding = 12.0f; - style.GrabMinSize = 7.0f; - style.GrabRounding = 0.0f; - style.TabRounding = 0.0f; - style.TabBorderSize = 1.0f; - style.TabMinWidthForCloseButton = 0.0f; - style.ColorButtonPosition = ImGuiDir_Right; - style.ButtonTextAlign = ImVec2(0.5f, 0.5f); - style.SelectableTextAlign = ImVec2(0.0f, 0.0f); - - style.Colors[ImGuiCol_Text] = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); - style.Colors[ImGuiCol_TextDisabled] = ImVec4(0.4980392158031464f, 0.4980392158031464f, 0.4980392158031464f, 1.0f); - style.Colors[ImGuiCol_WindowBg] = ImVec4(0.1764705926179886f, 0.1764705926179886f, 0.1764705926179886f, 1.0f); - style.Colors[ImGuiCol_ChildBg] = ImVec4(0.2784313857555389f, 0.2784313857555389f, 0.2784313857555389f, 0.0f); - style.Colors[ImGuiCol_PopupBg] = ImVec4(0.3098039329051971f, 0.3098039329051971f, 0.3098039329051971f, 1.0f); - style.Colors[ImGuiCol_Border] = ImVec4(0.2627451121807098f, 0.2627451121807098f, 0.2627451121807098f, 1.0f); - style.Colors[ImGuiCol_BorderShadow] = ImVec4(0.0f, 0.0f, 0.0f, 0.0f); - style.Colors[ImGuiCol_FrameBg] = ImVec4(0.1568627506494522f, 0.1568627506494522f, 0.1568627506494522f, 1.0f); - style.Colors[ImGuiCol_FrameBgHovered] = ImVec4(0.2000000029802322f, 0.2000000029802322f, 0.2000000029802322f, 1.0f); - style.Colors[ImGuiCol_FrameBgActive] = ImVec4(0.2784313857555389f, 0.2784313857555389f, 0.2784313857555389f, 1.0f); - style.Colors[ImGuiCol_TitleBg] = ImVec4(0.1450980454683304f, 0.1450980454683304f, 0.1450980454683304f, 1.0f); - style.Colors[ImGuiCol_TitleBgActive] = ImVec4(0.1450980454683304f, 0.1450980454683304f, 0.1450980454683304f, 1.0f); - style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.1450980454683304f, 0.1450980454683304f, 0.1450980454683304f, 1.0f); - style.Colors[ImGuiCol_MenuBarBg] = ImVec4(0.1921568661928177f, 0.1921568661928177f, 0.1921568661928177f, 1.0f); - style.Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.1568627506494522f, 0.1568627506494522f, 0.1568627506494522f, 1.0f); - style.Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.2745098173618317f, 0.2745098173618317f, 0.2745098173618317f, 1.0f); - style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.2980392277240753f, 0.2980392277240753f, 0.2980392277240753f, 1.0f); - style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(1.0f, 0.3882353007793427f, 0.0f, 1.0f); - style.Colors[ImGuiCol_CheckMark] = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); - style.Colors[ImGuiCol_SliderGrab] = ImVec4(0.3882353007793427f, 0.3882353007793427f, 0.3882353007793427f, 1.0f); - style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(1.0f, 0.3882353007793427f, 0.0f, 1.0f); - style.Colors[ImGuiCol_Button] = ImVec4(1.0f, 1.0f, 1.0f, 0.0f); - style.Colors[ImGuiCol_ButtonHovered] = ImVec4(1.0f, 1.0f, 1.0f, 0.1560000032186508f); - style.Colors[ImGuiCol_ButtonActive] = ImVec4(1.0f, 1.0f, 1.0f, 0.3910000026226044f); - style.Colors[ImGuiCol_Header] = ImVec4(0.3098039329051971f, 0.3098039329051971f, 0.3098039329051971f, 1.0f); - style.Colors[ImGuiCol_HeaderHovered] = ImVec4(0.4666666686534882f, 0.4666666686534882f, 0.4666666686534882f, 1.0f); - style.Colors[ImGuiCol_HeaderActive] = ImVec4(0.4666666686534882f, 0.4666666686534882f, 0.4666666686534882f, 1.0f); - style.Colors[ImGuiCol_Separator] = ImVec4(0.2627451121807098f, 0.2627451121807098f, 0.2627451121807098f, 1.0f); - style.Colors[ImGuiCol_SeparatorHovered] = ImVec4(0.3882353007793427f, 0.3882353007793427f, 0.3882353007793427f, 1.0f); - style.Colors[ImGuiCol_SeparatorActive] = ImVec4(1.0f, 0.3882353007793427f, 0.0f, 1.0f); - style.Colors[ImGuiCol_ResizeGrip] = ImVec4(1.0f, 1.0f, 1.0f, 0.25f); - style.Colors[ImGuiCol_ResizeGripHovered] = ImVec4(1.0f, 1.0f, 1.0f, 0.6700000166893005f); - style.Colors[ImGuiCol_ResizeGripActive] = ImVec4(1.0f, 0.3882353007793427f, 0.0f, 1.0f); - style.Colors[ImGuiCol_Tab] = ImVec4(0.09411764889955521f, 0.09411764889955521f, 0.09411764889955521f, 1.0f); - style.Colors[ImGuiCol_TabHovered] = ImVec4(0.3490196168422699f, 0.3490196168422699f, 0.3490196168422699f, 1.0f); - style.Colors[ImGuiCol_TabActive] = ImVec4(0.1921568661928177f, 0.1921568661928177f, 0.1921568661928177f, 1.0f); - style.Colors[ImGuiCol_TabUnfocused] = ImVec4(0.09411764889955521f, 0.09411764889955521f, 0.09411764889955521f, 1.0f); - style.Colors[ImGuiCol_TabUnfocusedActive] = ImVec4(0.1921568661928177f, 0.1921568661928177f, 0.1921568661928177f, 1.0f); - style.Colors[ImGuiCol_PlotLines] = ImVec4(0.4666666686534882f, 0.4666666686534882f, 0.4666666686534882f, 1.0f); - style.Colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.0f, 0.3882353007793427f, 0.0f, 1.0f); - style.Colors[ImGuiCol_PlotHistogram] = ImVec4(0.5843137502670288f, 0.5843137502670288f, 0.5843137502670288f, 1.0f); - style.Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.0f, 0.3882353007793427f, 0.0f, 1.0f); - style.Colors[ImGuiCol_TableHeaderBg] = ImVec4(0.1882352977991104f, 0.1882352977991104f, 0.2000000029802322f, 1.0f); - style.Colors[ImGuiCol_TableBorderStrong] = ImVec4(0.3098039329051971f, 0.3098039329051971f, 0.3490196168422699f, 1.0f); - style.Colors[ImGuiCol_TableBorderLight] = ImVec4(0.2274509817361832f, 0.2274509817361832f, 0.2470588237047195f, 1.0f); - style.Colors[ImGuiCol_TableRowBg] = ImVec4(0.0f, 0.0f, 0.0f, 0.0f); - style.Colors[ImGuiCol_TableRowBgAlt] = ImVec4(1.0f, 1.0f, 1.0f, 0.05999999865889549f); - style.Colors[ImGuiCol_TextSelectedBg] = ImVec4(1.0f, 1.0f, 1.0f, 0.1560000032186508f); - style.Colors[ImGuiCol_DragDropTarget] = ImVec4(1.0f, 0.3882353007793427f, 0.0f, 1.0f); - style.Colors[ImGuiCol_NavHighlight] = ImVec4(1.0f, 0.3882353007793427f, 0.0f, 1.0f); - style.Colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.0f, 0.3882353007793427f, 0.0f, 1.0f); - style.Colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.0f, 0.0f, 0.0f, 0.5860000252723694f); - style.Colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.0f, 0.0f, 0.0f, 0.5860000252723694f); - }; - - Derydocas_PhotoshopTheme_FromImThemes(ImGui::GetStyle()); - - ImGui_ImplGlfw_InitForOpenGL(window, true); - ImGui_ImplOpenGL3_Init("#version 460"); +void InitiateImGui(GLFWwindow *window) { + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO &io = ImGui::GetIO(); + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; + + io.Fonts->AddFontFromFileTTF("resources/fonts/Roboto-Medium.ttf", 16.0f); + ImGui::StyleColorsDark(); + + // Photoshop style by Derydoca from ImThemes + auto Derydocas_PhotoshopTheme_FromImThemes = [](ImGuiStyle &style) { + style.Alpha = 1.0f; + style.DisabledAlpha = 0.6000000238418579f; + style.WindowPadding = ImVec2(8.0f, 8.0f); + style.WindowRounding = 4.0f; + style.WindowBorderSize = 1.0f; + style.WindowMinSize = ImVec2(32.0f, 32.0f); + style.WindowTitleAlign = ImVec2(0.0f, 0.5f); + style.WindowMenuButtonPosition = ImGuiDir_Left; + style.ChildRounding = 4.0f; + style.ChildBorderSize = 1.0f; + style.PopupRounding = 2.0f; + style.PopupBorderSize = 1.0f; + style.FramePadding = ImVec2(4.0f, 3.0f); + style.FrameRounding = 2.0f; + style.FrameBorderSize = 1.0f; + style.ItemSpacing = ImVec2(8.0f, 4.0f); + style.ItemInnerSpacing = ImVec2(4.0f, 4.0f); + style.CellPadding = ImVec2(10.0f, 8.0f); + style.IndentSpacing = 21.0f; + style.ColumnsMinSpacing = 6.0f; + style.ScrollbarSize = 13.0f; + style.ScrollbarRounding = 12.0f; + style.GrabMinSize = 7.0f; + style.GrabRounding = 0.0f; + style.TabRounding = 0.0f; + style.TabBorderSize = 1.0f; + style.TabMinWidthForCloseButton = 0.0f; + style.ColorButtonPosition = ImGuiDir_Right; + style.ButtonTextAlign = ImVec2(0.5f, 0.5f); + style.SelectableTextAlign = ImVec2(0.0f, 0.0f); + + style.Colors[ImGuiCol_Text] = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); + style.Colors[ImGuiCol_TextDisabled] = ImVec4( + 0.4980392158031464f, 0.4980392158031464f, 0.4980392158031464f, 1.0f); + style.Colors[ImGuiCol_WindowBg] = ImVec4( + 0.1764705926179886f, 0.1764705926179886f, 0.1764705926179886f, 1.0f); + style.Colors[ImGuiCol_ChildBg] = ImVec4( + 0.2784313857555389f, 0.2784313857555389f, 0.2784313857555389f, 0.0f); + style.Colors[ImGuiCol_PopupBg] = ImVec4( + 0.3098039329051971f, 0.3098039329051971f, 0.3098039329051971f, 1.0f); + style.Colors[ImGuiCol_Border] = ImVec4( + 0.2627451121807098f, 0.2627451121807098f, 0.2627451121807098f, 1.0f); + style.Colors[ImGuiCol_BorderShadow] = ImVec4(0.0f, 0.0f, 0.0f, 0.0f); + style.Colors[ImGuiCol_FrameBg] = ImVec4( + 0.1568627506494522f, 0.1568627506494522f, 0.1568627506494522f, 1.0f); + style.Colors[ImGuiCol_FrameBgHovered] = ImVec4( + 0.2000000029802322f, 0.2000000029802322f, 0.2000000029802322f, 1.0f); + style.Colors[ImGuiCol_FrameBgActive] = ImVec4( + 0.2784313857555389f, 0.2784313857555389f, 0.2784313857555389f, 1.0f); + style.Colors[ImGuiCol_TitleBg] = ImVec4( + 0.1450980454683304f, 0.1450980454683304f, 0.1450980454683304f, 1.0f); + style.Colors[ImGuiCol_TitleBgActive] = ImVec4( + 0.1450980454683304f, 0.1450980454683304f, 0.1450980454683304f, 1.0f); + style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4( + 0.1450980454683304f, 0.1450980454683304f, 0.1450980454683304f, 1.0f); + style.Colors[ImGuiCol_MenuBarBg] = ImVec4( + 0.1921568661928177f, 0.1921568661928177f, 0.1921568661928177f, 1.0f); + style.Colors[ImGuiCol_ScrollbarBg] = ImVec4( + 0.1568627506494522f, 0.1568627506494522f, 0.1568627506494522f, 1.0f); + style.Colors[ImGuiCol_ScrollbarGrab] = ImVec4( + 0.2745098173618317f, 0.2745098173618317f, 0.2745098173618317f, 1.0f); + style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4( + 0.2980392277240753f, 0.2980392277240753f, 0.2980392277240753f, 1.0f); + style.Colors[ImGuiCol_ScrollbarGrabActive] = + ImVec4(1.0f, 0.3882353007793427f, 0.0f, 1.0f); + style.Colors[ImGuiCol_CheckMark] = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); + style.Colors[ImGuiCol_SliderGrab] = ImVec4( + 0.3882353007793427f, 0.3882353007793427f, 0.3882353007793427f, 1.0f); + style.Colors[ImGuiCol_SliderGrabActive] = + ImVec4(1.0f, 0.3882353007793427f, 0.0f, 1.0f); + style.Colors[ImGuiCol_Button] = ImVec4(1.0f, 1.0f, 1.0f, 0.0f); + style.Colors[ImGuiCol_ButtonHovered] = + ImVec4(1.0f, 1.0f, 1.0f, 0.1560000032186508f); + style.Colors[ImGuiCol_ButtonActive] = + ImVec4(1.0f, 1.0f, 1.0f, 0.3910000026226044f); + style.Colors[ImGuiCol_Header] = ImVec4( + 0.3098039329051971f, 0.3098039329051971f, 0.3098039329051971f, 1.0f); + style.Colors[ImGuiCol_HeaderHovered] = ImVec4( + 0.4666666686534882f, 0.4666666686534882f, 0.4666666686534882f, 1.0f); + style.Colors[ImGuiCol_HeaderActive] = ImVec4( + 0.4666666686534882f, 0.4666666686534882f, 0.4666666686534882f, 1.0f); + style.Colors[ImGuiCol_Separator] = ImVec4( + 0.2627451121807098f, 0.2627451121807098f, 0.2627451121807098f, 1.0f); + style.Colors[ImGuiCol_SeparatorHovered] = ImVec4( + 0.3882353007793427f, 0.3882353007793427f, 0.3882353007793427f, 1.0f); + style.Colors[ImGuiCol_SeparatorActive] = + ImVec4(1.0f, 0.3882353007793427f, 0.0f, 1.0f); + style.Colors[ImGuiCol_ResizeGrip] = ImVec4(1.0f, 1.0f, 1.0f, 0.25f); + style.Colors[ImGuiCol_ResizeGripHovered] = + ImVec4(1.0f, 1.0f, 1.0f, 0.6700000166893005f); + style.Colors[ImGuiCol_ResizeGripActive] = + ImVec4(1.0f, 0.3882353007793427f, 0.0f, 1.0f); + style.Colors[ImGuiCol_Tab] = ImVec4( + 0.09411764889955521f, 0.09411764889955521f, 0.09411764889955521f, 1.0f); + style.Colors[ImGuiCol_TabHovered] = ImVec4( + 0.3490196168422699f, 0.3490196168422699f, 0.3490196168422699f, 1.0f); + style.Colors[ImGuiCol_TabActive] = ImVec4( + 0.1921568661928177f, 0.1921568661928177f, 0.1921568661928177f, 1.0f); + style.Colors[ImGuiCol_TabUnfocused] = ImVec4( + 0.09411764889955521f, 0.09411764889955521f, 0.09411764889955521f, 1.0f); + style.Colors[ImGuiCol_TabUnfocusedActive] = ImVec4( + 0.1921568661928177f, 0.1921568661928177f, 0.1921568661928177f, 1.0f); + style.Colors[ImGuiCol_PlotLines] = ImVec4( + 0.4666666686534882f, 0.4666666686534882f, 0.4666666686534882f, 1.0f); + style.Colors[ImGuiCol_PlotLinesHovered] = + ImVec4(1.0f, 0.3882353007793427f, 0.0f, 1.0f); + style.Colors[ImGuiCol_PlotHistogram] = ImVec4( + 0.5843137502670288f, 0.5843137502670288f, 0.5843137502670288f, 1.0f); + style.Colors[ImGuiCol_PlotHistogramHovered] = + ImVec4(1.0f, 0.3882353007793427f, 0.0f, 1.0f); + style.Colors[ImGuiCol_TableHeaderBg] = ImVec4( + 0.1882352977991104f, 0.1882352977991104f, 0.2000000029802322f, 1.0f); + style.Colors[ImGuiCol_TableBorderStrong] = ImVec4( + 0.3098039329051971f, 0.3098039329051971f, 0.3490196168422699f, 1.0f); + style.Colors[ImGuiCol_TableBorderLight] = ImVec4( + 0.2274509817361832f, 0.2274509817361832f, 0.2470588237047195f, 1.0f); + style.Colors[ImGuiCol_TableRowBg] = ImVec4(0.0f, 0.0f, 0.0f, 0.0f); + style.Colors[ImGuiCol_TableRowBgAlt] = + ImVec4(1.0f, 1.0f, 1.0f, 0.05999999865889549f); + style.Colors[ImGuiCol_TextSelectedBg] = + ImVec4(1.0f, 1.0f, 1.0f, 0.1560000032186508f); + style.Colors[ImGuiCol_DragDropTarget] = + ImVec4(1.0f, 0.3882353007793427f, 0.0f, 1.0f); + style.Colors[ImGuiCol_NavHighlight] = + ImVec4(1.0f, 0.3882353007793427f, 0.0f, 1.0f); + style.Colors[ImGuiCol_NavWindowingHighlight] = + ImVec4(1.0f, 0.3882353007793427f, 0.0f, 1.0f); + style.Colors[ImGuiCol_NavWindowingDimBg] = + ImVec4(0.0f, 0.0f, 0.0f, 0.5860000252723694f); + style.Colors[ImGuiCol_ModalWindowDimBg] = + ImVec4(0.0f, 0.0f, 0.0f, 0.5860000252723694f); + }; + + Derydocas_PhotoshopTheme_FromImThemes(ImGui::GetStyle()); + + ImGui_ImplGlfw_InitForOpenGL(window, true); + ImGui_ImplOpenGL3_Init("#version 460"); } Mesh CreateQuad(int width, int height) { - Mesh mesh; + Mesh mesh; - mesh.vertices = { - Vertex{{-width / 2, -height / 2, 0.0f}, {0.0f, 0.0f}}, - Vertex{{width / 2, -height / 2, 0.0f}, {1.0f, 0.0f}}, - Vertex{{width / 2, height / 2, 0.0f}, {1.0f, 1.0f}}, - Vertex{{-width / 2, height / 2, 0.0f}, {0.0f, 1.0f}}, - }; + mesh.vertices = { + Vertex{{-width / 2, -height / 2, 0.0f}, {0.0f, 0.0f}}, + Vertex{{width / 2, -height / 2, 0.0f}, {1.0f, 0.0f}}, + Vertex{{width / 2, height / 2, 0.0f}, {1.0f, 1.0f}}, + Vertex{{-width / 2, height / 2, 0.0f}, {0.0f, 1.0f}}, + }; - mesh.indices = {0, 1, 2, 2, 3, 0}; - return mesh; -} + mesh.indices = {0, 1, 2, 2, 3, 0}; + return mesh; } +} // namespace Jenjin::Helpers diff --git a/engine/src/scene.cpp b/engine/src/scene.cpp index f256ef6..fa7119c 100644 --- a/engine/src/scene.cpp +++ b/engine/src/scene.cpp @@ -1,3 +1,5 @@ +#define GLFW_INCLUDE_NONE + #include "jenjin/scene.h" #include "jenjin/gameobject.h" #include "jenjin/helpers.h" @@ -8,203 +10,217 @@ #include +#include #include #include -#include using namespace Jenjin; -Scene::Scene() { - spdlog::trace("Scene::Scene()"); -} +Scene::Scene() { spdlog::trace("Scene::Scene()"); } -void Scene::SetTarget(Target* target) { - spdlog::trace("Scene::SetTarget({})", (void*)target); +void Scene::SetTarget(Target *target) { + spdlog::trace("Scene::SetTarget({})", (void *)target); - this->target = target; - this->camera.Resize(target->GetSize()); + this->target = target; + this->camera.Resize(target->GetSize()); } void Scene::AddGameObject(std::shared_ptr gameObject) { - gameObjects.emplace_back(gameObject); + gameObjects.emplace_back(gameObject); } void Scene::RemoveGameObject(std::shared_ptr gameObject) { - this->RemoveGameObject(gameObject.get()); + this->RemoveGameObject(gameObject.get()); } -void Scene::RemoveGameObject(GameObject* gameObject) { - gameObjects.erase(std::remove_if(gameObjects.begin(), gameObjects.end(), [gameObject](std::shared_ptr go) { - return go.get() == gameObject; - }), gameObjects.end()); +void Scene::RemoveGameObject(GameObject *gameObject) { + gameObjects.erase( + std::remove_if(gameObjects.begin(), gameObjects.end(), + [gameObject](std::shared_ptr go) { + return go.get() == gameObject; + }), + gameObjects.end()); } -void Scene::SetGameObjectTexture(GameObject* gameObject, const std::string& texturePath) { - if (this->textures.find(texturePath) == this->textures.end()) { - auto alpha = texturePath.find(".png") != std::string::npos; - this->textures[texturePath] = std::make_shared(texturePath.c_str(), alpha); - } +void Scene::SetGameObjectTexture(GameObject *gameObject, + const std::string &texturePath) { + if (this->textures.find(texturePath) == this->textures.end()) { + auto alpha = texturePath.find(".png") != std::string::npos; + this->textures[texturePath] = + std::make_shared(texturePath.c_str(), alpha); + } - gameObject->texturePath = texturePath; + gameObject->texturePath = texturePath; } -void Scene::SetGameObjectTexture(std::shared_ptr gameObject, const std::string& texturePath) { - SetGameObjectTexture(gameObject.get(), texturePath); +void Scene::SetGameObjectTexture(std::shared_ptr gameObject, + const std::string &texturePath) { + SetGameObjectTexture(gameObject.get(), texturePath); } void Scene::Build() { - spdlog::trace("Scene::Build()"); + spdlog::trace("Scene::Build()"); - // Clear out any old data - this->meshReferences.clear(); - this->meshReferences.reserve(gameObjects.size()); + // Clear out any old data + this->meshReferences.clear(); + this->meshReferences.reserve(gameObjects.size()); - std::vector vertices; - std::vector indices; + std::vector vertices; + std::vector indices; - for (auto& gameObject : gameObjects) { - int baseVertex = vertices.size(); - int baseIndex = indices.size(); + for (auto &gameObject : gameObjects) { + int baseVertex = vertices.size(); + int baseIndex = indices.size(); - std::vector& meshVertices = gameObject->mesh.vertices; - std::vector& meshIndices = gameObject->mesh.indices; + std::vector &meshVertices = gameObject->mesh.vertices; + std::vector &meshIndices = gameObject->mesh.indices; - MeshReference meshReference = { baseVertex, baseIndex, (int)meshIndices.size(), (int)meshVertices.size() }; - this->meshReferences.emplace_back(meshReference); + MeshReference meshReference = {baseVertex, baseIndex, + (int)meshIndices.size(), + (int)meshVertices.size()}; + this->meshReferences.emplace_back(meshReference); - vertices.insert(vertices.end(), meshVertices.begin(), meshVertices.end()); - indices.insert(indices.end(), meshIndices.begin(), meshIndices.end()); + vertices.insert(vertices.end(), meshVertices.begin(), meshVertices.end()); + indices.insert(indices.end(), meshIndices.begin(), meshIndices.end()); - gameObject->meshReferenceID = this->meshReferences.size() - 1; - } + gameObject->meshReferenceID = this->meshReferences.size() - 1; + } - spdlog::debug("Built {} mesh references... now generating buffers", this->meshReferences.size()); + spdlog::debug("Built {} mesh references... now generating buffers", + this->meshReferences.size()); - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); - glGenBuffers(1, &vbo); - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW); + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), + vertices.data(), GL_STATIC_DRAW); - glGenBuffers(1, &ebo); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW); + glGenBuffers(1, &ebo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), + indices.data(), GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position)); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texCoord)); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), + (void *)offsetof(Vertex, position)); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), + (void *)offsetof(Vertex, texCoord)); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); - spdlog::debug("Generated buffers (vao: {}, vbo: {}, ebo: {})", vao, vbo, ebo); + spdlog::debug("Generated buffers (vao: {}, vbo: {}, ebo: {})", vao, vbo, ebo); } -void Scene::Update() { - spdlog::trace("Scene::Update()"); -} +void Scene::Update() { spdlog::trace("Scene::Update()"); } void Scene::Render() { - this->shader.use(); - this->camera.Use(); - this->camera.Update(); - glBindVertexArray(vao); - glBindBuffer(GL_ARRAY_BUFFER, ebo); - - for (auto& gameObject : gameObjects) { - if (gameObject->meshReferenceID == -1) { - spdlog::error("GameObject has no mesh reference"); - continue; - } - - MeshReference* meshReference = &this->meshReferences[gameObject->meshReferenceID]; - - glm::mat4 model = glm::mat4(1.0f); - model = glm::translate(model, glm::vec3(gameObject->transform.position, 0.0f)); - model = glm::rotate(model, glm::radians(-gameObject->transform.rotation), glm::vec3(0.0f, 0.0f, 1.0f)); - model = glm::scale(model, glm::vec3(gameObject->transform.scale, 1.0f)); - - shader.set("u_model", model); - shader.set("u_color", gameObject->color); - - if (!gameObject->texturePath.empty()) { - this->textures[gameObject->texturePath]->bind(0); - shader.set("u_texture", 0); - } - - bool hasTexture = !gameObject->texturePath.empty(); - shader.set("u_hasTexture", hasTexture); - shader.set("u_mixColor", gameObject->mixColor && hasTexture); - - glDrawElementsBaseVertex(GL_TRIANGLES, meshReference->indexCount, GL_UNSIGNED_INT, 0, meshReference->baseVertex); - } + this->shader.use(); + this->camera.Use(); + this->camera.Update(); + glBindVertexArray(vao); + glBindBuffer(GL_ARRAY_BUFFER, ebo); + + for (auto &gameObject : gameObjects) { + if (gameObject->meshReferenceID == -1) { + spdlog::error("GameObject has no mesh reference"); + continue; + } + + MeshReference *meshReference = + &this->meshReferences[gameObject->meshReferenceID]; + + glm::mat4 model = glm::mat4(1.0f); + model = + glm::translate(model, glm::vec3(gameObject->transform.position, 0.0f)); + model = glm::rotate(model, glm::radians(-gameObject->transform.rotation), + glm::vec3(0.0f, 0.0f, 1.0f)); + model = glm::scale(model, glm::vec3(gameObject->transform.scale, 1.0f)); + + shader.set("u_model", model); + shader.set("u_color", gameObject->color); + + if (!gameObject->texturePath.empty()) { + this->textures[gameObject->texturePath]->bind(0); + shader.set("u_texture", 0); + } + + bool hasTexture = !gameObject->texturePath.empty(); + shader.set("u_hasTexture", hasTexture); + shader.set("u_mixColor", gameObject->mixColor && hasTexture); + + glDrawElementsBaseVertex(GL_TRIANGLES, meshReference->indexCount, + GL_UNSIGNED_INT, 0, meshReference->baseVertex); + } } struct GOBJSAVABLE { - Jenjin::GameObject::Transform transform; - glm::vec3 color; + Jenjin::GameObject::Transform transform; + glm::vec3 color; - char texturePath[128]; - char name[128]; + char texturePath[128]; + char name[128]; }; -void Scene::Save(const std::string& path) { - spdlog::trace("Scene::Save({})", path); +void Scene::Save(const std::string &path) { + spdlog::trace("Scene::Save({})", path); - std::ofstream file(path, std::ios::binary); - Save(file); + std::ofstream file(path, std::ios::binary); + Save(file); } -void Scene::Save(std::ofstream& file) { - spdlog::trace("Scene::Save({})", (void*)&file); +void Scene::Save(std::ofstream &file) { + spdlog::trace("Scene::Save({})", (void *)&file); - if (this->gameObjects.empty()) { - spdlog::debug("No game objects to save"); - return; - } + if (this->gameObjects.empty()) { + spdlog::debug("No game objects to save"); + return; + } - for (auto& go : this->gameObjects) { - GOBJSAVABLE gobj = { - .transform = go->transform, - .color = go->color, + for (auto &go : this->gameObjects) { + GOBJSAVABLE gobj = {.transform = go->transform, + .color = go->color, - .texturePath = { 0 }, - .name = { 0 } - }; + .texturePath = {0}, + .name = {0}}; - strncpy(gobj.name, go->name.c_str(), sizeof(gobj.name)); - strncpy(gobj.texturePath, go->texturePath.c_str(), sizeof(gobj.texturePath)); - gobj.name[sizeof(gobj.name) - 1] = 0; - gobj.texturePath[sizeof(gobj.texturePath) - 1] = 0; + strncpy(gobj.name, go->name.c_str(), sizeof(gobj.name)); + strncpy(gobj.texturePath, go->texturePath.c_str(), + sizeof(gobj.texturePath)); + gobj.name[sizeof(gobj.name) - 1] = 0; + gobj.texturePath[sizeof(gobj.texturePath) - 1] = 0; - file.write(reinterpret_cast(&gobj), sizeof(GOBJSAVABLE)); - } + file.write(reinterpret_cast(&gobj), sizeof(GOBJSAVABLE)); + } } -void Scene::Load(const std::string& path) { - spdlog::trace("Scene::Load({})", path); +void Scene::Load(const std::string &path) { + spdlog::trace("Scene::Load({})", path); - std::ifstream file(path, std::ios::binary); - this->Load(file); + std::ifstream file(path, std::ios::binary); + this->Load(file); } -void Scene::Load(std::ifstream& file) { - spdlog::trace("Scene::Load({})", (void*)&file); +void Scene::Load(std::ifstream &file) { + spdlog::trace("Scene::Load({})", (void *)&file); - this->gameObjects.clear(); + this->gameObjects.clear(); - for (GOBJSAVABLE gobj; file.read(reinterpret_cast(&gobj), sizeof(GOBJSAVABLE));) { - auto go = std::make_shared(gobj.name, Jenjin::Helpers::CreateQuad(2.0f, 2.0f)); - go->transform = gobj.transform; - go->color = gobj.color; - go->texturePath = gobj.texturePath; + for (GOBJSAVABLE gobj; + file.read(reinterpret_cast(&gobj), sizeof(GOBJSAVABLE));) { + auto go = std::make_shared( + gobj.name, Jenjin::Helpers::CreateQuad(2.0f, 2.0f)); + go->transform = gobj.transform; + go->color = gobj.color; + go->texturePath = gobj.texturePath; - // Load texture if it exists - if (std::strlen(go->texturePath.data()) > 0) { - this->SetGameObjectTexture(go, go->texturePath.data()); - } + // Load texture if it exists + if (std::strlen(go->texturePath.data()) > 0) { + this->SetGameObjectTexture(go, go->texturePath.data()); + } - this->AddGameObject(go); - } + this->AddGameObject(go); + } - this->Build(); + this->Build(); } diff --git a/engine/src/shader.cpp b/engine/src/shader.cpp index ea984a6..f6d8568 100644 --- a/engine/src/shader.cpp +++ b/engine/src/shader.cpp @@ -1,139 +1,142 @@ +#define GLFW_INCLUDE_NONE + #include "jenjin/shader.h" -#include #include +#include +#include #include #include #include -#include using namespace Jenjin; -Shader::Shader(const char* vertexPath, const char* fragmentPath) { - spdlog::trace("Shader::Shader(\"{}\", \"{}\")", vertexPath, fragmentPath); - - // Code buffers - std::string vertexCode; - std::string fragmentCode; - - // File streams - std::ifstream vShaderFile; - std::ifstream fShaderFile; - - // Enable exceptions for reading files - vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit); - fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit); - - try { - // Open files into file streams - vShaderFile.open(vertexPath); - fShaderFile.open(fragmentPath); - - // Read file streams into string streams - std::stringstream vShaderStream, fShaderStream; - - // Stream file contents into string streams - vShaderStream << vShaderFile.rdbuf(); - fShaderStream << fShaderFile.rdbuf(); - - // Close file streams - vShaderFile.close(); - fShaderFile.close(); - - // Convert string streams to strings - spdlog::debug("Reading vertex shader file: `{}`", vertexPath); - vertexCode = vShaderStream.str(); - spdlog::debug("Reading fragment shader file: `{}`", fragmentPath); - fragmentCode = fShaderStream.str(); - } catch(std::ifstream::failure &e) { - spdlog::error("Current working directory: {}", std::filesystem::current_path().string()); - spdlog::error("Failed to read shader files: `{}` `{}` for reason: `{}`", vertexPath, fragmentPath, e.what()); - } - - // C-style strings for OpenGL - const char* vShaderCode = vertexCode.c_str(); - const char* fShaderCode = fragmentCode.c_str(); - - // Shader objects (references via ID) - unsigned int vertex, fragment = 0; - - // Used to check for errors - int success; - char infoLog[512]; - - // Vertex shader - vertex = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vertex, 1, &vShaderCode, NULL); - glCompileShader(vertex); - - // Show any compilation errors - glGetShaderiv(vertex, GL_COMPILE_STATUS, &success); - if(!success) { - glGetShaderInfoLog(vertex, 512, NULL, infoLog); - spdlog::error("Failed to compile vertex shader:\n{}", infoLog); - }; - - // Fragment shader - fragment = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fragment, 1, &fShaderCode, NULL); - glCompileShader(fragment); - - // Show any compilation errors - glGetShaderiv(fragment, GL_COMPILE_STATUS, &success); - if(!success) { - glGetShaderInfoLog(fragment, 512, NULL, infoLog); - spdlog::error("Failed to compile fragment shader:\n{}", infoLog); - }; - - // Create a program - ID = glCreateProgram(); - glAttachShader(ID, vertex); - glAttachShader(ID, fragment); - glLinkProgram(ID); - - // Show any linking errors - glGetProgramiv(ID, GL_LINK_STATUS, &success); - if(!success) { - glGetProgramInfoLog(ID, 512, NULL, infoLog); - spdlog::error("Failed to link shaders: {}", infoLog); - } - - // Shader objects are now copied into the program and can be deleted - glDeleteShader(vertex); - glDeleteShader(fragment); +Shader::Shader(const char *vertexPath, const char *fragmentPath) { + spdlog::trace("Shader::Shader(\"{}\", \"{}\")", vertexPath, fragmentPath); + + // Code buffers + std::string vertexCode; + std::string fragmentCode; + + // File streams + std::ifstream vShaderFile; + std::ifstream fShaderFile; + + // Enable exceptions for reading files + vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); + fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); + + try { + // Open files into file streams + vShaderFile.open(vertexPath); + fShaderFile.open(fragmentPath); + + // Read file streams into string streams + std::stringstream vShaderStream, fShaderStream; + + // Stream file contents into string streams + vShaderStream << vShaderFile.rdbuf(); + fShaderStream << fShaderFile.rdbuf(); + + // Close file streams + vShaderFile.close(); + fShaderFile.close(); + + // Convert string streams to strings + spdlog::debug("Reading vertex shader file: `{}`", vertexPath); + vertexCode = vShaderStream.str(); + spdlog::debug("Reading fragment shader file: `{}`", fragmentPath); + fragmentCode = fShaderStream.str(); + } catch (std::ifstream::failure &e) { + spdlog::error("Current working directory: {}", + std::filesystem::current_path().string()); + spdlog::error("Failed to read shader files: `{}` `{}` for reason: `{}`", + vertexPath, fragmentPath, e.what()); + } + + // C-style strings for OpenGL + const char *vShaderCode = vertexCode.c_str(); + const char *fShaderCode = fragmentCode.c_str(); + + // Shader objects (references via ID) + unsigned int vertex, fragment = 0; + + // Used to check for errors + int success; + char infoLog[512]; + + // Vertex shader + vertex = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertex, 1, &vShaderCode, NULL); + glCompileShader(vertex); + + // Show any compilation errors + glGetShaderiv(vertex, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(vertex, 512, NULL, infoLog); + spdlog::error("Failed to compile vertex shader:\n{}", infoLog); + }; + + // Fragment shader + fragment = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragment, 1, &fShaderCode, NULL); + glCompileShader(fragment); + + // Show any compilation errors + glGetShaderiv(fragment, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(fragment, 512, NULL, infoLog); + spdlog::error("Failed to compile fragment shader:\n{}", infoLog); + }; + + // Create a program + ID = glCreateProgram(); + glAttachShader(ID, vertex); + glAttachShader(ID, fragment); + glLinkProgram(ID); + + // Show any linking errors + glGetProgramiv(ID, GL_LINK_STATUS, &success); + if (!success) { + glGetProgramInfoLog(ID, 512, NULL, infoLog); + spdlog::error("Failed to link shaders: {}", infoLog); + } + + // Shader objects are now copied into the program and can be deleted + glDeleteShader(vertex); + glDeleteShader(fragment); } Shader::~Shader() { - spdlog::trace("Shader::~Shader()"); + spdlog::trace("Shader::~Shader()"); - glDeleteProgram(ID); + glDeleteProgram(ID); } -void Shader::use() { - glUseProgram(ID); -} +void Shader::use() { glUseProgram(ID); } -void Shader::set(const char* name, float value) { - glUniform1f(glGetUniformLocation(ID, name), value); +void Shader::set(const char *name, float value) { + glUniform1f(glGetUniformLocation(ID, name), value); } -void Shader::set(const char* name, int value) { - glUniform1i(glGetUniformLocation(ID, name), value); +void Shader::set(const char *name, int value) { + glUniform1i(glGetUniformLocation(ID, name), value); } -void Shader::set(const char* name, bool value) { - glUniform1i(glGetUniformLocation(ID, name), (int)value); +void Shader::set(const char *name, bool value) { + glUniform1i(glGetUniformLocation(ID, name), (int)value); } -void Shader::set(const char* name, GLfloat *value) { - glUniformMatrix4fv(glGetUniformLocation(ID, name), 1, GL_FALSE, value); +void Shader::set(const char *name, GLfloat *value) { + glUniformMatrix4fv(glGetUniformLocation(ID, name), 1, GL_FALSE, value); } -void Shader::set(const char* name, glm::mat4 value) { - glUniformMatrix4fv(glGetUniformLocation(ID, name), 1, GL_FALSE, glm::value_ptr(value)); +void Shader::set(const char *name, glm::mat4 value) { + glUniformMatrix4fv(glGetUniformLocation(ID, name), 1, GL_FALSE, + glm::value_ptr(value)); } -void Shader::set(const char* name, glm::vec3 value) { - glUniform3fv(glGetUniformLocation(ID, name), 1, glm::value_ptr(value)); +void Shader::set(const char *name, glm::vec3 value) { + glUniform3fv(glGetUniformLocation(ID, name), 1, glm::value_ptr(value)); } diff --git a/engine/src/targets/default.cpp b/engine/src/targets/default.cpp index fa88c71..7cbb49e 100644 --- a/engine/src/targets/default.cpp +++ b/engine/src/targets/default.cpp @@ -1,38 +1,44 @@ +#define GLFW_INCLUDE_NONE + #include "jenjin/targets/default.h" #include "jenjin/engine.h" -#include #include +#include using namespace Jenjin::Targets; void DefaultTarget::PreRender() { - glClearColor(0.1f, 0.1f, 0.1f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + + static int i = 0; + if (i < 5) { + i++; - static int i = 0; if (i < 5) { i++; - Jenjin::EngineRef->GetCurrentScene()->GetCamera()->SetPosition(glm::vec3(0, 0, 0)); - } + Jenjin::EngineRef->GetCurrentScene()->GetCamera()->SetPosition( + glm::vec3(0, 0, 0)); + } } glm::vec2 DefaultTarget::GetSize() { - int width, height; - static auto ctx = glfwGetCurrentContext(); - glfwGetWindowSize(ctx, &width, &height); + int width, height; + static auto ctx = glfwGetCurrentContext(); + glfwGetWindowSize(ctx, &width, &height); - return glm::vec2(width, height); + return glm::vec2(width, height); } void DefaultTarget::Resize(glm::vec2 size) { - static auto ctx = glfwGetCurrentContext(); - Jenjin::EngineRef->GetCurrentScene()->GetCamera()->Resize(size); - glfwSetWindowSize(ctx, (int)size.x, (int)size.y); - glViewport(0, 0, (int)size.x, (int)size.y); + static auto ctx = glfwGetCurrentContext(); + Jenjin::EngineRef->GetCurrentScene()->GetCamera()->Resize(size); + glfwSetWindowSize(ctx, (int)size.x, (int)size.y); + glViewport(0, 0, (int)size.x, (int)size.y); } glm::vec2 DefaultTarget::GetMousePosition() { - static auto ctx = glfwGetCurrentContext(); - static double x, y; - glfwGetCursorPos(ctx, &x, &y); - return glm::vec2(x, y); + static auto ctx = glfwGetCurrentContext(); + static double x, y; + glfwGetCursorPos(ctx, &x, &y); + return glm::vec2(x, y); } diff --git a/engine/src/targets/editor.cpp b/engine/src/targets/editor.cpp index f933d64..bd8a527 100644 --- a/engine/src/targets/editor.cpp +++ b/engine/src/targets/editor.cpp @@ -1,3 +1,5 @@ +#define GLFW_INCLUDE_NONE + #include "jenjin/targets/editor.h" #include "jenjin/engine.h" #include "jenjin/framebuffer.h" @@ -12,99 +14,96 @@ using namespace Jenjin::Targets; -EditorTarget::EditorTarget() { -} +EditorTarget::EditorTarget() {} void EditorTarget::PreRender() { - static int i = 0; if (i < 5) { i++; - Jenjin::EngineRef->GetCurrentScene()->GetCamera()->SetPosition(glm::vec3(0, 0, 0)); - } + static int i = 0; + if (i < 5) { + i++; + + Jenjin::EngineRef->GetCurrentScene()->GetCamera()->SetPosition( + glm::vec3(0, 0, 0)); + } - glClearColor(0.1f, 0.1f, 0.1f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); - Resize(GetSize()); + Resize(GetSize()); - ImGui_ImplOpenGL3_NewFrame(); - ImGui_ImplGlfw_NewFrame(); - ImGui::NewFrame(); + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); - this->editor.show_all(Jenjin::EngineRef->GetCurrentScene()); + this->editor.show_all(Jenjin::EngineRef->GetCurrentScene()); - renderTexture.Bind(); + renderTexture.Bind(); - glClearColor(0.1f, 0.1f, 0.1f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - glViewport(0, 0, width, height); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + glViewport(0, 0, width, height); } void EditorTarget::Render() { - renderTexture.Unbind(); - - if (this->editor.paths.openScenePath.empty()) { - return; - } - - ImGui::Begin("Viewport"); - - ImVec2 size = ImGui::GetContentRegionAvail(); - this->width = size.x; - this->height = size.y; - - ImGui::GetWindowDrawList()->AddImage( - (void*)(intptr_t)renderTexture.texture, - ImVec2(ImGui::GetCursorScreenPos()), - ImVec2(ImGui::GetCursorScreenPos().x + size.x, - ImGui::GetCursorScreenPos().y + size.y), - ImVec2(0, 1), - ImVec2(1, 0) - ); - - std::string res_text = std::to_string((int)size.x) + "x" + std::to_string((int)size.y); - ImGui::GetWindowDrawList()->AddText( - ImVec2(ImGui::GetCursorScreenPos().x + 10, ImGui::GetCursorScreenPos().y + 10), - ImColor(255, 255, 255, 255), - res_text.c_str() - ); - - ImGui::End(); + renderTexture.Unbind(); + + if (this->editor.paths.openScenePath.empty()) { + return; + } + + ImGui::Begin("Viewport"); + + ImVec2 size = ImGui::GetContentRegionAvail(); + this->width = size.x; + this->height = size.y; + + ImGui::GetWindowDrawList()->AddImage( + (void *)(intptr_t)renderTexture.texture, + ImVec2(ImGui::GetCursorScreenPos()), + ImVec2(ImGui::GetCursorScreenPos().x + size.x, + ImGui::GetCursorScreenPos().y + size.y), + ImVec2(0, 1), ImVec2(1, 0)); + + std::string res_text = + std::to_string((int)size.x) + "x" + std::to_string((int)size.y); + ImGui::GetWindowDrawList()->AddText( + ImVec2(ImGui::GetCursorScreenPos().x + 10, + ImGui::GetCursorScreenPos().y + 10), + ImColor(255, 255, 255, 255), res_text.c_str()); + + ImGui::End(); } void EditorTarget::PostRender() { - ImGui::EndFrame(); - ImGui::Render(); + ImGui::EndFrame(); + ImGui::Render(); - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); } -glm::vec2 EditorTarget::GetSize() { - return glm::vec2(width, height); -} +glm::vec2 EditorTarget::GetSize() { return glm::vec2(width, height); } void EditorTarget::Resize(glm::vec2 size) { - this->width = size.x; - this->height = size.y; - renderTexture.Resize(width, height); - Jenjin::EngineRef->GetCurrentScene()->GetCamera()->Resize(size); + this->width = size.x; + this->height = size.y; + renderTexture.Resize(width, height); + Jenjin::EngineRef->GetCurrentScene()->GetCamera()->Resize(size); } glm::vec2 EditorTarget::GetMousePosition() { - // WARNING: Untested... + // WARNING: Untested... - static auto ctx = glfwGetCurrentContext(); - static double gx, gy; - glfwGetCursorPos(ctx, &gx, &gy); + static auto ctx = glfwGetCurrentContext(); + static double gx, gy; + glfwGetCursorPos(ctx, &gx, &gy); - // g_ is the whole window - // l_ is the viewport we need to calculate this - auto l_ = ImGui::GetWindowPos(); - auto lx = gx - l_.x; - auto ly = gy - l_.y; + // g_ is the whole window + // l_ is the viewport we need to calculate this + auto l_ = ImGui::GetWindowPos(); + auto lx = gx - l_.x; + auto ly = gy - l_.y; - return glm::vec2(lx, ly); + return glm::vec2(lx, ly); } // We handle window resizing ourselves -bool EditorTarget::RespondsToWindowResize() { - return false; -} +bool EditorTarget::RespondsToWindowResize() { return false; } diff --git a/engine/src/texture.cpp b/engine/src/texture.cpp index a56efd7..6bfffbc 100644 --- a/engine/src/texture.cpp +++ b/engine/src/texture.cpp @@ -1,53 +1,61 @@ +#define GLFW_INCLUDE_NONE + #include "jenjin/texture.h" -#include #include +#include #include #define STB_IMAGE_IMPLEMENTATION #include -Texture::Texture(const char* imagePath, bool alpha) { - spdlog::trace("Texture::Texture(\"{}\", {})", imagePath, alpha); - - stbi_set_flip_vertically_on_load(true); - - // Load and generate the texture - glGenTextures(1, &ID); - glBindTexture(GL_TEXTURE_2D, ID); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object, similar to glfwMakeContextCurrent - - // Set the texture wrapping parameters (if it's too small or too big) - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method) - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - - // Set texture filtering parameters for minification and magnification (mipmapping) - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - // Load the image, then create texture and generate the mipmaps - int w, h, c; - unsigned char *data = stbi_load(imagePath, &w, &h, &c, 0); - - if (data) { - spdlog::debug("Loaded texture {}: {}x{} with {} channels", imagePath, w, h, c); - glTexImage2D(GL_TEXTURE_2D, 0, alpha ? GL_RGBA : GL_RGB, w, h, 0, alpha ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, data); - glGenerateMipmap(GL_TEXTURE_2D); - } else { - const char *description = stbi_failure_reason(); - spdlog::error("Failed to load texture: {}", description); - } - - stbi_image_free(data); - - this->imagePath = imagePath; +Texture::Texture(const char *imagePath, bool alpha) { + spdlog::trace("Texture::Texture(\"{}\", {})", imagePath, alpha); + + stbi_set_flip_vertically_on_load(true); + + // Load and generate the texture + glGenTextures(1, &ID); + // All upcoming GL_TEXTURE_2D operations now have effect on + // this texture object, similar to glfwMakeContextCurrent + glBindTexture(GL_TEXTURE_2D, ID); + + // Set the texture wrapping parameters (if it's too small or too big) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + + // Set texture filtering parameters for minification and magnification + // (mipmapping) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, + GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + // Load the image, then create texture and generate the mipmaps + int w, h, c; + unsigned char *data = stbi_load(imagePath, &w, &h, &c, 0); + + if (data) { + spdlog::debug("Loaded texture {}: {}x{} with {} channels", imagePath, w, h, + c); + glTexImage2D(GL_TEXTURE_2D, 0, alpha ? GL_RGBA : GL_RGB, w, h, 0, + alpha ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, data); + glGenerateMipmap(GL_TEXTURE_2D); + } else { + const char *description = stbi_failure_reason(); + spdlog::error("Failed to load texture: {}", description); + } + + stbi_image_free(data); + + this->imagePath = imagePath; } Texture::~Texture() { - spdlog::trace("Texture::~Texture(\"{}\")", imagePath); - glDeleteTextures(1, &ID); + spdlog::trace("Texture::~Texture(\"{}\")", imagePath); + glDeleteTextures(1, &ID); } void Texture::bind(int id) { - glActiveTexture(GL_TEXTURE0 + id); - glBindTexture(GL_TEXTURE_2D, ID); + glActiveTexture(GL_TEXTURE0 + id); + glBindTexture(GL_TEXTURE_2D, ID); } diff --git a/jenjin/src/main.cpp b/jenjin/src/main.cpp index 88b4d20..82defff 100644 --- a/jenjin/src/main.cpp +++ b/jenjin/src/main.cpp @@ -1,9 +1,11 @@ +#define GLFW_INCLUDE_NONE + #include "jenjin/editor/utils.h" -#include "jenjin/targets/editor.h" #include "jenjin/targets/default.h" +#include "jenjin/targets/editor.h" -#include "jenjin/helpers.h" #include "jenjin/engine.h" +#include "jenjin/helpers.h" #include "jenjin/scene.h" #include @@ -12,61 +14,63 @@ #include #include -#define LAUNCH_ARGS Jenjin::Engine& engine, GLFWwindow* window, std::shared_ptr scene +#define LAUNCH_ARGS \ + Jenjin::Engine &engine, GLFWwindow *window, \ + std::shared_ptr scene void launchEditor(LAUNCH_ARGS); void launchRuntime(LAUNCH_ARGS); -int main(int argc, char* argv[]) { - bool editor = true; // We use the editor by default +int main(int argc, char *argv[]) { + bool editor = true; // We use the editor by default - for (int i = 0; i < argc; i++) { - if (strcmp(argv[i], "--runtime") == 0) { - editor = false; - } - } + for (int i = 0; i < argc; i++) { + if (strcmp(argv[i], "--runtime") == 0) { + editor = false; + } + } - GLFWwindow* window = Jenjin::Helpers::CreateWindow(800, 600, "Jenjin"); - Jenjin::Helpers::CheckWindow(window); - Jenjin::Helpers::InitiateImGui(window); + GLFWwindow *window = Jenjin::Helpers::CreateWindow(800, 600, "Jenjin"); + Jenjin::Helpers::CheckWindow(window); + Jenjin::Helpers::InitiateImGui(window); - Jenjin::Engine engine(window); - auto scene = std::make_shared(); - engine.AddScene(scene, true); + Jenjin::Engine engine(window); + auto scene = std::make_shared(); + engine.AddScene(scene, true); - (editor ? launchEditor : launchRuntime)(engine, window, scene); + (editor ? launchEditor : launchRuntime)(engine, window, scene); } void launchEditor(LAUNCH_ARGS) { - Jenjin::Editor::ensure_dir(Jenjin::Editor::get_jendir() + "/Projects"); + Jenjin::Editor::ensure_dir(Jenjin::Editor::get_jendir() + "/Projects"); - auto editor = Jenjin::Targets::EditorTarget(); - scene->SetTarget(&editor); + auto editor = Jenjin::Targets::EditorTarget(); + scene->SetTarget(&editor); - while (!glfwWindowShouldClose(window)) { - engine.Render(&editor); + while (!glfwWindowShouldClose(window)) { + engine.Render(&editor); - glfwPollEvents(); - glfwSwapBuffers(window); - } + glfwPollEvents(); + glfwSwapBuffers(window); + } } void launchRuntime(LAUNCH_ARGS) { - auto runtime = Jenjin::Targets::DefaultTarget(); - scene->SetTarget(&runtime); + auto runtime = Jenjin::Targets::DefaultTarget(); + scene->SetTarget(&runtime); - // Look for `main.jenscene` in the current directory - if (!std::filesystem::exists("main.jenscene")) { - spdlog::error("Could not find `main.jenscene` in the current directory"); - exit(1); - } + // Look for `main.jenscene` in the current directory + if (!std::filesystem::exists("main.jenscene")) { + spdlog::error("Could not find `main.jenscene` in the current directory"); + exit(1); + } - scene->Load("main.jenscene"); + scene->Load("main.jenscene"); - while (!glfwWindowShouldClose(window)) { - engine.Render(&runtime); + while (!glfwWindowShouldClose(window)) { + engine.Render(&runtime); - glfwPollEvents(); - glfwSwapBuffers(window); - } + glfwPollEvents(); + glfwSwapBuffers(window); + } } diff --git a/windows.sh b/windows.sh index 06ac4bb..bf7c605 100644 --- a/windows.sh +++ b/windows.sh @@ -1,13 +1,13 @@ #!/usr/bin/env bash cmake -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_SYSTEM_NAME=Windows \ - -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc \ - -DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++ \ - -DCMAKE_CXX_FLAGS="-static-libgcc -static-libstdc++ -static" \ - -DCMAKE_EXE_LINKER_FLAGS="-static" \ - -B build \ - -G Ninja + -DCMAKE_SYSTEM_NAME=Windows \ + -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc \ + -DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++ \ + -DCMAKE_CXX_FLAGS="-static-libgcc -static-libstdc++ -static" \ + -DCMAKE_EXE_LINKER_FLAGS="-static" \ + -B build \ + -G Ninja cmake --build build wine ./build/jenjin/jenjin.exe