Page 1 of 1

Compiling SDL for Mac

Posted: Fri Jun 10, 2011 11:42 am
by VoidElite
Hey guys! Whilst planning to port my engine(http://www.youtube.com/user/LunarSanity?feature=mhee) to Mac I decided to run a test. Not only am I planning to port it to Mac but to port the C++ code over to Objective-C and compile using GNUstep when I'm on Windows(I like Objective-C).

My code:

Code: Select all

#import <Foundation/Foundation.h>
#include <stdio.h>
#include <SDL/SDL.h>

#undef main
int main(int argc,char* argv[]){
  NSAutoreleasePool* pool=[[NSAutoreleasePool alloc]init];
  
  //The images
    SDL_Surface* hello = NULL;
    SDL_Surface* screen = NULL;

    //Start SDL
    SDL_Init( SDL_INIT_EVERYTHING );

    //Set up screen
    screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );

    //Load image
    hello = SDL_LoadBMP( "hello.bmp" );

    //Apply image to screen
    SDL_BlitSurface( hello, NULL, screen, NULL );

    //Update Screen
    SDL_Flip( screen );

    //Pause
    SDL_Delay( 2000 );

    //Free the loaded image
    SDL_FreeSurface( hello );

    //Quit SDL
    SDL_Quit();
  
  [pool drain];
  return 0;
}
Compiles fine with:

Code: Select all

Marc-Reeds-MacBook-Pro-2:documents Marc$ gcc -o Test Test.m -framework Foundation -framework SDL
My problem occurs at runtime when I execute:

Code: Select all

Marc-Reeds-MacBook-Pro-2:documents Marc$ ./Test
I get these runtime exceptions:

Code: Select all

Jun 10 17:36:37 Marc-Reeds-MacBook-Pro-2.local Test[799] <Error>: kCGErrorInvalidConnection: CGSGetCurrentCursorLocation: Invalid connection
Jun 10 17:36:37 Marc-Reeds-MacBook-Pro-2.local Test[799] <Error>: kCGErrorFailure: Set a breakpoint @ CGErrorBreakpoint() to catch errors as they are logged.
Jun 10 17:36:37 Marc-Reeds-MacBook-Pro-2.local Test[799] <Error>: kCGErrorInvalidConnection: CGSGetCurrentCursorLocation: Invalid connection
Jun 10 17:36:37 Marc-Reeds-MacBook-Pro-2.local Test[799] <Error>: kCGErrorInvalidConnection: CGSNewWindowWithOpaqueShape: Invalid connection
2011-06-10 17:36:37.761 Test[799:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error (1002) creating CGSWindow on line 262'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff96763216 __exceptionPreprocess + 198
	1   libobjc.A.dylib                     0x00007fff9614ca2e objc_exception_throw + 43
	2   CoreFoundation                      0x00007fff9676304a +[NSException raise:format:arguments:] + 106
	3   CoreFoundation                      0x00007fff96762fd4 +[NSException raise:format:] + 116
	4   AppKit                              0x00007fff8f2bf084 _NSCreateWindowWithOpaqueShape2 + 788
	5   AppKit                              0x00007fff8f254749 -[NSWindow _commonAwake] + 1845
	6   AppKit                              0x00007fff8f250ebc -[NSWindow _commonInitFrame:styleMask:backing:defer:] + 1871
	7   AppKit                              0x00007fff8f24fbd6 -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 1502
	8   AppKit                              0x00007fff8f24f5f2 -[NSWindow initWithContentRect:styleMask:backing:defer:] + 47
	9   SDL                                 0x000000010055be1d SDL_SoftStretch + 24461
	10  SDL                                 0x000000010055a57d SDL_SoftStretch + 18157
	11  SDL                                 0x000000010054f043 SDL_SetVideoMode + 595
	12  Test                                0x0000000100523dc8 main + 152
	13  Test                                0x0000000100523d24 start + 52
	14  ???                                 0x0000000000000001 0x0 + 1
)
terminate called throwing an exceptionAbort trap: 6
NOTE: I have no experience with Cocoa or any Mac Windowing APIs as such.

Anyone know what's wrong???

Re: Compiling SDL for Mac

Posted: Fri Jun 10, 2011 12:05 pm
by avansc
I believe that means your your NSApplication is not initialized.

try adding this

