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

Author Topic: Seting up with C#/Visual Studio  (Read 31286 times)

0 Members and 1 Guest are viewing this topic.

joeparrilla

  • Newbie
  • *
  • Posts: 14
    • View Profile
Seting up with C#/Visual Studio
« on: April 22, 2012, 11:15:38 pm »
Hi Guys,

    So my goal is to use SFML with C# in VS2010. Can  anyone help me out with some directions to get it all set up? I downloaded the SFML 2.0 .Net release, but I am not sure how to get it running with C#? Do I start a C# Console application? Also, is it basically set up for all .Net languages... or is there a specific setup for C#.net vs C++.net? I basically need to include the libraries and dlls.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Seting up with C#/Visual Studio
« Reply #1 on: April 23, 2012, 01:58:47 am »
Setting up SFML.Net is simple. You can use either a console application or windows form application, it doesn't make a difference other than a console application will show a black console window when it runs.

To hook up SFML.Net libraries right click your project in the solution explorer and select "Add Reference...". Then browse to the SFML.Net binding dlls under the "lib" folder that you downloaded. Now all you need is to add in the CSFML libraries. Now right click your solution and select "Add>>Existing Item...". Browse to the "extlibs" folder this time and add the unmanaged CSFML dlls as files to your project. Now select the CSFML dlls in the solution explorer and on their properties select "Copy if Newer" on the "Build to Output Folder" property.

Now you should be setup to start coding with SFML.Net. For ease of use add this to the top of your code files.
using SFML;
using SFML.Graphics;
using SFML.Window;

Any other questions, let me know ;)
« Last Edit: April 23, 2012, 02:16:18 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

joeparrilla

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Seting up with C#/Visual Studio
« Reply #2 on: April 23, 2012, 03:17:51 am »
I think I did it correctly but when I try to run this code:

Code: [Select]
using System;
using SFML.Audio;
using SFML.Window;
using SFML.Graphics;

namespace Example
{
    class Program
    {
        static void OnClose(object sender, EventArgs e)
        {
            // Close the window when OnClose event is received
            RenderWindow window = (RenderWindow)sender;
            window.Close();
        }

        static void Main(string[] args)
        {
            // Create the main window
            RenderWindow app = new RenderWindow(new VideoMode(800, 600), "SFML window");
            app.Closed += new EventHandler(OnClose);

            // Load a sprite to display
            Image  image  = new Image("cute_image.jpg");
            Sprite sprite = new Sprite(image);

            // Create a graphical string to display
            Font     arial = new Font("arial.ttf");
            String2D text  = new String2D("Hello SFML.Net", arial);

            // Load a music to play
            Music music = new Music("nice_music.ogg");
            music.Play();

            // Start the game loop
            while (app.IsOpened())
            {
                // Process events
                app.DispatchEvents();

                // Clear screen
                app.Clear();

                // Draw the sprite
                app.Draw(sprite);

                // Draw the string
                app.Draw(text);

                // Update the window
                app.Display();
            }
        }
    }
}


An empty console window comes up, but no SFML screen. Any ideas. I did remove the sprite and music portion though, and just left the text. Do I not have to use CMake to build the library like you do with the C++ version?
« Last Edit: April 23, 2012, 03:52:47 am by joeparrilla »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Seting up with C#/Visual Studio
« Reply #3 on: April 23, 2012, 04:02:34 am »
Just looking over your code, I can see that your close code is wrong. Just ignore the sender object and close the window.

This code works here, give it a try and then work it into your own code.
namespace SFMLTest
{
    class Program
    {
        static void Main(string[] args)
        {
            MySFMLProgram app = new MySFMLProgram();
            app.StartSFMLProgram();
        }
    }
    class MySFMLProgram
    {
        RenderWindow _window;
        public void StartSFMLProgram()
        {
            _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();
            }
        }
        void OnClosed(object sender, EventArgs e)
        {
            _window.Close();
        }
    }
}

Let me know how this code works for you.

As for CMake, no you do not need it for the SFML.Net version. The unmanaged libraries come precompiled in the "extlibs" folder. If you did want to compile the unmanaged parts yourself you would need to use CMake to build SFML first, and then build CSFML.
« Last Edit: April 23, 2012, 11:11:28 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Seting up with C#/Visual Studio
« Reply #4 on: April 23, 2012, 08:06:32 am »
Quote
Just looking over your code, I can see that your close code is wrong.
No, it's perfectly correct, and it's the right way to do things when you don't store the window as a class member.

