From fede442827bd6df20d695bb24713f8fbf465ee76 Mon Sep 17 00:00:00 2001 From: OusmBlueNinja <89956790+OusmBlueNinja@users.noreply.github.com> Date: Wed, 25 Dec 2024 00:23:10 -0600 Subject: [PATCH] added: Textures and fixed ECS --- assets/textures/default.png | Bin 0 -> 138 bytes assets/textures/player_1.png | Bin 0 -> 1420 bytes src/ECS.h | 2 +- src/main.cpp | 139 +++++++++++++++++++++++++++++---- vendor/stb/CMakeLists.txt | 11 --- vendor/stb/include/stb_image.h | 4 +- vendor/stb/src/stb_image.cpp | 2 - 7 files changed, 126 insertions(+), 32 deletions(-) create mode 100644 assets/textures/default.png create mode 100644 assets/textures/player_1.png delete mode 100644 vendor/stb/CMakeLists.txt delete mode 100644 vendor/stb/src/stb_image.cpp diff --git a/assets/textures/default.png b/assets/textures/default.png new file mode 100644 index 0000000000000000000000000000000000000000..6154743ba5bf1cafa25ba9a11dea3d1c28a7e7ba GIT binary patch literal 138 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz#^NA%Cx&(BWL^R}E}kxqArbCx zuPx+dFyLW6$p7x&Vm9@la08CYV()8;;l|Dr+*xxq*fkb3FmY)tIIxpJ;BVu(SO0x2 i92i+e0)T`ctC*!;ykq@*^QA!Z7(8A5T-G@yGywo_RVJ4J literal 0 HcmV?d00001 diff --git a/assets/textures/player_1.png b/assets/textures/player_1.png new file mode 100644 index 0000000000000000000000000000000000000000..23e0429813a72ae032aefb9b61fed015154d4f67 GIT binary patch literal 1420 zcmcgs{a4a=6#je>MJknX%9*L)!dO{WlQgFZrK!x8iuP(yjVvvV2`R2zeAAk}m$eiz zVWrkw4O^`#Bh<=b`9f-jC5A>JO$C}H3W$#Wg#EI6&b`l1&v~A6@43&3#PzXmGrVU2 z0Nki?(KH9bAKTT%F&{?mY;^#{p~bENVujD&0651`qgSWoG4vHr_xDBn$mKtyFsm|; z!co<^H_XVmBpkkT5IT8PedR5>m$P0wC-2AzyFDNRI>BCBT@1$u?K|<+T5bGARy>$TRCkBj!_Qu}wV?~jgV&AT>oIx)e6bgZBXw*dLf#=H_S`!vmdKO^ zI`jdXCoqZJ`$Z1PR-;nX%PRjf=UCXBHt8{{(8irPlTHM1Z~Qho09ZEygh1~o+4_o! zi=t?Jm37nsUETh&yVjA%XGC;dpnOsxve~_iN?-9Zjh<|D6_xNC|7gbS%bIctE08((%<>9;C9=lY-A`vhXe{)wM%QAHVt0!05I=Y=_hd zS<9*;L3ce0EX-@KiAS6Vv!LpE%nQ1$QB4ZFE#|#txMz@M8bZhoLmscoBSJU$^-Gv% znL5{{eHC~~ba2z61c4+(| zyH2Ti%FBNBq45dR_D-oOGK76EuwGW@+Wh(bME+ZgbgZ;Sh6MvMXV{!BO)o>@Auq(p zm#`8uVAB4hSLOESm)H|m5c$s1!oiMToie34aCby`_s!sKSM0o&wA+hQ199iBCv&D| z=vQp5GKGD7YPw`T6bGV^QIyJ0w)ebvPy~1T@4TR^*!$k-J?Papyu5$BF4;7ces3i% zd?6CPQE#121Nbg77T&0f z-?2$Sz%RqfTrRhL!81ph*tn38kaeZz@$o=uUVi=v>m-kUj8UtXTN@9c2b>X#yBKaxR1qU5a*RGEiuh~)XFP4LV AUH||9 literal 0 HcmV?d00001 diff --git a/src/ECS.h b/src/ECS.h index e6385e6..ecbd425 100644 --- a/src/ECS.h +++ b/src/ECS.h @@ -1,4 +1,4 @@ -// ECS.h +// src/ECS.h #pragma once #include diff --git a/src/main.cpp b/src/main.cpp index 0970032..7c54039 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,6 +11,14 @@ #include #include #include +#include +#include +#include +#include +#include + +#define STB_IMAGE_IMPLEMENTATION +#include "stb_image.h" // Include stb_image.h #include "ECS.h" // Include the ECS header @@ -58,6 +66,74 @@ private: #define LOGPOINT(msg) #endif +// ===================== +// TextureManager Implementation +// ===================== + +class TextureManager { +public: + GLuint LoadTexture(const std::string& path) { + // Check if texture already loaded + auto it = textures.find(path); + if (it != textures.end()) { + return it->second; + } + + // Load image + int width, height, channels; + unsigned char* data = stbi_load(path.c_str(), &width, &height, &channels, 4); + if (!data) { + Logger::GetInstance().Log(LogLevel::ERROR, "Failed to load texture: " + path); + return 0; + } + + // Generate OpenGL texture + GLuint textureID; + glGenTextures(1, &textureID); + glBindTexture(GL_TEXTURE_2D, textureID); + // Set texture parameters + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + // Upload texture data + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); + //glGenerateMipmap(GL_TEXTURE_2D); + + glBindTexture(GL_TEXTURE_2D, 0); + + // Free image data + stbi_image_free(data); + + // Store texture + textures[path] = textureID; + + Logger::GetInstance().Log(LogLevel::INFO, "Loaded texture: " + path); + return textureID; + } + + GLuint GetTexture(const std::string& path) { + auto it = textures.find(path); + if (it != textures.end()) { + return it->second; + } + return LoadTexture(path); + } + + void Cleanup() { + for (auto& pair : textures) { + glDeleteTextures(1, &pair.second); + } + textures.clear(); + } + +private: + std::unordered_map textures; +}; + +// Instantiate TextureManager +TextureManager textureManager; + // ===================== // ECS Instances // ===================== @@ -149,18 +225,40 @@ void ShowViewport() // Get the size of the viewport ImVec2 viewport_size = ImGui::GetContentRegionAvail(); - // For demonstration, we'll render a colored rectangle - // In a real engine, you'd render your scene here - - // Calculate the position relative to window + // Set viewport background to black + ImDrawList* draw_list = ImGui::GetWindowDrawList(); ImVec2 pos = ImGui::GetCursorScreenPos(); - - // Define rectangle dimensions ImVec2 rect_min = pos; ImVec2 rect_max = ImVec2(pos.x + viewport_size.x, pos.y + viewport_size.y); + draw_list->AddRectFilled(rect_min, rect_max, IM_COL32(0, 0, 0, 255)); // Black background - // Render a colored rectangle - ImGui::GetWindowDrawList()->AddRectFilled(rect_min, rect_max, IM_COL32(100, 100, 200, 255)); + // Iterate through active entities + const auto& entities = entityManager.GetActiveEntities(); + for (auto entity : entities) { + if (componentManager.HasComponent(entity) && componentManager.HasComponent(entity)) { + auto& sprite = componentManager.GetComponent(entity); + auto& transform = componentManager.GetComponent(entity); + + // Get texture ID + GLuint textureID = textureManager.GetTexture(sprite.texturePath); + if (textureID == 0) { + continue; // Failed to load texture + } + + // Convert GLuint to ImTextureID + ImTextureID imgui_tex_id = (ImTextureID)(intptr_t)textureID; + + // Define size based on scale + ImVec2 size(transform.scale.x, transform.scale.y); + + // Define position based on position + ImVec2 img_pos = ImVec2(pos.x + transform.position.x, pos.y + transform.position.y); + ImVec2 img_size = ImVec2(img_pos.x + size.x, img_pos.y + size.y); + + // Render the image + draw_list->AddImage(imgui_tex_id, img_pos, img_size); + } + } ImGui::End(); } @@ -289,11 +387,15 @@ void ShowInspector(EntityManager& em, ComponentManager& cm, Entity selectedEntit if (cm.HasComponent(selectedEntity)) { if (ImGui::TreeNode("Sprite")) { auto& sprite = cm.GetComponent(selectedEntity); - char buffer[256]; + static char buffer[256]; strncpy(buffer, sprite.texturePath.c_str(), sizeof(buffer)); buffer[sizeof(buffer) - 1] = '\0'; // Ensure null-termination if (ImGui::InputText("Texture Path", buffer, sizeof(buffer))) { - sprite.texturePath = std::string(buffer); + std::string newPath(buffer); + if (newPath != sprite.texturePath) { + sprite.texturePath = newPath; + Logger::GetInstance().Log(LogLevel::INFO, "Updated SpriteComponent texture path to " + newPath + " for Entity " + std::to_string(selectedEntity)); + } } if (ImGui::Button("Remove Sprite")) { @@ -309,8 +411,11 @@ void ShowInspector(EntityManager& em, ComponentManager& cm, Entity selectedEntit } else { if (ImGui::Button("Add Sprite")) { - cm.AddComponent(selectedEntity, SpriteComponent("path/to/texture.png")); - Logger::GetInstance().Log(LogLevel::INFO, "Added SpriteComponent to Entity " + std::to_string(selectedEntity)); + // Prompt the user to input texture path + // For simplicity, assign a default path + std::string defaultPath = "assets/textures/default.png"; // Ensure this texture exists + cm.AddComponent(selectedEntity, SpriteComponent(defaultPath)); + Logger::GetInstance().Log(LogLevel::INFO, "Added SpriteComponent to Entity " + std::to_string(selectedEntity) + " with texture path: " + defaultPath); } } @@ -345,6 +450,7 @@ int main(int, char**) glfwMakeContextCurrent(window); glfwSwapInterval(1); // Enable vsync + // Setup Dear ImGui context IMGUI_CHECKVERSION(); ImGui::CreateContext(); @@ -408,7 +514,7 @@ int main(int, char**) componentManager.RegisterComponent(); componentManager.RegisterComponent(); - // Create a default entity with TransformComponent + // Create a default entity with TransformComponent (without SpriteComponent) Entity defaultEntity = entityManager.CreateEntity(); componentManager.AddComponent(defaultEntity, TransformComponent()); @@ -436,8 +542,10 @@ int main(int, char**) ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus; const ImGuiViewport* viewport = ImGui::GetMainViewport(); - ImGui::SetNextWindowPos(viewport->Pos); - ImGui::SetNextWindowSize(viewport->Size); + + // Adjust the docking space position by 20 pixels to avoid overlapping with the main menu bar + ImGui::SetNextWindowPos(ImVec2(viewport->Pos.x, viewport->Pos.y + 20)); + ImGui::SetNextWindowSize(ImVec2(viewport->Size.x, viewport->Size.y - 20)); ImGui::SetNextWindowViewport(viewport->ID); ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f); @@ -486,6 +594,7 @@ int main(int, char**) } // Cleanup + textureManager.Cleanup(); ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); diff --git a/vendor/stb/CMakeLists.txt b/vendor/stb/CMakeLists.txt deleted file mode 100644 index b0ffa9c..0000000 --- a/vendor/stb/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -cmake_minimum_required(VERSION 3.22) - -project(stb) - -file(GLOB_RECURSE SOURCES src/*.cpp) - -add_library(stb) - -target_include_directories(stb PUBLIC include) - -target_sources(stb PRIVATE ${SOURCES}) \ No newline at end of file diff --git a/vendor/stb/include/stb_image.h b/vendor/stb/include/stb_image.h index 656d7e7..af81b13 100644 --- a/vendor/stb/include/stb_image.h +++ b/vendor/stb/include/stb_image.h @@ -1,5 +1,4 @@ -#ifndef STB_IMAG_H -#define STB_IMAG_H +#pragma once /* stb_image - v2.30 - public domain image loader - http://nothings.org/stb no warranty implied; use at your own risk @@ -7990,4 +7989,3 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ */ -#endif \ No newline at end of file diff --git a/vendor/stb/src/stb_image.cpp b/vendor/stb/src/stb_image.cpp deleted file mode 100644 index 148bce1..0000000 --- a/vendor/stb/src/stb_image.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#define STB_IMAGE_IMPLEMENTATION -#include \ No newline at end of file