Igor Delovski Board Forum Index Igor Delovski Board
My Own Personal Slashdot!
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

NSEvent & NSRunLoop

 
Post new topic   Reply to topic    Igor Delovski Board Forum Index -> Mac Cocoa
Mac Cocoa  
Author Message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 2600
Location: Europe

PostPosted: Wed Sep 22, 2021 5:37 pm    Post subject: NSEvent & NSRunLoop Reply with quote

so - Cocoa: how to run a modal window while performing a background task?

"... if you must use a modal dialog, you can make the main run loop run by
giving it some time while the modal dialog is open:"

Code:
NSModalSession session = [NSApp beginModalSessionForWindow:[self window]];
int result = NSRunContinuesResponse;

while (result == NSRunContinuesResponse)
{
    //run the modal session
    //once the modal window finishes, it will return a different result and break out of the loop
    result = [NSApp runModalSession:session];

    //this gives the main run loop some time so your other code processes
    [[NSRunLoop currentRunLoop] limitDateForMode:NSDefaultRunLoopMode];

    //do some other non-intensive task if necessary
}

[NSApp endModalSession:session];
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 2600
Location: Europe

PostPosted: Wed Sep 22, 2021 5:39 pm    Post subject: Reply with quote

so - Best way to make NSRunLoop wait for a flag to be set?

Code:
NSDate *loopUntil = [NSDate dateWithTimeIntervalSinceNow:0.1];
while (webViewIsLoading && [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate:loopUntil])
  loopUntil = [NSDate dateWithTimeIntervalSinceNow:0.1];


"With your distantFuture timeout, the runloop will wait foreever until it
processes an event. So when it returns to you, it has just processed an event
of some kind."
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 2600
Location: Europe

PostPosted: Thu Sep 23, 2021 12:45 pm    Post subject: Reply with quote

so - What's the modern equivalent of GetNextEvent in Cocoa?

"I suppose the nearest thing to a Cocoa equivalent would be -[NSApplication
nextEventMatchingMask:untilDate:inMode:dequeue:].

The preferred way of handing events is the don't call us, we'll call you
scheme, where the app calls NSApplicationMain or RunApplicationEventLoop
and events are dispatched to you."
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 2600
Location: Europe

PostPosted: Thu Sep 23, 2021 12:54 pm    Post subject: Reply with quote

so - Cocoa message loop? (vs. windows message loop)

Code:
bool quit = false;

while (!quit)   {
   NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES];
   switch([(NSEvent *)event type])  {
      case NSKeyDown:
         quit = true;
         break;
      default:
         [NSApp sendEvent:event];
         break;
   }
   [event release];
}


"But beware... check out the Activity Monitor. You will see that your
application is using 100% CPU.

...

You could use a Core Foundation runloop instead, and have event sources
for your threads so that the run loop will wake up and call processEvents()
methods. This is an improvement over your polling code, since the run loop
will let the thread sleep if there are no events waiting.

See CFRunLoopSourceCreate(), CFRunLoopSourceSignal() and CFRunLoop-
WakeUp() for more information."
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 2600
Location: Europe

PostPosted: Sat Oct 09, 2021 8:20 pm    Post subject: Reply with quote

Handmade Hero - Main game loop on OS X

"So my question is: How do you properly create a game run loop on OS X?

I found 'the windows way' on OS X online and merged it with my code."
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 2600
Location: Europe

PostPosted: Fri Oct 15, 2021 8:40 pm    Post subject: Reply with quote

so - Run loop doesn't proceed events

"You are assuming that running the NSRunLoop in the NSDefaultRunMode
processes user input events such as key presses or mouse clicks. I don't think
that's the case."
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 2600
Location: Europe

PostPosted: Wed Oct 27, 2021 7:28 am    Post subject: Reply with quote

gitlab - 6502js But C

"For programmers, the OS layer contains an example on how to use X11,
Cocoa/Quartz2D or Win32 to draw stuff without a library like SDL2. See for
programmers for more info.

windows.c, linux.c, and mac6502/mac6502/mac.m contain working examples
of how to create a window, receive user input and draw rects using Win32, X11
(via XCB/XKBCommon) and Cocoa/Quartz2D.

If you need, for example, a working example of how to use Cocoa/Quartz2D
with a custom game loop, how to get input via XCB/X11 and XKBCommon, or
how to deal with multiple OSes in general, I hope my code can help you out,
because that information was hard to find."
Back to top
View user's profile Send private message
Ike
Kapetan


Joined: 17 Jun 2006
Posts: 2600
Location: Europe

PostPosted: Sun Dec 05, 2021 1:12 pm    Post subject: Reply with quote

adc - Handling Mouse Events

"Mouse events are dispatched by an NSWindow object to the NSView object
over which the event occurred.
Subsequent key events are dispatched to that view object if it accepts first
responder status."


Example: Cropped Image
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Igor Delovski Board Forum Index -> Mac Cocoa All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Delovski.hr
Powered by php-B.B. © 2001, 2005 php-B.B. Group