Friday, 17 May 2013

Using web workers to JSON.parse large json files

Previously I had been working on converting FBX 3D animations to JSON to allow for easier loading of 3D animations into WebGL and OpenGL. However, using the standard JSON.parse JavaScript function is slow, and especially noticeable if you're running animations.


However, this noticeable lag can be combatted pretty easily with the use of webworkers. Instead of running the JSON.parse function on the main thread, just pass the unparsed string data over to a web worker and call JSON.parse there.

After implementing this change in the up coming realtime 3d games editor, around 1.8 seconds of unresponsive UI lag per ~4mb of json parsing was avoided.


You can find generalised implementation up on github for your royalty free, do whatever you want with it, and don't blame me for crashes, use.

Download: json.async

Video walkthrough


Sunday, 12 May 2013

C++ Threading on Windows Phone 8

I have to admit, the whole multi-thread paradigm for Windows 8 and Windows Phone had me a little lost, so I have been fiercely avoiding it until really needing it for loading FBX animations dynamically.

The problem was that all the examples out there seemed to always call WinRT system operations, which made things confusing at first.

But in the end though, it turned out to be super simple.
 // Get a reference to current thread
 auto currentThread = Concurrency::task_continuation_context::use_current();

 Concurrency::create_task([this]  
 {  
      // This part is run on another thread  
      moveVerticesToOrigin();  
 }).then([this]()  
 {  
      // This part runs back on the original thread  
      movedVerticesToOrigin();  
 },  
 // schedules then() part to run in the current thread  
 currentThread );  

The first part of create_task runs moveVerticesToOrigin on another thread.
The then part runs back on the original thread create_task was called.

Simple!


Here's another example showing you how to pass parameters around
 // Get a reference to current thread
 auto currentThread = Concurrency::task_continuation_context::use_current();

 // I will copy the variable textFileData in the lambda below
  CCText textFileData = fileData;  
   
 // This lambda opens up with this and textFileData  
 // Which means that it'll make a copy of both those variables.  
 Concurrency::create_task([this, textFileData]  
 {  
      // Uses the copied version of textFileData to run this->loadData  
      bool loaded = loadData( textFileData.buffer );  
   
      // Returns a boolean  
      return loaded;  
 }).then([this](bool loaded)  
 {  
      // The loaded boolean is picked up from the first function's return  
   
      // This stuff runs on the original thread.  
      if( loaded )  
      {  
           this->loaded();  
      }  
      else  
      {  
           CCText script = "CCPrimitive3D.Loaded( ";  
           script += primitiveID.buffer;  
           script += " );";  
           CCAppManager::WebJSRunJavaScript( script.buffer, false, false );  
      }  
 }, currentThread );  

In our first function we make a copy of the textFileData parameter from outside the function, then use it on another thread.

We then pass the result loaded, into the then function which is run back on the original thread.

So there! It's actually super easy :)


Saturday, 11 May 2013

Disable HTC Syc Manager Autostarting

Hey guys, got the HTC One the other day, and I love the fact that you can actually use ndk-gdb without having to manually root the device!

Things I don't love however is the automatic mount and popup prompting you to install HTC Sync Manager.

To disable HTC Sync Manager auto-mounting
Launch terminal

Type
diskutil info /Volumes/HTC Sync Manager

Make a note of the Volume UUID.

Next
sudo vifs

Go to the bottom of the file
tap the i key

And paste the following
UUID=YOURVOLUMEUUID none hfs rw,noauto
Replacing YOURVOLUMEUUID with the one you made a note of earlier

Hit Esc
Then type the following to save.
:wq

Credit: http://www.tannr.com/2009/09/01/preventing-a-volume-from-automatically-mounting-in-os-x/


To disable Android File Transfer auto-starting
Instead of HTC Sync Manager, use Android File Transfer to browse files on the phone.

Note!
Android File Transfer too likes auto-starting.

To disable, go to /Applications/
Right click on Android File Manager -> Show file contents
Inside Contents/Resources
rename Android File Transfer.app to Android File Transfer_DELETED.app

Go to /Users/YOURUSERNAME/Library/Application Support/Google/Android File Transfer.
rename Android File Transfer.app to Android File Transfer_DELETED.app

Credit: http://productforums.google.com/forum/#!topic/mobile/3hPIZjP0rDo