MHPhoenix OSX : easier xplatform SDL with pygame

This forum is aimed at user contributions, in the form of assets, side projects, code patches and similar.

Moderator: joepal

MHPhoenix OSX : easier xplatform SDL with pygame

Postby piper » Mon Mar 02, 2009 11:36 am

I have succeeded to build and run MHPhoenix trunk r790 on OSX ...
Thanks for this project, it has been a fun first exposure
to 3d/rendering for me.
I hope you will be able to adopt some of the below suggestions
to allow OSX users to benefit from this great project.


There were a few technical hurdles to getting it working on OSX,

1) windows line endings makes svn operations like diff problematic
on OSX (and Linux?), a temporary workaround is to use :

Code: Select all
          svn diff -x --ignore-eol-style


But a better solution is possible by setting some svn properties:
http://www.tribler.org/trac/wiki/svn:eol-style




2) SDL on OSX has special needs regarding initialization and the SDLMain.m
... without this get crashes regarding
2009-02-27 10:58:01.570 Python[31871:613] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error (1002) creating CGSWindow'


I tried a minimal solution, of adding :
Code: Select all
          os-x/include/SDLMain.h
          os-x/src/SDLMAIN.m
          os-x/SDLMain.nib


but came to the conclusion that is would be much easier to let another
project handle these niggles : namely pygame
http://www.pygame.org

Letting it take care of initializing SDL by changing main.py, adding :
Code: Select all
   
import pygame
pygame.init()



The pygame installation I used is based on libsdl* Frameworks
rather than dylib ... requiring some include changes such as :


svn diff -x --ignore-eol-style include/glmodule.h
Code: Select all
Index: include/glmodule.h
===================================================================
--- include/glmodule.h  (revision 790)
+++ include/glmodule.h  (working copy)
@@ -28,8 +28,16 @@
 #ifndef GLMODULE_H
 #define GLMODULE_H 1
 
+
+#ifdef __APPLE__
+#include <SDL/SDL.h>
+#include <SDL/SDL_opengl.h>
+#else
 #include <SDL.h>
 #include <SDL_opengl.h>
+#endif
+
+
 #include "gltexture.h"
 
 void mhDrawText(float x, float y, const char *message);


3) add support for "import mh" by adding to main.c :

Code: Select all
+void initmh()
+{   
+    printf("initmh\n");
+       Py_InitModule("mh", EmbMethods);
+       initGlobals(); /* initialize all our globals */
+#ifdef __APPLE__
+    initMouseScrollWheelTrap();
+#endif
+}


4) To allow zooming on OSX with : ctrl + cmd + mouse/tracpad

Code: Select all
Index: src/glmodule.c
===================================================================
--- src/glmodule.c      (revision 790)
+++ src/glmodule.c      (working copy)
@@ -37,7 +37,7 @@
 #include <SDL_syswm.h>
 #elif __APPLE__
 #include <AGL/agl.h>
-#include <Fonts.h>
+//#include <Fonts.h>
 #else
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
@@ -119,7 +119,10 @@
 #ifdef __WIN32__
         g_sdlImageHandle = SDL_LoadObject("SDL_image");
 #elif __APPLE__
-        g_sdlImageHandle = SDL_LoadObject("libSDL_image-1.2.0.dylib");
+        //g_sdlImageHandle = SDL_LoadObject("libSDL_image-1.2.0.dylib");
+        //g_sdlImageHandle = SDL_LoadObject("/opt/local/lib/libSDL_image-1.2.0.dylib");
+         g_sdlImageHandle = SDL_LoadObject("/Library/Frameworks/SDL_image.framework/SDL_image");
+       
 #else
         g_sdlImageHandle = SDL_LoadObject("libSDL_image-1.2.so.0");
 #endif
@@ -382,7 +385,8 @@

         callMouseMotion(s);
         mhGetPickedCoords(x,y);
-        if (!(G.modifiersKeyState & KMOD_ALT) && !(G.modifiersKeyState & KMOD_CTRL) && !(G.modifiersKeyState & KMOD_SHIFT))
+        //if (!(G.modifiersKeyState & KMOD_ALT) && !(G.modifiersKeyState & KMOD_CTRL) && !(G.modifiersKeyState & KMOD_SHIFT))
+        if (!(G.modifiersKeyState & KMOD_ALT) &&  !(G.modifiersKeyState & KMOD_SHIFT))
         {
             if ( g_buttons[0] && g_buttons[2] )
             {
@@ -400,8 +404,13 @@
                 {
                     if ( g_buttons[2] )
                     {
-                        G.translX += (float) 0.05f * G.diffx;
-                        G.translY -= (float) 0.05f * G.diffy;
+                        //  to give zoom on OS X
+                        if(G.modifiersKeyState & KMOD_CTRL){
+                           G.zoom -= (float) 0.05f * G.diffx;
+                        } else {   
+                           G.translX += (float) 0.05f * G.diffx;
+                           G.translY -= (float) 0.05f * G.diffy;
+                        }   
                     }
                 }
             }



5) rendering missing .sdr ,

I found i had to shader compile (using pixies sdrc)
a few .sl into .sdr with :
Code: Select all
         sdrc *.sl


6) renderer operation requires $PIXIEHOME/bin in the environment PATH

I guess this needs to be more controlled for users without development experience ?





Notes on PyGame building :

PyGame is a mature xplatform python lib on top of SDL... so it
has mature solutions(and documentation) to the
xplatform SDL niggles to do with SDLMain and how
OSX needs that sorted out.

