-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhello.d
137 lines (117 loc) · 3.79 KB
/
hello.d
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
import core.runtime;
import core.sys.windows.windows;
import core.sys.windows.windef;
import core.sys.windows.winuser;
import core.sys.windows.wingdi;
extern(System) {
alias GLenum = uint;
alias GLuint = uint;
alias GLint = int;
alias GLsizei = int;
alias GLbitfield = uint;
alias GLfloat = float;
void glEnableClientState(GLenum array);
void glVertexPointer(GLint size, GLenum type, GLsizei stride, const GLfloat* pointer);
void glColorPointer(GLint size, GLenum type, GLsizei stride, const GLfloat* pointer);
void glDrawArrays(GLenum mode, GLint first, GLsizei count);
enum GL_COLOR_ARRAY = 0x8076;
enum GL_VERTEX_ARRAY = 0x8074;
enum GL_FLOAT = 0x1406;
enum GL_TRIANGLE_STRIP = 0x0005;
}
auto toUTF16z(S)(S s) {
return cast(const(wchar)*)s.ptr;
}
extern (Windows)
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
Runtime.initialize();
int result = myWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
Runtime.terminate();
return result;
}
int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
string appName = "OpenGLTriangle";
WNDCLASS wndclass = {};
wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wndclass.lpfnWndProc = &WndProc;
wndclass.hInstance = hInstance;
wndclass.lpszClassName = appName.toUTF16z;
RegisterClass(&wndclass);
HWND hwnd = CreateWindow(
appName.toUTF16z,
"Hello, World!",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
640,
480,
null,
null,
hInstance,
null);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, null, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
extern (Windows)
LRESULT WndProc(void* hwnd, uint message, uint wParam, int lParam) nothrow {
static HGLRC hglrc;
static HDC hdc;
try {
switch (message) {
case WM_CREATE:
hdc = GetDC(cast(HWND) hwnd);
PIXELFORMATDESCRIPTOR pfd = PIXELFORMATDESCRIPTOR(
nSize: PIXELFORMATDESCRIPTOR.sizeof,
nVersion: 1,
dwFlags: PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
iPixelType: PFD_TYPE_RGBA,
cColorBits: 32,
cDepthBits: 24,
iLayerType: PFD_MAIN_PLANE
);
int format = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, format, &pfd);
hglrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hglrc);
return 0;
case WM_PAINT:
render();
SwapBuffers(hdc);
ValidateRect(cast(HWND) hwnd, null);
return 0;
case WM_DESTROY:
wglMakeCurrent(null, null);
wglDeleteContext(hglrc);
ReleaseDC(cast(HWND) hwnd, hdc);
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(cast(HWND) hwnd, message, wParam, lParam);
}
} catch (Throwable) {
return DefWindowProc(cast(HWND) hwnd, message, wParam, lParam);
}
}
void render() {
GLfloat[9] colors = [
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f
];
GLfloat[6] vertices = [
0.0f, 0.5f,
0.5f, -0.5f,
-0.5f, -0.5f
];
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(3, GL_FLOAT, 0, colors.ptr);
glVertexPointer(2, GL_FLOAT, 0, vertices.ptr);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
}