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

Author Topic: Mono, Pinvoke, MacOS, issue with events  (Read 2926 times)

0 Members and 1 Guest are viewing this topic.

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Mono, Pinvoke, MacOS, issue with events
« on: September 11, 2012, 11:42:33 am »
I'm trying to integrate SFML window in my application, i created a separate SFML window (with Pinvoke), everything works well, except event handeling. In C# i created a timer :
renderTimer.Interval = 1;
renderTimer.Enabled = true;
renderTimer.Tick += (object sender, EventArgs e) => {
        Render();
};

[DllImport("Lib", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
static extern void Render();
 

In Render function i do poling events and rendering content. I faced a problem that i can catch MouseButtonPressed event only if i start to spam clicking mouse button (is there a multithreaded implementation of event handeling ?). With combination of isButtonPressed and getPosition everything works as expected.
P.S. on Windows everything is Ok.
« Last Edit: September 11, 2012, 11:44:22 am by Acrobat »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Mono, Pinvoke, MacOS, issue with events
« Reply #1 on: September 11, 2012, 11:48:46 am »
It's almost impossible to help you, with a project that mixes two languages and almost no code shown.

You should try to reduce the problem to the minimum, and show the relevant code.
Laurent Gomila - SFML developer

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Re: Mono, Pinvoke, MacOS, issue with events
« Reply #2 on: September 11, 2012, 04:28:28 pm »
No need for minimum code, seems to me it's not so easy to fix.
1. if i create write main loop like in C++ everything works (but without form), i tried to create and run form in parallel, but it freezes, got no message updates. (also maybe something with Mono, haven't tested it in Windows)
2. Tried to create sfml window and poll events in separate thread, got messages about OS/OSX limitations.
3. Tried to embed window (by passing handle) in form, got messages that NSWindow or NSView expected (but that code works well in windows)
don't have any ideas.
P.S. minimal example is not so minimal  ;D

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Mono, Pinvoke, MacOS, issue with events
« Reply #3 on: September 11, 2012, 04:30:17 pm »
OS X can be so tricky sometimes... ;D

I have no idea, maybe Hiura will be able to help you.
Laurent Gomila - SFML developer

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Mono, Pinvoke, MacOS, issue with events
« Reply #4 on: September 11, 2012, 08:13:02 pm »
Unfortunately, I've no idea how Mono is integrated with Cocoa. It can be anything, including something that Apple "forget" to document and that Mono doesn't support..

The only thing that might (with triple conditional) help is this :
try changing YES by NO in https://github.com/SFML/SFML/blob/master/src/SFML/Window/OSX/SFApplication.m#L46, recompile SFML and try your app.

But this means memory leaks!


EDIT :

Sorry. Forget that. It results in undefined behaviour. So I've really no clue for this issue.
« Last Edit: September 11, 2012, 08:23:29 pm by Hiura »
SFML / OS X developer

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Re: Mono, Pinvoke, MacOS, issue with events
« Reply #5 on: September 12, 2012, 08:32:53 am »
I'm parsing events from C# (created a timer and call update)
The difference between Win and OSX version :
In Windows : events are stored until they are polled (and no problem).
In OSX they somehow disappear from queue, sometimes i can handle events - at the moment when event occurs and it was momentary polled (not so big chance)

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Re: Mono, Pinvoke, MacOS, issue with events
« Reply #6 on: September 12, 2012, 09:12:18 am »
I can't understand how are they removed from event queue without polling. (i assume they are received)

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Mono, Pinvoke, MacOS, issue with events
« Reply #7 on: September 12, 2012, 09:25:07 am »
The thing is that SFML relies mostly on hacks : Cocoa was definitely not design to be used that way. And I guess Mono does some hacking too. So maybe one of those hacks clashes with another one, or maybe there is simply a bug in Mono. I don't know.

Maybe a minimal code that reproduce the problem can help me getting some insights on the issue.
SFML / OS X developer

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Re: Mono, Pinvoke, MacOS, issue with events
« Reply #8 on: September 12, 2012, 09:50:45 am »
Quote
Maybe a minimal code that reproduce the problem can help me getting some insights on the issue.
You can take a .net tutorial, run it with mono.
Then add a form, and try to run it in parallel  :D
I did it like that :
1. In ctor i initialized sfml.
2. Created a timer, timer calls Frame :
API_EXPORT void Frame()
{
        sf::RenderWindow & window = ... get window
        sf::Event event;
        while (window.pollEvent(event)) {
                // get events here
        }
        // clear
        // draw
}
 
3. I use delegates to get results on events :

#ifdef SFML_SYSTEM_WINDOWS
typedef void (__stdcall * clickDelegate)(cVector2);
#else
typedef void (* clickDelegate)(cVector2);
#endif
 

unsafe private delegate void OnClickDelegate(PointF pos);

unsafe private void click(PointF pos)
{
       Text = pos.X.ToString() + "  " + pos.Y.ToString();
}
 

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Mono, Pinvoke, MacOS, issue with events
« Reply #9 on: September 12, 2012, 10:15:01 am »
I've never code any .Net app in fact. (Only a few bit of silverlight last year, and now I've forget everything..)

Could you give me something that I can simply build & run? That would be awesome.  ;)
SFML / OS X developer

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Re: Mono, Pinvoke, MacOS, issue with events
« Reply #10 on: September 12, 2012, 11:01:48 am »
I'll make a complete and minimal project later, currentrly i'm on windows.

 

anything