2024-12-25 21:44:33 +00:00
|
|
|
// src/Engine.cpp
|
|
|
|
|
2024-12-25 23:01:05 +00:00
|
|
|
// Settings
|
|
|
|
|
2024-12-26 03:06:17 +00:00
|
|
|
#define VSync 1
|
2024-12-25 23:01:05 +00:00
|
|
|
|
2024-12-25 21:44:33 +00:00
|
|
|
#include "Engine.h"
|
|
|
|
#include <cstdio>
|
|
|
|
#include <chrono>
|
|
|
|
#include <GL/glew.h>
|
|
|
|
#include <GLFW/glfw3.h>
|
2024-12-27 01:34:34 +00:00
|
|
|
#include <string>
|
2024-12-25 21:44:33 +00:00
|
|
|
|
|
|
|
// Dear ImGui
|
|
|
|
#include "imgui.h"
|
|
|
|
#include "imgui_impl_glfw.h"
|
|
|
|
#include "imgui_impl_opengl3.h"
|
|
|
|
|
|
|
|
#include "Windows/RenderWindow.h"
|
|
|
|
#include "Windows/PerformanceWindow.h"
|
|
|
|
#include "Windows/LoggerWindow.h"
|
2024-12-25 23:35:38 +00:00
|
|
|
#include "Windows/InspectorWindow.h"
|
2024-12-26 03:06:17 +00:00
|
|
|
#include "Windows/SceneWindow.h"
|
|
|
|
|
2024-12-26 04:00:13 +00:00
|
|
|
#include "Engine/ThemeManager.h"
|
|
|
|
|
2024-12-27 01:34:34 +00:00
|
|
|
// #define YAML_CPP_STATIC_DEFINE
|
2024-12-26 03:06:17 +00:00
|
|
|
#include <yaml-cpp/yaml.h>
|
|
|
|
|
|
|
|
#include "TestModel.h"
|
|
|
|
|
2024-12-25 23:01:05 +00:00
|
|
|
AssetManager g_AssetManager;
|
|
|
|
|
2024-12-25 23:35:38 +00:00
|
|
|
LoggerWindow *g_LoggerWindow;
|
|
|
|
|
2024-12-27 01:34:34 +00:00
|
|
|
std::vector<std::shared_ptr<GameObject>> g_GameObjects;
|
2024-12-26 03:06:17 +00:00
|
|
|
|
2024-12-27 01:34:34 +00:00
|
|
|
int g_GPU_Triangles_drawn_to_screen = 0;
|
2024-12-26 03:06:17 +00:00
|
|
|
|
2024-12-26 00:15:18 +00:00
|
|
|
|
2024-12-27 01:34:34 +00:00
|
|
|
GameObject *g_SelectedObject; // Pointer to the currently selected object
|
2024-12-26 00:15:18 +00:00
|
|
|
|
2024-12-27 01:34:34 +00:00
|
|
|
bool MyEngine::Init(int width, int height, const std::string &title)
|
2024-12-25 21:44:33 +00:00
|
|
|
{
|
2024-12-25 23:01:05 +00:00
|
|
|
DEBUG_PRINT("[START] Engine Init");
|
2024-12-25 21:44:33 +00:00
|
|
|
// ------------------------------------------
|
|
|
|
// 1) Initialize GLFW
|
|
|
|
// ------------------------------------------
|
|
|
|
if (!glfwInit())
|
|
|
|
{
|
|
|
|
fprintf(stderr, "[Engine] Failed to initialize GLFW\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup hints for OpenGL 3.3 Core
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
|
|
|
|
// Create window
|
|
|
|
m_Window = glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr);
|
|
|
|
if (!m_Window)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "[Engine] Failed to create GLFW window\n");
|
|
|
|
glfwTerminate();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
glfwMakeContextCurrent(m_Window);
|
2024-12-25 23:01:05 +00:00
|
|
|
glfwSwapInterval(VSync); // vsync
|
2024-12-25 21:44:33 +00:00
|
|
|
|
|
|
|
// ------------------------------------------
|
|
|
|
// 2) Initialize GLEW
|
|
|
|
// ------------------------------------------
|
|
|
|
if (glewInit() != GLEW_OK)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "[Engine] Failed to initialize GLEW\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------
|
|
|
|
// 3) Initialize ImGui
|
|
|
|
// ------------------------------------------
|
|
|
|
IMGUI_CHECKVERSION();
|
|
|
|
ImGui::CreateContext();
|
2024-12-27 01:34:34 +00:00
|
|
|
ImGuiIO &io = ImGui::GetIO();
|
2024-12-25 21:44:33 +00:00
|
|
|
(void)io;
|
|
|
|
// Enable docking
|
|
|
|
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
|
|
|
|
// (Optional) Multi-viewport
|
|
|
|
// io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
|
|
|
|
|
|
|
|
// Style
|
|
|
|
ImGui::StyleColorsDark();
|
|
|
|
|
|
|
|
// Platform/Renderer bindings
|
|
|
|
ImGui_ImplGlfw_InitForOpenGL(m_Window, true);
|
|
|
|
ImGui_ImplOpenGL3_Init("#version 330");
|
|
|
|
|
|
|
|
// Initialize windows
|
2024-12-27 01:34:34 +00:00
|
|
|
m_RenderWindow = std::make_unique<RenderWindow>();
|
2024-12-25 21:44:33 +00:00
|
|
|
m_PerformanceWindow = std::make_unique<PerformanceWindow>();
|
2024-12-27 01:34:34 +00:00
|
|
|
m_LoggerWindow = std::make_unique<LoggerWindow>();
|
|
|
|
m_InspectorWindow = std::make_unique<InspectorWindow>();
|
|
|
|
m_SceneWindow = std::make_unique<SceneWindow>();
|
2024-12-25 21:44:33 +00:00
|
|
|
|
|
|
|
// Some initial logs
|
|
|
|
m_LoggerWindow->AddLog("Engine initialized.");
|
|
|
|
m_LoggerWindow->AddLog("Welcome to Tesseract Engine!");
|
|
|
|
|
2024-12-25 23:35:38 +00:00
|
|
|
g_LoggerWindow = m_LoggerWindow.get();
|
|
|
|
|
2024-12-25 21:44:33 +00:00
|
|
|
m_Running = true;
|
|
|
|
m_LastTime = glfwGetTime();
|
2024-12-25 23:01:05 +00:00
|
|
|
DEBUG_PRINT("[OK] Engine Init ");
|
|
|
|
|
2024-12-25 21:44:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyEngine::Run()
|
|
|
|
{
|
2024-12-25 23:01:05 +00:00
|
|
|
DEBUG_PRINT("[START] Engine Run ");
|
2024-12-26 00:15:18 +00:00
|
|
|
|
2024-12-27 01:34:34 +00:00
|
|
|
DEBUG_PRINT("Transition to Editor");
|
2024-12-26 00:15:18 +00:00
|
|
|
|
|
|
|
// Pseudocode:
|
2024-12-27 01:34:34 +00:00
|
|
|
int newId = g_GameObjects.size();
|
|
|
|
auto newGameObject = std::make_shared<GameObject>(newId, ("Default"));
|
2024-12-26 03:06:17 +00:00
|
|
|
|
2024-12-27 01:34:34 +00:00
|
|
|
DEBUG_PRINT("Created Default GameObject");
|
2024-12-26 00:15:18 +00:00
|
|
|
|
2024-12-27 01:34:34 +00:00
|
|
|
newGameObject->AddComponent(std::make_shared<TransformComponent>());
|
|
|
|
newGameObject->AddComponent(std::make_shared<MeshComponent>());
|
|
|
|
|
|
|
|
DEBUG_PRINT("Added Componenets");
|
|
|
|
|
|
|
|
// Suppose we loaded a VAO, an EBO with 36 indices for the cube,
|
2024-12-26 00:15:18 +00:00
|
|
|
// and a texture ID from the asset manager
|
2024-12-27 01:34:34 +00:00
|
|
|
auto mesh = newGameObject->GetComponent<MeshComponent>();
|
|
|
|
auto transform = newGameObject->GetComponent<TransformComponent>();
|
|
|
|
|
|
|
|
DEBUG_PRINT("Got pointers to Componenets");
|
|
|
|
|
|
|
|
if (mesh)
|
|
|
|
{
|
|
|
|
// printf("Got Valid Mesh Component\n");
|
|
|
|
mesh->vao = CreateCubeVAO();
|
|
|
|
mesh->indexCount = 36;
|
|
|
|
mesh->textureID = static_cast<GLuint>(reinterpret_cast<uintptr_t>(g_AssetManager.loadAsset(AssetType::TEXTURE, "assets/textures/wood.png")));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-12-26 00:15:18 +00:00
|
|
|
|
2024-12-27 01:34:34 +00:00
|
|
|
DEBUG_PRINT("Could not find Mesh Component\n");
|
|
|
|
}
|
2024-12-26 03:06:17 +00:00
|
|
|
|
2024-12-27 01:34:34 +00:00
|
|
|
if (transform)
|
|
|
|
{
|
|
|
|
// printf("Got Valid Transform Component\n");
|
|
|
|
transform->position = glm::vec3(0.f, 0.f, 0.f);
|
|
|
|
transform->rotation = glm::vec3(0.f, 0.5f, 0.f);
|
|
|
|
transform->scale = glm::vec3(1.f, 1.f, 1.f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-12-26 00:15:18 +00:00
|
|
|
|
2024-12-27 01:34:34 +00:00
|
|
|
DEBUG_PRINT("Could not find Transform Component");
|
|
|
|
}
|
|
|
|
|
|
|
|
g_AssetManager.loadAsset(AssetType::TEXTURE, "assets/textures/bricks.png");
|
|
|
|
g_AssetManager.loadAsset(AssetType::TEXTURE, "assets/textures/default.png");
|
|
|
|
g_AssetManager.loadAsset(AssetType::TEXTURE, "assets/textures/lush_grass.png");
|
2024-12-26 00:15:18 +00:00
|
|
|
|
2024-12-27 01:34:34 +00:00
|
|
|
g_GameObjects.push_back(newGameObject);
|
|
|
|
DEBUG_PRINT("Put componenent into Global Componenets Subsystem");
|
|
|
|
|
|
|
|
// printf("%p\n", &g_GameObjects);
|
|
|
|
|
|
|
|
// Possibly create more GameObjects with different positions or textures
|
2024-12-26 04:00:13 +00:00
|
|
|
|
|
|
|
ThemeManager_ChangeTheme(2);
|
2024-12-27 01:34:34 +00:00
|
|
|
DEBUG_PRINT("Changed Theme to default");
|
2024-12-26 04:00:13 +00:00
|
|
|
|
2024-12-25 21:44:33 +00:00
|
|
|
while (!glfwWindowShouldClose(m_Window) && m_Running)
|
|
|
|
{
|
|
|
|
// Poll
|
|
|
|
glfwPollEvents();
|
|
|
|
|
|
|
|
// Calculate FPS
|
|
|
|
double current_time = glfwGetTime();
|
|
|
|
double delta = current_time - m_LastTime;
|
|
|
|
m_FrameCount++;
|
2024-12-25 23:42:58 +00:00
|
|
|
if (delta >= 0.1)
|
2024-12-25 21:44:33 +00:00
|
|
|
{
|
|
|
|
m_Fps = (float)(m_FrameCount / delta);
|
2024-12-27 01:34:34 +00:00
|
|
|
m_Ms = 100.0f / m_Fps;
|
2024-12-25 21:44:33 +00:00
|
|
|
m_FrameCount = 0;
|
|
|
|
m_LastTime = current_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start new frame
|
|
|
|
BeginFrame();
|
|
|
|
|
|
|
|
// Show main DockSpace
|
|
|
|
ShowDockSpace();
|
|
|
|
|
2024-12-26 00:53:17 +00:00
|
|
|
m_InspectorWindow->Show();
|
2024-12-25 23:35:38 +00:00
|
|
|
|
2024-12-25 21:44:33 +00:00
|
|
|
// Show our windows
|
2024-12-27 01:34:34 +00:00
|
|
|
m_RenderWindow->Show(); // The spinning triangle as ImGui::Image
|
|
|
|
|
|
|
|
m_PerformanceWindow->Show(m_Fps, m_Ms); // FPS & ms
|
|
|
|
|
|
|
|
m_LoggerWindow->Show(); // Logs
|
2024-12-25 23:01:05 +00:00
|
|
|
|
2024-12-26 03:06:17 +00:00
|
|
|
m_SceneWindow->Show();
|
|
|
|
|
2024-12-25 23:01:05 +00:00
|
|
|
// After rendering
|
|
|
|
|
2024-12-27 01:34:34 +00:00
|
|
|
m_PerformanceWindow->UpdatePerformanceStats(-1, g_GPU_Triangles_drawn_to_screen);
|
2024-12-25 21:44:33 +00:00
|
|
|
|
|
|
|
// End frame
|
|
|
|
EndFrame();
|
|
|
|
}
|
2024-12-25 23:01:05 +00:00
|
|
|
DEBUG_PRINT("[OK] Engine Run ");
|
2024-12-25 21:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyEngine::Cleanup()
|
|
|
|
{
|
2024-12-25 23:01:05 +00:00
|
|
|
DEBUG_PRINT("[START] Engine Cleanup ");
|
2024-12-27 01:34:34 +00:00
|
|
|
|
2024-12-25 21:44:33 +00:00
|
|
|
// ImGui cleanup
|
|
|
|
ImGui_ImplOpenGL3_Shutdown();
|
|
|
|
ImGui_ImplGlfw_Shutdown();
|
|
|
|
ImGui::DestroyContext();
|
|
|
|
|
|
|
|
// GLFW cleanup
|
|
|
|
if (m_Window)
|
|
|
|
{
|
|
|
|
glfwDestroyWindow(m_Window);
|
|
|
|
m_Window = nullptr;
|
|
|
|
}
|
|
|
|
glfwTerminate();
|
|
|
|
|
|
|
|
m_Running = false;
|
2024-12-25 23:01:05 +00:00
|
|
|
DEBUG_PRINT("[OK] Engine Cleanup ");
|
2024-12-25 21:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyEngine::BeginFrame()
|
|
|
|
{
|
|
|
|
// ImGui new frame
|
|
|
|
ImGui_ImplOpenGL3_NewFrame();
|
|
|
|
ImGui_ImplGlfw_NewFrame();
|
|
|
|
ImGui::NewFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyEngine::EndFrame()
|
|
|
|
{
|
|
|
|
// Render ImGui
|
|
|
|
ImGui::Render();
|
|
|
|
|
|
|
|
// Clear the default framebuffer
|
|
|
|
int display_w, display_h;
|
|
|
|
glfwGetFramebufferSize(m_Window, &display_w, &display_h);
|
|
|
|
glViewport(0, 0, display_w, display_h);
|
|
|
|
glClearColor(0.05f, 0.05f, 0.06f, 1.0f);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
// Draw the ImGui data
|
|
|
|
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
|
|
|
|
|
|
|
// (Optional) handle multi-viewport
|
2024-12-27 01:34:34 +00:00
|
|
|
ImGuiIO &io = ImGui::GetIO();
|
2024-12-25 21:44:33 +00:00
|
|
|
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
|
|
|
|
{
|
2024-12-27 01:34:34 +00:00
|
|
|
GLFWwindow *backup_current_context = glfwGetCurrentContext();
|
2024-12-25 21:44:33 +00:00
|
|
|
ImGui::UpdatePlatformWindows();
|
|
|
|
ImGui::RenderPlatformWindowsDefault();
|
|
|
|
glfwMakeContextCurrent(backup_current_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap
|
|
|
|
glfwSwapBuffers(m_Window);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyEngine::ShowDockSpace()
|
|
|
|
{
|
|
|
|
static bool dockspaceOpen = true;
|
|
|
|
static bool opt_fullscreen = true;
|
2024-12-27 01:34:34 +00:00
|
|
|
// Initialize dockspace_flags without ImGuiDockNodeFlags_DockSpace
|
|
|
|
static ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_PassthruCentralNode;
|
2024-12-25 21:44:33 +00:00
|
|
|
|
|
|
|
ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking;
|
|
|
|
if (opt_fullscreen)
|
|
|
|
{
|
2024-12-27 01:34:34 +00:00
|
|
|
ImGuiViewport *viewport = ImGui::GetMainViewport();
|
2024-12-25 21:44:33 +00:00
|
|
|
ImGui::SetNextWindowPos(viewport->WorkPos);
|
|
|
|
ImGui::SetNextWindowSize(viewport->WorkSize);
|
|
|
|
ImGui::SetNextWindowViewport(viewport->ID);
|
2024-12-27 01:34:34 +00:00
|
|
|
window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;
|
|
|
|
window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus;
|
2024-12-25 21:44:33 +00:00
|
|
|
}
|
|
|
|
|
2024-12-27 01:34:34 +00:00
|
|
|
// Style adjustments
|
2024-12-25 21:44:33 +00:00
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
|
|
|
|
|
|
|
|
ImGui::Begin("DockSpace", &dockspaceOpen, window_flags);
|
|
|
|
ImGui::PopStyleVar(2);
|
|
|
|
|
2024-12-27 01:34:34 +00:00
|
|
|
// Menu bar
|
2024-12-25 21:44:33 +00:00
|
|
|
if (ImGui::BeginMenuBar())
|
|
|
|
{
|
|
|
|
if (ImGui::BeginMenu("File"))
|
|
|
|
{
|
|
|
|
if (ImGui::MenuItem("Exit"))
|
|
|
|
m_Running = false; // Stop the engine
|
|
|
|
ImGui::EndMenu();
|
|
|
|
}
|
|
|
|
ImGui::EndMenuBar();
|
|
|
|
}
|
|
|
|
|
|
|
|
// DockSpace
|
2024-12-27 01:34:34 +00:00
|
|
|
ImGuiIO &io = ImGui::GetIO();
|
2024-12-25 21:44:33 +00:00
|
|
|
if (io.ConfigFlags & ImGuiConfigFlags_DockingEnable)
|
|
|
|
{
|
2024-12-27 01:34:34 +00:00
|
|
|
// Optional: Log the flags for debugging
|
|
|
|
// DEBUG_PRINT("DockSpace Flags: %d", dockspace_flags);
|
|
|
|
|
2024-12-25 21:44:33 +00:00
|
|
|
ImGuiID dockspace_id = ImGui::GetID("MyDockSpace");
|
|
|
|
ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ImGui::Text("Docking is not enabled. Set io.ConfigFlags |= ImGuiConfigFlags_DockingEnable.");
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::End();
|
|
|
|
}
|