OS X, SDL & Cocoa

Goal
Write Java app that uses SDL through JNI and launched from command line.

Progress
Successfully compiled .jnilib for OS X

What I think I need
Need minimal OS X specific code to open a window, etc. in Main.c

Wierd.
There are two types of errors and they are random:

  1. App Launches but all I have is a spinning cursor and can’t do
    anything inside my app window
    OR

SDL Version used: 1.2.5
-JNI- JNI_OnLoad
path:
.:/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java
2003-03-25 10:56:08.712 java[21871] *** _NSAutoreleaseNoPool(): Object
0xa3078478 of class NSCFString autoreleased with no pool in place -
just leaking
2003-03-25 10:56:08.750 java[21871] *** _NSAutoreleaseNoPool(): Object
0xa3078168 of class NSCFString autoreleased with no pool in place -
just leaking
2003-03-25 10:56:08.828 java[21871] *** _NSAutoreleaseNoPool(): Object
0x4b1f200 of class NSCFString autoreleased with no pool in place - just
leaking
2003-03-25 10:56:08.830 java[21871] *** _NSAutoreleaseNoPool(): Object
0x4b1f270 of class NSCFString autoreleased with no pool in place - just
leaking
2003-03-25 10:56:08.831 java[21871] *** _NSAutoreleaseNoPool(): Object
0x4b1e7c0 of class NSCFDictionary autoreleased with no pool in place -
just leaking
2003-03-25 10:56:08.832 java[21871] *** _NSAutoreleaseNoPool(): Object
0x4b1e440 of class NSCFData autoreleased with no pool in place - just
leaking
2003-03-25 10:56:08.833 java[21871] *** _NSAutoreleaseNoPool(): Object
0x4b1e530 of class NSCFData autoreleased with no pool in place - just
leaking
…etc
Fatal signal: Bus Error (SDL Parachute Deployed)

Code form Main.c=================================================================
#include <jni.h>
#include “Main.h”
#include “SDL.h”
#include <cocoa.h>

#ifdef WIN32

#include <stdlib.h>

#endif

/*

  • Class: Main
  • Method: SDLInit
  • Signature: (I)I
    */

typedef struct CPSProcessSerNum
{
UInt32 lo;
UInt32 hi;
} CPSProcessSerNum;

extern OSErr CPSGetCurrentProcess( CPSProcessSerNum *psn);
extern OSErr CPSEnableForegroundOperation( CPSProcessSerNum *psn,
UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5);
extern OSErr CPSSetFrontProcess( CPSProcessSerNum *psn);

@interface NSAppleMenuController:NSObject {}

  • (void)controlMenu:(NSMenu *)aMenu;
    @end

@interface SDLApplication : NSApplication
@end

@implementation SDLApplication
/* Invoked from the Quit menu item */

  • (void)terminate:(id)sender
    {
    /* Post a SDL_QUIT event */
    SDL_Event event;
    event.type = SDL_QUIT;
    SDL_PushEvent(&event);
    }
    @end

JNIEXPORT jint JNICALL Java_sdl_core_Main_SDLInit (JNIEnv * env, jclass
cls, jint ind)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[ SDLApplication sharedApplication ];

  CPSProcessSerNum PSN;
     /* Tell the dock about us */
     if (!CPSGetCurrentProcess(&PSN))
         if  

(!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103))
if (!CPSSetFrontProcess(&PSN))
[SDLApplication sharedApplication];

 [ NSApp setMainMenu:[[NSMenu alloc] init] ];
 NSAppleMenuController *appleMenuController;
 NSMenu *appleMenu;
 NSMenuItem *appleMenuItem;

 appleMenuController = [[NSAppleMenuController alloc] init];
 appleMenu = [[NSMenu alloc] initWithTitle:@""];
 appleMenuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil  

keyEquivalent:@""];

 [appleMenuItem setSubmenu:appleMenu];

 /* yes, we do need to add it and then remove it --
    if you don't add it, it doesn't get displayed
    if you don't remove it, you have an extra, titleless item in the  

menubar
when you remove it, it appears to stick around
very, very odd */
[[NSApp mainMenu] addItem:appleMenuItem];
[appleMenuController controlMenu:appleMenu];
[[NSApp mainMenu] removeItem:appleMenuItem];
[appleMenu release];
[appleMenuItem release];

 NSMenu		*windowMenu;
 NSMenuItem	*windowMenuItem;
 NSMenuItem	*menuItem;


 windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];

 /* "Minimize" item */
 menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize"  

action:@selector(performMiniaturize:) keyEquivalent:@“m”];
[windowMenu addItem:menuItem];
[menuItem release];

 /* Put menu into the menubar */
 windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window"  

action:nil keyEquivalent:@""];
[windowMenuItem setSubmenu:windowMenu];
[[NSApp mainMenu] addItem:windowMenuItem];

 /* Tell the application object that this is now the window menu */
 [NSApp setWindowsMenu:windowMenu];

 /* Finally give up our references to the objects */
 [windowMenu release];
 [windowMenuItem release];


[NSApp finishLaunching];
[pool release];

int val = SDL_Init(ind);
return val;
}

/*

  • Class: Main
  • Method: SDLInitSubSystem
  • Signature: (I)I
    */
    JNIEXPORT jint JNICALL Java_sdl_core_Main_SDLInitSubSystem (JNIEnv
    *env, jclass cls, jint ind)
    {
    int val = SDL_InitSubSystem(ind);
    return val;
    }

/*

  • Class: Main
  • Method: SDLQuitSubSystem
  • Signature: (I)V
    */
    JNIEXPORT void JNICALL Java_sdl_core_Main_SDLQuitSubSystem (JNIEnv *
    env, jclass cls, jint ind)
    {
    SDL_QuitSubSystem(ind);
    }

/*

  • Class: Main
  • Method: SDLWasInit
  • Signature: (I)I
    */
    JNIEXPORT jint JNICALL Java_sdl_core_Main_SDLWasInit (JNIEnv * env,
    jclass cls, jint ind)
    {
    return (jint)-1;

}

/*

  • Class: Main
  • Method: SDLQuit
  • Signature: ()V
    */
    JNIEXPORT void JNICALL Java_sdl_core_Main_SDLQuit (JNIEnv *env, jclass
    cls)
    {
    SDL_Quit();
    }

/*

  • Class: sdl_core_Main
  • Method: Rand
  • Signature: ()I
    */
    JNIEXPORT jint JNICALL Java_sdl_core_Main_Rand
    (JNIEnv * env, jclass cls)
    {
    return rand();
    }

/*

  • Class: sdl_core_Main
  • Method: SRand
  • Signature: (I)V
    */
    JNIEXPORT void JNICALL Java_sdl_core_Main_SRand
    (JNIEnv * env, jclass cls, jint ind)
    {
    srand(ind);
    }
    =================================================================


Brian Sugar
President & CEO

Sugar Media Inc
525 Brannan Suite 200
San Francisco, CA 94107
http://sugar.tv

415 794 3541
@Brian_Sugar