www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - *trying* to starting using D

reply "Diggory" <diggsey googlemail.com> writes:
I'm new to D, and I'm trying to port a very small win32 opengl 
app (~100 lines) from C++ to D but the bindings just don't seem 
up to it...

Firstly a significant portion of wingdi.h seems to be missing 
from the default windows bindings which come with phobos, enough 
to make initialising an opengl context impossible.

Eventually I found the bindings project - 
"http://www.dsource.org/projects/bindings" - which appears to 
have the relevant definitions but compilation still fails with a 
series of errors such as "Error 42: Symbol Undefined 
_ChoosePixelFormat 8"

I assume this is a linker problem but I have no idea what I'm 
supposed to link in, and only a rough idea of how I'd actually do 
it?

The opengl bindings from that same binding project seem to be 
written for an old version of D as they don't compile. I replaced 
all the "typedef"s with "alias" and moved it into a directory 
named "c" to match the module name and that seemed to work 
although I then got more errors like above.

I found the opengl bindings in "derelict" but that has too many 
dependencies for my liking.
Apr 19 2013
next sibling parent kraybit <stdin kraybit.com> writes:
On 4/20/13 01:40 , Diggory wrote:
 I'm new to D, and I'm trying to port a very small win32 opengl app (~100
 lines) from C++ to D but the bindings just don't seem up to it...
 [...]

I don't know if I can help, but would it be possible to post the code you have so far? (I believe something like http://pastebin.com is preferred.) /k
Apr 19 2013
prev sibling next sibling parent "Rob T" <alanb ucora.com> writes:
On Friday, 19 April 2013 at 23:40:58 UTC, Diggory wrote:
 I found the opengl bindings in "derelict" but that has too many 
 dependencies for my liking.

The dsource website confuses people new to D, it's not maintained anymore and applies to the D1 era. For some reason it just won't go away. I think derelict is your best bet right now, however look in here for recent bindings that may apply. https://github.com/D-Programming-Deimos Finally, you should post questions like this in the D learn section http://forum.dlang.org/group/digitalmars.D.learn --rt
Apr 19 2013
prev sibling next sibling parent "Diggory" <diggsey googlemail.com> writes:
Ah I didn't see that board, could a moderator move this please?

The problem is that neither "derelict" or "deimos" define a 
specific "windows" binding. "deimos" has some parts of the 
windows api in "wintypes.d" but it conflicts with the built in 
windows api.

When importing under an alias, aside from the obvious downside of 
having two copies of certain structures which are not 
interchangeable (eg. PIXELFORMATDESCRIPTOR) some of the necessary 
functions are still not declared in either (eg. SetPixelFormat)


Here's my original C++ code, the idea is that I can compile this 
code into a static library, and then I can link some platform 
independent code into it which defines "gloopMain()" and start 
making opengl calls to draw stuff. To get it to run on linux all 
I have to do is link against the linux version. It's pretty 
similar to GLUT except it works the way I want...

Header
[code]
enum GloopFlags {
	gloop_fullScreen = 1,
	gloop_resizable = 2,
	gloop_depthBuffer = 4
};

struct GloopParams {
	int x, y, width, height;
	LPCTSTR title;
	int flags;
};

extern GloopParams gloopParams;
void gloopMain();

#define GLOOP(x, y, w, h, title, flags) GloopParams gloopParams = 
{x, y, w, h, title, flags}; void gloopMain()

bool GloopLoop();
[/code]

Source
[code]
#include "stdafx.h"
#include "Gloop.h"

#define CLASS_NAME _T("GloopWindow")

HDC hdc = 0;

bool GloopLoop() {
	SwapBuffers(hdc);

	Sleep(0);

	MSG msg;
	while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) {
		switch (msg.message) {
		case WM_QUIT:
			return false;
		default:
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return true;
}

LRESULT CALLBACK wndProc(HWND hwnd, UINT message, WPARAM wParam, 
LPARAM lParam) {
	switch (message) {
	case WM_SIZE:
		glViewport(0, 0, LOWORD(lParam), HIWORD(lParam));
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}

	return DefWindowProc(hwnd, message, wParam, lParam);
}

bool setupPixelFormat(HDC hdc)
{
	PIXELFORMATDESCRIPTOR pfd, *ppfd;
	int pixelformat;

	ppfd = &pfd;

	ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
	ppfd->nVersion = 1;
	ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | 
PFD_DOUBLEBUFFER;
	ppfd->dwLayerMask = PFD_MAIN_PLANE;
	ppfd->iPixelType = PFD_TYPE_RGBA;
	ppfd->cColorBits = 24;
	ppfd->cDepthBits = (gloopParams.flags & gloop_depthBuffer) ? 16 
: 0;
	ppfd->cAccumBits = 0;
	ppfd->cStencilBits = 0;

	pixelformat = ChoosePixelFormat(hdc, ppfd);

	if ( (pixelformat = ChoosePixelFormat(hdc, ppfd)) == 0 ) {
		MessageBox(NULL, _T("ChoosePixelFormat failed"), _T("Error"), 
MB_OK);
		return false;
	}

	if (SetPixelFormat(hdc, pixelformat, ppfd) == FALSE) {
		MessageBox(NULL, _T("SetPixelFormat failed"), _T("Error"), 
MB_OK);
		return false;
	}

	return true;
}

int APIENTRY WinMain(_In_ HINSTANCE hInstance,
                      _In_opt_ HINSTANCE hPrevInstance,
                      _In_ LPSTR    lpCmdLine,
                      _In_ int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	WNDCLASSEX wndClass = {0};
	wndClass.cbSize = sizeof(wndClass);
	wndClass.hInstance = hInstance;
	wndClass.lpfnWndProc = &wndProc;
	wndClass.lpszClassName = CLASS_NAME;
	wndClass.hCursor = LoadCursor(nullptr, IDC_ARROW);
	RegisterClassEx(&wndClass);

	DWORD styles = 
WS_OVERLAPPED|WS_CLIPCHILDREN|WS_CLIPSIBLINGS|WS_VISIBLE;
	if (gloopParams.flags & gloop_resizable)
		styles |= WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
	if (gloopParams.flags & gloop_fullScreen)
		styles |= WS_MAXIMIZE | WS_POPUP;
	else
		styles |= WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU;

	RECT rc = {
		gloopParams.x, gloopParams.y,
		gloopParams.x + gloopParams.width, gloopParams.y + 
gloopParams.height
	};

	AdjustWindowRectEx(&rc, styles, FALSE, 0);

	HWND hwnd = CreateWindowEx(
		0,
		CLASS_NAME,
		gloopParams.title,
		styles,
		rc.left,
		rc.top,
		rc.right - rc.left,
		rc.bottom - rc.top,
		nullptr,
		nullptr,
		hInstance,
		nullptr
		);

	hdc = GetDC(hwnd);
	if (!setupPixelFormat(hdc))
		return 0;

	HGLRC hrc = wglCreateContext(hdc);
	wglMakeCurrent(hdc, hrc);

	gloopMain();

	return 0;
}
[/code]

(no idea if these code tags will work...)
Apr 19 2013
prev sibling parent "Rob T" <alanb ucora.com> writes:
On Saturday, 20 April 2013 at 00:44:20 UTC, Diggory wrote:
 Ah I didn't see that board, could a moderator move this please?

This forum is actually a news group run through the nntp system and as such there's no moderator and no way to move a thread. Your best bet for answers it to repost in d.learn.
 The problem is that neither "derelict" or "deimos" define a 
 specific "windows" binding. "deimos" has some parts of the 
 windows api in "wintypes.d" but it conflicts with the built in 
 windows api.

You may also get some answers in the derelict forums http://dblog.aldacron.net/forum/index.php --rt
Apr 19 2013