[NSApplication sharedApplication];

http://developer.apple.com/library/mac/ ... TS10003361

Re: Compiling SDL for Mac

Posted: Fri Jun 10, 2011 12:16 pm
by VoidElite
avansc wrote:I believe that means your your NSApplication is not initialized.

try adding this

[NSApplication sharedApplication];

http://developer.apple.com/library/mac/ ... TS10003361
Well I get:

Code: Select all

Test.m: In function ‘main’:
Test.m:9: error: ‘NSApplication’ undeclared (first use in this function)
Test.m:9: error: (Each undeclared identifier is reported only once
Test.m:9: error: for each function it appears in.)
I'm presuming I have to create an instance of NSApplication then pass the message to that?

Re: Compiling SDL for Mac

Posted: Fri Jun 10, 2011 12:23 pm
by VoidElite
Thanks dude! My bad, your code worked it's just I didn't import the Cocoa header also I didn't link. Now it works. ;)

By the way what does that message to NSApplication actually do? Is it a class method?

Re: Compiling SDL for Mac

Posted: Fri Jun 10, 2011 12:31 pm
by avansc
VoidElite wrote:Thanks dude! My bad, your code worked it's just I didn't import the Cocoa header also I didn't link. Now it works. ;)

By the way what does that message to NSApplication actually do? Is it a class method?

The Apple Developer site is very good. Its got tons of samples and tutorial on top of the the Docs.

You can access all this information through Xcode's help as well.
Apple Developer Docs wrote: The sharedApplication class method initializes the display environment and connects your program to the window server and the display server. The NSApplication object maintains a list of all the NSWindow objects the application uses, so it can retrieve any of the application’s NSView objects. sharedApplication also initializes the global variable NSApp, which you use to retrieve the NSApplication instance. sharedApplication only performs the initialization once; if you invoke it more than once, it simply returns the NSApplication object it created previously.
http://developer.apple.com/library/mac/ ... rence.html

Re: Compiling SDL for Mac

Posted: Fri Jun 10, 2011 3:45 pm
by VoidElite
avansc wrote:
VoidElite wrote:Thanks dude! My bad, your code worked it's just I didn't import the Cocoa header also I didn't link. Now it works. ;)

By the way what does that message to NSApplication actually do? Is it a class method?

The Apple Developer site is very good. Its got tons of samples and tutorial on top of the the Docs.

You can access all this information through Xcode's help as well.
Apple Developer Docs wrote: The sharedApplication class method initializes the display environment and connects your program to the window server and the display server. The NSApplication object maintains a list of all the NSWindow objects the application uses, so it can retrieve any of the application’s NSView objects. sharedApplication also initializes the global variable NSApp, which you use to retrieve the NSApplication instance. sharedApplication only performs the initialization once; if you invoke it more than once, it simply returns the NSApplication object it created previously.
http://developer.apple.com/library/mac/ ... rence.html
Thankyou! I shall live on that documentation from now on. Is there like a Apple Developer documentation app for iOS? Just for quick reference. Or maybe a book that just contains all that?

Re: Compiling SDL for Mac

