-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
309 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dmd hello.d gdi32.lib user32.lib opengl32.lib msimg32.lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
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 glClear(GLbitfield mask); | ||
void glClearColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha); | ||
void glBegin(GLenum mode); | ||
void glEnd(); | ||
void glVertex2f(GLfloat x, GLfloat y); | ||
void glColor3f(GLfloat red, GLfloat green, GLfloat blue); | ||
} | ||
|
||
enum GL_COLOR_BUFFER_BIT = 0x4000; | ||
enum GL_TRIANGLES = 0x0004; | ||
|
||
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() { | ||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | ||
glClear(GL_COLOR_BUFFER_BIT); | ||
|
||
glBegin(GL_TRIANGLES); | ||
glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-0.5f, -0.5f); | ||
glColor3f(0.0f, 1.0f, 0.0f); glVertex2f( 0.5f, -0.5f); | ||
glColor3f(0.0f, 0.0f, 1.0f); glVertex2f( 0.0f, 0.5f); | ||
glEnd(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
compile: | ||
``` | ||
dmd hello.d gdi32.lib user32.lib opengl32.lib | ||
``` | ||
Result: | ||
``` | ||
+------------------------------------------+ | ||
|Hello, World! [_][~][X]| | ||
+------------------------------------------+ | ||
| | | ||
| / \ | | ||
| / \ | | ||
| / \ | | ||
| / \ | | ||
| / \ | | ||
| / \ | | ||
| / \ | | ||
| / \ | | ||
| - - - - - - - - - - - - - - - - - | | ||
+------------------------------------------+ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dmd hello.d gdi32.lib user32.lib opengl32.lib msimg32.lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
compile: | ||
``` | ||
dmd hello.d gdi32.lib user32.lib opengl32.lib | ||
``` | ||
Result: | ||
``` | ||
+------------------------------------------+ | ||
|Hello, World! [_][~][X]| | ||
+------------------------------------------+ | ||
| | | ||
| / \ | | ||
| / \ | | ||
| / \ | | ||
| / \ | | ||
| / \ | | ||
| / \ | | ||
| / \ | | ||
| / \ | | ||
| - - - - - - - - - - - - - - - - - | | ||
+------------------------------------------+ | ||
``` |