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.
This commit is contained in:
kelson8 2024-06-18 13:13:26 -04:00
parent f3c5095964
commit eca8fe3c1d
13 changed files with 692 additions and 398 deletions

4
.gitignore vendored
View File

@ -1,3 +1,7 @@
# Added by kelson8
Main - Copy.cpp
old/**
# ---> C++
# Prerequisites
*.d

View File

@ -182,8 +182,8 @@
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>d3d9.lib;opengl32.lib;glfw3.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(ProjectDir)lib\GLFW\lib-vc2022;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>opengl32.lib;glfw3.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>.\lib\GLFW\lib-vc2022;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@ -213,6 +213,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="lib\ImGui\backends\imgui_impl_opengl3.cpp" />
<ClCompile Include="Main.cpp" />
<ClCompile Include="lib\ImGui\backends\imgui_impl_dx9.cpp" />
<ClCompile Include="lib\ImGui\backends\imgui_impl_win32.cpp" />
@ -221,12 +222,17 @@
<ClCompile Include="lib\ImGui\imgui_draw.cpp" />
<ClCompile Include="lib\ImGui\imgui_tables.cpp" />
<ClCompile Include="lib\ImGui\imgui_widgets.cpp" />
<ClCompile Include="menus\main_menu.cpp" />
<ClCompile Include="test\directx9_test.cpp" />
<ClCompile Include="test\opengl_test.cpp" />
<ClCompile Include="util\textFileFunctions.cpp" />
<ClCompile Include="util\keystates.cpp" />
<ClCompile Include="util\text_file_functions.cpp" />
<ClCompile Include="util\text_functions.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="lib\ImGui\backends\imgui_impl_dx9.h" />
<ClInclude Include="lib\ImGui\backends\imgui_impl_opengl3.h" />
<ClInclude Include="lib\ImGui\backends\imgui_impl_opengl3_loader.h" />
<ClInclude Include="lib\ImGui\backends\imgui_impl_win32.h" />
<ClInclude Include="lib\ImGui\imconfig.h" />
<ClInclude Include="lib\ImGui\imgui.h" />
@ -234,9 +240,16 @@
<ClInclude Include="lib\ImGui\imstb_rectpack.h" />
<ClInclude Include="lib\ImGui\imstb_textedit.h" />
<ClInclude Include="lib\ImGui\imstb_truetype.h" />
<ClInclude Include="menus\main_menu.h" />
<ClInclude Include="menus\pch.h" />
<ClInclude Include="test\directx9_test.h" />
<ClInclude Include="test\opengl_test.h" />
<ClInclude Include="util\textFileFunctions.h" />
<ClInclude Include="util\keystates.h" />
<ClInclude Include="util\text_file_functions.h" />
<ClInclude Include="util\text_functions.h" />
</ItemGroup>
<ItemGroup>
<None Include="lib\ImGui\backends\imgui_impl_metal.mm" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

307
menus/main_menu.cpp Normal file
View File

@ -0,0 +1,307 @@
#include "imgui.h"
#include "imgui_impl_dx9.h"
#if _WIN32
#include "imgui_impl_win32.h"
#include <Windows.h>
#include "../util/keystates.h"
#endif
#include <d3d9.h>
#include <tchar.h>
#include <string>
#include <iostream>
#include <fstream>
#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: <INVALID>");
//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);
//}
}

6
menus/main_menu.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
class MainMenu {
public:
static void MainMenuTest();
};

View File

@ -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 <Windows.h>
#include "../util/keystates.h"
#endif
#include <d3d9.h>
@ -38,7 +41,9 @@
#include <iostream>
#include <fstream>
#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
@ -86,11 +91,10 @@ 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()
@ -108,6 +112,7 @@ std::string testString1()
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);
@ -144,7 +149,7 @@ void DirectX9Test::directX9Test()
{
#ifdef _TEST
std::cout << testString1();
//std::cout << testString1();
#endif
// Create application window
@ -197,18 +202,19 @@ void DirectX9Test::directX9Test()
//IM_ASSERT(font != nullptr);
// Our state
bool show_demo_window = true;
//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;
//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 dark_mode = false;
//static bool define_test = false;
//static bool list_values = false;
while (!done)
{
@ -243,183 +249,14 @@ void DirectX9Test::directX9Test()
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
#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", &dark_mode))
{
if (!dark_mode)
if (ImGui::Begin("KCNet ImGui", nullptr, ImGuiWindowFlags_MenuBar))
{
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;
//}
// This works.
if (ImGui::IsMousePosValid())
ImGui::Text("Mouse pos: (%g, %g)", io.MousePos.x, io.MousePos.y);
else
ImGui::Text("Mouse pos: <INVALID>");
//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");
#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
MainMenu::MainMenuTest();
// Text file functions test menu
if (ImGui::CollapsingHeader("Text File Functions"))
@ -468,7 +305,7 @@ void DirectX9Test::directX9Test()
// Handle loss of D3D9 device
if (result == D3DERR_DEVICELOST && g_pd3dDevice->TestCooperativeLevel() == D3DERR_DEVICENOTRESET)
ResetDevice();
}
}
ImGui_ImplDX9_Shutdown();
ImGui_ImplWin32_Shutdown();

View File

@ -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;
};

View File

@ -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;

34
util/keystates.cpp Normal file
View File

@ -0,0 +1,34 @@
#include "keystates.h"
#include <string>
#include <iostream>
// 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 << "]" <<std::endl;
}
}
//static char cars()
//{
// return &cars;
//}

11
util/keystates.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#if _WIN32
#include <Windows.h>
#endif
class KeyStates {
public:
static char* charTest();
static void test();
};

View File

@ -1,7 +1,7 @@
#include <string>
#include <iostream>
#include <fstream>
#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;
@ -83,3 +86,48 @@ std::string TextFileFunctions::printTextOutput(std::string file)
std::cout << "File doesn't exist!";
}
}
// 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

View File

@ -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);
};

16
util/text_functions.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "imgui.h"
#include "imgui_impl_dx9.h"
#if _WIN32
#include "imgui_impl_win32.h"
#include <Windows.h>
#include "../util/keystates.h"
#endif
// My code
#include "text_functions.h"
void TextFunctions::SetBulletText(const char* text)
{
ImGui::BulletText(text);
}

5
util/text_functions.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
class TextFunctions {
public:
static void SetBulletText(const char* text);
};