Suppose I want to make a program which has no user interaction, just a static animation of some shapes on the screen and then ends. If I don’t add an event loop in the main loop, then the window won’t show up when I try to render stuff on the screen. It only works if I add an event loop.
Is there any technical reason why this is? Internally, does SDL only render stuff when you poll events? Or is the reason more of an OS thing?
AFIK this is platform dependent. I know it is an issue on macOS, but I’ve seen enough bad SDL examples not include it to figure it must work on at least Windows.
I dont know for certain, but I can speculate that something needs to run the GUI runloop so you either need to do it yourself, or you let SDL do it by polling events.
If you are talking about macOS I can elaborate quite a bit more if you want.
Yes I am on macOS. Can you elaborate a bit on how it works on macOS?
The Cocoa (and CoreFoundation) frameworks are designed with the concept of “runloops”. These runloops are how all notifications, UI events, timers, etc are serviced. If the runloop doesnt get ran then none of these “runloop sources” will fire. One of these sources is the mach port connecting to the windowserver, so if it doesnt get serviced, no window gets created.
SDL runs the runloop when you poll events by calling
Cocoa_PumpEventsUntilDate which calls
NSApp nextEventMatchingMask:NSEventMaskAny untilDate:expiration inMode:NSDefaultRunLoopMode dequeue:YES.
if you want you can always run the runloop yourself. If you are using Coca you can use
run method of
NSApplication on the main thread. You can also get more fine grain control by directly using
NSRunloop. You can use CoreFoundation if you want to avoid Objective-C, it has a corresponding
consider that if you have no event loop then the application GUI will be completely unresponsive (eg menus, dock icon, etc). The entire Cocoa UI is driven by the runloop, to make an application do what you want without a runloop you would have to avoid Cocoa and SDL uses Cocoa.