Quote
On a side note, make sure you are using SFML.Net 2.0 RC. RenderWindow.IsOpen() doesn't exist in the RC. RenderWindow.IsOpened() is what should be in your code.
You're right (your code is ok) but it's the other way round: IsOpen is correct, IsOpened is not ;)
Laurent Gomila - SFML developer

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Seting up with C#/Visual Studio
« Reply #5 on: April 23, 2012, 11:10:50 am »
Quote
Just looking over your code, I can see that your close code is wrong.
No, it's perfectly correct, and it's the right way to do things when you don't store the window as a class member.
???  I never knew that, Its just that I never bothered to check what SFML returns on the Closed event XD.

Quote
On a side note, make sure you are using SFML.Net 2.0 RC. RenderWindow.IsOpen() doesn't exist in the RC. RenderWindow.IsOpened() is what should be in your code.
You're right (your code is ok) but it's the other way round: IsOpen is correct, IsOpened is not ;)
Thanks for straightening me here too. Total goof by me... Even in the code I posted it is IsOpen(). Its the benefit of a late night post.  8)
« Last Edit: April 23, 2012, 11:13:00 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Seting up with C#/Visual Studio
« Reply #6 on: April 23, 2012, 11:13:50 am »
Quote
I never knew that, Its just that I never bothered to check what SFML sends on the Closed event XD.
In fact it's the standard way of handling events, there's nothing SFML-specific here.
Laurent Gomila - SFML developer

joeparrilla

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Seting up with C#/Visual Studio
« Reply #7 on: April 23, 2012, 06:32:11 pm »
I pasted that code DIRECTLY from the documentation that came with the SFML.NET download. Im guessing that I am doing something wrong with the setup because no window appears, only the console. I added the 3 .dll's as references to the project. I then added the 3 .dlls from the extlib folder as external items to the project., Then I finally set them all to "copy if newer". Am I missing something?

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Seting up with C#/Visual Studio
« Reply #8 on: April 23, 2012, 07:02:29 pm »
Ok here is the your issue. The example code you posted above is for 1.6. Inside the doc folder of the SFML.Net 2.0 folder the example code is correct, so I think you may have mistakenly mixed a 1.6 doc in somewhere.

SFML 2.0 entirely changed and broke all existing 1.6 code.

Joe, just copy the code I gave you above^^ and let me know if it works.  :D
« Last Edit: April 23, 2012, 07:06:29 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Seting up with C#/Visual Studio
« Reply #9 on: April 23, 2012, 07:04:23 pm »
There's nothing to fix. The doc in the 2.0-rc package is up to date. This doc can only be in a 1.6 package.
Laurent Gomila - SFML developer

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Seting up with C#/Visual Studio
« Reply #10 on: April 23, 2012, 07:09:53 pm »
There's nothing to fix. The doc in the 2.0-rc package is up to date. This doc can only be in a 1.6 package.
I noticed that after I posted. I went and took a look at the 2.0-rc package so i edited my post^^...

But there is a small mistake. The 2.0-rc package has RenderWindow.IsOpened() instead of .IsOpen().
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

joeparrilla

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Seting up with C#/Visual Studio
« Reply #11 on: April 23, 2012, 08:05:12 pm »
Ok so it works. Weird. I was accidentally using the 1.6 release, but I was using the example from the 1.6 docs anyway... so it should've worked. I now have the 2.0 release, but it forces me to use the x86 version even though I am using a 64 bit system. Not a big issue... it works anyways.

I did have to change while (window.IsOpened()) to while (window.IsOpen()) though. Is that a mistake in the example? It seems like IsOpen() is the correct method.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Seting up with C#/Visual Studio
« Reply #12 on: April 23, 2012, 08:11:50 pm »
Quote
The 2.0-rc package has RenderWindow.IsOpened() instead of .IsOpen()
Oh, you're right on this one ;D

Quote
it forces me to use the x86 version
Why?
Laurent Gomila - SFML developer

joeparrilla

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Seting up with C#/Visual Studio
« Reply #13 on: April 24, 2012, 12:24:49 am »
Quote
it forces me to use the x86 version
Why?

Not sure. I attached the x64 versions and VS was complaining that it was not compatible with my processor. Im running an i7 Quad Core with 64bit Windows 7

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Seting up with C#/Visual Studio
« Reply #14 on: April 24, 2012, 12:40:30 am »
Ok so it works. Weird. I was accidentally using the 1.6 release, but I was using the example from the 1.6 docs anyway... so it should've worked.
If you have a ATI card that would explain why 1.6 wasn't working. http://en.sfml-dev.org/forums/index.php?topic=3438.0
I did have to change while (window.IsOpened()) to while (window.IsOpen()) though. Is that a mistake in the example? It seems like IsOpen() is the correct method.
Yes, .IsOpen() is correct. Read above^^ as me and Laurent went back and forth on it.  ;D

As for the 64 bit trouble, I will pass to Laurent  8)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor