From 22deb5a7ad3e892bbf08e35d0359aca2fcf84216 Mon Sep 17 00:00:00 2001 From: "Grayson Riffe (Laptop)" Date: Thu, 19 Aug 2021 16:08:26 -0500 Subject: [PATCH] Added readFile and writeFile utility functions and changeConfig member function --- Game/Game.vcxproj.user | 16 +++++++ NothinFancy/src/Application.cpp | 67 ++++++++++++++++++++------- NothinFancy/src/Utility.cpp | 25 +++++++++- NothinFancy/src/include/Application.h | 4 +- NothinFancy/src/include/Utility.h | 4 ++ notes.txt | 7 ++- 6 files changed, 99 insertions(+), 24 deletions(-) diff --git a/Game/Game.vcxproj.user b/Game/Game.vcxproj.user index 966b4ff..9eb418d 100644 --- a/Game/Game.vcxproj.user +++ b/Game/Game.vcxproj.user @@ -3,4 +3,20 @@ true + + $(OutDir) + WindowsLocalDebugger + + + $(OutDir) + WindowsLocalDebugger + + + $(OutDir) + WindowsLocalDebugger + + + $(OutDir) + WindowsLocalDebugger + \ No newline at end of file diff --git a/NothinFancy/src/Application.cpp b/NothinFancy/src/Application.cpp index f2f28fa..a67505b 100644 --- a/NothinFancy/src/Application.cpp +++ b/NothinFancy/src/Application.cpp @@ -18,8 +18,10 @@ namespace nf { m_hInst = GetModuleHandle(NULL); registerWindowClass(); RECT windowSize = getWindowRect(); - - m_window = CreateWindowEx(NULL, m_wclassName, toWide(m_currentConfig.title), WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, 1280, windowSize.bottom, NULL, NULL, m_hInst, NULL); + int x = 0; + int y = 0; + calculateNewWindowPos(x, y); + m_window = CreateWindowEx(NULL, m_wclassName, toWide(m_currentConfig.title), WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX, x, y, windowSize.right, windowSize.bottom, NULL, NULL, m_hInst, NULL); m_defaultWindowStyle = GetWindowLong(m_window, GWL_STYLE); SetProp(m_window, L"App", this); if (m_currentConfig.fullscreen) toggleFullscreen(); @@ -58,17 +60,6 @@ namespace nf { } } - void Application::changeState(const char* stateName) { - if (m_states.find(stateName) != m_states.end()) { - m_currentState->onExit(); - m_currentState = m_states[stateName]; - m_currentState->onEnter(this); - } - else { - Error(("State \"" + (std::string)stateName + (std::string)"\" doesn't exist!").c_str()); - } - } - void Application::run() { addIntroState(); showWindow(true); @@ -95,6 +86,31 @@ namespace nf { ShowWindow(m_window, SW_HIDE); } + void Application::changeState(const char* stateName) { + if (m_states.find(stateName) != m_states.end()) { + m_currentState->onExit(); + m_currentState = m_states[stateName]; + m_currentState->onEnter(this); + } + else { + Error(("State \"" + (std::string)stateName + (std::string)"\" doesn't exist!").c_str()); + } + } + + void Application::changeConfig(const Config& in) { + SetWindowText(m_window, toWide(in.title)); + if (in.fullscreen != m_currentConfig.fullscreen) { + m_currentConfig = in; + toggleFullscreen(); + } + if (m_currentConfig.fullscreen) + return; + m_currentConfig = in; + int x = 0, y = 0; + calculateNewWindowPos(x, y); + SetWindowPos(m_window, HWND_TOP, x, y, in.width, in.height, SWP_NOOWNERZORDER | SWP_FRAMECHANGED); + } + const Config& Application::getConfig() const { return m_currentConfig; } @@ -175,7 +191,7 @@ namespace nf { else { SetWindowLong(m_window, GWL_STYLE, m_defaultWindowStyle); SetWindowPlacement(m_window, &m_wndPlacement); - SetWindowPos(m_window, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED); + SetWindowPos(m_window, NULL, 0, 0, m_currentConfig.width, m_currentConfig.height, SWP_NOMOVE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED); } } @@ -191,19 +207,29 @@ namespace nf { return temp; } + void Application::calculateNewWindowPos(int& x, int& y) { + MONITORINFO mi = { sizeof(mi) }; + GetMonitorInfo(MonitorFromWindow(m_window, MONITOR_DEFAULTTOPRIMARY), &mi); + int monX = (mi.rcWork.right - mi.rcWork.left) / 2; + int monY = (mi.rcWork.bottom - mi.rcWork.top) / 2; + + x = monX - (m_currentConfig.width / 2); + y = monY - (m_currentConfig.height / 2); + } + LRESULT CALLBACK Application::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { Application* app = (Application*)GetProp(hWnd, L"App"); - //TODO: Dragging blocks thread switch (uMsg) { case WM_CREATE: { return 0; } case WM_SYSKEYDOWN: { if (GetKeyState(VK_RETURN) & 0x8000) { + app->m_currentConfig.fullscreen = !app->m_currentConfig.fullscreen; app->toggleFullscreen(); return 0; } - return 0; + break; } case WM_MENUCHAR: { return MNC_CLOSE << 16; @@ -253,7 +279,8 @@ namespace nf { return 0; } } - return DefWindowProc(hWnd, uMsg, wParam, lParam);//TODO: Fill out events + return DefWindowProc(hWnd, uMsg, wParam, lParam); + //TODO: Fill out events } void Application::createOpenGLContext() { @@ -303,6 +330,10 @@ namespace nf { Application::~Application() { Log("Exiting NF application"); - //TODO: Iterate through m_activeStates and m_states and exit and unload them + + for (std::pair state : m_states) { + IGamestate* curr = state.second; + delete curr; + } } } diff --git a/NothinFancy/src/Utility.cpp b/NothinFancy/src/Utility.cpp index 10205c0..7b0656c 100644 --- a/NothinFancy/src/Utility.cpp +++ b/NothinFancy/src/Utility.cpp @@ -1,4 +1,3 @@ -//TODO: Debug logger //TODO: File IO functions #include @@ -48,6 +47,30 @@ namespace nf { MultiByteToWideChar(CP_ACP, NULL, in, -1, out, length); return out; } + + bool writeFile(const char* filename, const std::string& in) { + std::ofstream out; + out.open(filename); + if (!out) { + Error(("File \"" + (std::string)filename + (std::string)"\" could not be written!").c_str()); + return false; + } + out << in; + out.close(); + return true; + } + + std::string readFile(const char* filename) { + std::ifstream in; + in.open(filename); + if (!in) { + Error("Cannot find file"); + return NULL; + } + std::stringstream ss; + ss << in.rdbuf(); + return ss.str(); + } } //Nvidia Optimius support diff --git a/NothinFancy/src/include/Application.h b/NothinFancy/src/include/Application.h index d337a80..dd41135 100644 --- a/NothinFancy/src/include/Application.h +++ b/NothinFancy/src/include/Application.h @@ -19,9 +19,10 @@ namespace nf { void setWindowCursor(HCURSOR hCursor); void addState(IGamestate* state, const char* stateName); void addDefaultState(const char* stateName); - void changeState(const char* stateName); void run(); void showWindow(bool show); + void changeState(const char* stateName); + void changeConfig(const Config& in); const Config& getConfig() const; int getFPS() const; bool isInput(unsigned int code); @@ -33,6 +34,7 @@ namespace nf { void registerWindowClass(); void toggleFullscreen(); RECT getWindowRect() const; + void calculateNewWindowPos(int& x, int& y); static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); diff --git a/NothinFancy/src/include/Utility.h b/NothinFancy/src/include/Utility.h index 9f4ef5a..7ea7982 100644 --- a/NothinFancy/src/include/Utility.h +++ b/NothinFancy/src/include/Utility.h @@ -3,6 +3,8 @@ #include #include #include +#include +#include namespace nf { #ifdef _DEBUG @@ -39,4 +41,6 @@ std::exit(-1) #endif const wchar_t* toWide(const char* in); + bool writeFile(const char* filename, const std::string& in); + std::string readFile(const char* filename); } \ No newline at end of file diff --git a/notes.txt b/notes.txt index ab8b712..f1d4d84 100644 --- a/notes.txt +++ b/notes.txt @@ -17,11 +17,10 @@ Remember to use tasks (//TODO: ) *Namespaced *Debug and log system *NatVis -Video options +Config changing *Alt-Enter -File IO functions -Options file (Executable directory) -Input +*File IO functions +Input (mouse position) Audio *Game states Text rendering