As I prefer to use macports distributions (not fink)
but like to keep using the system python ... I followed
http://www.pygame.org/wiki/MacSVNCompile
to install pygame from source , which uses the framework
installs of its dependencies :

Code: Select all
   sudo port install libsdl-framework
   sudo port install libsdl_image-framework 
   sudo port install libsdl_ttf-framework 
   sudo port install libsdl_mixer-framework
   ...

(the first 2 are needed by MHPhoenix already )
piper
 
Posts: 3
Joined: Mon Mar 02, 2009 10:20 am

Re: MHPhoenix OSX : easier xplatform SDL with pygame

Postby Manuel » Mon Mar 02, 2009 2:06 pm

Hi!
Thanks to compile it. Actually we have a lack of osx maintainers.

piper wrote:
but came to the conclusion that is would be much easier to let another
project handle these niggles : namely pygame
http://www.pygame.org

Letting it take care of initializing SDL by changing main.py, adding :
Code: Select all
   
import pygame
pygame.init()



Pygame require the full python installed on the user machine, so we had avoided to use it. Actually MH can run on Windows machine without force the user to install python and other dependencies (in Blender style...). Of course, we can consider to add it only for OSX, but I prefer to have a most uniform code and dependencies as possible. What happen if you use just the

os-x/include/SDLMain.h
os-x/src/SDLMAIN.m
os-x/SDLMain.nib

?


5) rendering missing .sdr ,

I found i had to shader compile (using pixies sdrc)
a few .sl into .sdr with :
Code: Select all
         sdrc *.sl



Very strange. Take a look at main.py, lines 73-80:

Code: Select all
# Create aqsis shaders
os.system("aqsl data/shaders/aqsis/lightmap_aqsis.sl -o data/shaders/aqsis/lightmap.slx")
os.system("aqsl data/shaders/renderman/skin.sl -o data/shaders/renderman/skin.slx")

# Create pixie shaders
os.system("sdrc data/shaders/pixie/lightmap_pixie.sl -o data/shaders/pixie/lightmap.sdr")
os.system("sdrc data/shaders/pixie/read2dbm_pixie.sl -o data/shaders/pixie/read2dbm.sdr")
os.system("sdrc data/shaders/renderman/skin.sl -o data/shaders/renderman/skin.sdr")


This should compile them automatically.


6) renderer operation requires $PIXIEHOME/bin in the environment PATH

I guess this needs to be more controlled for users without development experience ?


Path is ok in Win and Linux (Tried Ubuntu). Maybe this is an issue of Pixie installer for OSX?
Manuel
 

Re: MHPhoenix OSX : easier xplatform SDL with pygame

Postby piper » Thu Mar 05, 2009 12:19 pm

pygame require the full python installed on the user machine, so we had avoided to use it.
Actually MH can run on Windows machine without force the user to install python and other dependencies (in Blender style...).
Of course, we can consider to add it only for OSX, but I prefer to have a most uniform code and dependencies as possible.


I understand wrt Windows, however on OSX/Linux full python comes as standard, so leveraging the full python together
with Pygame allows a more unified source between platforms as you
can then eliminate most of the OSX only files...

For development building all you need is a setup-osx.py file such as the below
Code: Select all
#!/usr/bin/env python
from distutils.core import setup, Extension

def frameworks(*args):
    lst = []
    for arg in args:
        lst.extend(['-framework', arg])
    return lst

mh   = Extension('mh',
                    include_dirs = ['os-x/include', 'include' , '/Library/Frameworks/SDL.framework/Headers' ],
                    extra_compile_args=[], 
                    extra_link_args=frameworks( 'AGL','AppKit','ApplicationServices',  'GLUT','OpenGL','Python' , 'Cocoa','Foundation' , 'SDL', 'SDL_image' ),
                    libraries = [],
                    library_dirs = ['/opt/local/lib'],
                    sources = ['src/core.c', 'src/glmodule.c' ,'src/main.c' , 'os-x/src/MouseEventTrap.m' ]  )

setup (name = 'MH',
         ext_modules = [mh ]
      )
 


Allowing you to run the app with
Code: Select all
python main.py


For clickable applications I guess py2app could be used



You asked:
What happen if you use just the
os-x/include/SDLMain.h
os-x/src/SDLMAIN.m
os-x/SDLMain.nib
?


I got those from the tarball ...
Code: Select all
svn export http://svn.libsdl.org/branches/SDL-1.2/Xcode.tar.gz


Unfortunately you have to fiddle around with InterfaceBuilder (editing the .nib file) and
the resulting main menu I got was rather wierd... multiple apple icons and garbled text.
Perhaps it could be fixed somehow... but the pygame route was a lot easier
with less code to worry about so I did not pursue the issue.
piper
 
Posts: 3
Joined: Mon Mar 02, 2009 10:20 am

Re: MHPhoenix OSX : easier xplatform SDL with pygame

Postby Manuel » Thu Mar 05, 2009 9:54 pm

I appreciate your help, but we have choose to don't make a only-python-app for various reasons. In example, pygame not only need the full python installation, but even the numpy, or numeric.
Well, in our experience,have more dependencies can create problems, in particular with newbie users, mainly under windows (but over 90% of downloads came from a Windows machine).

We have considered pyopengl too, but there are some problems with them too.
Finally, we have decided to develop a small, compact, and very reliable our C core, to be used as base engine for the MH python architecture. So we should try to avoid pygame and his big dependencies package, trying to compile MH as we actually do in Linux and Windows. If all fail, we can consider pygame...but, I prefer no, at the moment.

What about shader compile issue?

Regards,

Manuel
Manuel
 


Return to User contributions

Who is online

Users browsing this forum: No registered users and 1 guest