Thursday, April 28, 2011

Simple clutter-sharp window


I will quickly show howto create a clutter window using clutter-sharp. Clutter-sharp can be installed using the Ubuntu packet manager.

First of all, a class is needed which provides functions to initialize clutter and call the main loop. This class is named ClutterHelper.

1. Reference clutter functions
[DllImport ("clutter")]
static extern void clutter_main();
  
[DllImport ("clutter")]
static extern void clutter_init (int argc, IntPtr argv);

2. Encapsulate those funcitons
public static void Init( ) {
   clutter_init (0, IntPtr.Zero);
}
  
public static void Main() {
   //Call clutter main loop
   clutter_main(); 
}


The name in DllImport references the dll assembly. In windows this would be "clutter.dll", however in linux the library is named otherwise. To tell mono which name should be used in linux, a config file is used. This config file must have the same name as the assembly with an appendix ".config" (for instance "MyAssembly.exe.config") and must be in the same directory. In this config file the name of the windows library and the linux library are then mapped.





After creating this helper class, a main class Main is used to create a clutter window. In the Main function of this class clutter is initialized, a new stage is created and the main-loop is called.

static void Main ()
{
   ClutterHelper.Init();

   Stage stage = Stage.Default;
   stage.Color = new Clutter.Color(0,0,0,255);
   stage.SetSize(512,512);

   stage.Show();
   
   ClutterHelper.Main();
}

1 comment:

  1. are you compiling this with any '-pkg:' attributes? or did you have to specify your library references in mono.

    I've been trying to make a simple proof-of-functionality program using clutter-sharp but am having little to no luck. I can't seem to get Clutter_init() to do anything but spew errors. any help would be greatly appreciated.

    -Brian (bjenks22446@gmail.com)

    ReplyDelete