OSX - Using Objective-C to write Monkey libs?

Monkey Targets Forums/Desktop/OSX - Using Objective-C to write Monkey libs?

Danilo(Posted 2014) [#1]
How can we write addon-libraries for targets C++ or Desktop_Game
using Objective-C on MacOSX? Somebody got an example, please?

We can write C++ functions and import it with:
Import "native/Window.cpp"

The functions in Window.cpp get inserted directly into the final generated c++ code. This works.


How is it possible to use this with Objective-C, so we can write a Cocoa GUI lib?

.m files just get ignored:
Import "native/Window.m"

The generated code contains the comment:
//#include "native/Window.m"


I also tried to make a static lib with Xcode and include the resulting lib:
Import "native/libWindow.a"
#LIBS+="native/libWindow.a"

Those files don't get copied to build folder and seem to be ignored.


Danilo(Posted 2014) [#2]
Answer:
We can just use Objective-C code within C++ codes, when using the Desktop_Game target.

Window.monkey
Strict

Import "Window.cpp"

Extern
    Function CreateWindow:Int(x:Int,y:Int,width:Int,height:Int,title:String)="CreateWindow"
Public

Function Main:Int()
    CreateWindow(100,100,800,600,"My Monkey Window")
    Return 0
End

Window.cpp
#include <cocoa/cocoa.h>

@interface MyWindowDelegate : NSObject<NSWindowDelegate>
@end

@implementation MyWindowDelegate
    - (BOOL)windowShouldClose:(id)window {
    	[window release];
    	NSApplication *app = [NSApplication sharedApplication];
    	[app terminate:app];
        return YES;
    }

    - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
        return NSTerminateNow;
    }
@end

int CreateWindow(int x, int y, int width, int height, String title) {
    NSApplication *app = [NSApplication sharedApplication];
    unsigned int styleMask = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask;

    NSWindow *w1 = [ [NSWindow alloc]
                     initWithContentRect:NSMakeRect( x, y, width, height )
                     styleMask:styleMask
                     backing:NSBackingStoreBuffered
                     defer:NO
                   ];

    MyWindowDelegate *delegate = [[MyWindowDelegate alloc] init];
    [w1 setDelegate:delegate];
    [w1 makeKeyWindow];
    [w1 makeKeyAndOrderFront:app];
    [w1 setTitle:title.ToNSString()];
    [w1 setAcceptsMouseMovedEvents:YES];
    [w1 setPreventsApplicationTerminationWhenModal:NO];
    [w1 setReleasedWhenClosed:YES];

    [w1 center];
    [w1 update];
    [w1 display];

    //[app runModalForWindow:w1];
    [app run];
    //[w1 release];

    return 0;
}



Danilo(Posted 2014) [#3]
Updated to close the window and terminate the app.


Emil_halim(Posted 2014) [#4]
Hi Danilo,

i have no idea about Objective-C , but i have add new features to trans ext.

with PrjImport new keyword , we can include many files with our project , it's file has corresponding language compiler, such as c,cpp,asm,s,zir.

and each file will compiled and linked to our final exe file, now it works for gcc under windows.