ReceiveNextEvent Cocoa Equivalent?

Member
Posts: 129
Joined: 2005.02
Post: #16
Yeah I realize that Rasp~
Quote this message in a reply
Member
Posts: 198
Joined: 2005.01
Post: #17
Also, this is pretty basic, but in case you missed it... you have to create at least one thread using AppKit before you can start creating your own pthread-based threads, or it won't realize it needs to go into thread-safe mode for some stuff. You can spawn off a thread that starts up an empty stub and quits immediately, but you have to do that at least once using NSThread.

FWIW, I'm doing something very similar to what you're saying in my stuff and it works fine. I also have a cross-platform setup that assumes control of the "main loop". There was a recent discussion about this on Apple's game dev list and multiple people there seemed to think it was a fine way to do it (as well as spawning another thread to do the "main loop").

Cryptic Allusion Games / Cryptic Allusion, LLC
http://www.cagames.com/
Quote this message in a reply
Member
Posts: 129
Joined: 2005.02
Post: #18
We are utilizing pthreads; not anything via NSThread. I instigated the latest discussion about loops on Mac-games-dev@lists.apple.com this past week, but I'm not sure if that is what you are referring to. John Stiles says there should be a "end all" OpenGL example with a proper event structure, I'm assuming.
Quote this message in a reply
Member
Posts: 198
Joined: 2005.01
Post: #19
Yeah, that was the discussion I was thinking of.

Re Cocoa and threading, this is from the Xcode API docs... If you look up NSThread, and click on "Multithreading" near the top, it has some info about this:

Quote:When you use detachNewThreadSelector:toTarget:withObject:, your application becomes multithreaded. At any time you can send isMultiThreaded to find out if the application is multithreaded, that is, if a thread was ever detached from the main thread. isMultiThreaded returns YES even if the detached thread has completed execution.

When the first thread is detached, NSThread posts an NSWillBecomeMultiThreadedNotification. The observer methods invoked to receive this notification execute in the main thread, not the new thread, before the new thread begins executing.
...
NSThreads are built upon POSIX threads (pthreads). You can create your own pthreads, but you must be cautious about using Cocoa from them. When you detach an NSThread, Cocoa enters a multithreaded mode, which activates greater thread safety measures. When you create your own pthreads, Cocoa does not know to change modes. To force Cocoa into its multithreaded mode, you must detach an NSThread, which could immediately exit. To identify whether Cocoa is already in multithreaded mode, use the NSThread class method isMultiThreaded.

If you need access to the pthread within an NSThread, call the pthread_self function from within that thread.

Cryptic Allusion Games / Cryptic Allusion, LLC
http://www.cagames.com/
Quote this message in a reply
Member
Posts: 129
Joined: 2005.02
Post: #20
that is very very interesting. i wonder if what i'm suffering, listed above (original post about about window crashing), has something to do with this. I'm going to try to detach a thread that does nothing
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #21
If you are using pthreads and not NSThreads you want to ensure that you enter multithreaded mode (you can just make an empty NSThread once during initialization), otherwise none of either AppKit or Foundation are guaranteed to be thread-safe. There's an article about this in the Threaded Programming programming topic in the documentation.

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #22
Wow, sorry, a few people beat me with that information Rasp

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Member
Posts: 129
Joined: 2005.02
Post: #23
Okay I am unfamiliar with NSThreads. Can I spawn one in applicationDidFinishLaunching with:
Code:
    [NSThread detachNewThreadSelector:@selector(deadThread:) toTarget:self withObject:nil];
calling this method:

Code:
- (void) deadThread:(id)arg;
{
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   // empty
   [pool release];
}

This crashes.
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #24
Very strange - that looks fine. Does debugging show you what exactly causes it to crash? What line? Does the thread spawn at all?

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Member
Posts: 129
Joined: 2005.02
Post: #25
It actually crashes when I report if the app is multithreaded with:
NSLog(@"is multi: %@\n", [NSThread isMultiThreaded]);

