开发者

How to create a Cocoa library and use it in python

I've been making a game and the python library I was used is terrible (Pyglet). I want to try using Cocoa for the OSX version.

I'll be able to figure out using the objects from classes like NSWindow and NSOpenGLView and then put these objects in my own class for the game loop.

I have no idea how I can use PyObjC to load a dynamic Objective-C library I can make and then use the class I will make in python to setup the game which I suppose can be looped by NSTimer.

However, the loop method will also need to call a python method from one of many python classes. My game consists of many python classes which are used for different sections of the game (Mapmaker,GameSession,AnacondaGame etc.). The game loop will need to call a loop method in any of these classes depending on the current section and pass even information.

PyObjC is "bi-directional" apparently so how is that done?

Alternatively I could create two methods to be called by python and I add the python code in-between, where the loop is controlled by python.

The "documentation" on the PyObjC website only seem to explain how to use Cocoa in python and nothing else.

What I can't do is make a fixed GUI with the interface builder because the library will need to create windows based on the python input to an initialisation method of my class.

Knowing t开发者_运维技巧he syntax of Objective-C isn't a big problem and I can reefer to the Cocoa documentation to make the objects I require.

Thank you for any help. It will be appreciated very much. I'm sick of using broken libraries like pygame and pyglet, using the platform specific OS APIs seems to be the best method to ensure quality.


PyObjC bridges Python to the Objective-C runtime, so if you create NSObject subclasses in Python, they'll be accessible from Objective-C code running in the same process. What this means is that you'll need to encapsulate all of your Python functionality in a subclass of NSObject that you can access over the bridge.

The way I'd do this is by having a singleton controller class on the Objective-C side that has a method like -(void)pythonReady:(PythonClass *)pythonObject, and also handles the loading of the Python code (which ensures that the controller class exists when your Python code is loaded). Then, in your Python code, after creating an instance of your PythonClass, you can call pythonReady: on your controller singleton. Then, in pythonReady: on the Objective-C side, you can call whatever methods you need on pythonObject, which will run the code on the Python side.

To load the Python code from your controller class, you can do something like this:

#import <Python/Python.h>

@implementation PythonController (Loading)
- (void)loadPython {
    NSString *pathToScript = @"/some/path/to/script.py";
    setenv("PYTHONPATH", [@"/some/path/to/" UTF8String], 1);
    Py_SetProgramName("/usr/bin/python");
    Py_Initialize();
    FILE *pyScript = fopen([pathToScript UTF8String], "r");
    int result = PyRun_SimpleFile(pyScript, [[pathToScript lastPathComponent] UTF8String]);
    if (result != 0) { NSLog(@"Loading Python Failed!"); }
}
@end

Basically, we simply use the Python C API to run the script inside the current process. The script itself starts the bridge to the runtime in the current process, where you can then use the Cocoa API to access your controller singleton.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