Posted: Sat Jun 11, 2011 7:44 pm
by Falco Girgis
VoidElite wrote:Hey guys! Whilst planning to port my engine(http://www.youtube.com/user/LunarSanity?feature=mhee) to Mac I decided to run a test. Not only am I planning to port it to Mac but to port the C++ code over to Objective-C and compile using GNUstep when I'm on Windows(I like Objective-C).
May I be so much of an asshole as to inquire why? Why are you porting C++ code to Objective-C, when it will work perfectly fine as-is and interface without an issue to any Mac-required Objective-C?

I'm just curious as to why you have chosen to put yourself through that. If it's to learn Objective-C, go for it. I'm just making sure you aren't misinformed regarding C++ support on OSX/iOS and C++/Objective-C interoperability.

Re: Compiling SDL for Mac

Posted: Sun Jun 12, 2011 8:32 am
by VoidElite
GyroVorbis wrote:
VoidElite wrote:Hey guys! Whilst planning to port my engine(http://www.youtube.com/user/LunarSanity?feature=mhee) to Mac I decided to run a test. Not only am I planning to port it to Mac but to port the C++ code over to Objective-C and compile using GNUstep when I'm on Windows(I like Objective-C).
May I be so much of an asshole as to inquire why? Why are you porting C++ code to Objective-C, when it will work perfectly fine as-is and interface without an issue to any Mac-required Objective-C?

I'm just curious as to why you have chosen to put yourself through that. If it's to learn Objective-C, go for it. I'm just making sure you aren't misinformed regarding C++ support on OSX/iOS and C++/Objective-C interoperability.
I just like the look of Objective-C syntax to be honest.

I am well aware that C/C++/Objective-C are all multi-platform. ;)

Re: Compiling SDL for Mac

Posted: Sun Jun 12, 2011 9:54 am
by avansc
VoidElite wrote:
avansc wrote:
VoidElite wrote:Thanks dude! My bad, your code worked it's just I didn't import the Cocoa header also I didn't link. Now it works. ;)

By the way what does that message to NSApplication actually do? Is it a class method?

The Apple Developer site is very good. Its got tons of samples and tutorial on top of the the Docs.

You can access all this information through Xcode's help as well.
Apple Developer Docs wrote: The sharedApplication class method initializes the display environment and connects your program to the window server and the display server. The NSApplication object maintains a list of all the NSWindow objects the application uses, so it can retrieve any of the application’s NSView objects. sharedApplication also initializes the global variable NSApp, which you use to retrieve the NSApplication instance. sharedApplication only performs the initialization once; if you invoke it more than once, it simply returns the NSApplication object it created previously.
http://developer.apple.com/library/mac/ ... rence.html
Thankyou! I shall live on that documentation from now on. Is there like a Apple Developer documentation app for iOS? Just for quick reference. Or maybe a book that just contains all that?

Yeap there is.

http://developer.apple.com/

and more specifically what you were looking for.

http://developer.apple.com/devcenter/ios/index.action

You'll note that if you are viewing documentation, for example this page,
http://developer.apple.com/library/ios/ ... TP40008793

you have the option of downloading a nifty PDF version, there should be a button in the top right-ish.

http://developer.apple.com/library/ios/ ... gGuide.pdf

Re: Compiling SDL for Mac

Posted: Sun Jun 12, 2011 3:26 pm
by LeonBlade
VoidElite wrote:
GyroVorbis wrote:
VoidElite wrote:Hey guys! Whilst planning to port my engine(http://www.youtube.com/user/LunarSanity?feature=mhee) to Mac I decided to run a test. Not only am I planning to port it to Mac but to port the C++ code over to Objective-C and compile using GNUstep when I'm on Windows(I like Objective-C).
May I be so much of an asshole as to inquire why? Why are you porting C++ code to Objective-C, when it will work perfectly fine as-is and interface without an issue to any Mac-required Objective-C?

I'm just curious as to why you have chosen to put yourself through that. If it's to learn Objective-C, go for it. I'm just making sure you aren't misinformed regarding C++ support on OSX/iOS and C++/Objective-C interoperability.
I just like the look of Objective-C syntax to be honest.

I am well aware that C/C++/Objective-C are all multi-platform. ;)
I use a Mac... but I'll be dammed if I use Objective-C for my game engine...

Re: Compiling SDL for Mac

Posted: Sun Jun 12, 2011 9:47 pm
by Falco Girgis
LeonBlade wrote:
VoidElite wrote:
GyroVorbis wrote:
VoidElite wrote:Hey guys! Whilst planning to port my engine(http://www.youtube.com/user/LunarSanity?feature=mhee) to Mac I decided to run a test. Not only am I planning to port it to Mac but to port the C++ code over to Objective-C and compile using GNUstep when I'm on Windows(I like Objective-C).
May I be so much of an asshole as to inquire why? Why are you porting C++ code to Objective-C, when it will work perfectly fine as-is and interface without an issue to any Mac-required Objective-C?

I'm just curious as to why you have chosen to put yourself through that. If it's to learn Objective-C, go for it. I'm just making sure you aren't misinformed regarding C++ support on OSX/iOS and C++/Objective-C interoperability.
I just like the look of Objective-C syntax to be honest.

I am well aware that C/C++/Objective-C are all multi-platform. ;)
I use a Mac... but I'll be dammed if I use Objective-C for my game engine...
Ditto. And I was a little more confused by the fact that he looked interested in DC development judging by his signature (so it's not like he's going to be porting an Obj-C engine there any day). But to each his own.