Archive for the ‘Mac’ Category

Mouse tracking in NSView

Saturday, February 13th, 2010

Tracking the position of the mouse does seem like a trivial task. At a first glance it seemed like it would be a matter to override the -mouseMoved: selector in my NSView subclass and make sure the window accepts mouseMoved events by calling [[self window] setAcceptsMouseMovedEvents:YES]; when the view had loaded. Well, not exactly. I tried every conceivable combination of placements of these peices of code, including making the view the first responder, but I could never manage to intercept the mouseMoved event.

Then I found out about NSTrackingArea. Perhaps I was being overly ignorant, but from reading the event handling docs, it was not clear to me that I would need to use a tracking area to be able to recieve mouseMoved events.

Using NSTrackingArea is pretty straight forward, although at first I got a cryptic error message in the console:

trackingArea options 0x2 do not specify when the tracking area is active

As it turns out, NSTrackingArea requires a combination of several options to work properly. In this case I needed to supply “NSTrackingMouseMoved+NSTrackingActiveInKeyWindow” in the options: attribute.

Putting the following code in the -viewDidMoveToWindow method in my NSView subclass finally managed to get the view to recieve mouseMoved events:

NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:[self frame]
	options:NSTrackingMouseMoved+NSTrackingActiveInKeyWindow
	owner:self
	userInfo:nil];
[self addTrackingArea:trackingArea];
[self becomeFirstResponder];

ImageSplitter.app

Friday, January 29th, 2010

Did you ever feel the need to split a big image into a bunch of equally sized slices? Well, I do sometimes, so I decided to make a permanent solution to the problem. I prestent to you: ImageSplitter.app

ImageSplitter a very simple Cocoa application that does one thing. Can you guess it? Correct, it splits images :)

I needed a simple way to split large background images into smaller chunks for an iPhone game that I’m currently working on. Im sure there are a lot of apps out there that could do this for me, but none that I could find that did only that, and in the specific way that I needed.

Well, now there is!

Screen shot 2010-01-29 at 13.15.24

Simply drag the image onto the image well, set the slice size and format, and hit Split. The image will split up into a series of files named mySplittedImage_0.png, mySplittedImage_1.png etc.

Happy splitting!

SamHaXe + Snow Leopard

Sunday, November 29th, 2009

Ex spies and big cats don’t always get along well. The main problem you will face when trying to compile SamHaXe on OS X 10.6 is that the Neko version you got from installing HaXe (Which I will assume you have installed) is a 32 bit one. SamHaXe will not be able to link against libneko.dylib when you compile, because your target architecture is 64-bit in Snow Leopard.

After some fiddling around and some kick-ass help from the SamHaXe mailing list I finally managed to get SamHaXe up and running on Snow Leopard. In a nutshell, the solution is to compile a 64-bit Neko before you compile SamHaXe.

Here’s how how to do it:

  1. First install MacPorts. It will make it a lot easier to get the right dependencies.
  2. Then install PCRE and ImageMagick:
    $ sudo port install ImageMagick pcre

    This will take ages, so go get a good book or something. :)

  3. Neko will need to link against Hans Boehms garbage collector, so you will need to compile and install that before getting on with Neko. Go and get version 7.1 from the downloads section at http://www.hpl.hp.com/personal/Hans_Boehm/gc/.
    If you configure and compile it right away you will get an error due to Apples aggressive deprecation policy, so you will have to get your hands dirty with some preprocessor code here.
    Open mach_dep.c and find these lines:

    #if !defined(HAVE_PUSH_REGS) && defined(UNIX_LIKE)
    # include <ucontext.h>
    #endif

    Change them to this:

    #if !defined(HAVE_PUSH_REGS) && defined(UNIX_LIKE) && !defined(DARWIN)
    # include <ucontext.h>
    #elif defined(DARWIN)
    # include <sys/ucontext.h>
    #endif

    Now you can do the usual

    $ ./configure
    $ make
    $ make install
  4. Ok, now you’re all set for compiling a 64-bit Neko. Version 1.8.1 that is the current official version has a bug in it that prevents using modules on Snow Leopard, so you will have to get the CVS version. There’s instructions on the Neko downloads page: http://nekovm.org/download.
    Unfortunately, there’s a new OS X bug in the CVS version that you will have to work around manually for now (Hopefully this is fixed soon though). Here’s how:

    • Create a file called “clock_gettime_stub.c” in [neko-source-dir]/libs/std. Copy the contents from http://le-depotoir.googlecode.com/svn/trunk/misc/clock_gettime_stub.c into that file.
      [Edit: It appears that this file doesn't get compiled. So all you need is the change to neko.h below. This will allow Neko to compile, but I suspect it will crash at run time if something accesses the sys_thread_cpu_time() function. I will leave this be as long as it works for me though.]
    • Open [neko-source-dir]/vm/neko.h and put the following lines right above C_FUNCTION_END at the bottom of the file:
      #ifdef NEKO_MAC
       typedef enum {
           CLOCK_REALTIME,
           CLOCK_MONOTONIC,
           CLOCK_PROCESS_CPUTIME_ID,
           CLOCK_THREAD_CPUTIME_ID
       } clockid_t;
       EXTERN int clock_gettime(clockid_t clk_id, struct timespec *tp);
       #endif
      

      [Edit: This has been fixed on CVS now, so this step should no longer be needed]

    Now you can compile Neko like this:

    $ make MACOSX=1

    You will be asked three times about headers for mod_neko, mod_tora and mysql. I decided to simply skip these as I won’t need them.

  5. Now you have a 64-bit binary version of neko in the [neko-source-dir]/bin directory. To install it to your system, make a backup copy of /usr/lib/neko and then copy the contents of [neko-source-dir]/bin into it and replace what ever is in there.
  6. Now you should be able to compile SamHaXe. Follow these steps:
    • Create ant.config and put these lines in it. The paths should be correct for a default installation of MacPorts.
       haxe.path=/usr/bin
       haxe.stdpath=/usr/lib/haxe/std
       imagemagick.path=/opt/local/bin
       imagemagick.include.path=/opt/local/include/ImageMagick
       imagemagick.library.path=/opt/local/lib
       imagemagick.library.name=MagickWand
       neko.path=/usr/lib/neko/
       neko.include.path=/usr/lib/neko/include
       neko.library.path=${neko.path}
       freetype.path=/opt/local/bin
       freetype.include.path=/opt/local/include/
       freetype.library.path=/opt/local/lib
    • Open build.xml and change image-imagemagick.dylib to libimage-imagemagick.dylib on line 238 and font.dylib to libfont.dylib on line 317
    • Now type ‘ant’ in the terminal to compile.
  7. That’s it! If all went like it should you now have a fully working SamHaXe in the [samhaxe-source-dir]/bin directory.

A problem you might encounter is that haxelib won’t work with the 64-bit neko, since haxelib is 32-bit by default. To fix this, you can compile a new haxelib from /usr/lib/haxe/std/tools/haxelib/. Simply cd to that directory and do “haxe haxelib.hxml”. Then copy the new binary over the old /usr/bin/haxelib.