{"id":689,"date":"2012-01-19T20:00:00","date_gmt":"2012-01-19T19:00:00","guid":{"rendered":"http:\/\/shadowcovenant.com\/blog\/2012\/01\/19\/gui-work-and-more-internal-structure\/"},"modified":"2012-01-19T20:00:00","modified_gmt":"2012-01-19T19:00:00","slug":"gui-work-and-more-internal-structure","status":"publish","type":"post","link":"http:\/\/shadowcovenant.com\/blog\/2012\/01\/19\/gui-work-and-more-internal-structure\/","title":{"rendered":"GUI work and more internal structure&hellip;"},"content":{"rendered":"<p align=\"justify\">Nothing much to add to the post at <a href=\"http:\/\/www.spellcasterstudios.com\/\">Spellcaster Studios<\/a>\u2026 Writing for two blogs is kind of a pain from time to time\u2026 <img decoding=\"async\" style=\"border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none\" class=\"wlEmoticon wlEmoticon-smile\" alt=\"Smile\" src=\"http:\/\/shadowcovenant.com\/blog\/wp-content\/uploads\/2012\/01\/wlEmoticon-smile2.png\" \/><\/p>\n<p align=\"justify\">Spent the last couple of nights working on scripting internal structures, streamlining some functions and inventing a way to pass arrays\/lists from LuaJIT to C++ and vice-versa\u2026<\/p>\n<p align=\"justify\"><a href=\"http:\/\/shadowcovenant.com\/blog\/wp-content\/uploads\/2012\/01\/image.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/shadowcovenant.com\/blog\/wp-content\/uploads\/2012\/01\/image_thumb.png\" width=\"457\" height=\"368\" \/><\/a><\/p>\n<p align=\"justify\">My problem was that the raycasting should return a set of intersections (not just the first one)\u2026 The way I found to do this was to create a semi-templated class to handle the array itself, get some functions to access it\/destroy it and do the conversion in the raycasting function itself\u2026<\/p>\n<p align=\"justify\">The end result is something like this:<\/p>\n<pre>ffi = require(&quot;ffi&quot;)\nffi.cdef[[\n\n\tint             LuaIntersectionArray_get_size(LuaIntersectionArray* ar);\n\tLuaIntersection\tLuaIntersectionArray_get(LuaIntersectionArray* ar,\n                                                 int index);\n\tvoid            LuaIntersectionArray_free(LuaIntersectionArray* ar);\n]]\n\nLuaIntersectionArray_mt = {\n   __index = {\n      type = function(a) return &quot;LuaArray&lt;LuaIntersection&gt;<luaintersection>&quot; end,\n      log = function(a,log_type,pre_text) std.log(log_type,pre_text..type(a)) end,\n      size = function(array) return ffi.C.LuaIntersectionArray_get_size(array) end,\n      get = function(array,index) return ffi.C.LuaIntersectionArray_get(array,index) end,\n      free = function(array) ffi.C.LuaIntersectionArray_free(array) end,\n   },\n}\nLuaIntersectionArray = ffi.metatype(&quot;LuaIntersectionArray&quot;,LuaIntersectionArray_mt)\n\n\nfunction get_lua_array(c_array)\n   local lua_array={}\n   for i=0,c_array:size()-1 do\n      local obj=c_array:get(i)\n      table.insert(lua_array,obj)\n   end\n   c_array:free()\n   return lua_array\nend<\/pre>\n<p>&#160;<\/p>\n<p align=\"justify\">Of course I have to do this for every type of array I want to support on Lua (except the get_lua_array function, that is generic).<\/p>\n<p align=\"justify\">On the C\/C++ side, I create the <em>LuaIntersectionArray <\/em>class, with the access methods, using a <em>#define <\/em>directive that declares all the functions, etc. <\/p>\n<p align=\"justify\">Then, on the Lua side, when I want to receive an array of intersections, I can do something like this:<\/p>\n<pre>ffi = require(&quot;ffi&quot;)\nffi.cdef[[\n\n\tLuaIntersectionArray*\traycast_static_mesh_array(LuaRay ray);\n]]\n----------------------------------------------------------------------\n-- Shortcut functions\nfunction raycast_static_mesh_array(ray)\n   return get_lua_array(ffi.C.raycast_static_mesh_array(ray))\nend<\/pre>\n<p>&#160;<\/p>\n<p align=\"justify\">It\u2019s not that elegant, can lead to memory leaks (although it shouldn\u2019t happen if I keep things contained like this), but it\u2019s pretty simple to use, since the return of raycast_static_mesh_array (which only raycasts static meshes and returns an array) is a simple Lua array\/table (same thing there, really).<\/p>\n<p align=\"justify\">&#160;<\/p>\n<p align=\"justify\">On other notes, check out the animation system on the new Max Payne game, it\u2019s fantastic!<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:7ffa8307-091a-4a8f-95f6-43d5e9f329fa\" class=\"wlWriterEditableSmartContent\">\n<div><object width=\"560\" height=\"315\"><param name=\"movie\" value=\"http:\/\/www.youtube.com\/v\/9FTvPSnkV5A?hl=en&amp;hd=1\"><\/param><embed src=\"http:\/\/www.youtube.com\/v\/9FTvPSnkV5A?hl=en&amp;hd=1\" type=\"application\/x-shockwave-flash\" width=\"560\" height=\"315\"><\/embed><\/object><\/div>\n<\/div>\n<p>I wish I could spend 4 or 5 months just researching IK and animation to do such good looking stuff! <img decoding=\"async\" style=\"border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none\" class=\"wlEmoticon wlEmoticon-smile\" alt=\"Smile\" src=\"http:\/\/shadowcovenant.com\/blog\/wp-content\/uploads\/2012\/01\/wlEmoticon-smile2.png\" \/><\/p>\n<div id=\"tweetbutton689\" class=\"tw_button\" style=\"\"><a href=\"http:\/\/twitter.com\/share?url=http%3A%2F%2Fshadowcovenant.com%2Fblog%2F2012%2F01%2F19%2Fgui-work-and-more-internal-structure%2F&amp;text=GUI%20work%20and%20more%20internal%20structure%26hellip%3B&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fshadowcovenant.com%2Fblog%2F2012%2F01%2F19%2Fgui-work-and-more-internal-structure%2F\" class=\"twitter-share-button\"  style=\"width:55px;height:22px;background:transparent url('http:\/\/shadowcovenant.com\/blog\/wp-content\/plugins\/wp-tweet-button\/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;\">Tweet<\/a><\/div>","protected":false},"excerpt":{"rendered":"<p>Nothing much to add to the post at Spellcaster Studios\u2026 Writing for two blogs is kind of a pain from time to time\u2026 Spent the last couple of nights working on scripting internal structures, streamlining some functions and inventing a way to pass arrays\/lists from LuaJIT to C++ and vice-versa\u2026 My problem was that the [&hellip;]<\/p>\n<div id=\"tweetbutton689\" class=\"tw_button\" style=\"\"><a href=\"http:\/\/twitter.com\/share?url=http%3A%2F%2Fshadowcovenant.com%2Fblog%2F2012%2F01%2F19%2Fgui-work-and-more-internal-structure%2F&amp;text=GUI%20work%20and%20more%20internal%20structure%26hellip%3B&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fshadowcovenant.com%2Fblog%2F2012%2F01%2F19%2Fgui-work-and-more-internal-structure%2F\" class=\"twitter-share-button\"  style=\"width:55px;height:22px;background:transparent url('http:\/\/shadowcovenant.com\/blog\/wp-content\/plugins\/wp-tweet-button\/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;\">Tweet<\/a><\/div>","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,6,88,91],"tags":[104,102,53,94,101,56,92,103],"class_list":["post-689","post","type-post","status-publish","format-standard","hentry","category-development","category-games","category-grey","category-scripting","tag-animation","tag-arrays","tag-c","tag-classes","tag-gui","tag-lua","tag-luajit","tag-max-payne"],"_links":{"self":[{"href":"http:\/\/shadowcovenant.com\/blog\/wp-json\/wp\/v2\/posts\/689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shadowcovenant.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shadowcovenant.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shadowcovenant.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/shadowcovenant.com\/blog\/wp-json\/wp\/v2\/comments?post=689"}],"version-history":[{"count":0,"href":"http:\/\/shadowcovenant.com\/blog\/wp-json\/wp\/v2\/posts\/689\/revisions"}],"wp:attachment":[{"href":"http:\/\/shadowcovenant.com\/blog\/wp-json\/wp\/v2\/media?parent=689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shadowcovenant.com\/blog\/wp-json\/wp\/v2\/categories?post=689"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shadowcovenant.com\/blog\/wp-json\/wp\/v2\/tags?post=689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}