{"id":691,"date":"2012-04-09T14:55:48","date_gmt":"2012-04-09T11:25:48","guid":{"rendered":"http:\/\/www.blog.seganx.com\/?p=691"},"modified":"2024-03-18T17:47:28","modified_gmt":"2024-03-18T17:47:28","slug":"next-container-library","status":"publish","type":"post","link":"https:\/\/sajad-b.com\/?p=691","title":{"rendered":"Next Container Library"},"content":{"rendered":"<p>Developing &#8220;Rush for Glory&#8221; game is going to be finished. so I decided to rewrite some parts of the engine recently. this may cause you to rewrite the whole source code. In the first step, I rewrote the containers library. in this new version, I changed the structure of containers to decrease the usage of templates&lt;&gt; to more easier to understand. I used a sampler to decrease allocations. also implementing memory allocators is more easy than before and changeable during the code in some cases. finally implementing a new memory allocator that uses stack memory to avoid calling the malloc function.<\/p>\n<p>Note that some new features are not designed for general purposes. there are many limitations to using them and must be used with care because those features are designed to develop the game and most of the capabilities depend on the needs of a game engine.<\/p>\n<p>Here are some highlights :<\/p>\n<p>Added math base functions to the library with some fast functions like :<\/p>\n<div class=\"codecolorer-container cpp railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><div class=\"cpp codecolorer\"><span class=\"co1\">\/\/ fast sqrt root using Log Base 2 Approximation With One Extra Babylonian Steps<\/span><br \/>\n<span class=\"kw4\">float<\/span> sx_sqrt_fast<span class=\"br0\">&#40;<\/span> <span class=\"kw4\">const<\/span> <span class=\"kw4\">float<\/span> x <span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/ return sine( x ) from table. maximum absolute error is 0.001f<\/span><br \/>\n<span class=\"kw4\">float<\/span> sx_sin_fast<span class=\"br0\">&#40;<\/span> <span class=\"kw4\">const<\/span> <span class=\"kw4\">float<\/span> x <span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/ return cosine( x ) from table. maximum absolute error is 0.001f<\/span><br \/>\n<span class=\"kw4\">float<\/span> sx_cos_fast<span class=\"br0\">&#40;<\/span> <span class=\"kw4\">const<\/span> <span class=\"kw4\">float<\/span> x <span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/ compute the sine and cosine of the angle x at the same time. maximum absolute error is 0.001f<\/span><br \/>\n<span class=\"kw4\">void<\/span> sx_sin_cos_fast<span class=\"br0\">&#40;<\/span> <span class=\"kw4\">const<\/span> <span class=\"kw4\">float<\/span> IN x, <span class=\"kw4\">float<\/span><span class=\"sy3\">&amp;<\/span> OUT s, <span class=\"kw4\">float<\/span><span class=\"sy3\">&amp;<\/span> OUT c<span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span><\/div><\/div>\n<p>and some useful conventional functions<\/p>\n<p><strong>MemManFixed::SetBuffer(void* buffer);<\/strong><br \/>\nI can change the memory buffer of the allocator by setting a new buffer. this feature is available only on the fixed memory manager. it means that it&#8217;s suitable just for arrays and strings.<br \/>\nFor example, I wanna change\/fill a string in a structure. in this example when I set members of the structure to the allocator, all string functions can be applied easily.<\/p>\n<div class=\"codecolorer-container cpp railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><div class=\"cpp codecolorer\"><span class=\"kw4\">struct<\/span> FileInfo <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; wchar &nbsp; path<span class=\"br0\">&#91;<\/span><span class=\"nu0\">256<\/span><span class=\"br0\">&#93;<\/span><span class=\"sy4\">;<\/span><br \/>\n&nbsp; &nbsp; wchar &nbsp; name<span class=\"br0\">&#91;<\/span><span class=\"nu0\">64<\/span><span class=\"br0\">&#93;<\/span><span class=\"sy4\">;<\/span><br \/>\n&nbsp; &nbsp; wchar &nbsp; type<span class=\"br0\">&#91;<\/span><span class=\"nu0\">32<\/span><span class=\"br0\">&#93;<\/span><span class=\"sy4\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\nFileIfo fileInfo<span class=\"sy4\">;<\/span><br \/>\n<br \/>\nMemManFixed memTmp<span class=\"sy4\">;<\/span><br \/>\nString strTmp<span class=\"br0\">&#40;<\/span> <span class=\"nu0\">0<\/span>, <span class=\"sy3\">&amp;<\/span>memTmp <span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/ extract file path from file name and copy that to fileInfo.path<\/span><br \/>\nmemTmp.<span class=\"me1\">SetBuffer<\/span><span class=\"br0\">&#40;<\/span> <span class=\"sy3\">&amp;<\/span>fileInfo.<span class=\"me1\">path<\/span> <span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span><br \/>\nstrTmp <span class=\"sy1\">=<\/span> fileName<span class=\"sy4\">;<\/span><br \/>\nstrTmp.<span class=\"me1\">ExtractFilePath<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/ extract the name of the file and copy that to fileInfo.name<\/span><br \/>\nmemTmp.<span class=\"me1\">SetBuffer<\/span><span class=\"br0\">&#40;<\/span> <span class=\"sy3\">&amp;<\/span>fileInfo.<span class=\"me1\">name<\/span> <span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span><br \/>\nstrTmp <span class=\"sy1\">=<\/span> fileName<span class=\"sy4\">;<\/span><br \/>\nstrTmp.<span class=\"me1\">ExtractFileName<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/ extract file type and copy that to the fileInfo.type<\/span><br \/>\nmemTmp.<span class=\"me1\">SetBuffer<\/span><span class=\"br0\">&#40;<\/span> <span class=\"sy3\">&amp;<\/span>fileInfo.<span class=\"me1\">type<\/span> <span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span><br \/>\nstrTmp <span class=\"sy1\">=<\/span> fileName<span class=\"sy4\">;<\/span><br \/>\nstrTmp.<span class=\"me1\">ExtractFileExtension<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span><\/div><\/div>\n<p><strong>MemManFixed_inline&lt;memSizeInByte&gt;<\/strong><br \/>\nThis memory manager uses a memory stack of functions. by the way, this feature is available only on the fixed memory manager. this means that it&#8217;s suitable just for arrays and strings.<\/p>\n<p>In this example, I try to collect some nodes from the scene manager. this will happen at each frame more than once, at the renderer, AI system &amp;, etc. Implementing a new Array class causes calling allocation\/deallocation. the other solution is declaring a static array. the other solution is using a memory stack with some limitations but fast and easy memory management.<br \/>\nexample :<\/p>\n<div class=\"codecolorer-container cpp railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><div class=\"cpp codecolorer\">MemManFixed_inline<span class=\"sy1\">&lt;<\/span>MAX_NODE_COUNT<span class=\"sy1\">&gt;<\/span> tmpMem<span class=\"sy4\">;<\/span><br \/>\nArray<span class=\"sy1\">&lt;<\/span>Node<span class=\"sy2\">*<\/span><span class=\"sy1\">&gt;<\/span> nodes<span class=\"br0\">&#40;<\/span> <span class=\"nu0\">0<\/span>, <span class=\"sy3\">&amp;<\/span>tmpMem <span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/ collect nodes from the scene manager<\/span><br \/>\ng_engine<span class=\"sy2\">-<\/span><span class=\"sy1\">&gt;<\/span>m_scene<span class=\"sy2\">-<\/span><span class=\"sy1\">&gt;<\/span>GetNodesByFrustum<span class=\"br0\">&#40;<\/span> cameraFrustum, nodes <span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">for<\/span> <span class=\"br0\">&#40;<\/span> <span class=\"kw4\">int<\/span> i <span class=\"sy1\">=<\/span> <span class=\"nu0\">0<\/span><span class=\"sy4\">;<\/span> i <span class=\"sy1\">&lt;<\/span> nodes.<span class=\"me1\">Count<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span> i<span class=\"sy2\">++<\/span> <span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/ ... do something !<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>Although there are some changes like adding new functions and changing algorithms in the other stuff to increase performance and capabilities, but those still need to be optimized more and more.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Developing &#8220;Rush for Glory&#8221; game is going to be finished. so I decided to rewrite some parts of the engine recently. this may cause you to rewrite the whole source code. In the first step, I rewrote the containers library. in this new version, I changed the structure of containers to decrease the usage of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[7],"tags":[20,27],"class_list":["post-691","post","type-post","status-publish","format-standard","hentry","category-engine","tag-containers","tag-memory"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/sajad-b.com\/index.php?rest_route=\/wp\/v2\/posts\/691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sajad-b.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sajad-b.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sajad-b.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sajad-b.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=691"}],"version-history":[{"count":2,"href":"https:\/\/sajad-b.com\/index.php?rest_route=\/wp\/v2\/posts\/691\/revisions"}],"predecessor-version":[{"id":877,"href":"https:\/\/sajad-b.com\/index.php?rest_route=\/wp\/v2\/posts\/691\/revisions\/877"}],"wp:attachment":[{"href":"https:\/\/sajad-b.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sajad-b.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sajad-b.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}