-
Notifications
You must be signed in to change notification settings - Fork 0
/
RenderWindow.cpp
125 lines (106 loc) · 3.02 KB
/
RenderWindow.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
/**
* \author Marie DARRIGOL & Anthony LEONARD & Ophélie PELLOUX-PRAYER & Olivier SOLDANO
* \project Ray-Tracing
* \file RenderWindow.cpp
* \brief Window in which the picture is rendered
*/
#include "RenderWindow.h"
RenderWindow *winInstance = NULL;
LRESULT CALLBACK RenderWindow::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_PAINT:
winInstance->d2dBmp->CopyFromMemory(NULL, winInstance->bmp, 1024 * 4);
winInstance->d2dRTarget->BeginDraw();
winInstance->d2dRTarget->DrawBitmap(winInstance->d2dBmp);
winInstance->d2dRTarget->EndDraw();
break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
winInstance->d2dRTarget->Release();
winInstance->d2dFact->Release();
winInstance->d2dBmp->Release();
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
RenderWindow::RenderWindow(HINSTANCE hInstance, int nCmdShow)
{
if (winInstance == NULL)
winInstance = this;
else
abort();
// On crée un buffer d'image
this->bmp = (UINT8*)malloc(1028 * 768 * 4);
// On nettoie le buffer d'image
for (int i = 0; i < 1024 * 768; i++) {
bmp[i * 4] = 0;
bmp[i * 4 + 1] = 0;
bmp[i * 4 + 2] = 0;
bmp[i * 4 + 3] = 0;
}
HRESULT hr;
hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &d2dFact);
if (hr != S_OK) abort();
WNDCLASSEX wc = { };
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = RenderWindow::WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) COLOR_WINDOW + 1;
wc.lpszMenuName = NULL;
wc.lpszClassName = RenderWindowClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (RegisterClassEx(&wc) == 0)
abort();
RECT winRect = RECT();
winRect.top = 0;
winRect.bottom = 768;
winRect.left = 0;
winRect.right = 1024;
AdjustWindowRectEx(&winRect, WS_OVERLAPPEDWINDOW ^ (WS_MAXIMIZEBOX | WS_THICKFRAME), false, 0);
hWindow = CreateWindowEx(
0,
RenderWindowClassName,
L"RayCPP",
WS_OVERLAPPEDWINDOW ^ (WS_MAXIMIZEBOX | WS_THICKFRAME),
CW_USEDEFAULT, CW_USEDEFAULT,
winRect.right - winRect.left, winRect.bottom - winRect.top,
NULL, NULL, hInstance, NULL);
hr = d2dFact->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(
hWindow,
D2D1::SizeU(
1024,
768
)
),
&d2dRTarget
);
if (hr != S_OK) abort();
hr = d2dRTarget->CreateBitmap(
D2D1::SizeU(1024, 768),
D2D1::BitmapProperties(D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE)),
&d2dBmp);
if (hr != S_OK) abort();
ShowWindow(hWindow, nCmdShow);
}
void RenderWindow::setPixel(const unsigned int x, const unsigned int y, const unsigned char r, const unsigned char g, const unsigned char b) {
bmp[y * 1024 * 4 + x * 4 + 0] = b;
bmp[y * 1024 * 4 + x * 4 + 1] = g;
bmp[y * 1024 * 4 + x * 4 + 2] = r;
//bmp[y * 1024 * 4 + x * 4 + 3] // alpha channel not used
}
RenderWindow::~RenderWindow()
{
free(bmp);
}