Skip to content

Commit

Permalink
Added D Lang OpenGL samples
Browse files Browse the repository at this point in the history
  • Loading branch information
cx20 committed Dec 29, 2024
1 parent 22a5e5a commit d7a658a
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ If it is a graphics library, it will display a triangle, otherwise it will displ
|DirectX10 | | |[Link](https://github.com/cx20/hello/tree/master/cpp/directx10/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_mfc/directx10/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_atl/directx10/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_wtl/directx10/triangle) | | | | | |
|DirectX11 | |[Link](https://github.com/cx20/hello/tree/master/c/directx11/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp/directx11/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_mfc/directx11/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_atl/directx11/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_wtl/directx11/triangle) |[Link](https://github.com/cx20/hello/tree/master/d/directx11/triangle) | | | | |
|DirectX12 | |[Link](https://github.com/cx20/hello/tree/master/c/directx12/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp/directx12/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_mfc/directx12/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_atl/directx12/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_wtl/directx12/triangle) |[Link](https://github.com/cx20/hello/tree/master/d/directx12/triangle) | | |[Link](https://github.com/cx20/hello/tree/master/rust/directx12/triangle) | |
|OpenGL1.0 |[Link](https://github.com/cx20/hello/tree/master/masm/opengl1.0/triangle) |[Link](https://github.com/cx20/hello/tree/master/c/opengl1.0/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp/opengl1.0/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_mfc/opengl1.0/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_atl/opengl1.0/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_wtl/opengl1.0/triangle) | | |[Link](https://github.com/cx20/hello/tree/master/pascal/opengl1.0/triangle) |[Link](https://github.com/cx20/hello/tree/master/rust/opengl1.0/triangle) | |
|OpenGL1.1 | |[Link](https://github.com/cx20/hello/tree/master/c/opengl1.1/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp/opengl1.1/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_mfc/opengl1.1/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_atl/opengl1.1/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_wtl/opengl1.1/triangle) | | |[Link](https://github.com/cx20/hello/tree/master/pascal/opengl1.1/triangle) |[Link](https://github.com/cx20/hello/tree/master/rust/opengl1.1/triangle) | |
|OpenGL1.0 |[Link](https://github.com/cx20/hello/tree/master/masm/opengl1.0/triangle) |[Link](https://github.com/cx20/hello/tree/master/c/opengl1.0/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp/opengl1.0/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_mfc/opengl1.0/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_atl/opengl1.0/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_wtl/opengl1.0/triangle) |[Link](https://github.com/cx20/hello/tree/master/d/opengl1.0/triangle) | |[Link](https://github.com/cx20/hello/tree/master/pascal/opengl1.0/triangle) |[Link](https://github.com/cx20/hello/tree/master/rust/opengl1.0/triangle) | |
|OpenGL1.1 | |[Link](https://github.com/cx20/hello/tree/master/c/opengl1.1/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp/opengl1.1/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_mfc/opengl1.1/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_atl/opengl1.1/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_wtl/opengl1.1/triangle) |[Link](https://github.com/cx20/hello/tree/master/d/opengl1.1/triangle) | |[Link](https://github.com/cx20/hello/tree/master/pascal/opengl1.1/triangle) |[Link](https://github.com/cx20/hello/tree/master/rust/opengl1.1/triangle) | |
|OpenGL2.0 | |[Link](https://github.com/cx20/hello/tree/master/c/opengl2.0/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp/opengl2.0/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_mfc/opengl2.0/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_atl/opengl2.0/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_wtl/opengl2.0/triangle) | | |[Link](https://github.com/cx20/hello/tree/master/pascal/opengl2.0/triangle) | | |
|OpenGL3.3 | |[Link](https://github.com/cx20/hello/tree/master/c/opengl3.3/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp/opengl3.3/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_mfc/opengl3.3/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_atl/opengl3.3/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_wtl/opengl3.3/triangle) | | |[Link](https://github.com/cx20/hello/tree/master/pascal/opengl3.3/triangle) | | |
|OpenGL4.6 | |[Link](https://github.com/cx20/hello/tree/master/c/opengl4.6/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp/opengl4.6/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_mfc/opengl4.6/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_atl/opengl4.6/triangle) |[Link](https://github.com/cx20/hello/tree/master/cpp_wtl/opengl4.6/triangle) | | |[Link](https://github.com/cx20/hello/tree/master/pascal/opengl4.6/triangle) | | |
Expand Down
1 change: 1 addition & 0 deletions d/opengl1.0/triangle/build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dmd hello.d gdi32.lib user32.lib opengl32.lib msimg32.lib
126 changes: 126 additions & 0 deletions d/opengl1.0/triangle/hello.d
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();
}
21 changes: 21 additions & 0 deletions d/opengl1.0/triangle/readme.md
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]|
+------------------------------------------+
| |
| / \ |
| / \ |
| / \ |
| / \ |
| / \ |
| / \ |
| / \ |
| / \ |
| - - - - - - - - - - - - - - - - - |
+------------------------------------------+
```
1 change: 1 addition & 0 deletions d/opengl1.1/triangle/build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dmd hello.d gdi32.lib user32.lib opengl32.lib msimg32.lib
137 changes: 137 additions & 0 deletions d/opengl1.1/triangle/hello.d
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);
}
21 changes: 21 additions & 0 deletions d/opengl1.1/triangle/readme.md
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]|
+------------------------------------------+
| |
| / \ |
| / \ |
| / \ |
| / \ |
| / \ |
| / \ |
| / \ |
| / \ |
| - - - - - - - - - - - - - - - - - |
+------------------------------------------+
```

0 comments on commit d7a658a

Please sign in to comment.