C Header on OSX with 1.7.5

iPhone / iPad / OSX / xcode

Moderators: ezraanderson, nosheet

C Header on OSX with 1.7.5

Postby naturally » Tue Feb 14, 2017 9:00 pm

Just thought I'd share this solution I had for 'compiling' your code to a C header and using it on Apple 1.7.5 hosts, since it's different than 1.5 at least:

At the end of Init() in SDLHost.cpp:

Code: Select all
  1.         #ifdef __APPLE__

  2.        

  3.                 //are we a bundle?

  4.                 CFBundleRef bref = CFBundleGetMainBundle ();

  5.                 if ( bref == NULL || CFBundleGetIdentifier ( bref ) == NULL ) {

  6.                         AKUModulesParseArgs ( argc, argv );

  7.                 }

  8.                 else {

  9.                

  10.                         CFURLRef bundleurl = CFBundleCopyResourcesDirectoryURL ( bref );

  11.                         assert ( bundleurl != NULL );

  12.                        

  13.                         UInt8 buf [ PATH_MAX ];

  14.                         CFURLGetFileSystemRepresentation ( bundleurl, true, buf, PATH_MAX );

  15.  

  16.                         AKUSetWorkingDirectory(( const char * )buf );

  17.                         AKULoadFuncFromFile ( "bootstrap.lua" );

  18.                         AKUCallFunc ();

  19.                         AKULoadFuncFromBuffer ( game_core, game_core_SIZE, "main.lua", AKU_DATA_ZIPPED);

  20.                         AKUCallFunc();

  21.                 }

  22.         #else

  23.        

  24.                 AKUModulesParseArgs ( argc, argv );

  25.        

  26.         #endif



It's nice that the functions are now built in!

Steps:

1 - Edit "bootstrap.lua" and comment out the "dofile" line
2 - Edit SDLHost.cpp to the above
3 - Add #include "your-game-core.h" to the top of SDLHost.cpp

You should also probably skip the bootstrap steps (lines 17-18) by including it in the game-core, maybe to prevent code injection?

Use squish and then this script to make your .h file:

Code: Select all
  1. ----------------------------------------------------------------

  2. -- Copyright (c) 2010-2011 Zipline Games, Inc.

  3. -- All Rights Reserved.

  4. -- <!-- m --><a class="postlink" href="http://getmoai.com">http://getmoai.com</a><!-- m -->

  5. ----------------------------------------------------------------

  6.  

  7. function dumpBytecodeAsHeader ( filename, headername, outname )

  8.  

  9.         print ( filename )

  10.         print ( headername )

  11.         print ( outname )

  12.        

  13.         -- dump the function to Lua bytecode

  14.         local compiled = string.dump ( loadfile ( filename ))

  15.        

  16.         -- convert the Lua bytecode to a cpp header called 'bundled_lua' with 12 columns

  17.         local header = MOAIDataBuffer.toCppHeader ( compiled, headername, 12 )

  18.        

  19.         -- write the header to a file

  20.         local file = io.open ( outname, 'wb' )

  21.         file:write ( header )

  22.         file:close ()

  23.  

  24. end

  25.  

  26. function dumpZippedTextAsHeader ( filename, headername, outname )

  27.  

  28.         print ( filename )

  29.         print ( headername )

  30.         print ( outname )

  31.        

  32.         -- dump the function to Lua bytecode

  33.         local data = io.open ( filename, "r" ):read ( '*all' )

  34.         data = data .. string.char ( 0 ) -- null terminate the string

  35.         data = MOAIDataBuffer.deflate ( data, 9 )

  36.        

  37.         -- convert the Lua bytecode to a cpp header called 'bundled_lua' with 12 columns

  38.         local header = MOAIDataBuffer.toCppHeader ( data, headername, 12 )

  39.        

  40.         -- write the header to a file

  41.         local file = io.open ( outname, 'wb' )

  42.         file:write ( header )

  43.         file:close ()

  44.  

  45. end

  46. dumpZippedTextAsHeader('your-code-file.lua', 'game_core', 'game_core_zip.h')

Image - Don't Be Patchman - a sneak-and-grow adventure!
User avatar
naturally
 
Posts: 710
Joined: Thu Aug 29, 2013 8:05 pm
Location: Canada

Return to Apple

Who is online

Users browsing this forum: No registered users and 0 guests

cron

x