From eca8fe3c1d225a5a98a6b53921d9a7705f753d24 Mon Sep 17 00:00:00 2001 From: kelson8 Date: Tue, 18 Jun 2024 13:13:26 -0400 Subject: [PATCH] Move main menu into its own file. Modify text file functions and rename file. Add functions for text in imgui with the text_functions file. Add keystates file. --- .gitignore | 4 + ConsoleAppTest.vcxproj | 21 +- menus/main_menu.cpp | 307 +++++++++ menus/main_menu.h | 6 + test/directx9_test.cpp | 601 +++++++----------- test/directx9_test.h | 8 + test/opengl_test.cpp | 10 +- util/keystates.cpp | 34 + util/keystates.h | 11 + ...eFunctions.cpp => text_file_functions.cpp} | 64 +- ...tFileFunctions.h => text_file_functions.h} | 3 +- util/text_functions.cpp | 16 + util/text_functions.h | 5 + 13 files changed, 692 insertions(+), 398 deletions(-) create mode 100644 menus/main_menu.cpp create mode 100644 menus/main_menu.h create mode 100644 util/keystates.cpp create mode 100644 util/keystates.h rename util/{textFileFunctions.cpp => text_file_functions.cpp} (63%) rename util/{textFileFunctions.h => text_file_functions.h} (59%) create mode 100644 util/text_functions.cpp create mode 100644 util/text_functions.h diff --git a/.gitignore b/.gitignore index cbef543..5d12dbd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +# Added by kelson8 +Main - Copy.cpp +old/** + # ---> C++ # Prerequisites *.d diff --git a/ConsoleAppTest.vcxproj b/ConsoleAppTest.vcxproj index 489ff7a..ac854bf 100644 --- a/ConsoleAppTest.vcxproj +++ b/ConsoleAppTest.vcxproj @@ -182,8 +182,8 @@ Console true - d3d9.lib;opengl32.lib;glfw3.lib;%(AdditionalDependencies) - $(ProjectDir)lib\GLFW\lib-vc2022;%(AdditionalLibraryDirectories) + opengl32.lib;glfw3.lib;%(AdditionalDependencies) + .\lib\GLFW\lib-vc2022;%(AdditionalLibraryDirectories) @@ -213,6 +213,7 @@ + @@ -221,12 +222,17 @@ + - + + + + + @@ -234,9 +240,16 @@ + + - + + + + + + diff --git a/menus/main_menu.cpp b/menus/main_menu.cpp new file mode 100644 index 0000000..92ac321 --- /dev/null +++ b/menus/main_menu.cpp @@ -0,0 +1,307 @@ +#include "imgui.h" +#include "imgui_impl_dx9.h" + +#if _WIN32 +#include "imgui_impl_win32.h" +#include +#include "../util/keystates.h" +#endif + +#include +#include + +#include +#include +#include + +#include "../util/text_file_functions.h" +#include "../test/directx9_test.h" +#include "../util/text_functions.h" +#include "main_menu.h" + +// https://www.geeksforgeeks.org/macros-and-its-types-in-c-cpp/ +// Test macros +#define LIMIT 5 + +// This file is inactive, not in use yet. + +// Boolean values +bool DirectX9Test::show_demo_window = true; +bool DirectX9Test::button1_clicked = false; +bool DirectX9Test::show_app_main_menu_bar = false; +bool DirectX9Test::toggle_text = false; +bool DirectX9Test::dark_mode = false; +bool DirectX9Test::define_test = false; +bool DirectX9Test::list_values = false; + +// + +void MainMenu::MainMenuTest() { + ImGuiIO& io = ImGui::GetIO(); (void)io; + + //if (ImGui::Begin("KCNet ImGui", nullptr, ImGuiWindowFlags_MenuBar)) + //{ + // Menu bar + if (ImGui::BeginMenuBar()) + { + if (ImGui::BeginMenu("Test")) + { + ImGui::MenuItem("Main menu bar", NULL, &DirectX9Test::show_app_main_menu_bar); + ImGui::EndMenu(); + } + ImGui::EndMenuBar(); + } + + // Main testing header. + if (ImGui::CollapsingHeader("test1")) + { + ImGui::BulletText("Hello, this should show up."); + ImGui::Separator(); + + // Menu toggle button + if (ImGui::Button("Menu toggle")) + { + std::cout << "Test" << std::endl; + // How would I check if dark mode is enabled? + //if (ImGui::GetStyle == ImGui) { + //ImGui::StyleColorsLight(); + //} + //else + // { + //ImGui::StyleColorsDark(); + //} + + } + + // I couldn't figure this out yet. + // This shows up for a second then dies. + ImGui::Separator(); + if (!ImGui::Button("Click")) + { + DirectX9Test::button1_clicked = false; + } + else + { + DirectX9Test::button1_clicked = true; + } + + if (!DirectX9Test::button1_clicked) + { + TextFunctions::SetBulletText("You clicked the button"); + } + else + { + ImGui::BulletText("Button not pressed."); + + //ImGui::BulletText("You clicked the button!"); + } + //} + + //if (ImGui::Button("Click")) + //{ + + // setBulletText("You clicked the button"); + // //ImGui::BulletText("You clicked the button!"); + //} + //else + //{ + // ImGui::BulletText("Button not pressed."); + //} + + ImGui::Separator(); + + + // This below works for a dark mode toggle. + // 5-16-2024 @ 9:14PM + +#ifndef _TEST +#define _TEST +#endif + + +#ifdef _TEST + // Well the checkbox is inverted lol, oh well I'll fix it later. + // If it is checked it turns off dark mode, I guess its because I have dark mode as the default theme. + if (!ImGui::Checkbox("Dark Mode", &DirectX9Test::dark_mode)) + { + if (!DirectX9Test::dark_mode) + { + + ImGui::StyleColorsDark(); + ImGui::BulletText("On"); + } + else + { + ImGui::StyleColorsLight(); + ImGui::BulletText("Off"); + } + } + +#else + ImGui::Checkbox("Dark Mode", &dark_mode); + if (dark_mode) + { + ImGui::StyleColorsLight(); + ImGui::BulletText("Off"); + } + else + { + ImGui::StyleColorsDark(); + + ImGui::BulletText("On"); + } +#endif //_TEST + // Disable preprocessor +#undef _TEST + + // This works for toggling the demo window on and off + ImGui::Checkbox("Demo window", &DirectX9Test::show_demo_window); + + // Possibly implement feature to read from a text file and write it to some output box. + //ImGui::Text("Test"); + + //const char *test = "Test"; + + //// I never did get this working yet. + //// https://www.programiz.com/cpp-programming/buffer + //char* buf = new char[40]; + ////char InputBuf[256]; + //// https://stackoverflow.com/questions/502856/whats-the-difference-between-size-t-and-int-in-c + //size_t num = 1; + ////char deref = &test; + + //// This doesn't work right. + //if (ImGui::InputText("##Username", buf, num)) + //{ + // //std::cout << buf; + //} + + // This works. + if (ImGui::IsMousePosValid()) + ImGui::Text("Mouse pos: (%g, %g)", io.MousePos.x, io.MousePos.y); + else + ImGui::Text("Mouse pos: "); + + //if (ImGui::Checkbox("Show Text", &toggle_text)) + //{ + // ImGui::BulletText("Hello"); + //} + //else + //{ + // ImGui::BulletText("Hola"); + //} + + + // How do I update this text in code? + // Like if a button is pressed. + //ImGui::Separator(); + //ImGui::BulletText("Test"); + + // I'm quite sure this will only work on Windows, will need tested on Linux once I get opengl working. +#ifdef _WIN32 + + static bool test = false; + ImGui::Text("Key Press: "); + ImGui::SameLine(); + //if (ImGui::Checkbox("Test", &test)) + ImGui::Checkbox("Test", &test); + + if (test) + { + // https://stackoverflow.com/questions/41600981/how-do-i-check-if-a-key-is-pressed-on-c + /*Check if high-order bit is set (1 << 15)*/ + // I wonder if I can create a for loop to iterate over the list of keys + + + // I couldn't get the below working. + //char keyboard_chars[26] = { 'A', 'B', 'C', 'D', + //'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', + //'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; + + enum keyboard_chars_enum { + A = 0, B, C, D, + E, F, G, H, I, J, K, L, M, N, O, + P, Q, R, S, T, U, V, W, X, Y, Z + }; + + //https://www.codeproject.com/Questions/207914/To-get-Number-of-elements-in-typedef-struct + char input = Z - A; + + ////int test = 1; + + // https://cplusplus.com/forum/beginner/6463/ + // I never did get this working + //switch (keyboard_chars_enum) + //switch (input) + //{ + //case A: + // ImGui::Text("A Key pressed"); + //} + + //for (int i =0; i< keyboard_chars_enum) + // This one didn't work + //for (int i = 0; i < input; i++) { + // if (GetKeyState(i) & 0x8000) + // ImGui::Text(i + " key was pressed"); + //} + + + // This works + //if (GetKeyState('A') & 0x8000) + //{ + // ImGui::Text("A Key pressed"); + //} + + //if (GetKeyState(VK_SHIFT) & 0x8000) + //{ + // ImGui::Text("Shift Key pressed"); + //} + // + } + else + { + ImGui::Text("Goodbye"); + } +#endif //_WIN32 + + + //#define _TEST + //#ifdef _TEST + // ImGui::Checkbox("Define Test", &DirectX9Test::define_test); + // + // if (define_test) + // { + // ImGui::Text("Value of limit is: " + LIMIT); + // //std::cout << "Value of limit is " << LIMIT; + // } + //#endif //_TEST + //#undef _TEST + + + + ImGui::Checkbox("List values", &DirectX9Test::list_values); + + if (DirectX9Test::list_values) + { + ImGui::Text(KeyStates::charTest()); + + + //Sleep(2000); + // This spams the console too much. + KeyStates::test(); + } + else + { + ImGui::Text("Disabled"); + } + } + + // This works for a column, having multiple items on the same row. + ImGui::Columns(2); + ImGui::Text("Hello"); + ImGui::NextColumn(); + ImGui::Text("World"); + + ImGui::Columns(1); + //} +} \ No newline at end of file diff --git a/menus/main_menu.h b/menus/main_menu.h new file mode 100644 index 0000000..0c646cf --- /dev/null +++ b/menus/main_menu.h @@ -0,0 +1,6 @@ +#pragma once + +class MainMenu { +public: + static void MainMenuTest(); +}; \ No newline at end of file diff --git a/test/directx9_test.cpp b/test/directx9_test.cpp index c503102..4104ba0 100644 --- a/test/directx9_test.cpp +++ b/test/directx9_test.cpp @@ -3,6 +3,8 @@ // I would like to move opengl and directx into their own classes. // So I can switch between them and the main file won't be so messy. +// This is my ImGui C++ demo, I mess around with it sometimes. + #ifndef _TEST #define _TEST #endif @@ -29,6 +31,7 @@ #if _WIN32 #include "imgui_impl_win32.h" #include +#include "../util/keystates.h" #endif #include @@ -38,7 +41,9 @@ #include #include -#include "../util/textFileFunctions.h" +#include "../util/text_file_functions.h" +#include "../util/text_functions.h" +#include "../menus/main_menu.h" // https://www.geeksforgeeks.org/macros-and-its-types-in-c-cpp/ // Test macros @@ -63,14 +68,14 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); static void HelpMarker(const char* desc) { - ImGui::TextDisabled("(?)"); - if (ImGui::BeginItemTooltip()) - { - ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f); - ImGui::TextUnformatted(desc); - ImGui::PopTextWrapPos(); - ImGui::EndTooltip(); - } + ImGui::TextDisabled("(?)"); + if (ImGui::BeginItemTooltip()) + { + ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f); + ImGui::TextUnformatted(desc); + ImGui::PopTextWrapPos(); + ImGui::EndTooltip(); + } } // Windows specific features @@ -86,55 +91,55 @@ bool isDown = keyState & 0x8000; // Will this work? -static void setBulletText(const char* text) -{ - ImGui::BulletText(text); -} - +//static void setBulletText(const char* text) +//{ +// ImGui::BulletText(text); +//} // int test() { - int num1 = 5; - int num2 = 10; + int num1 = 5; + int num2 = 10; - return num1 + num2; + return num1 + num2; } // https://www.geeksforgeeks.org/converting-number-to-string-in-cpp/ // Prints the value of std::string testString1() { - int num1 = 5; - int num2 = 10; - int sum = num1 + num2; - std::string num1_string = std::to_string(num1); - std::string num2_string = std::to_string(num2); - std::string sum_string = std::to_string(sum); + int num1 = 5; + int num2 = 10; + int sum = num1 + num2; + // Convert the numbers to strings for the return value. + std::string num1_string = std::to_string(num1); + std::string num2_string = std::to_string(num2); + std::string sum_string = std::to_string(sum); - return "The value of " + num1_string + " + " + num2_string + " = " + sum_string; + return "The value of " + num1_string + " + " + num2_string + " = " + sum_string; } // imgui_demo line 256 static void ShowWindow(bool* p_open) { - static bool show_main_menu = false; + static bool show_main_menu = false; - if (show_main_menu) - { - if (ImGui::Begin("Test")) - { - ImGui::Text("Hello World!"); - } - } + if (show_main_menu) + { + if (ImGui::Begin("Test")) + { + ImGui::Text("Hello World!"); + } + } - if (!ImGui::Begin("Test", p_open)) - { + if (!ImGui::Begin("Test", p_open)) + { - ImGui::End(); - return; - } + ImGui::End(); + return; + } } @@ -144,376 +149,208 @@ void DirectX9Test::directX9Test() { #ifdef _TEST - std::cout << testString1(); + //std::cout << testString1(); #endif - // Create application window - //ImGui_ImplWin32_EnableDpiAwareness(); - WNDCLASSEXW wc = { sizeof(wc), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"ImGui Example", nullptr }; - ::RegisterClassExW(&wc); - HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui DirectX9 Example", WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, nullptr, nullptr, wc.hInstance, nullptr); + // Create application window + //ImGui_ImplWin32_EnableDpiAwareness(); + WNDCLASSEXW wc = { sizeof(wc), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"ImGui Example", nullptr }; + ::RegisterClassExW(&wc); + HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui DirectX9 Example", WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, nullptr, nullptr, wc.hInstance, nullptr); - // Initialize Direct3D - if (!CreateDeviceD3D(hwnd)) - { - CleanupDeviceD3D(); - ::UnregisterClassW(wc.lpszClassName, wc.hInstance); - //return 1; - } + // Initialize Direct3D + if (!CreateDeviceD3D(hwnd)) + { + CleanupDeviceD3D(); + ::UnregisterClassW(wc.lpszClassName, wc.hInstance); + //return 1; + } - // Show the window - ::ShowWindow(hwnd, SW_SHOWDEFAULT); - ::UpdateWindow(hwnd); + // Show the window + ::ShowWindow(hwnd, SW_SHOWDEFAULT); + ::UpdateWindow(hwnd); - // Setup Dear ImGui context - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); - ImGuiIO& io = ImGui::GetIO(); (void)io; - io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls - io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls + // Setup Dear ImGui context + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); (void)io; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls + io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls - // Setup Dear ImGui style - ImGui::StyleColorsDark(); - //ImGui::StyleColorsLight(); + // Setup Dear ImGui style + ImGui::StyleColorsDark(); + //ImGui::StyleColorsLight(); - // Setup Platform/Renderer backends - ImGui_ImplWin32_Init(hwnd); - ImGui_ImplDX9_Init(g_pd3dDevice); + // Setup Platform/Renderer backends + ImGui_ImplWin32_Init(hwnd); + ImGui_ImplDX9_Init(g_pd3dDevice); - // Load Fonts - // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them. - // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple. - // - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit). - // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call. - // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering. - // - Read 'docs/FONTS.md' for more instructions and details. - // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ ! - //io.Fonts->AddFontDefault(); - //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f); - //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f); - //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f); - //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f); - //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese()); - //IM_ASSERT(font != nullptr); + // Load Fonts + // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them. + // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple. + // - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit). + // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call. + // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering. + // - Read 'docs/FONTS.md' for more instructions and details. + // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ ! + //io.Fonts->AddFontDefault(); + //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f); + //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f); + //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f); + //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f); + //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese()); + //IM_ASSERT(font != nullptr); - // Our state - bool show_demo_window = true; - bool show_another_window = false; - ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); + // Our state + //bool show_demo_window = true; + bool show_another_window = false; + ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); - // Show window - // Main loop - bool done = false; - bool button1_clicked = false; - static bool show_app_main_menu_bar = false; - //static bool toggle_text = false; - static bool dark_mode = false; - static bool define_test = false; + // Show window + // Main loop + bool done = false; + //bool button1_clicked = false; + //bool show_app_main_menu_bar = false; + //static bool toggle_text = false; + //static bool dark_mode = false; + //static bool define_test = false; + //static bool list_values = false; - while (!done) - { - // Poll and handle messages (inputs, window resize, etc.) - // See the WndProc() function below for our to dispatch events to the Win32 backend. - MSG msg; - while (::PeekMessage(&msg, nullptr, 0U, 0U, PM_REMOVE)) - { - ::TranslateMessage(&msg); - ::DispatchMessage(&msg); - if (msg.message == WM_QUIT) - done = true; - } - if (done) - break; + while (!done) + { + // Poll and handle messages (inputs, window resize, etc.) + // See the WndProc() function below for our to dispatch events to the Win32 backend. + MSG msg; + while (::PeekMessage(&msg, nullptr, 0U, 0U, PM_REMOVE)) + { + ::TranslateMessage(&msg); + ::DispatchMessage(&msg); + if (msg.message == WM_QUIT) + done = true; + } + if (done) + break; - // Handle window resize (we don't resize directly in the WM_SIZE handler) - if (g_ResizeWidth != 0 && g_ResizeHeight != 0) - { - g_d3dpp.BackBufferWidth = g_ResizeWidth; - g_d3dpp.BackBufferHeight = g_ResizeHeight; - g_ResizeWidth = g_ResizeHeight = 0; - ResetDevice(); - } + // Handle window resize (we don't resize directly in the WM_SIZE handler) + if (g_ResizeWidth != 0 && g_ResizeHeight != 0) + { + g_d3dpp.BackBufferWidth = g_ResizeWidth; + g_d3dpp.BackBufferHeight = g_ResizeHeight; + g_ResizeWidth = g_ResizeHeight = 0; + ResetDevice(); + } - // Start the Dear ImGui frame - ImGui_ImplDX9_NewFrame(); - ImGui_ImplWin32_NewFrame(); - ImGui::NewFrame(); + // Start the Dear ImGui frame + ImGui_ImplDX9_NewFrame(); + ImGui_ImplWin32_NewFrame(); + ImGui::NewFrame(); - // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!). - if (show_demo_window) - ImGui::ShowDemoWindow(&show_demo_window); + // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!). + if (show_demo_window) + ImGui::ShowDemoWindow(&show_demo_window); - if (ImGui::Begin("My new window", nullptr, ImGuiWindowFlags_MenuBar)) - { - // Menu bar - if (ImGui::BeginMenuBar()) - { - if (ImGui::BeginMenu("Test")) - { - ImGui::MenuItem("Main menu bar", NULL, &show_app_main_menu_bar); - ImGui::EndMenu(); - } - ImGui::EndMenuBar(); - } - - // Main testing header. - if (ImGui::CollapsingHeader("test1")) - { - ImGui::BulletText("Hello, this should show up."); - ImGui::Separator(); - - // Menu toggle button - if (ImGui::Button("Menu toggle")) - { - std::cout << "Test" << std::endl; - // How would I check if dark mode is enabled? - //if (ImGui::GetStyle == ImGui) { - //ImGui::StyleColorsLight(); - //} - //else - // { - //ImGui::StyleColorsDark(); - //} - - } - - // I couldn't figure this out yet. - // This shows up for a second then dies. - ImGui::Separator(); - if (!ImGui::Button("Click")) - { - button1_clicked = false; - } - else - { - button1_clicked = true; - } - - if (!button1_clicked) - { - setBulletText("You clicked the button"); - } - else - { - ImGui::BulletText("Button not pressed."); - - //ImGui::BulletText("You clicked the button!"); - } - //} - - //if (ImGui::Button("Click")) - //{ - - // setBulletText("You clicked the button"); - // //ImGui::BulletText("You clicked the button!"); - //} - //else - //{ - // ImGui::BulletText("Button not pressed."); - //} - - ImGui::Separator(); + //MainMenu::MainMenuTest(); +//#define _OLD_CODE - // This below works for a dark mode toggle. - // 5-16-2024 @ 9:14PM + if (ImGui::Begin("KCNet ImGui", nullptr, ImGuiWindowFlags_MenuBar)) + { -#ifndef _TEST -#define _TEST -#endif + MainMenu::MainMenuTest(); + // Text file functions test menu + if (ImGui::CollapsingHeader("Text File Functions")) + { + ImGui::BulletText("I will add reading files, writing and more to this."); + ImGui::Separator(); -#ifdef _TEST - // Well the checkbox is inverted lol, oh well I'll fix it later. - // If it is checked it turns off dark mode, I guess its because I have dark mode as the default theme. - if (!ImGui::Checkbox("Dark Mode", &dark_mode)) - { - if (!dark_mode) - { + // I almost got this working 5-17-2024 @ 3:14PM + // It doesn't print out all of the lines from the return value. + if (ImGui::Button("Test")) + { + TextFileFunctions::printTextOutput("test.txt"); + //std::cout << TextFileFunctions::printTextOutput("test.txt") << std::endl; + } + } + // End Text file functions test menu - ImGui::StyleColorsDark(); - ImGui::BulletText("On"); - } - else - { - ImGui::StyleColorsLight(); - ImGui::BulletText("Off"); - } - } - -#else - ImGui::Checkbox("Dark Mode", &dark_mode); - if (dark_mode) - { - ImGui::StyleColorsLight(); - ImGui::BulletText("Off"); - } - else - { - ImGui::StyleColorsDark(); - - ImGui::BulletText("On"); - } -#endif //_TEST - // Disable preprocessor -#undef _TEST - - // This works for toggling the demo window on and off - ImGui::Checkbox("Demo window", &show_demo_window); - - - // Possibly implement feature to read from a text file and write it to some output box. - //ImGui::Text("Test"); - - //const char *test = "Test"; - - //// I never did get this working yet. - //// https://www.programiz.com/cpp-programming/buffer - //char* buf = new char[40]; - ////char InputBuf[256]; - //// https://stackoverflow.com/questions/502856/whats-the-difference-between-size-t-and-int-in-c - //size_t num = 1; - ////char deref = &test; - - //// This doesn't work right. - //if (ImGui::InputText("##Username", buf, num)) - //{ - // //std::cout << buf; - //} + //if (ImGui::BeginMenu("My menu")) + //{ + // //ImGui::BulletText("You should see this"); + // ImGui::MenuItem("test", NULL); + // ImGui::EndMenu(); + //} - // This works. - if (ImGui::IsMousePosValid()) - ImGui::Text("Mouse pos: (%g, %g)", io.MousePos.x, io.MousePos.y); - else - ImGui::Text("Mouse pos: "); + } + // End ImGui + ImGui::End(); - //if (ImGui::Checkbox("Show Text", &toggle_text)) - //{ - // ImGui::BulletText("Hello"); - //} - //else - //{ - // ImGui::BulletText("Hola"); - //} + // Rendering + ImGui::EndFrame(); + g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE); + g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); + g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE); + D3DCOLOR clear_col_dx = D3DCOLOR_RGBA((int)(clear_color.x * clear_color.w * 255.0f), (int)(clear_color.y * clear_color.w * 255.0f), (int)(clear_color.z * clear_color.w * 255.0f), (int)(clear_color.w * 255.0f)); + g_pd3dDevice->Clear(0, nullptr, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clear_col_dx, 1.0f, 0); + if (g_pd3dDevice->BeginScene() >= 0) + { + ImGui::Render(); + ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData()); + g_pd3dDevice->EndScene(); + } + HRESULT result = g_pd3dDevice->Present(nullptr, nullptr, nullptr, nullptr); + // Handle loss of D3D9 device + if (result == D3DERR_DEVICELOST && g_pd3dDevice->TestCooperativeLevel() == D3DERR_DEVICENOTRESET) + ResetDevice(); +} - // How do I update this text in code? - // Like if a button is pressed. - //ImGui::Separator(); - //ImGui::BulletText("Test"); + ImGui_ImplDX9_Shutdown(); + ImGui_ImplWin32_Shutdown(); + ImGui::DestroyContext(); - -#ifdef _TEST - ImGui::Checkbox("Define Test", &define_test); - - if (define_test) - { - ImGui::Text("Value of limit is: " + LIMIT); - std::cout << "Value of limit is " << LIMIT; - } -#endif //_TEST - - } - - // End main test menu - - - - // Text file functions test menu - if (ImGui::CollapsingHeader("Text File Functions")) - { - ImGui::BulletText("I will add reading files, writing and more to this."); - ImGui::Separator(); - - // I almost got this working 5-17-2024 @ 3:14PM - // It doesn't print out all of the lines from the return value. - if (ImGui::Button("Test")) - { - TextFileFunctions::printTextOutput("test.txt"); - //std::cout << TextFileFunctions::printTextOutput("test.txt") << std::endl; - } - } - // End Text file functions test menu - - //if (ImGui::BeginMenu("My menu")) - //{ - // //ImGui::BulletText("You should see this"); - // ImGui::MenuItem("test", NULL); - // ImGui::EndMenu(); - //} - - - - } - // End ImGui - ImGui::End(); - - // Rendering - ImGui::EndFrame(); - g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE); - g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); - g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE); - D3DCOLOR clear_col_dx = D3DCOLOR_RGBA((int)(clear_color.x * clear_color.w * 255.0f), (int)(clear_color.y * clear_color.w * 255.0f), (int)(clear_color.z * clear_color.w * 255.0f), (int)(clear_color.w * 255.0f)); - g_pd3dDevice->Clear(0, nullptr, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clear_col_dx, 1.0f, 0); - if (g_pd3dDevice->BeginScene() >= 0) - { - ImGui::Render(); - ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData()); - g_pd3dDevice->EndScene(); - } - HRESULT result = g_pd3dDevice->Present(nullptr, nullptr, nullptr, nullptr); - - // Handle loss of D3D9 device - if (result == D3DERR_DEVICELOST && g_pd3dDevice->TestCooperativeLevel() == D3DERR_DEVICENOTRESET) - ResetDevice(); - } - - ImGui_ImplDX9_Shutdown(); - ImGui_ImplWin32_Shutdown(); - ImGui::DestroyContext(); - - CleanupDeviceD3D(); - ::DestroyWindow(hwnd); - ::UnregisterClassW(wc.lpszClassName, wc.hInstance); + CleanupDeviceD3D(); + ::DestroyWindow(hwnd); + ::UnregisterClassW(wc.lpszClassName, wc.hInstance); } // Helper functions bool CreateDeviceD3D(HWND hWnd) { - if ((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == nullptr) - return false; + if ((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == nullptr) + return false; - // Create the D3DDevice - ZeroMemory(&g_d3dpp, sizeof(g_d3dpp)); - g_d3dpp.Windowed = TRUE; - g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; - g_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; // Need to use an explicit format with alpha if needing per-pixel alpha composition. - g_d3dpp.EnableAutoDepthStencil = TRUE; - g_d3dpp.AutoDepthStencilFormat = D3DFMT_D16; - g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; // Present with vsync - //g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // Present without vsync, maximum unthrottled framerate - if (g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &g_d3dpp, &g_pd3dDevice) < 0) - return false; + // Create the D3DDevice + ZeroMemory(&g_d3dpp, sizeof(g_d3dpp)); + g_d3dpp.Windowed = TRUE; + g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; + g_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; // Need to use an explicit format with alpha if needing per-pixel alpha composition. + g_d3dpp.EnableAutoDepthStencil = TRUE; + g_d3dpp.AutoDepthStencilFormat = D3DFMT_D16; + g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; // Present with vsync + //g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // Present without vsync, maximum unthrottled framerate + if (g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &g_d3dpp, &g_pd3dDevice) < 0) + return false; - return true; + return true; } void CleanupDeviceD3D() { - if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = nullptr; } - if (g_pD3D) { g_pD3D->Release(); g_pD3D = nullptr; } + if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = nullptr; } + if (g_pD3D) { g_pD3D->Release(); g_pD3D = nullptr; } } void ResetDevice() { - ImGui_ImplDX9_InvalidateDeviceObjects(); - HRESULT hr = g_pd3dDevice->Reset(&g_d3dpp); - if (hr == D3DERR_INVALIDCALL) - IM_ASSERT(0); - ImGui_ImplDX9_CreateDeviceObjects(); + ImGui_ImplDX9_InvalidateDeviceObjects(); + HRESULT hr = g_pd3dDevice->Reset(&g_d3dpp); + if (hr == D3DERR_INVALIDCALL) + IM_ASSERT(0); + ImGui_ImplDX9_CreateDeviceObjects(); } // Forward declare message handler from imgui_impl_win32.cpp @@ -526,26 +363,26 @@ extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { - if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam)) - return true; + if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam)) + return true; - switch (msg) - { - case WM_SIZE: - if (wParam == SIZE_MINIMIZED) - return 0; - g_ResizeWidth = (UINT)LOWORD(lParam); // Queue resize - g_ResizeHeight = (UINT)HIWORD(lParam); - return 0; - case WM_SYSCOMMAND: - if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu - return 0; - break; - case WM_DESTROY: - ::PostQuitMessage(0); - return 0; - } - return ::DefWindowProcW(hWnd, msg, wParam, lParam); + switch (msg) + { + case WM_SIZE: + if (wParam == SIZE_MINIMIZED) + return 0; + g_ResizeWidth = (UINT)LOWORD(lParam); // Queue resize + g_ResizeHeight = (UINT)HIWORD(lParam); + return 0; + case WM_SYSCOMMAND: + if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu + return 0; + break; + case WM_DESTROY: + ::PostQuitMessage(0); + return 0; + } + return ::DefWindowProcW(hWnd, msg, wParam, lParam); } #endif //_TEST diff --git a/test/directx9_test.h b/test/directx9_test.h index c600a6b..077c205 100644 --- a/test/directx9_test.h +++ b/test/directx9_test.h @@ -2,4 +2,12 @@ class DirectX9Test { public: static void directX9Test(); + static bool show_app_main_menu_bar; + static bool button1_clicked; + static bool dark_mode; + static bool define_values; + static bool list_values; + static bool show_demo_window; + static bool define_test; + static bool toggle_text; }; \ No newline at end of file diff --git a/test/opengl_test.cpp b/test/opengl_test.cpp index 1c6f68e..759118e 100644 --- a/test/opengl_test.cpp +++ b/test/opengl_test.cpp @@ -3,12 +3,13 @@ // Moving opengl test into this file until I'm ready to mess with it. // 5-25-2024 @ 12:21PM +// https://www.reddit.com/r/cpp/comments/16scrps/at_what_point_in_your_c_development_do_you/ + #ifndef _OPENGL //#define _OPENGL #endif -// I never did get this one working. -// Keeps giving linker errors and library not found errors. +// I almost got this working, now it says that ImGui_ImplOpenGL3_Init is not defined. #ifdef _OPENGL // Dear ImGui: standalone example application for GLFW + OpenGL 3, using programmable pipeline @@ -98,7 +99,8 @@ void OpenGLTest::openGLTest() #ifdef __EMSCRIPTEN__ ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback("#canvas"); #endif - ImGui_ImplOpenGL3_Init(glsl_version); + //ImGui_ImplOpenGL3_Init(glsl_version); + ImGui_ImplOpenGL3_Init("#version 130"); // Load Fonts // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them. @@ -191,6 +193,8 @@ void OpenGLTest::openGLTest() ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); glfwSwapBuffers(window); + + ImGui::EndFrame(); } #ifdef __EMSCRIPTEN__ EMSCRIPTEN_MAINLOOP_END; diff --git a/util/keystates.cpp b/util/keystates.cpp new file mode 100644 index 0000000..afcf691 --- /dev/null +++ b/util/keystates.cpp @@ -0,0 +1,34 @@ +#include "keystates.h" +#include +#include + +// What was this file doing? +char keyboard_chars[26] = { 'A', 'B', 'C', 'D', + 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', + 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; + +static void chars() +{ + std::cout << keyboard_chars; +} + +char* KeyStates::charTest() +{ + return keyboard_chars; +} + +// I wonder how to return the list of arrays? +void KeyStates::test() +{ + for (int i = 0; i < keyboard_chars[i]; i++) + { + std::cout << keyboard_chars[i] << " [" << i << "]" < +#endif + +class KeyStates { +public: + static char* charTest(); + static void test(); +}; + diff --git a/util/textFileFunctions.cpp b/util/text_file_functions.cpp similarity index 63% rename from util/textFileFunctions.cpp rename to util/text_file_functions.cpp index c049e32..f6d1f2f 100644 --- a/util/textFileFunctions.cpp +++ b/util/text_file_functions.cpp @@ -1,7 +1,7 @@ #include #include #include -#include "textFileFunctions.h" +#include "text_file_functions.h" // Checking if the file exists breaks without this // https://stackoverflow.com/questions/19321804/this-function-or-variable-may-be-unsafe-visual-studio @@ -51,11 +51,14 @@ void TextFileFunctions::readTextFile(std::string file) //else cout << "Unable to open file!"; } -//static std::string testString1(std::string file std::string text) -//std::string TextFileFunctions::testString1(std::string file) -// This doesn't fully work yet, once I finish it, it'll show up on a ImGui tab. -std::string TextFileFunctions::printTextOutput(std::string file) +#ifndef _TEST +#define _TEST +#endif + +// Add test function for this, I need to figure out how to return an array like this. +#ifdef _TEST +void TextFileFunctions::printTextOutput(std::string file) { std::string line; std::ifstream myfile(file); @@ -66,8 +69,8 @@ std::string TextFileFunctions::printTextOutput(std::string file) { while (std::getline(myfile, line)) { - //std::cout << line << '\n'; - return line; + std::cout << line << '\n'; + //return line; } //return line; @@ -82,4 +85,49 @@ std::string TextFileFunctions::printTextOutput(std::string file) { std::cout << "File doesn't exist!"; } -} \ No newline at end of file +} + +// https://www.digitalocean.com/community/tutorials/return-array-in-c-plus-plus-function +//void* test() +//{ +// +//} + + +#else + +//static std::string testString1(std::string file std::string text) +//std::string TextFileFunctions::testString1(std::string file) + +// This doesn't fully work yet, once I finish it, it'll show up on a ImGui tab. +//std::string TextFileFunctions::printTextOutput(std::string file) +void TextFileFunctions::printTextOutput(std::string file) +{ + std::string line; + std::ifstream myfile(file); + + if (TextFileFunctions::fileExistCheck(file)) + { + if (myfile.is_open()) + { + while (std::getline(myfile, line)) + { + std::cout << line << '\n'; + //return line; + } + + //return line; + myfile.close(); + } + else + { + std::cout << "Unable to open file!"; + } + } + else + { + std::cout << "File doesn't exist!"; + } +} +#endif //_TEST + diff --git a/util/textFileFunctions.h b/util/text_file_functions.h similarity index 59% rename from util/textFileFunctions.h rename to util/text_file_functions.h index e105c1a..b7ca6a3 100644 --- a/util/textFileFunctions.h +++ b/util/text_file_functions.h @@ -3,5 +3,6 @@ class TextFileFunctions { public: inline static bool fileExistCheck(const std::string& name); static void readTextFile(std::string file); - static std::string printTextOutput(std::string file); + //static std::string printTextOutput(std::string file); + static void printTextOutput(std::string file); }; diff --git a/util/text_functions.cpp b/util/text_functions.cpp new file mode 100644 index 0000000..979cd5b --- /dev/null +++ b/util/text_functions.cpp @@ -0,0 +1,16 @@ +#include "imgui.h" +#include "imgui_impl_dx9.h" + +#if _WIN32 +#include "imgui_impl_win32.h" +#include +#include "../util/keystates.h" +#endif + +// My code +#include "text_functions.h" + +void TextFunctions::SetBulletText(const char* text) +{ + ImGui::BulletText(text); +} \ No newline at end of file diff --git a/util/text_functions.h b/util/text_functions.h new file mode 100644 index 0000000..3e6f25d --- /dev/null +++ b/util/text_functions.h @@ -0,0 +1,5 @@ +#pragma once +class TextFunctions { +public: + static void SetBulletText(const char* text); +};