From 8390807b08d023f540b5e77c6311e0e893e26354 Mon Sep 17 00:00:00 2001
From: trass3r <trass3r@4e206d99-4929-0410-ac5d-dfc041789085>
Date: Thu, 4 Mar 2010 02:21:18 +0000
Subject: [PATCH] + added OSX support with a patch from egladil * fixed pong
 example to use stdlib instead of csfml-system

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1439 4e206d99-4929-0410-ac5d-dfc041789085
---
 DSFML/import/dsfml/system/all.d          |  7 ++++
 DSFML/import/dsfml/system/dllloader.d    | 43 +++++++++++++++++++++---
 DSFML/import/dsfml/window/windowhandle.d |  6 +++-
 DSFML/samples/dsfml/pong/pong.d          | 30 +++++++++++++----
 4 files changed, 74 insertions(+), 12 deletions(-)

diff --git a/DSFML/import/dsfml/system/all.d b/DSFML/import/dsfml/system/all.d
index 6668bbaa..fb6ccec9 100644
--- a/DSFML/import/dsfml/system/all.d
+++ b/DSFML/import/dsfml/system/all.d
@@ -34,6 +34,13 @@ version (linux)
 	}
 }
 
+version (darwin)
+{
+	version (build)
+	{
+		pragma(link, "dl"); //Link libdl.dylib (dlopen, dlsym)
+	}
+}
 
 public import
 	dsfml.system.lock,
diff --git a/DSFML/import/dsfml/system/dllloader.d b/DSFML/import/dsfml/system/dllloader.d
index 1c31c501..9ffbcad8 100644
--- a/DSFML/import/dsfml/system/dllloader.d
+++ b/DSFML/import/dsfml/system/dllloader.d
@@ -43,7 +43,7 @@ else
 		import std.windows.syserror; // for error strings
 		alias HMODULE MODULEHANDLE; 
 	}
-	version (linux)
+	else version (linux)
 	{
 		import std.c.linux.linux;
 		alias void* MODULEHANDLE;
@@ -51,6 +51,21 @@ else
 		const int RTLD_NOW = 0x00002;
 		const int RTLD_GLOBAL = 0x00100;
 	}
+	else version (darwin)
+	{
+		alias void* MODULEHANDLE;
+
+		const int RTLD_NOW = 0x2;
+		const int RTLD_GLOBAL = 0x8;
+
+		extern (C)
+		{
+			void* dlopen(char* file, int mode);
+			int   dlclose(void* handle);
+			void* dlsym(void* handle, char* name);
+			char* dlerror();
+		}
+	}
 }
 
 static this()
@@ -91,10 +106,14 @@ class DllLoader
 		{
 			string libraryName = library ~ ".dll";
 		}
-		version (linux)
+		else version (linux)
 		{
 			string libraryName = "lib" ~ library ~ ".so";
 		}
+		else version (darwin)
+		{   
+			string libraryName = "lib" ~ library ~ ".dylib";
+		}
 		
 		if (libraryName in alreadyLoaded)
 		{
@@ -121,7 +140,11 @@ class DllLoader
 			{
 				symb = GetProcAddress(m_lib, toStringz(symbolName));
 			}
-			version (linux)
+			else version (linux)
+			{
+				symb = dlsym(m_lib, toStringz(symbolName));
+			}
+			else version (darwin)
 			{
 				symb = dlsym(m_lib, toStringz(symbolName));
 			}
@@ -145,7 +168,11 @@ class DllLoader
 			{
 				FreeLibrary(m_lib);
 			}
-			version (linux)
+			else version (linux)
+			{
+				dlclose(m_lib);
+			}
+			else version (darwin)
 			{
 				dlclose(m_lib);
 			}
@@ -176,10 +203,16 @@ private:
 			{
 				m_lib = LoadLibraryA(toStringz(libraryPath));
 			}
-			version (linux)
+			else version (linux)
 			{
 				m_lib = dlopen(toStringz(libraryPath), RTLD_NOW | RTLD_GLOBAL);
 			}
+			else version (darwin)
+			{
+				m_lib = dlopen(toStringz(libraryPath), RTLD_NOW | RTLD_GLOBAL);
+				if (m_lib is null)
+					m_lib = dlopen(toStringz("@executable_path/" ~ libraryPath), RTLD_NOW | RTLD_GLOBAL);
+			}
 		}
 		if (m_lib is null)
 		{
diff --git a/DSFML/import/dsfml/window/windowhandle.d b/DSFML/import/dsfml/window/windowhandle.d
index b07a7843..19d30fd8 100644
--- a/DSFML/import/dsfml/window/windowhandle.d
+++ b/DSFML/import/dsfml/window/windowhandle.d
@@ -40,4 +40,8 @@ else version(linux)
 	// Unix - X11 defines an unsigned integer handle (Window)
 	typedef ulong WindowHandle;
 }
