Commit 80fa0592 authored by Clark Lin's avatar Clark Lin
Browse files

add exception handling

parent fd34c5a5
# Exclude .vs folder
.vs/
# Exclude x64 folder
x64/

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33122.133
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "StudyDx", "StudyDx\StudyDx.vcxproj", "{6821842A-19B3-46B6-860F-076A856A77F9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6821842A-19B3-46B6-860F-076A856A77F9}.Debug|x64.ActiveCfg = Debug|x64
{6821842A-19B3-46B6-860F-076A856A77F9}.Debug|x64.Build.0 = Debug|x64
{6821842A-19B3-46B6-860F-076A856A77F9}.Debug|x86.ActiveCfg = Debug|Win32
{6821842A-19B3-46B6-860F-076A856A77F9}.Debug|x86.Build.0 = Debug|Win32
{6821842A-19B3-46B6-860F-076A856A77F9}.Release|x64.ActiveCfg = Release|x64
{6821842A-19B3-46B6-860F-076A856A77F9}.Release|x64.Build.0 = Release|x64
{6821842A-19B3-46B6-860F-076A856A77F9}.Release|x86.ActiveCfg = Release|Win32
{6821842A-19B3-46B6-860F-076A856A77F9}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B234F6BD-3426-4A7F-8570-EA7292475F6F}
EndGlobalSection
EndGlobal
/******************************************************************************************
* Chili Direct3D Engine *
* Copyright 2018 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of Chili Direct3D Engine. *
* *
* Chili Direct3D Engine is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili Direct3D Engine is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili Direct3D Engine. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#include "ChiliException.h"
#include <sstream>
ChiliException::ChiliException( int line,const char* file ) noexcept
:
line( line ),
file( file )
{}
const char* ChiliException::what() const noexcept
{
std::ostringstream oss;
oss << GetType() << std::endl
<< GetOriginString();
whatBuffer = oss.str();
return whatBuffer.c_str();
}
const char* ChiliException::GetType() const noexcept
{
return "Chili Exception";
}
int ChiliException::GetLine() const noexcept
{
return line;
}
const std::string& ChiliException::GetFile() const noexcept
{
return file;
}
std::string ChiliException::GetOriginString() const noexcept
{
std::ostringstream oss;
oss << "[File] " << file << std::endl
<< "[Line] " << line;
return oss.str();
}
\ No newline at end of file
/******************************************************************************************
* Chili Direct3D Engine *
* Copyright 2018 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of Chili Direct3D Engine. *
* *
* Chili Direct3D Engine is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili Direct3D Engine is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili Direct3D Engine. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#pragma once
#include <exception>
#include <string>
class ChiliException : public std::exception
{
public:
ChiliException(int line, const char* file) noexcept;
const char* what() const noexcept override;
virtual const char* GetType() const noexcept;
int GetLine() const noexcept;
const std::string& GetFile() const noexcept;
std::string GetOriginString() const noexcept;
private:
int line;
std::string file;
protected:
mutable std::string whatBuffer;
};
/******************************************************************************************
* Chili Direct3D Engine *
* Copyright 2018 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of Chili Direct3D Engine. *
* *
* Chili Direct3D Engine is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili Direct3D Engine is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili Direct3D Engine. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#pragma once
// target Windows 7 or later
#define _WIN32_WINNT 0x0601
#include <sdkddkver.h>
// The following #defines disable a bunch of unused windows stuff. If you
// get weird errors when trying to do some windows stuff, try removing some
// (or all) of these defines (it will increase build time though).
#define WIN32_LEAN_AND_MEAN
#define NOGDICAPMASKS
#define NOSYSMETRICS
#define NOMENUS
#define NOICONS
#define NOSYSCOMMANDS
#define NORASTEROPS
#define OEMRESOURCE
#define NOATOM
#define NOCLIPBOARD
#define NOCOLOR
#define NOCTLMGR
#define NODRAWTEXT
#define NOKERNEL
#define NONLS
#define NOMEMMGR
#define NOMETAFILE
#define NOMINMAX
#define NOOPENFILE
#define NOSCROLL
#define NOSERVICE
#define NOSOUND
#define NOTEXTMETRIC
#define NOWH
#define NOCOMM
#define NOKANJI
#define NOHELP
#define NOPROFILER
#define NODEFERWINDOWPOS
#define NOMCX
#define NORPC
#define NOPROXYSTUB
#define NOIMAGE
#define NOTAPE
#define STRICT
#include <Windows.h>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{6821842a-19b3-46b6-860f-076a856a77f9}</ProjectGuid>
<RootNamespace>StudyDx</RootNamespace>
<WindowsTargetPlatformVersion>10.0.22000.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<FloatingPointModel>Fast</FloatingPointModel>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<FloatingPointModel>Fast</FloatingPointModel>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="ChiliException.cpp" />
<ClCompile Include="Window.cpp" />
<ClCompile Include="WindowsMessageMap.cpp" />
<ClCompile Include="WinMain.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="ChiliException.h" />
<ClInclude Include="ChiliWin.h" />
<ClInclude Include="Window.h" />
<ClInclude Include="WindowsMessageMap.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="WinMain.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ChiliException.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Window.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="WindowsMessageMap.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ChiliException.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ChiliWin.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Window.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="WindowsMessageMap.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>
\ No newline at end of file
/******************************************************************************************
* Chili Direct3D Engine *
* Copyright 2018 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of Chili Direct3D Engine. *
* *
* Chili Direct3D Engine is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili Direct3D Engine is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili Direct3D Engine. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#include "Window.h"
int CALLBACK WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
try
{
Window wnd(800, 300, "Donkey Fart Box");
MSG msg;
BOOL gResult;
while ((gResult = GetMessage(&msg, nullptr, 0, 0)) > 0)
{
// TranslateMessage will post auxilliary WM_CHAR messages from key msgs
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// check if GetMessage call itself borked
if (gResult == -1)
{
return -1;
}
// wParam here is the value passed to PostQuitMessage
return msg.wParam;
}
catch (const ChiliException& e)
{
MessageBox(nullptr, e.what(), e.GetType(), MB_OK | MB_ICONEXCLAMATION);
}
catch (const std::exception& e)
{
MessageBox(nullptr, e.what(), "Standard Exception", MB_OK | MB_ICONEXCLAMATION);
}
catch (...)
{
MessageBox(nullptr, "No details available", "Unknown Exception", MB_OK | MB_ICONEXCLAMATION);
}
return -1;
}
\ No newline at end of file
/******************************************************************************************
* Chili Direct3D Engine *
* Copyright 2018 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of Chili Direct3D Engine. *
* *
* Chili Direct3D Engine is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili Direct3D Engine is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili Direct3D Engine. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#include "Window.h"
#include <sstream>
// Window Class Stuff
Window::WindowClass Window::WindowClass::wndClass;
Window::WindowClass::WindowClass() noexcept
:
hInst( GetModuleHandle( nullptr ) )
{
WNDCLASSEX wc = { 0 };
wc.cbSize = sizeof( wc );
wc.style = CS_OWNDC;
wc.lpfnWndProc = HandleMsgSetup;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = GetInstance();
wc.hIcon = nullptr;
wc.hCursor = nullptr;
wc.hbrBackground = nullptr;
wc.lpszMenuName = nullptr;
wc.lpszClassName = GetName();
wc.hIconSm = nullptr;
RegisterClassEx( &wc );
}
Window::WindowClass::~WindowClass()
{
UnregisterClass( wndClassName,GetInstance() );
}
const char* Window::WindowClass::GetName() noexcept
{
return wndClassName;
}
HINSTANCE Window::WindowClass::GetInstance() noexcept
{
return wndClass.hInst;
}
// Window Stuff
Window::Window( int width,int height,const char* name )
{
// calculate window size based on desired client region size
RECT wr;
wr.left = 100;
wr.right = width + wr.left;
wr.top = 100;
wr.bottom = height + wr.top;
if (FAILED(AdjustWindowRect(&wr, WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU, FALSE)))
{
throw CHWND_LAST_EXCEPT();
};
// create window & get hWnd
hWnd = CreateWindow(
WindowClass::GetName(),name,
WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,
CW_USEDEFAULT,CW_USEDEFAULT,wr.right - wr.left,wr.bottom - wr.top,
nullptr,nullptr,WindowClass::GetInstance(),this // this 用来将该Window的实例和WinAPI连接起来
);
// check for error
if (hWnd == nullptr)
{
throw CHWND_LAST_EXCEPT();
}
// show window
ShowWindow( hWnd,SW_SHOWDEFAULT );
}
Window::~Window()
{
DestroyWindow( hWnd );
}
LRESULT CALLBACK Window::HandleMsgSetup( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept
{
// use create parameter passed in from CreateWindow() to store window class pointer at WinAPI side
if( msg == WM_NCCREATE )
{
// extract ptr to window class from creation data
// 使用CREATESTRUCT函数的第一个参数lpCreateParams,通过reinterpret_cast,获取到CREATESTRUCT函数的指针
// lpCreateParams的指针和CREATESTRUCT的指针指向同一个物理地址,只是寻址对象不同
const CREATESTRUCTW* const pCreate = reinterpret_cast<CREATESTRUCTW*>(lParam);
// 取得指向Window的指针
Window* const pWnd = static_cast<Window*>(pCreate->lpCreateParams);
// 将Window类,存储到WinAPI
// set WinAPI-managed user data to store ptr to window class
SetWindowLongPtr( hWnd,GWLP_USERDATA,reinterpret_cast<LONG_PTR>(pWnd) );
// set message proc to normal (non-setup) handler now that setup is finished
SetWindowLongPtr( hWnd,GWLP_WNDPROC,reinterpret_cast<LONG_PTR>(&Window::HandleMsgThunk) );
// forward message to window class handler
return pWnd->HandleMsg( hWnd,msg,wParam,lParam );
}
// if we get a message before the WM_NCCREATE message, handle with default handler
return DefWindowProc( hWnd,msg,wParam,lParam );
}
LRESULT CALLBACK Window::HandleMsgThunk( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept
{
// retrieve ptr to window class
Window* const pWnd = reinterpret_cast<Window*>(GetWindowLongPtr( hWnd,GWLP_USERDATA ));
// forward message to window class handler
return pWnd->HandleMsg( hWnd,msg,wParam,lParam );
}
LRESULT Window::HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept
{
switch( msg )
{
// we don't want the DefProc to handle this message because
// we want our destructor to destroy the window, so return 0 instead of break
case WM_CLOSE:
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd,msg,wParam,lParam );
}
// Window Exception Stuff
Window::Exception::Exception(int line, const char* file, HRESULT hr) noexcept
:
ChiliException(line, file),
hr(hr)
{}
const char* Window::Exception::what() const noexcept
{
std::ostringstream oss;
oss << GetType() << std::endl
<< "[Error Code] " << GetErrorCode() << std::endl
<< "[Description] " << GetErrorString() << std::endl
<< GetOriginString();
whatBuffer = oss.str();
return whatBuffer.c_str();
}
const char* Window::Exception::GetType() const noexcept
{
return "Chili Window Exception";
}
std::string Window::Exception::TranslateErrorCode(HRESULT hr) noexcept
{
char* pMsgBuf = nullptr;
// windows will allocate memory for err string and make our pointer point to it
DWORD nMsgLen = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, hr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPSTR>(&pMsgBuf), 0, nullptr
);
// 0 string length returned indicates a failure
if (nMsgLen == 0)
{
return "Unidentified error code";
}
// copy error string from windows-allocated buffer to std::string
std::string errorString = pMsgBuf;
// free windows buffer
LocalFree(pMsgBuf);
return errorString;
}
HRESULT Window::Exception::GetErrorCode() const noexcept
{
return hr;
}
std::string Window::Exception::GetErrorString() const noexcept
{
return TranslateErrorCode(hr);
}
/******************************************************************************************
* Chili Direct3D Engine *
* Copyright 2018 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of Chili Direct3D Engine. *
* *
* Chili Direct3D Engine is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili Direct3D Engine is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili Direct3D Engine. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#pragma once
#include "ChiliWin.h"
#include "ChiliException.h"
class Window
{
public:
class Exception : public ChiliException
{
public:
Exception(int line, const char* file, HRESULT hr) noexcept;
const char* what() const noexcept override;
virtual const char* GetType() const noexcept;
static std::string TranslateErrorCode(HRESULT hr) noexcept;
HRESULT GetErrorCode() const noexcept;
std::string GetErrorString() const noexcept;
private:
HRESULT hr;
};
private:
// singleton manages registration/cleanup of window class
class WindowClass
{
public:
static const char* GetName() noexcept;
static HINSTANCE GetInstance() noexcept;
private:
WindowClass() noexcept;
~WindowClass();
WindowClass(const WindowClass&) = delete;
WindowClass& operator=(const WindowClass&) = delete;
static constexpr const char* wndClassName = "Chili Direct3D Engine Window";
static WindowClass wndClass;
HINSTANCE hInst;
};
public:
Window(int width, int height, const char* name);
~Window();
Window(const Window&) = delete;
Window& operator=(const Window&) = delete;
private:
static LRESULT CALLBACK HandleMsgSetup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept;
static LRESULT CALLBACK HandleMsgThunk(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept;
LRESULT HandleMsg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept;
private:
int width;
int height;
HWND hWnd;
};
// error exception helper macro
#define CHWND_EXCEPT( hr ) Window::Exception( __LINE__,__FILE__,hr )
#define CHWND_LAST_EXCEPT() Window::Exception( __LINE__,__FILE__,GetLastError() )
\ No newline at end of file
/******************************************************************************************
* Chili Direct3D Engine *
* Copyright 2018 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of Chili Direct3D Engine. *
* *
* Chili Direct3D Engine is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili Direct3D Engine is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili Direct3D Engine. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#include "WindowsMessageMap.h"
#include <Windows.h>
#include <string>
#include <sstream>
#include <iomanip>
// secret messages
#define WM_UAHDESTROYWINDOW 0x0090
#define WM_UAHDRAWMENU 0x0091
#define WM_UAHDRAWMENUITEM 0x0092
#define WM_UAHINITMENU 0x0093
#define WM_UAHMEASUREMENUITEM 0x0094
#define WM_UAHNCPAINTMENUPOPUP 0x0095
#define REGISTER_MESSAGE(msg){msg,#msg}
WindowsMessageMap::WindowsMessageMap()
:
map({
REGISTER_MESSAGE(WM_CREATE),
REGISTER_MESSAGE(WM_DESTROY),
REGISTER_MESSAGE(WM_MOVE),
REGISTER_MESSAGE(WM_SIZE),
REGISTER_MESSAGE(WM_ACTIVATE),
REGISTER_MESSAGE(WM_SETFOCUS),
REGISTER_MESSAGE(WM_KILLFOCUS),
REGISTER_MESSAGE(WM_ENABLE),
REGISTER_MESSAGE(WM_SETREDRAW),
REGISTER_MESSAGE(WM_SETTEXT),
REGISTER_MESSAGE(WM_GETTEXT),
REGISTER_MESSAGE(WM_GETTEXTLENGTH),
REGISTER_MESSAGE(WM_PAINT),
REGISTER_MESSAGE(WM_CLOSE),
REGISTER_MESSAGE(WM_QUERYENDSESSION),
REGISTER_MESSAGE(WM_QUIT),
REGISTER_MESSAGE(WM_QUERYOPEN),
REGISTER_MESSAGE(WM_ERASEBKGND),
REGISTER_MESSAGE(WM_SYSCOLORCHANGE),
REGISTER_MESSAGE(WM_ENDSESSION),
REGISTER_MESSAGE(WM_SHOWWINDOW),
REGISTER_MESSAGE(WM_CTLCOLORMSGBOX),
REGISTER_MESSAGE(WM_CTLCOLOREDIT),
REGISTER_MESSAGE(WM_CTLCOLORLISTBOX),
REGISTER_MESSAGE(WM_CTLCOLORBTN),
REGISTER_MESSAGE(WM_CTLCOLORDLG),
REGISTER_MESSAGE(WM_CTLCOLORSCROLLBAR),
REGISTER_MESSAGE(WM_CTLCOLORSTATIC),
REGISTER_MESSAGE(WM_WININICHANGE),
REGISTER_MESSAGE(WM_SETTINGCHANGE),
REGISTER_MESSAGE(WM_DEVMODECHANGE),
REGISTER_MESSAGE(WM_ACTIVATEAPP),
REGISTER_MESSAGE(WM_FONTCHANGE),
REGISTER_MESSAGE(WM_TIMECHANGE),
REGISTER_MESSAGE(WM_CANCELMODE),
REGISTER_MESSAGE(WM_SETCURSOR),
REGISTER_MESSAGE(WM_MOUSEACTIVATE),
REGISTER_MESSAGE(WM_CHILDACTIVATE),
REGISTER_MESSAGE(WM_QUEUESYNC),
REGISTER_MESSAGE(WM_GETMINMAXINFO),
REGISTER_MESSAGE(WM_ICONERASEBKGND),
REGISTER_MESSAGE(WM_NEXTDLGCTL),
REGISTER_MESSAGE(WM_SPOOLERSTATUS),
REGISTER_MESSAGE(WM_DRAWITEM),
REGISTER_MESSAGE(WM_MEASUREITEM),
REGISTER_MESSAGE(WM_DELETEITEM),
REGISTER_MESSAGE(WM_VKEYTOITEM),
REGISTER_MESSAGE(WM_CHARTOITEM),
REGISTER_MESSAGE(WM_SETFONT),
REGISTER_MESSAGE(WM_GETFONT),
REGISTER_MESSAGE(WM_QUERYDRAGICON),
REGISTER_MESSAGE(WM_COMPAREITEM),
REGISTER_MESSAGE(WM_COMPACTING),
REGISTER_MESSAGE(WM_NCCREATE),
REGISTER_MESSAGE(WM_NCDESTROY),
REGISTER_MESSAGE(WM_NCCALCSIZE),
REGISTER_MESSAGE(WM_NCHITTEST),
REGISTER_MESSAGE(WM_NCPAINT),
REGISTER_MESSAGE(WM_NCACTIVATE),
REGISTER_MESSAGE(WM_GETDLGCODE),
REGISTER_MESSAGE(WM_NCMOUSEMOVE),
REGISTER_MESSAGE(WM_NCLBUTTONDOWN),
REGISTER_MESSAGE(WM_NCLBUTTONUP),
REGISTER_MESSAGE(WM_NCLBUTTONDBLCLK),
REGISTER_MESSAGE(WM_NCRBUTTONDOWN),
REGISTER_MESSAGE(WM_NCRBUTTONUP),
REGISTER_MESSAGE(WM_NCRBUTTONDBLCLK),
REGISTER_MESSAGE(WM_NCMBUTTONDOWN),
REGISTER_MESSAGE(WM_NCMBUTTONUP),
REGISTER_MESSAGE(WM_NCMBUTTONDBLCLK),
REGISTER_MESSAGE(WM_KEYDOWN),
REGISTER_MESSAGE(WM_KEYUP),
REGISTER_MESSAGE(WM_CHAR),
REGISTER_MESSAGE(WM_DEADCHAR),
REGISTER_MESSAGE(WM_SYSKEYDOWN),
REGISTER_MESSAGE(WM_SYSKEYUP),
REGISTER_MESSAGE(WM_SYSCHAR),
REGISTER_MESSAGE(WM_SYSDEADCHAR),
REGISTER_MESSAGE(WM_KEYLAST),
REGISTER_MESSAGE(WM_INITDIALOG),
REGISTER_MESSAGE(WM_COMMAND),
REGISTER_MESSAGE(WM_SYSCOMMAND),
REGISTER_MESSAGE(WM_TIMER),
REGISTER_MESSAGE(WM_HSCROLL),
REGISTER_MESSAGE(WM_VSCROLL),
REGISTER_MESSAGE(WM_INITMENU),
REGISTER_MESSAGE(WM_INITMENUPOPUP),
REGISTER_MESSAGE(WM_MENUSELECT),
REGISTER_MESSAGE(WM_MENUCHAR),
REGISTER_MESSAGE(WM_ENTERIDLE),
REGISTER_MESSAGE(WM_MOUSEWHEEL),
REGISTER_MESSAGE(WM_MOUSEMOVE),
REGISTER_MESSAGE(WM_LBUTTONDOWN),
REGISTER_MESSAGE(WM_LBUTTONUP),
REGISTER_MESSAGE(WM_LBUTTONDBLCLK),
REGISTER_MESSAGE(WM_RBUTTONDOWN),
REGISTER_MESSAGE(WM_RBUTTONUP),
REGISTER_MESSAGE(WM_RBUTTONDBLCLK),
REGISTER_MESSAGE(WM_MBUTTONDOWN),
REGISTER_MESSAGE(WM_MBUTTONUP),
REGISTER_MESSAGE(WM_MBUTTONDBLCLK),
REGISTER_MESSAGE(WM_PARENTNOTIFY),
REGISTER_MESSAGE(WM_MDICREATE),
REGISTER_MESSAGE(WM_MDIDESTROY),
REGISTER_MESSAGE(WM_MDIACTIVATE),
REGISTER_MESSAGE(WM_MDIRESTORE),
REGISTER_MESSAGE(WM_MDINEXT),
REGISTER_MESSAGE(WM_MDIMAXIMIZE),
REGISTER_MESSAGE(WM_MDITILE),
REGISTER_MESSAGE(WM_MDICASCADE),
REGISTER_MESSAGE(WM_MDIICONARRANGE),
REGISTER_MESSAGE(WM_MDIGETACTIVE),
REGISTER_MESSAGE(WM_MDISETMENU),
REGISTER_MESSAGE(WM_CUT),
REGISTER_MESSAGE(WM_COPYDATA),
REGISTER_MESSAGE(WM_COPY),
REGISTER_MESSAGE(WM_PASTE),
REGISTER_MESSAGE(WM_CLEAR),
REGISTER_MESSAGE(WM_UNDO),
REGISTER_MESSAGE(WM_RENDERFORMAT),
REGISTER_MESSAGE(WM_RENDERALLFORMATS),
REGISTER_MESSAGE(WM_DESTROYCLIPBOARD),
REGISTER_MESSAGE(WM_DRAWCLIPBOARD),
REGISTER_MESSAGE(WM_PAINTCLIPBOARD),
REGISTER_MESSAGE(WM_VSCROLLCLIPBOARD),
REGISTER_MESSAGE(WM_SIZECLIPBOARD),
REGISTER_MESSAGE(WM_ASKCBFORMATNAME),
REGISTER_MESSAGE(WM_CHANGECBCHAIN),
REGISTER_MESSAGE(WM_HSCROLLCLIPBOARD),
REGISTER_MESSAGE(WM_QUERYNEWPALETTE),
REGISTER_MESSAGE(WM_PALETTEISCHANGING),
REGISTER_MESSAGE(WM_PALETTECHANGED),
REGISTER_MESSAGE(WM_DDE_INITIATE),
REGISTER_MESSAGE(WM_DDE_TERMINATE),
REGISTER_MESSAGE(WM_DDE_ADVISE),
REGISTER_MESSAGE(WM_DDE_UNADVISE),
REGISTER_MESSAGE(WM_DDE_ACK),
REGISTER_MESSAGE(WM_DDE_DATA),
REGISTER_MESSAGE(WM_DDE_REQUEST),
REGISTER_MESSAGE(WM_DDE_POKE),
REGISTER_MESSAGE(WM_DDE_EXECUTE),
REGISTER_MESSAGE(WM_DROPFILES),
REGISTER_MESSAGE(WM_POWER),
REGISTER_MESSAGE(WM_WINDOWPOSCHANGED),
REGISTER_MESSAGE(WM_WINDOWPOSCHANGING),
REGISTER_MESSAGE(WM_HELP),
REGISTER_MESSAGE(WM_NOTIFY),
REGISTER_MESSAGE(WM_CONTEXTMENU),
REGISTER_MESSAGE(WM_TCARD),
REGISTER_MESSAGE(WM_MDIREFRESHMENU),
REGISTER_MESSAGE(WM_MOVING),
REGISTER_MESSAGE(WM_STYLECHANGED),
REGISTER_MESSAGE(WM_STYLECHANGING),
REGISTER_MESSAGE(WM_SIZING),
REGISTER_MESSAGE(WM_SETHOTKEY),
REGISTER_MESSAGE(WM_PRINT),
REGISTER_MESSAGE(WM_PRINTCLIENT),
REGISTER_MESSAGE(WM_POWERBROADCAST),
REGISTER_MESSAGE(WM_HOTKEY),
REGISTER_MESSAGE(WM_GETICON),
REGISTER_MESSAGE(WM_EXITMENULOOP),
REGISTER_MESSAGE(WM_ENTERMENULOOP),
REGISTER_MESSAGE(WM_DISPLAYCHANGE),
REGISTER_MESSAGE(WM_STYLECHANGED),
REGISTER_MESSAGE(WM_STYLECHANGING),
REGISTER_MESSAGE(WM_GETICON),
REGISTER_MESSAGE(WM_SETICON),
REGISTER_MESSAGE(WM_SIZING),
REGISTER_MESSAGE(WM_MOVING),
REGISTER_MESSAGE(WM_CAPTURECHANGED),
REGISTER_MESSAGE(WM_DEVICECHANGE),
REGISTER_MESSAGE(WM_PRINT),
REGISTER_MESSAGE(WM_PRINTCLIENT),
REGISTER_MESSAGE(WM_IME_SETCONTEXT),
REGISTER_MESSAGE(WM_IME_NOTIFY),
REGISTER_MESSAGE(WM_NCMOUSELEAVE),
REGISTER_MESSAGE(WM_EXITSIZEMOVE),
REGISTER_MESSAGE(WM_UAHDESTROYWINDOW),
REGISTER_MESSAGE(WM_DWMNCRENDERINGCHANGED),
REGISTER_MESSAGE(WM_ENTERSIZEMOVE),
})
{}
std::string WindowsMessageMap::operator()(DWORD msg, LPARAM lp, WPARAM wp) const
{
constexpr int firstColWidth = 25;
const auto i = map.find(msg);
std::ostringstream oss;
if (i != map.end())
{
oss << std::left << std::setw(firstColWidth) << i->second << std::right;
}
else
{
std::ostringstream padss;
padss << "Unknown message: 0x" << std::hex << msg;
oss << std::left << std::setw(firstColWidth) << padss.str() << std::right;
}
oss << " LP: 0x" << std::hex << std::setfill('0') << std::setw(8) << lp;
oss << " WP: 0x" << std::hex << std::setfill('0') << std::setw(8) << wp << std::endl;
return oss.str();
}
\ No newline at end of file
/******************************************************************************************
* Chili Direct3D Engine *
* Copyright 2018 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of Chili Direct3D Engine. *
* *
* Chili Direct3D Engine is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili Direct3D Engine is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili Direct3D Engine. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#pragma once
#include <unordered_map>
#include <Windows.h>
#include <string>
class WindowsMessageMap
{
public:
WindowsMessageMap();
std::string operator()(DWORD msg, LPARAM lp, WPARAM wp) const;
private:
std::unordered_map<DWORD, std::string> map;
};
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment