From 8e4ed26755acc6f4bd02f0bbea6388b69bfc48eb Mon Sep 17 00:00:00 2001 From: "Grayson Riffe (Desktop)" Date: Sat, 14 Aug 2021 19:56:23 -0500 Subject: [PATCH] Window Procedure work and Alt-Enter fullscreen toggle --- Game/Game.rc | 60 ------------------------- Game/Game.vcxproj | 6 --- Game/Game.vcxproj.filters | 10 ----- Game/resource.h | 14 ------ Game/src/Game.cpp | 8 +++- NothinFancy/src/Application.cpp | 64 +++++++++++++++++++++++---- NothinFancy/src/include/Application.h | 6 ++- NothinFancy/src/include/Utility.h | 4 ++ 8 files changed, 71 insertions(+), 101 deletions(-) delete mode 100644 Game/Game.rc delete mode 100644 Game/resource.h diff --git a/Game/Game.rc b/Game/Game.rc deleted file mode 100644 index 3b3fdcb..0000000 --- a/Game/Game.rc +++ /dev/null @@ -1,60 +0,0 @@ -// Microsoft Visual C++ generated resource script. -// - -#include "resource.h" - -#define APSTUDIO_READONLY_SYMBOLS -///////////////////////////////////////////////////////////////////////////// -// -// Generated from the TEXTINCLUDE 2 resource. -// -#include "winres.h" - -///////////////////////////////////////////////////////////////////////////// -#undef APSTUDIO_READONLY_SYMBOLS - -///////////////////////////////////////////////////////////////////////////// -// English (United States) resources - -#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) -LANGUAGE 9, 1 - -#ifdef APSTUDIO_INVOKED -///////////////////////////////////////////////////////////////////////////// -// -// TEXTINCLUDE -// - -1 TEXTINCLUDE -BEGIN - "resource.h\0" -END - -2 TEXTINCLUDE -BEGIN - "#include ""winres.h""\r\n" - "\0" -END - -3 TEXTINCLUDE -BEGIN - "\r\n" - "\0" -END - -#endif // APSTUDIO_INVOKED - -#endif // English (United States) resources -///////////////////////////////////////////////////////////////////////////// - - - -#ifndef APSTUDIO_INVOKED -///////////////////////////////////////////////////////////////////////////// -// -// Generated from the TEXTINCLUDE 3 resource. -// - - -///////////////////////////////////////////////////////////////////////////// -#endif // not APSTUDIO_INVOKED diff --git a/Game/Game.vcxproj b/Game/Game.vcxproj index 4f00eae..ee2fb3d 100644 --- a/Game/Game.vcxproj +++ b/Game/Game.vcxproj @@ -166,12 +166,6 @@ - - - - - - diff --git a/Game/Game.vcxproj.filters b/Game/Game.vcxproj.filters index f4cf4bc..08a7b41 100644 --- a/Game/Game.vcxproj.filters +++ b/Game/Game.vcxproj.filters @@ -19,14 +19,4 @@ Source Files - - - Header Files - - - - - Resource Files - - \ No newline at end of file diff --git a/Game/resource.h b/Game/resource.h deleted file mode 100644 index 675e3f3..0000000 --- a/Game/resource.h +++ /dev/null @@ -1,14 +0,0 @@ -//{{NO_DEPENDENCIES}} -// Microsoft Visual C++ generated include file. -// Used by Game.rc - -// Next default values for new objects -// -#ifdef APSTUDIO_INVOKED -#ifndef APSTUDIO_READONLY_SYMBOLS -#define _APS_NEXT_RESOURCE_VALUE 101 -#define _APS_NEXT_COMMAND_VALUE 40001 -#define _APS_NEXT_CONTROL_VALUE 1001 -#define _APS_NEXT_SYMED_VALUE 101 -#endif -#endif diff --git a/Game/src/Game.cpp b/Game/src/Game.cpp index 7e40c2c..c9bb61c 100644 --- a/Game/src/Game.cpp +++ b/Game/src/Game.cpp @@ -4,11 +4,15 @@ using namespace nf; int main(int argc, char* argv[]) { + //TODO: Argument parser + Config conf = { 1280, 720, false, "Test"}; Application app(conf); - + //app.setWindowIcon(...); + // app.setWindowCursor(...); //Create game states, load some assets, launch loop that continually updates and renders the current state and switches states when appropriate. - //Show window + app.showWindow(true); + app.startLoop(); return 0; } \ No newline at end of file diff --git a/NothinFancy/src/Application.cpp b/NothinFancy/src/Application.cpp index cc15b27..960f184 100644 --- a/NothinFancy/src/Application.cpp +++ b/NothinFancy/src/Application.cpp @@ -1,14 +1,13 @@ #include "Application.h" #include "Utility.h" -#include -//TODO: delete this namespace nf { DEBUGINIT; Application::Application(Config& config) : m_currentConfig(config), - m_wndPlacement{ sizeof(m_wndPlacement) } + m_wndPlacement{ sizeof(m_wndPlacement) }, + m_running(false) { Log("Creating NF application"); Log("Width: " + std::to_string(m_currentConfig.width) + ", Height: " + std::to_string(m_currentConfig.height) + ", Fullscreen: " + std::to_string(m_currentConfig.fullscreen) + ", Title: " + m_currentConfig.title); @@ -18,12 +17,32 @@ namespace nf { 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); + SetProp(m_window, L"App", this); if(m_currentConfig.fullscreen) toggleFullscreen(); - showWindow(true); } - Config& Application::getConfig() { - return m_currentConfig; + void Application::setWindowIcon(HANDLE hIcon) { + SendMessage(m_window, WM_SETICON, ICON_SMALL, (LPARAM)hIcon); + SendMessage(m_window, WM_SETICON, ICON_SMALL, (LPARAM)hIcon); + } + + void Application::setWindowCursor(HCURSOR hCursor) { + SetClassLongPtr(m_window, GCLP_HCURSOR, (LONG_PTR)hCursor); + } + + void Application::startLoop() { + m_running = true; + MSG msg = { }; + while (m_running) { + //TODO: FPS and delta timing + while (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) { + TranslateMessage(&msg); + DispatchMessage(&msg); + if (msg.message == WM_QUIT) + m_running = false; + } + //TODO: Update and render current state + } } void Application::showWindow(bool show) { @@ -33,11 +52,16 @@ namespace nf { ShowWindow(m_window, SW_HIDE); } + Config& Application::getConfig() { + return m_currentConfig; + } + void Application::registerWindowClass() { m_wclassName = L"NFClass"; WNDCLASS wclass = { }; wclass.lpszClassName = m_wclassName; - //TODO: Add custom cursor and window icon at request of frontend using future resource format + wclass.hCursor = NULL; + wclass.hIcon = NULL; wclass.hInstance = m_hInst; wclass.lpfnWndProc = Application::WindowProc; RegisterClass(&wclass); @@ -72,8 +96,32 @@ namespace nf { } LRESULT CALLBACK Application::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { + Application* app = (Application*)GetProp(hWnd, L"App"); switch (uMsg) { - + case WM_CREATE: { + + return 0; + } + case WM_SYSKEYDOWN: { + if (GetKeyState(VK_RETURN) & 0x8000) { + app->toggleFullscreen(); + return 0; + } + break; + } + case WM_MENUCHAR: { + return MNC_CLOSE << 16; + } + case WM_CLOSE: { + //State onExit() in order + //unload anything else + DestroyWindow(hWnd); + return 0; + } + case WM_DESTROY: { + PostQuitMessage(0); + return 0; + } } return DefWindowProc(hWnd, uMsg, wParam, lParam);//TODO: Fill out events } diff --git a/NothinFancy/src/include/Application.h b/NothinFancy/src/include/Application.h index 9e08fe6..b0fc922 100644 --- a/NothinFancy/src/include/Application.h +++ b/NothinFancy/src/include/Application.h @@ -9,8 +9,11 @@ namespace nf { Application() = delete; Application(Application& other) = delete; - Config& getConfig(); + void setWindowIcon(HANDLE hIcon); + void setWindowCursor(HCURSOR hCursor); + void startLoop(); void showWindow(bool show); + Config& getConfig(); ~Application(); private: @@ -21,6 +24,7 @@ namespace nf { static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); Config m_currentConfig; + bool m_running; HINSTANCE m_hInst; LPCWSTR m_wclassName; HWND m_window; diff --git a/NothinFancy/src/include/Utility.h b/NothinFancy/src/include/Utility.h index 84b1213..ec66379 100644 --- a/NothinFancy/src/include/Utility.h +++ b/NothinFancy/src/include/Utility.h @@ -1,5 +1,6 @@ #pragma once #include +#include #include #include @@ -7,9 +8,12 @@ namespace nf { #ifdef _DEBUG #define __FILENAME__ strrchr(__FILE__, '\\') + 1 #define DEBUGINIT std::chrono::steady_clock::time_point Debug::m_initTime = std::chrono::high_resolution_clock::now(); +#define SleepS(x) std::this_thread::sleep_for(std::chrono::seconds(x)) +#define SleepMS(x) std::this_thread::sleep_for(std::chrono::milliseconds(x)) #define Log(x) Debug::LogImp(x) #define Error(x) Debug::ErrorImp(x,__FILENAME__, __LINE__);\ DebugBreak(); + class Debug { private: static std::chrono::steady_clock::time_point m_initTime;