-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRasterSurface.cpp
225 lines (215 loc) · 8.88 KB
/
RasterSurface.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// The RasterSurface is a simple & efficient way to copy a block of pixels to
// the screen. Author: L.Norri CD GX1 & GX2, FullSail University
#include "RasterSurface.h" // definitions
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <wingdi.h>
#include <atomic>
#include <future>
#include <mutex>
#include <thread>
// variables used by the RasterSurface
HWND window = nullptr;
HDC windowDC = nullptr;
std::thread windowHandler;
DWORD windowHandlerID = -1;
std::atomic_bool windowClosed;
unsigned int *bitmap = nullptr;
unsigned int bitmapWidth = 0;
unsigned int bitmapHeight = 0;
std::mutex bitmapMutex;
std::condition_variable bitmapRedraw;
std::future<unsigned int *> bitmapAllocator;
std::atomic_bool bitmapPresent;
// Handles all windows messages (Messages may arrive cross-thread without a
// valid HWND) hWnd may be set artifically due to cross-thread message posting
// (NULL HWNDs are ignored)
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam) {
switch (message) {
case (WM_DESTROY): {
windowClosed = true; // window closing, updates disabled
bitmapRedraw
.notify_one(); // tell main to stop waiting for a redraw and exit
// close down the window
window = nullptr; // dont stall get message
PostQuitMessage(0);
break;
}
}
return DefWindowProcW(hWnd, message, wParam, lParam);
}
// This function transfers the internal block of pixels to the screen
// It will also update the title bar FPS once every second
bool PresentFrame() {
// update screen contents & increment frame count
if (bitmap && window && windowDC) // bitmap should be allocated at this point
{
// lock down the bitmap object and use it to paint to the window surface
std::unique_lock<std::mutex> pixelLock(bitmapMutex);
// SetDIBitsToDevice version
BITMAPINFO toDraw;
ZeroMemory(&toDraw, sizeof(BITMAPINFO));
toDraw.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
toDraw.bmiHeader.biWidth = bitmapWidth;
toDraw.bmiHeader.biHeight = -int(bitmapHeight); // flip
toDraw.bmiHeader.biPlanes = 1;
toDraw.bmiHeader.biBitCount = 32;
toDraw.bmiHeader.biCompression = BI_RGB;
// Draw to frontbuffer
SetDIBitsToDevice(windowDC, 0, 0, bitmapWidth, bitmapHeight, 0, 0, 0,
bitmapHeight, bitmap, &toDraw, DIB_RGB_COLORS);
// increase frame count and notify render thread to continue
bitmapPresent = false; // increase frame count
bitmapRedraw.notify_one(); // tell main thread to continue rendering
// Update visible frame rate every second
static ULONGLONG frameCount = 0;
++frameCount;
static ULONGLONG framesPast = frameCount;
static ULONGLONG prevCount = GetTickCount();
if (GetTickCount64() - prevCount > 1000) // only update every second
{
char buffer[256];
sprintf_s(buffer, "Raster Surface. FPS: %d",
static_cast<int>(frameCount - framesPast));
SetWindowTextA(window, buffer);
framesPast = frameCount;
prevCount = GetTickCount64();
}
return true;
}
return false;
}
// This thread will handle all updates to the window
void ProcessRasterSurface(unsigned int _width, unsigned int _height,
std::promise<unsigned int *> bitmapInit) {
// DIB pixel buffers may not be fragmented across heaps
// VirtualAlloc garuntees true memory contiguity (needed by SetDIBitsToDevice)
unsigned int *frontbuffer =
(unsigned int *)VirtualAlloc(nullptr, (_width * _height) << 2,
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
bitmapInit.set_value(frontbuffer); // fufill promise
// Create a win32 window and manage it on this thread
WNDCLASSEX wndClass;
ZeroMemory(&wndClass, sizeof(WNDCLASSEX));
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.style = CS_OWNDC; // optimization
wndClass.lpfnWndProc = WndProc;
wndClass.lpszClassName = L"RasterSurfaceApplication";
wndClass.hInstance = GetModuleHandleW(0);
wndClass.hCursor = LoadCursorW(0, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOWFRAME);
wndClass.hIcon = LoadIconW(0, IDI_APPLICATION);
RegisterClassExW(&wndClass);
// adjust window size to contain backbuffer
RECT window_size = {0, 0, _width, _height};
AdjustWindowRect(&window_size, WS_OVERLAPPEDWINDOW, false);
// Launch window and start managment on other thread
window = CreateWindowW(
L"RasterSurfaceApplication", L"Raster Surface",
WS_OVERLAPPEDWINDOW & ~(WS_THICKFRAME | WS_MAXIMIZEBOX), CW_USEDEFAULT,
CW_USEDEFAULT, window_size.right - window_size.left,
window_size.bottom - window_size.top, NULL, NULL, GetModuleHandleW(0), 0);
// Present visible window
if (window) {
ShowWindow(window, SW_SHOW);
windowDC = GetDC(window);
// Handle window messages
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while (msg.message != WM_QUIT) { // handles ANY message on this thread (not
// just ones bound to the HWND)
if (PeekMessageW(
&msg, 0, 0, 0,
PM_REMOVE)) // HWND NULL to listen for "PostThreadMessage"
{
// we assume all messages are directed at our window
if (msg.hwnd == NULL) // this is a cross-thread message
msg.hwnd = window;
// The below funtions will not operate on NULL HWND messages
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
// If any frames are ready to be presented we should do so
// This operation is synchronized with "RS_Update"
if (bitmapPresent) PresentFrame();
}
}
// delete the front buffer "aka: bitmap" (before thread shuts down)
VirtualFree(frontbuffer, 0, MEM_RELEASE);
bitmap = nullptr;
// deallocate window
UnregisterClassW(L"RasterSurfaceApplication", GetModuleHandleW(0));
}
// Handles unexpected termination of the console window.
BOOL WINAPI ConsoleCtrlHandler(DWORD ctrlCode);
// Spawns & manages a win32 window of the requested size. (the "RasterSurface")
bool RS_Initialize(_In_range_(1, 0xFFFF) unsigned int _width,
_In_range_(1, 0xFFFF) unsigned int _height) {
// Create a win32 window and manage it on another thread
bitmapPresent = false; // no bitmap is available yet
windowClosed = false; // window is being created
bitmapWidth = _width; // save x size
bitmapHeight = _height; // save y size
// bitmap creation will be fufilled on secondary thread. (allow immediate
// drawing)
std::promise<unsigned int *> bitmapGen;
bitmapAllocator = bitmapGen.get_future();
// handle messages & buffer updates on dedicated thread
windowHandler =
std::thread(ProcessRasterSurface, _width, _height, std::move(bitmapGen));
windowHandlerID = GetThreadId(static_cast<HANDLE>(
windowHandler.native_handle())); // what is the new thread's ID?
// allows gracefull exit when console window is closed
SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
return true;
}
// Updates the RasterSurface with a block of raw XRGB pixel data.
// Incoming data must 32bit pixels 8 bits per channel.
bool RS_Update(_In_reads_(_numPixels) const unsigned int *_argbPixels,
_In_range_(1, 0xFFFFFFFF) unsigned int _numPixels) {
// Wait for the drawing surface to intialize
if (bitmapAllocator.valid())
bitmap = bitmapAllocator.get(); // retreive allocated value (blocking)
// begin transfer
if (bitmap) { // track the current frame being rendered
static unsigned int internalCount = 0;
// if we have a valid window, lets transfer the memory block to the screen
{
std::unique_lock<std::mutex> pixelLock(bitmapMutex); // protect bitmap
// wait for last paint to occur if we are ahead
bitmapRedraw.wait(pixelLock,
[&]() { return !bitmapPresent || windowClosed; });
// if the window has been closed, allow no more updates
if (windowClosed) return false;
// copy bitmap data so we can continue to
// draw while it is transfered to frontbuffer
memcpy_s(bitmap, _numPixels << 2, _argbPixels, _numPixels << 2);
// notify win32 thread we are ready to present the new image
bitmapPresent = true;
}
return true;
}
return false;
}
// Deallocates the RasterSurface and cleans up any leftover memory.
bool RS_Shutdown() {
// tell window to close
PostThreadMessageW(windowHandlerID, WM_DESTROY, 0, 0);
// wait for thread to yeild
windowHandler.join();
// the bitmap is released by the window
window = nullptr;
windowDC = nullptr;
bitmap = nullptr;
return true;
}
// Handles unexpected termination of the console window.
BOOL WINAPI ConsoleCtrlHandler(DWORD ctrlCode) {
// Cleanly exit in case of unclean close
if (ctrlCode == CTRL_BREAK_EVENT || ctrlCode == CTRL_CLOSE_EVENT ||
ctrlCode == CTRL_LOGOFF_EVENT || ctrlCode == CTRL_SHUTDOWN_EVENT)
RS_Shutdown(); // kill window and wait for shutdown
// allow other handlers to end process
return FALSE;
}