Code:
Date/Time:      2005-03-27 17:14:56 -0600
OS Version:     10.3.8 (Build 7U16)
Report Version: 2

Command: Game
Path:    /Users/mstultz/Desktop/Cocoa/Game/build/Game.app/Contents/MacOS/Game
Version: ??? (1.0)
PID:     5017
Thread:  0

Exception:  EXC_BAD_ACCESS (0x0001)
Codes:      KERN_PROTECTION_FAILURE (0x0002) at 0x00000007

Thread 0 Crashed:
0   com.apple.CoreFoundation     0x901b5a10 __CFCopyFormattingDescription + 0x18
1   com.apple.CoreFoundation     0x9019ece8 _CFStringAppendFormatAndArgumentsAux + 0xfe8
2   com.apple.CoreFoundation     0x901a4a44 _CFStringCreateWithFormatAndArgumentsAux + 0x90
3   com.apple.Foundation         0x90a5a7a8 NSLogv + 0x4c
4   com.apple.Foundation         0x90a74abc NSLog + 0x1c
5   com.MyCompany.Game         0x0003ceec -[GameController applicationDidFinishLaunching:] + 0xb4 (GameController.mm:25)
Quote this message in a reply
Member
Posts: 129
Joined: 2005.02
Post: #26
I changed the NSLog from %@ to %1, and it reports a 1. So now that it knows that it's a multithreaded App, it still crashes when I go to the about window:

Exception: EXC_BAD_ACCESS (0x0001)
Codes: KERN_PROTECTION_FAILURE (0x0002) at 0x000002c0

Thread 0 Crashed:
0 libobjc.A.dylib 0x908311f4 objc_msgSend + 0x14
1 com.apple.CoreFoundation 0x90195888 CFDictionarySetValue + 0x17c
2 com.apple.Foundation 0x909f4620 -[NSCFDictionary setObject:forKey:] + 0xb8
3 com.apple.AppKit 0x930fd15c -[NSSystemInfoPanel setOptionsDictionary:] + 0x84
4 com.apple.AppKit 0x930fcdc0 -[NSApplication(NSStandardAboutPanel) orderFrontStandardAboutPanelWithOptions:] + 0x40

This is not using NSTimers currently. I am going to try another version with the same setup, using NSTimers later tonight. I still don't see why the about window would bring down the app.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #27
er, %1? Don't you mean %d?
Quote this message in a reply
Member
Posts: 129
Joined: 2005.02
Post: #28
Oops yeah, I was posting that in haste. I meant to say %d reported 1. My mind is just too fast for my fingers Ninja
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #29
So... The code I have is fairly involved, but it works. Basically, I have a general application class (C++) which is subclassed for each platform. Events are read by a loop on the windows side, and by event handlers for Carbon, and the OS specific events are converted to an internal format which is the same on both platforms.

Source archive: http://dog4.dyndns.org:8080/svn/yag/yag/Sources/
(Note that the above URL is not up 100% of the time)

In there, look for YAGUtilitiesLib/application.cpp and .h, and in YAGGraphicsLib/ wingraphicsapp.cpp and macxgraphicsapp.cpp

On the Mac side, a bunch of Carbon event handlers and an idle timer are installed, the relevant function there sits in MacXApplication::Setup(), while on win32 we use an event loop, so there the beef is in Win32Application::RunEventLoop().

Note that everything is mangled with experimental and OpenGL code, but you should be able to get the gist of it.
Quote this message in a reply
Member
Posts: 129
Joined: 2005.02
Post: #30
How does SDL handle their events? I'm sure they deploy their own event scheme, but are they basically tied in a huge while ( 1 ) loop?
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  GetAsyncKeyState( ), equivalent on OS X? Phosphoer 2 4,075 Aug 30, 2009 06:04 PM
Last Post: ThemsAllTook
  Taking advantage of 3DNow, SSE, MMX or whatever the PPC equivalent is called... Jones 3 3,667 Oct 24, 2006 07:06 AM
Last Post: Jones