diff --git a/.gitmodules b/.gitmodules index e613a59..710fe1b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,7 @@ [submodule "rlImGui"] path = rlImGui url = https://github.com/raylib-extras/rlImGui +[submodule "imgui"] + path = imgui + url = https://github.com/ocornut/imgui + branch = docking diff --git a/Makefile b/Makefile index 1f04a03..0916796 100644 --- a/Makefile +++ b/Makefile @@ -1,21 +1,37 @@ BINARY=gear CC=g++ -CFLAGS=-O3 -Wall -Wextra -Iimgui -IrlImGui +CFLAGS=-O3 -Wall -Wextra -Iimgui -IrlImGui -Iuuid -std=c++11 LDFLAGS=-lraylib -lm -CFILES=$(shell find -L * -type f -name '*.cpp') +CFILES=$(shell find src -type f -name '*.cpp') OBJ=$(CFILES:.cpp=.o) +DEPFILES=$(shell echo imgui/*.cpp) \ + imgui/misc/cpp/imgui_stdlib.cpp \ + $(shell echo rlImGui/*.cpp) \ + uuid/uuid.cpp +DEPOBJ=$(DEPFILES:.cpp=.o) -$(BINARY): $(OBJ) Makefile - $(CC) $(OBJ) $(LDFLAGS) -o $@ +COMPILE=$(CC) $(CFLAGS) -c $< -o $@ -%.o: %.cpp - $(CC) $(CFLAGS) -c $< -o $@ +$(BINARY): $(OBJ) $(DEPOBJ) Makefile + $(CC) $(OBJ) $(DEPOBJ) $(LDFLAGS) -o $@ + +src/%.o: src/%.cpp + $(COMPILE) + +imgui/%.o: imgui/%.cpp + $(COMPILE) + +rlImGui/%.o: rlImGui/%.cpp + $(COMPILE) + +uuid/%.o: uuid/%.cpp + $(COMPILE) run: $(BINARY) ./$(BINARY) clean: - rm -rf $(OBJ) $(BINARY) + rm -rf $(OBJ) $(DEPS) $(BINARY) diff --git a/gear b/gear index 41d2a69..9f6a44e 100755 Binary files a/gear and b/gear differ diff --git a/imgui b/imgui new file mode 160000 index 0000000..b555984 --- /dev/null +++ b/imgui @@ -0,0 +1 @@ +Subproject commit b555984a94b1c5a5f2f192d1513547c4b112c976 diff --git a/imgui.ini b/imgui.ini index 39adeec..3d0a924 100644 --- a/imgui.ini +++ b/imgui.ini @@ -1,36 +1,66 @@ [Window][DockSpaceViewport_11111111] Pos=0,19 Size=1362,710 +Collapsed=0 [Window][Debug##Default] Pos=60,60 Size=400,400 +Collapsed=0 [Window][Game Objects] Pos=0,19 -Size=231,710 +Size=277,710 +Collapsed=0 +DockId=0x00000003,0 [Window][Properties] -Pos=781,20 -Size=281,528 +Pos=1099,19 +Size=263,510 +Collapsed=0 +DockId=0x00000005,0 [Window][Viewport] -Pos=233,19 -Size=846,710 +Pos=279,19 +Size=818,710 +Collapsed=0 +DockId=0x00000007,0 [Window][Dear ImGui Demo] -Pos=233,476 -Size=846,253 +Pos=279,497 +Size=818,232 +Collapsed=0 +DockId=0x00000008,0 [Window][Dear ImGui Metrics/Debugger] Pos=233,476 Size=846,253 +Collapsed=0 [Window][About] Pos=1081,545 Size=281,184 +Collapsed=0 [Window][Debug] -Pos=781,549 -Size=281,180 +Pos=1099,531 +Size=263,198 +Collapsed=0 +DockId=0x00000006,0 + +[Window][Dear ImGui Style Editor] +Pos=60,60 +Size=353,704 +Collapsed=0 + +[Docking][Data] +DockSpace ID=0x8B93E3BD Window=0xA787BDB4 Pos=0,19 Size=1362,710 Split=X Selected=0x13926F0B + DockNode ID=0x00000003 Parent=0x8B93E3BD SizeRef=277,710 Selected=0x09CA5163 + DockNode ID=0x00000004 Parent=0x8B93E3BD SizeRef=1083,710 Split=X + DockNode ID=0x00000001 Parent=0x00000004 SizeRef=818,710 Split=Y Selected=0x13926F0B + DockNode ID=0x00000007 Parent=0x00000001 SizeRef=818,476 CentralNode=1 Selected=0x13926F0B + DockNode ID=0x00000008 Parent=0x00000001 SizeRef=818,232 Selected=0xE87781F4 + DockNode ID=0x00000002 Parent=0x00000004 SizeRef=263,710 Split=Y Selected=0x199AB496 + DockNode ID=0x00000005 Parent=0x00000002 SizeRef=263,510 Selected=0x199AB496 + DockNode ID=0x00000006 Parent=0x00000002 SizeRef=263,198 Selected=0x392A5ADD diff --git a/main.o b/main.o deleted file mode 100644 index e24924a..0000000 Binary files a/main.o and /dev/null differ diff --git a/main.cpp b/src/main.cpp similarity index 82% rename from main.cpp rename to src/main.cpp index e08f75d..78d904a 100644 --- a/main.cpp +++ b/src/main.cpp @@ -1,13 +1,9 @@ -#include -#include -#include -#include +#include "pch.h" -#include "viewport.h" - -typedef struct game_object_t { - char name[128]; -} game_object; +struct game_object { + std::string name; + std::string uuid; +}; const char* gl_versions[7] = { "UNKNOWN", @@ -24,11 +20,11 @@ Camera camera; int main() { SetConfigFlags(FLAG_WINDOW_RESIZABLE); InitWindow(800, 600, "gear"); - //InitViewport(); + InitViewport(); rlImGuiSetup(true); - /*ImGuiIO *ioptr = igGetIO(); - ioptr->ConfigFlags |= ImGuiConfigFlags_DockingEnable;*/ + ImGuiIO& io = ImGui::GetIO(); + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; camera = (Camera){}; camera.position = (Vector3) { 10.0f, 10.0f, 10.0f }; @@ -39,7 +35,7 @@ int main() { //SetTargetFPS(60); - ImVec4* colors = ImGui::GetStyle().Colors; + ImVec4* colors = ImGui::GetStyle().Colors; colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); colors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f); colors[ImGuiCol_WindowBg] = ImVec4(0.29f, 0.34f, 0.26f, 1.00f); @@ -104,23 +100,25 @@ int main() { bool show_demo = false; bool show_debug = true; - /*sl_vec(game_object*) game_objects = { 0 }; + std::vector game_objects; for(int i = 0; i < 10000; i++) { - game_object* obj = calloc(sizeof(game_object), 1); - snprintf(obj->name, 128, "bob %d", i); - sl_vec_push(game_objects, obj); - }*/ + game_object* obj = new game_object; + obj->name = std::string("object ") + std::to_string(i); + obj->uuid = UUID::generate_uuid(); + + game_objects.push_back(obj); + } game_object* selected = NULL; while(!WindowShouldClose()) { - //UpdateViewport(); + UpdateViewport(); BeginDrawing(); { ClearBackground(BLACK); - /*BeginViewport(); + BeginViewport(); { ClearBackground(BLACK); BeginMode3D(camera); @@ -133,62 +131,65 @@ int main() { DrawFPS(10, 10); //DrawText(TextFormat( - int y = 50; + /*int y = 50; for(sl_vec_it(obj, game_objects)) { DrawText(obj->name, 10, y, 20, WHITE); y += 20; - } + }*/ } - EndViewport();*/ + EndViewport(); rlImGuiBegin(); { if(ImGui::BeginMainMenuBar()) { - if(ImGui::BeginMenu("File", true)) { - if(ImGui::MenuItem("Quit", "ESC", false, true)) { + if(ImGui::BeginMenu("File")) { + if(ImGui::MenuItem("Quit", "ESC")) { CloseWindow(); } ImGui::EndMenu(); } - if(ImGui::BeginMenu("Debug", true)) { + if(ImGui::BeginMenu("Debug")) { ImGui::Checkbox("Show demo window", &show_demo); + ImGui::Checkbox("Show debug info", &show_debug); ImGui::EndMenu(); } ImGui::EndMainMenuBar(); } - //ImGui::DockSpaceOverViewport(NULL, 0, NULL); + ImGui::DockSpaceOverViewport(); if(show_demo) ImGui::ShowDemoWindow(&show_demo); - //RenderViewport(); + RenderViewport(); - ImGui::Begin("Game Objects", NULL, 0); + ImGui::Begin("Game Objects"); { - /*for(sl_vec_it(obj, game_objects)) { - char* name = (*obj)->name; - if(strcmp(name, "") == 0) name = " "; - if(igSelectable_Bool(name, selected == *obj, 0, (ImVec2){0, 0})) - selected = *obj; - }*/ + for(const auto& obj : game_objects) { + ImGui::PushID(obj->uuid.c_str()); + if(ImGui::Selectable(obj->name.c_str(), selected == obj)) + selected = obj; + ImGui::PopID(); + } } ImGui::End(); - ImGui::Begin("Properties", NULL, 0); + ImGui::Begin("Properties"); { if(selected != NULL) { - ImGui::InputText("##name", selected->name, 128, 0, NULL, NULL); + ImGui::InputText("##name", &selected->name); + ImGui::Text("UUID: %s", selected->uuid.c_str()); } } ImGui::End(); if(show_debug) { - ImGui::Begin("Debug", &show_debug, 0); + ImGui::Begin("Debug", &show_debug); { ImGui::Text("raylib %s", RAYLIB_VERSION); + ImGui::Text("imgui %s", IMGUI_VERSION); ImGui::Text("OpenGL %s", gl_versions[rlGetVersion()]); ImGui::Text("FPS: %d", GetFPS()); ImGui::Text("Frame time: %f", GetFrameTime()); diff --git a/src/main.o b/src/main.o new file mode 100644 index 0000000..687b965 Binary files /dev/null and b/src/main.o differ diff --git a/src/pch.cpp b/src/pch.cpp new file mode 100644 index 0000000..1d9f38c --- /dev/null +++ b/src/pch.cpp @@ -0,0 +1 @@ +#include "pch.h" diff --git a/src/pch.h b/src/pch.h new file mode 100644 index 0000000..a665bbd --- /dev/null +++ b/src/pch.h @@ -0,0 +1,11 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "viewport.h" diff --git a/src/pch.o b/src/pch.o new file mode 100644 index 0000000..c839f41 Binary files /dev/null and b/src/pch.o differ diff --git a/viewport.c b/src/viewport.cpp similarity index 74% rename from viewport.c rename to src/viewport.cpp index 46b8a05..37af8e5 100644 --- a/viewport.c +++ b/src/viewport.cpp @@ -1,8 +1,7 @@ #include "viewport.h" #include -#include -#include -#include +#include +#include extern Camera camera; RenderTexture viewport; @@ -30,11 +29,11 @@ void EndViewport() { } void RenderViewport() { - igPushStyleVar_Vec2(ImGuiStyleVar_WindowPadding, (ImVec2){0.0f, 0.0f}); + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, (ImVec2){0.0f, 0.0f}); - igBegin("Viewport", NULL, ImGuiWindowFlags_NoScrollbar); + ImGui::Begin("Viewport", NULL, ImGuiWindowFlags_NoScrollbar); { - if(igIsWindowHovered(0)) { + if(ImGui::IsWindowHovered()) { if(IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) { if(!IsCursorHidden()) DisableCursor(); @@ -48,17 +47,16 @@ void RenderViewport() { EnableCursor(); } - ImVec2 area; - igGetContentRegionAvail(&area); + ImVec2 area = ImGui::GetContentRegionAvail(); if(viewport.texture.width != area.x || viewport.texture.height != area.y) { new_viewport = LoadRenderTexture(area.x, area.y); swap_viewport = true; } - rligImageRenderTexture(&viewport); + rlImGuiImageRenderTexture(&viewport); } - igEnd(); + ImGui::End(); - igPopStyleVar(1); + ImGui::PopStyleVar(); } diff --git a/viewport.h b/src/viewport.h similarity index 100% rename from viewport.h rename to src/viewport.h diff --git a/src/viewport.o b/src/viewport.o new file mode 100644 index 0000000..c282033 Binary files /dev/null and b/src/viewport.o differ diff --git a/uuid/uuid.cpp b/uuid/uuid.cpp new file mode 100644 index 0000000..4c03534 --- /dev/null +++ b/uuid/uuid.cpp @@ -0,0 +1,31 @@ +#include "uuid.h" +#include +#include +#include + +std::string UUID::generate_uuid() { + std::stringstream hexstream; + hexstream << UUID::generate_hex(4) << "-" << UUID::generate_hex(2) << "-" + << UUID::generate_hex(2) << "-" << UUID::generate_hex(2) << "-" + << UUID::generate_hex(6); + return hexstream.str(); +} + +std::string UUID::generate_hex(const unsigned int len) { + std::stringstream ss; + for (auto i = 0; i < len; i++) { + const auto rc = random_char(); + std::stringstream hexstream; + hexstream << std::hex << rc; + auto hex = hexstream.str(); + ss << (hex.length() < 2 ? '0' + hex : hex); + } + return ss.str(); +} + +unsigned int UUID::random_char() { + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution<> dis(0, 255); + return dis(gen); +} diff --git a/uuid/uuid.h b/uuid/uuid.h new file mode 100644 index 0000000..abfebf3 --- /dev/null +++ b/uuid/uuid.h @@ -0,0 +1,37 @@ +// UUID.hpp +#ifndef UUID_hpp +#define UUID_hpp + +#include + +// Random generation courtesy of https://lowrey.me/guid-generation-in-c-11/ + +class UUID { +public: + /** + * @brief Generates a uuid string in the form + * b9317db-02a2-4882-9b94-d1e1defe8c56 + * + * @return std::string + */ + static std::string generate_uuid(); + + /** + * @brief + * + * @param len Length in bytes + * @return std::string String random hex chars (2x length of the bytes) + */ + static std::string generate_hex(const unsigned int len); + +private: + /** + * @brief Generates a safe pseudo-random character + * + * @return unsigned int + */ + static unsigned int random_char(); +}; + + +#endif diff --git a/uuid/uuid.o b/uuid/uuid.o new file mode 100644 index 0000000..21a08ce Binary files /dev/null and b/uuid/uuid.o differ