Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Definitive guide to SFML.NET on OSX  (Read 9214 times)

0 Members and 1 Guest are viewing this topic.

theframeworkguy

  • Newbie
  • *
  • Posts: 2
    • View Profile
Definitive guide to SFML.NET on OSX
« on: September 25, 2014, 02:53:40 pm »
I previously found the lack of a solid tutorial difficult, ending up spending around 5-6 days attempting to get SFML.NET to work on OSX. I finally got it to work after trudging through problem after problem. So I decided to write a tutorial to help newer users get setup on OSX.

I'm going to assume you'll be using Xamarin IDE. If you don't, it's a great tool for working with C# on OSX, and I highly recommend you use it.

Resources

If you're using the latest SFML version (2.1), you'll want to download the following libraries for your project.


Project Setup

Once you've created a new Command Line project from Xamarin, you can go ahead and add the SFML.Net bindings by going into your references and adding the contents of the lib folder inside the sfmlnet zip file. This should be three DLL's.

  • sfmlnet-audio-2.dll
  • sfmlnet-window-2.dll
  • sfmlnet-graphics-2.dll

Just select them in the lib directory, and click add.



CSFML Setup

The SFML.Net binding does not use the SFML library directly. Instead, it uses the CSFML binding, so we have to add this to our binary directory. Copy the following files from the lib directory inside the CSFML zip you downloaded into your bin/Debug/ directory.

  • libcsfml-audio-2.1.dylib
  • libcsfml-window-2.1.dylib
  • libcsfml-graphics-2.1.dylib



SFML Setup

Now that we have added the CSFML binding, all that's left to add is the actual SFML library itself.  Copy the following files from the lib directory inside the SFML zip you downloaded into your bin/Debug/ directory.

  • libsfml-audio-2.1.dylib
  • libsfml-window-2.1.dylib
  • libsfml-graphics-2.1.dylib



You'll also have to copy the external libraries used by SFML from the extlibs folder into your bin/Debug/ directory as well.

  • freetype.framework
  • sndfile.framework



Basic Application

In order to make sure everything is working fine, replace the contents of Program.cs with the following example code.

using System;
using SFML;
using SFML.Graphics;
using SFML.Window;

namespace TestSFML
{
        class MainClass
        {
                private static RenderWindow _window;

                public static void Main(string[] args)
                {
                        _window = new RenderWindow(new VideoMode(800, 600), "SFML window");
                        _window.SetVisible(true);
                        _window.Closed += new EventHandler(OnClosed);
                        while (_window.IsOpen())
                        {
                                _window.DispatchEvents();
                                _window.Clear(Color.Red);
                                _window.Display();
                        }
                }

                private static void OnClosed(object sender, EventArgs e)
                {
                        _window.Close();
                }
        }
}

If you're doing good so far, the program should build successfully. When you attempt to run it, you should encounter the following error. This is due to the method in which the binding maintains compatibility with all of the supported platforms. If you were running on windows, you would be able to drop in the required dll's, and mono would be able to correctly reference them. Since we're using mono on OSX, we have to actually map the dll's to the correct dynamic libraries. This is very easy to do.



Create a file named sfmlnet-graphics-2.dll.config inside your bin/Debug/ folder and fill it with the following contents.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
        <dllmap dll="csfml-graphics-2" target="libcsfml-graphics.2.1.dylib"/>
        <dllmap dll="csfml-audio-2" target="libcsfml-audio.2.1.dylib"/>
        <dllmap dll="csfml-window-2" target="libcsfml-window.2.1.dylib"/>
</configuration>
 

This will redirect the libraries properly, and mono should be able to find them.

Linking

If you're done the previous steps, you'll notice that mapping the dlls still results in errors. This is due to the way SFML finds the other libraries. Fixing this is possible, but difficult through the command line. Using the InstallToolGUI binary which you downloaded, you can easily fix the relative paths. First though, we need to be able to read the errors that mono produces in-case we do something wrong. This needs to be done via the command line. Start by changing the directory to your project binary folder.

Code: [Select]
> cd ~/Projects/TestSFML/TestSFML/bin/Debug

You can now debug your setup by typing the following command, which will output a block of text which should explain why your application may not be able to find the dll. If an error occurs loading or linking the dynamic library, the error will still tell you it cannot find it. Even if it exists.

Code: [Select]
> cd ~/Projects/TestSFML/TestSFML/bin/Debug
> MONO_LOG_LEVEL=debug mono TestSFML.exe
...

