Compiling SDL for Mac

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
VoidElite
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 54
Joined: Sun Apr 24, 2011 5:25 am
Current Project: Lunar Sanity, 2D engine for PC.
Favorite Gaming Platforms: Playstation
Programming Language of Choice: C
Location: England

Compiling SDL for Mac

Post 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???
I love 16-bit Assembly language programming. I'm currently having an affair with C++. I'm working on a 2D Game Engine called Lunar Sanity for PC and soon DC. I own three games consoles: Dreamcast, Xbox 360 and Atari Flashback. I'm on a Mac and soon a PC. I love Windows XP as it works great(can run 16-bit GUIs). I've been programming for 3 years(since I was 11).

I settling into my hybrid(procedural&object orientated) life. It's all good so far. :)
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Compiling SDL for Mac

Post by avansc »

I believe that means your your NSApplication is not initialized.

try adding this

[NSApplication sharedApplication];

http://developer.apple.com/library/mac/ ... TS10003361
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
VoidElite
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 54
Joined: Sun Apr 24, 2011 5:25 am
Current Project: Lunar Sanity, 2D engine for PC.
Favorite Gaming Platforms: Playstation
Programming Language of Choice: C
Location: England

Re: Compiling SDL for Mac

Post 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?
I love 16-bit Assembly language programming. I'm currently having an affair with C++. I'm working on a 2D Game Engine called Lunar Sanity for PC and soon DC. I own three games consoles: Dreamcast, Xbox 360 and Atari Flashback. I'm on a Mac and soon a PC. I love Windows XP as it works great(can run 16-bit GUIs). I've been programming for 3 years(since I was 11).

I settling into my hybrid(procedural&object orientated) life. It's all good so far. :)
User avatar
VoidElite
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 54
Joined: Sun Apr 24, 2011 5:25 am
Current Project: Lunar Sanity, 2D engine for PC.
Favorite Gaming Platforms: Playstation
Programming Language of Choice: C
Location: England

Re: Compiling SDL for Mac

Post 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?
I love 16-bit Assembly language programming. I'm currently having an affair with C++. I'm working on a 2D Game Engine called Lunar Sanity for PC and soon DC. I own three games consoles: Dreamcast, Xbox 360 and Atari Flashback. I'm on a Mac and soon a PC. I love Windows XP as it works great(can run 16-bit GUIs). I've been programming for 3 years(since I was 11).

I settling into my hybrid(procedural&object orientated) life. It's all good so far. :)
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Compiling SDL for Mac

Post 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
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
VoidElite
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 54
Joined: Sun Apr 24, 2011 5:25 am
Current Project: Lunar Sanity, 2D engine for PC.
Favorite Gaming Platforms: Playstation
Programming Language of Choice: C
Location: England

Re: Compiling SDL for Mac

Post 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?
I love 16-bit Assembly language programming. I'm currently having an affair with C++. I'm working on a 2D Game Engine called Lunar Sanity for PC and soon DC. I own three games consoles: Dreamcast, Xbox 360 and Atari Flashback. I'm on a Mac and soon a PC. I love Windows XP as it works great(can run 16-bit GUIs). I've been programming for 3 years(since I was 11).

I settling into my hybrid(procedural&object orientated) life. It's all good so far. :)
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Compiling SDL for Mac

Post 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.
User avatar
VoidElite
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 54
Joined: Sun Apr 24, 2011 5:25 am
Current Project: Lunar Sanity, 2D engine for PC.
Favorite Gaming Platforms: Playstation
Programming Language of Choice: C
Location: England

Re: Compiling SDL for Mac

Post 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. ;)
I love 16-bit Assembly language programming. I'm currently having an affair with C++. I'm working on a 2D Game Engine called Lunar Sanity for PC and soon DC. I own three games consoles: Dreamcast, Xbox 360 and Atari Flashback. I'm on a Mac and soon a PC. I love Windows XP as it works great(can run 16-bit GUIs). I've been programming for 3 years(since I was 11).

I settling into my hybrid(procedural&object orientated) life. It's all good so far. :)
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Compiling SDL for Mac

Post 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
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: Compiling SDL for Mac

Post 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...
There's no place like ~/
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Compiling SDL for Mac

Post 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.
Post Reply