/
WinUtility.cpp
137 lines (118 loc) · 3.26 KB
/
WinUtility.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <Gosu/WinUtility.hpp>
#include <Gosu/Utility.hpp>
#include <stdexcept>
#include <vector>
namespace
{
typedef std::vector<std::tr1::function<bool (MSG&)> > Hooks;
Hooks hooks;
bool handledByHook(MSG& message)
{
for (Hooks::iterator i = hooks.begin(); i != hooks.end(); ++i)
if ((*i)(message))
return true;
return false;
}
}
HINSTANCE Gosu::Win::instance()
{
return check(::GetModuleHandle(0), "getting the module handle");
}
void Gosu::Win::handleMessage()
{
MSG message;
BOOL ret = ::GetMessage(&message, 0, 0, 0);
switch (ret)
{
case -1:
{
// GetMessage() failed.
throwLastError("trying to get the next message");
}
case 0:
{
// GetMessage() found a WM_QUIT message.
// IMPR: Is there a better way to respond to this?
break;
}
default:
{
// Normal behaviour, if the message does not get handled by
// something else.
if (!handledByHook(message))
{
::TranslateMessage(&message);
::DispatchMessage(&message);
}
}
}
}
void Gosu::Win::processMessages()
{
MSG message;
while (::PeekMessage(&message, 0, 0, 0, PM_REMOVE))
if (!handledByHook(message))
{
::TranslateMessage(&message);
::DispatchMessage(&message);
}
}
void Gosu::Win::registerMessageHook(const std::tr1::function<bool (MSG&)>& hook)
{
hooks.push_back(hook);
}
void Gosu::Win::throwLastError(const std::string& action)
{
// Obtain error message from Windows.
char* buffer;
if (!::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0,
::GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPSTR>(&buffer), 0, 0) || buffer == 0)
{
// IMPR: Can we do better than this?
throw std::runtime_error("Unknown error");
}
// Move the message out of the ugly char* buffer.
std::string message;
try
{
message = buffer;
}
catch (...)
{
::LocalFree(buffer);
throw;
}
::LocalFree(buffer);
// Optionally prepend the action.
if (!action.empty())
message = "While " + action + ", the following error occured: " +
message;
// Now throw it.
throw std::runtime_error(message);
}
std::wstring Gosu::Win::appFilename()
{
static std::wstring result;
if (!result.empty())
return result;
wchar_t buffer[MAX_PATH * 2];
check(::GetModuleFileName(0, buffer, MAX_PATH * 2),
"getting the module filename");
result = buffer;
return result;
}
std::wstring Gosu::Win::appDirectory()
{
static std::wstring result;
if (!result.empty())
return result;
result = appFilename();
std::wstring::size_type lastDelim = result.find_last_of(L"\\/");
if (lastDelim != std::wstring::npos)
result.resize(lastDelim + 1);
else
result = L"";
return result;
}