Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Clark Lin
hw3d
Commits
3b2e1c50
Commit
3b2e1c50
authored
Dec 09, 2018
by
chili
Browse files
some basic windows error checks
parent
e5933fa6
Changes
2
Show whitespace changes
Inline
Side-by-side
hw3d/Window.cpp
View file @
3b2e1c50
...
@@ -61,7 +61,7 @@ HINSTANCE Window::WindowClass::GetInstance() noexcept
...
@@ -61,7 +61,7 @@ HINSTANCE Window::WindowClass::GetInstance() noexcept
// Window Stuff
// Window Stuff
Window
::
Window
(
int
width
,
int
height
,
const
char
*
name
)
noexcept
Window
::
Window
(
int
width
,
int
height
,
const
char
*
name
)
{
{
// calculate window size based on desired client region size
// calculate window size based on desired client region size
RECT
wr
;
RECT
wr
;
...
@@ -69,7 +69,10 @@ Window::Window( int width,int height,const char* name ) noexcept
...
@@ -69,7 +69,10 @@ Window::Window( int width,int height,const char* name ) noexcept
wr
.
right
=
width
+
wr
.
left
;
wr
.
right
=
width
+
wr
.
left
;
wr
.
top
=
100
;
wr
.
top
=
100
;
wr
.
bottom
=
height
+
wr
.
top
;
wr
.
bottom
=
height
+
wr
.
top
;
AdjustWindowRect
(
&
wr
,
WS_CAPTION
|
WS_MINIMIZEBOX
|
WS_SYSMENU
,
FALSE
);
if
(
FAILED
(
AdjustWindowRect
(
&
wr
,
WS_CAPTION
|
WS_MINIMIZEBOX
|
WS_SYSMENU
,
FALSE
)
)
)
{
throw
CHWND_LAST_EXCEPT
();
};
// create window & get hWnd
// create window & get hWnd
hWnd
=
CreateWindow
(
hWnd
=
CreateWindow
(
WindowClass
::
GetName
(),
name
,
WindowClass
::
GetName
(),
name
,
...
@@ -77,6 +80,11 @@ Window::Window( int width,int height,const char* name ) noexcept
...
@@ -77,6 +80,11 @@ Window::Window( int width,int height,const char* name ) noexcept
CW_USEDEFAULT
,
CW_USEDEFAULT
,
wr
.
right
-
wr
.
left
,
wr
.
bottom
-
wr
.
top
,
CW_USEDEFAULT
,
CW_USEDEFAULT
,
wr
.
right
-
wr
.
left
,
wr
.
bottom
-
wr
.
top
,
nullptr
,
nullptr
,
WindowClass
::
GetInstance
(),
this
nullptr
,
nullptr
,
WindowClass
::
GetInstance
(),
this
);
);
// check for error
if
(
hWnd
==
nullptr
)
{
throw
CHWND_LAST_EXCEPT
();
}
// show window
// show window
ShowWindow
(
hWnd
,
SW_SHOWDEFAULT
);
ShowWindow
(
hWnd
,
SW_SHOWDEFAULT
);
}
}
...
@@ -135,7 +143,7 @@ Window::Exception::Exception( int line,const char * file,HRESULT hr ) noexcept
...
@@ -135,7 +143,7 @@ Window::Exception::Exception( int line,const char * file,HRESULT hr ) noexcept
hr
(
hr
)
hr
(
hr
)
{}
{}
const
char
*
Window
::
Exception
::
what
()
const
noexcept
const
char
*
Window
::
Exception
::
what
()
const
noexcept
{
{
std
::
ostringstream
oss
;
std
::
ostringstream
oss
;
oss
<<
GetType
()
<<
std
::
endl
oss
<<
GetType
()
<<
std
::
endl
...
@@ -154,17 +162,21 @@ const char* Window::Exception::GetType() const noexcept
...
@@ -154,17 +162,21 @@ const char* Window::Exception::GetType() const noexcept
std
::
string
Window
::
Exception
::
TranslateErrorCode
(
HRESULT
hr
)
noexcept
std
::
string
Window
::
Exception
::
TranslateErrorCode
(
HRESULT
hr
)
noexcept
{
{
char
*
pMsgBuf
=
nullptr
;
char
*
pMsgBuf
=
nullptr
;
// windows will allocate memory for err string and make our pointer point to it
DWORD
nMsgLen
=
FormatMessage
(
DWORD
nMsgLen
=
FormatMessage
(
FORMAT_MESSAGE_ALLOCATE_BUFFER
|
FORMAT_MESSAGE_ALLOCATE_BUFFER
|
FORMAT_MESSAGE_FROM_SYSTEM
|
FORMAT_MESSAGE_IGNORE_INSERTS
,
FORMAT_MESSAGE_FROM_SYSTEM
|
FORMAT_MESSAGE_IGNORE_INSERTS
,
nullptr
,
hr
,
MAKELANGID
(
LANG_NEUTRAL
,
SUBLANG_DEFAULT
),
nullptr
,
hr
,
MAKELANGID
(
LANG_NEUTRAL
,
SUBLANG_DEFAULT
),
reinterpret_cast
<
LPSTR
>
(
&
pMsgBuf
),
0
,
nullptr
reinterpret_cast
<
LPSTR
>
(
&
pMsgBuf
),
0
,
nullptr
);
);
// 0 string length returned indicates a failure
if
(
nMsgLen
==
0
)
if
(
nMsgLen
==
0
)
{
{
return
"Unidentified error code"
;
return
"Unidentified error code"
;
}
}
// copy error string from windows-allocated buffer to std::string
std
::
string
errorString
=
pMsgBuf
;
std
::
string
errorString
=
pMsgBuf
;
// free windows buffer
LocalFree
(
pMsgBuf
);
LocalFree
(
pMsgBuf
);
return
errorString
;
return
errorString
;
}
}
...
...
hw3d/Window.h
View file @
3b2e1c50
...
@@ -54,7 +54,7 @@ private:
...
@@ -54,7 +54,7 @@ private:
HINSTANCE
hInst
;
HINSTANCE
hInst
;
};
};
public:
public:
Window
(
int
width
,
int
height
,
const
char
*
name
)
noexcept
;
Window
(
int
width
,
int
height
,
const
char
*
name
);
~
Window
();
~
Window
();
Window
(
const
Window
&
)
=
delete
;
Window
(
const
Window
&
)
=
delete
;
Window
&
operator
=
(
const
Window
&
)
=
delete
;
Window
&
operator
=
(
const
Window
&
)
=
delete
;
...
@@ -71,3 +71,4 @@ private:
...
@@ -71,3 +71,4 @@ private:
// error exception helper macro
// error exception helper macro
#define CHWND_EXCEPT( hr ) Window::Exception( __LINE__,__FILE__,hr )
#define CHWND_EXCEPT( hr ) Window::Exception( __LINE__,__FILE__,hr )
#define CHWND_LAST_EXCEPT() Window::Exception( __LINE__,__FILE__,GetLastError() )
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment