*trying* to starting using D

Diggory diggsey at googlemail.com
Fri Apr 19 17:44:19 PDT 2013


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...)


More information about the Digitalmars-d mailing list