-
+else version(darwin)
+{
+	// Mac OS X defines a void* handle (NSWindow)
+	typedef void* WindowHandle;
+}
\ No newline at end of file
diff --git a/DSFML/samples/dsfml/pong/pong.d b/DSFML/samples/dsfml/pong/pong.d
index ee92d9a3..74bd598b 100644
--- a/DSFML/samples/dsfml/pong/pong.d
+++ b/DSFML/samples/dsfml/pong/pong.d
@@ -13,6 +13,8 @@ version (Tango)
 else
 {
 	import std.math;
+	import std.perf;
+	import std.random;
 }
 
 void main()
@@ -23,7 +25,7 @@ void main()
 	// Create the window of the application
 	RenderWindow app = new RenderWindow(VideoMode(800, 600, 32), "SFML Pong");
 
-	app.useVerticalSync(true);
+	app.useVerticalSync(false);
 	
 	Input i = app.getInput();
 	
@@ -43,6 +45,10 @@ void main()
 	End.move(150.f, 200.f);
 	End.setColor(Color(50, 50, 250));
 
+	Text fps = new Text(""c, font, 30);
+	fps.move(50.f, 50.f);
+	fps.setColor(Color.BLACK);
+	
 	// Create the sprites of the background, the paddles and the ball
 	Sprite LeftPaddle = new Sprite(PaddleImage);
 	Sprite RightPaddle = new Sprite(PaddleImage);
@@ -53,8 +59,8 @@ void main()
 	Ball.move((app.getView().getWidth() - Ball.getSize().x) / 2, (app.getView().getHeight() - Ball.getSize().y) / 2);
 
 	// Define the paddles properties
-	Clock AITimer = new Clock();
-	const float AITime	 = 0.1f;
+	auto AITimer = new PerformanceCounter();
+	const long AITime	 = 100; // 100 ms
 	float LeftPaddleSpeed  = 400.f;
 	float RightPaddleSpeed = 400.f;
 
@@ -64,12 +70,14 @@ void main()
 	do
 	{
 		// Make sure the ball initial angle is not too much vertical
-		BallAngle = Randomizer.random(0.f, 2 * PI);
+		BallAngle = uniform(0.f, 2 * PI);
 	} while (abs(cos(BallAngle)) < 0.7f);
 
 	bool IsPlaying = true;
 
 	Event evt;
+	uint iFps = 0;
+	auto fpsClock = new PerformanceCounter();
 	while (app.isOpened())
 	{
 		app.clear(Color(255, 255, 255, 255));
@@ -102,9 +110,10 @@ void main()
 			}
 
 			// Update the computer's paddle direction according to the ball position
-			if (AITimer.getElapsedTime() > AITime)
+			AITimer.stop();
+			if (AITimer.milliseconds > AITime)
 			{
-				AITimer.reset();
+				AITimer.start();
 				if ((RightPaddleSpeed < 0) && (Ball.getPosition().y + Ball.getSize().y > RightPaddle.getPosition().y + RightPaddle.getSize().y))
 					RightPaddleSpeed = -RightPaddleSpeed;
 				if ((RightPaddleSpeed > 0) && (Ball.getPosition().y < RightPaddle.getPosition().y))
@@ -173,6 +182,15 @@ void main()
 		app.draw(RightPaddle);
 		app.draw(Ball);
 
+		fpsClock.stop();
+		if(fpsClock.seconds >= 1)
+		{
+			fps.setString(std.string.format("%d fps", iFps));
+			iFps = 0;
+			fpsClock.start();
+		}
+		++iFps;
+		app.draw(fps);
 		// If the game is over, display the end message
 		if (!IsPlaying)
 			app.draw(End);