diff trunk/tests/ChipmunkDemos/framework.d @ 16:af2f61a96318

ported chipmunk demos
author Extrawurst
date Sat, 04 Dec 2010 02:02:29 +0100
parents
children a376e5d3eb00
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/trunk/tests/ChipmunkDemos/framework.d	Sat Dec 04 02:02:29 2010 +0100
@@ -0,0 +1,93 @@
+
+// written in the D programming language
+
+/++
+ +	Authors: Stephan Dilly, www.extrawurst.org
+ +/
+
+module framework;
+
+import derelict.sdl.sdl;
+import derelict.opengl.gl;
+import derelict.opengl.glu;
+
+import std.string;
+import std.stdio;
+
+void startup(string _title,int _width,int _height)
+{
+	DerelictGL.load();
+	DerelictGLU.load();
+	DerelictSDL.load();
+	
+	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
+	{
+		throw new Exception("Failed to initialize SDL");
+	}
+	
+	// Enable key repeating
+	if ((SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL)))
+	{
+		throw new Exception("Failed to set key repeat");
+	}
+
+	//enable to get ascii/unicode info of key event
+	SDL_EnableUNICODE(1);
+	
+	// Set the OpenGL attributes
+	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
+	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
+	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
+	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
+
+	// Set the window title
+	SDL_WM_SetCaption(cast(char*)toStringz(_title), null);
+
+	int mode = SDL_OPENGL;
+
+	// Now open a SDL OpenGL window with the given parameters
+	if (SDL_SetVideoMode(_width, _height, 32, mode) is null)
+	{
+		throw new Exception("Failed to open SDL window");
+	}
+}
+
+alias void delegate(int,int,bool) MouseButton;
+alias void delegate(int,int) MouseMove;
+alias void delegate(int,bool) KeyEvent;
+public bool processEvents(KeyEvent _keyevent,MouseMove _mmove,MouseButton _mbutton)
+{
+	SDL_Event event;
+	while (SDL_PollEvent(&event))
+	{
+		switch (event.type)
+		{
+			case SDL_KEYUP:
+			case SDL_KEYDOWN:
+				_keyevent(event.key.keysym.sym,event.type == SDL_KEYDOWN);
+				break;
+			
+			case SDL_MOUSEMOTION:
+				_mmove(event.motion.x,event.motion.y);
+				break;
+			
+			case SDL_MOUSEBUTTONUP:
+			case SDL_MOUSEBUTTONDOWN:
+				_mbutton(event.button.x,event.button.y,event.type == SDL_MOUSEBUTTONDOWN);
+				break;
+
+			case SDL_QUIT:
+				return false;
+
+			default:
+				break;
+		}
+	}
+	
+	return true;
+}
+
+void shutdown()
+{
+	SDL_Quit();
+}
\ No newline at end of file