If you read the log, it's not clear why it cannot find the library. While the filename is correct, the actual id is not. In the next step, we'll go through and fix this.

Fixing library paths

We'll start by fixing the CSFML binding, start the InstallNameToolGUI application and open libcsfml-graphics-2.1.dylib in your binary directory. Firstly, change the dynamic library ID so that it matches the filename correctly. Press the Set ID button to save. You should then remove all the prefix paths in the list, and make sure it is the correct filename. Everything in the CSFML files should reference the SFML libraries.

Here is an example.



Once you are done changing all the CSFML dynamic libraries, you can progress by altering the SFML libraries in a similar way. Again, renaming the dynamic library ID and altering all the relative paths. In both graphics and audio, you will have to alter the freetype and sndfile frameworks paths.

Here is an example.



You're done!

If you've done everything correctly, you will end up with a rather satisfying window on your screen.

« Last Edit: March 02, 2015, 03:29:59 pm by theframeworkguy »

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Definitive guide to SFML.NET on OSX
« Reply #1 on: September 25, 2014, 04:18:46 pm »

Great work!  :D

It would be even more awesome if you could put it on the wiki (here) so it doesn't get lost on the forum.
SFML / OS X developer

theframeworkguy

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Definitive guide to SFML.NET on OSX
« Reply #2 on: September 26, 2014, 01:16:24 am »
Moved to wiki. It would be great if somebody with a relatively clean Mac could make sure this completely works. I may have missed some things out due to my existing developer workspace.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Definitive guide to SFML.NET on OSX
« Reply #3 on: September 27, 2014, 09:51:05 am »
I did give it a go in a clean OS X 10.8 VM, went through the steps you described (I just had to install the CLT to make InstallNameTool work so I've updated your wiki entry) but when I run it it crashes. Now, I've never used Mono and it has been a few years since I've written a line of C# so I have no idea what to do to fix it. Also I don't have much time for that so I'm not asking specifically for help, I'll just post the error I get and if you have an idea what could be wrong with my installation maybe you can fix the tutorial (it's also possible that I did something wrong too - or simply because I'm using VMware...).


In any case, the tutorial is quite good !  ;)

(click to show/hide)
SFML / OS X developer

Richy19

  • Full Member
  • ***
  • Posts: 190
    • View Profile
Re: Definitive guide to SFML.NET on OSX
« Reply #4 on: October 09, 2014, 09:07:59 pm »
I tried this today and after a bit of tinkering managed to get the libraries all loading ok, however I got an exception on start up
Quote
sf::err() << "Cannot import this Window Handle because it is neither "
                  << "a <NSWindow*> nor <NSView*> object "
                  << "(or any of their subclasses). You gave a <"
                  << [[nsHandle className] UTF8String]
                  << "> object."
                  << std::endl;

From line 175 here https://github.com/SFML/SFML/blob/master/src/SFML/Window/OSX/WindowImplCocoa.mm
This was using SFML 2.0

If anyone has it setup could they just upload a barebones working project? Would the libraries preserve their settings or would that not work?

myl

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • Email
Re: Definitive guide to SFML.NET on OSX
« Reply #5 on: February 19, 2015, 01:01:13 am »
This work is awesome! Thank you so much. I ported a rather large application using SFML 2.0.

Btw I noticed a difference in the way SFML behaves on OS X and Windows. Rendering an empty string (not null) crashes the process on OS X, but not on windows.

myl

jdmoser

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Definitive guide to SFML.NET on OSX
« Reply #6 on: March 01, 2016, 08:42:49 am »
Hiya guys. I thought it would be important to share my success story following problems with this tutorial. If somebody important could update the wiki that would probably be beneficial, haha (that's why I'm posting here)

So I ran through this tutorial, twice, to the letter, and I could not get it to work (I'm on El Capitan). My project compiled fine, but would crash upon running with a DllNotFoundException saying it could not find "libcsfml-graphics.2.1.dylib"

Turns out the problem was during the InstallNameToolGui tool steps. The tutorial explains that changing the path to only the filename suffices.... it does not. I had to define the explicit full path to the file, and only then did I get a successful run with the test red screen.

Here's an example of what my successful libcsfml-graphics.2.1.dylib file looks like in InstallNameToolGui. Defining the full path is all I needed to do differently. I hope this helps some other lost soul in the future!


 

anything