??xml version="1.0" encoding="utf-8" standalone="yes"?>久久精品国产精品亜洲毛片,久久九色综合九色99伊人,久久久久99精品成人片http://m.shnenglu.com/woaidongmao/category/11554.html文章均收录自他h博客Q但不喜标题前加-[转脓]Q因其丑陋,见谅Q~zh-cnTue, 10 May 2011 14:06:49 GMTTue, 10 May 2011 14:06:49 GMT60数据/配置 的存储方?Lua?/title><link>http://m.shnenglu.com/woaidongmao/archive/2010/12/28/137601.html</link><dc:creator>肥仔</dc:creator><author>肥仔</author><pubDate>Tue, 28 Dec 2010 05:06:00 GMT</pubDate><guid>http://m.shnenglu.com/woaidongmao/archive/2010/12/28/137601.html</guid><wfw:comment>http://m.shnenglu.com/woaidongmao/comments/137601.html</wfw:comment><comments>http://m.shnenglu.com/woaidongmao/archive/2010/12/28/137601.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/woaidongmao/comments/commentRss/137601.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/woaidongmao/services/trackbacks/137601.html</trackback:ping><description><![CDATA[<p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">前言<span lang="EN-US"><?xml:namespace prefix = o /><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span></span><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">在《数?span lang="EN-US">/</span>配置 的存储方?span lang="EN-US"> Json</span>??span lang="EN-US">JsonCpp</span>库用ؓ例》《数?span lang="EN-US">/</span>配置 的存储方?span lang="EN-US"> Json</span>??span lang="EN-US">Cocos2D For Iphone+TouchJson</span>Z 》中我ȝ了一下怎么使用<span lang="EN-US">Json</span>作ؓ配置Q但是,其实<span lang="EN-US">Json</span>虽然语法格式单,同时解析库的使用也非常简单,但是Q因U简单性,~少有的时候的非帔R要的功能。比如配|文件中对另一行配|的引用Q比如配|段的承等Q这些可以?span lang="EN-US">XML</span>来解冻I另外Q有的时候还会需要在配置文g中进行简单的q算Q比如我常怼有想某个图片羃攑ֈ目前分L率的几分之几q样的需求,此时Q用一个完整的可以q行q算的语a来做配置的诱惑实在太大了。。。。。?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>Lua</span><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">是为此而生的。。。。。。虽然现?span lang="EN-US">Lua</span>的功能已l远q不止这样了Q但是最初设?span lang="EN-US">Lua</span>的目的就是一个简单的配置语言。见<span lang="EN-US">Lua</span>的历?。虽然其实用<span lang="EN-US">Python</span>作ؓ配置也完全可行,但是<span lang="EN-US">Lua</span>有着速度快的优点Q而且Q?span lang="EN-US">Lua</span>实在?span lang="EN-US">Python</span>太多了Q很适合嵌入到程序中厅R?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">对于使用<span lang="EN-US">Lua</span>作ؓ配置Q事实上q当于?span lang="EN-US">C++</span>中嵌?span lang="EN-US">Lua</span>Q只不过不用其脚本语a的一些复杂特性,仅仅其作ؓ一个配|文件来看?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">q里q是以原来《数?span lang="EN-US">/</span>配置 的存储方?span lang="EN-US"> Json</span>??span lang="EN-US">JsonCpp</span>库用ؓ例》文中开始的<span lang="EN-US">SDL</span>工程作ؓ例子?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">首先搭徏可以内嵌使用<span lang="EN-US">Lua</span>?span lang="EN-US">C++</span>~译环境Q下载一?span lang="EN-US">luabinaries </span>是简单的办法Q现在我使用的是<span lang="EN-US">lua5_1_4_Win32_dll8_lib.zip </span>。这里我甚至不需要一个可以运行的<span lang="EN-US">Lua</span>交互环境。。。。?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">单示?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">首先建立一个简单的<span lang="EN-US">Lua</span>文g<span lang="EN-US">,</span>命名?span lang="EN-US">picture.lua<o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">name="dragon.png" <o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">rotation=180 <o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">虽然只有单的两行Q也是个合法?span lang="EN-US">Lua</span>E序?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">然后?span lang="EN-US">C++</span>E序中读取出来,q个q程实际上是使用?span lang="EN-US">Lua</span>?span lang="EN-US">C API</span>Q具体的<span lang="EN-US">Lua</span>?span lang="EN-US">C API</span>q里׃详细讲解了,那比较复杂,可以参考?span lang="EN-US">Programming in Lua</span>》,中文版也已经有了。这里仅仅作Z个例子提供参考吧?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">现在可以开始完?span lang="EN-US">C++</span>的程序了Q?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">首先Q包含必要的头文Ӟ<span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">extern "C" {<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">#include "lua/lua.h"<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">#include "lua/lauxlib.h"<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">#include "lua/lualib.h"<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">}<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">因ؓ<span lang="EN-US">lua</span>的头文g完全是只考虑<span lang="EN-US">C</span>语言的情况,所以在<span lang="EN-US">C++</span>中用的时候需要自己添?span lang="EN-US">extern "C"</span>。这是典型的?span lang="EN-US">C++</span>不屑的态度Q因为现在大部分的纯<span lang="EN-US">C</span>语言写的库,都会很自然的通过宏判断来自动使用<span lang="EN-US">extern "C"</span>语句?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">然后Q?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">using<span style="mso-spacerun: yes">  </span>namespace<span style="mso-spacerun: yes">  </span>std;<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">struct<span style="mso-spacerun: yes">   </span>PictureInfo {<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>string name;<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>float<span style="mso-spacerun: yes">  </span>rotation;<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">}gPictureInfo;<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">void<span style="mso-spacerun: yes">   </span>PictureInit() {<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>lua_State *L = luaL_newstate();<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>if ( luaL_dofile(L, "picture.lua" ) != 0 ) {<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>printf("Error happen." );<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>// handle the error. <o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>exit(1 );<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>}<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>lua_getglobal(L, "name" );<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>assert(lua_isstring(L, -1 ) == 1 );<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>gPictureInfo.name = lua_tostring(L, -1 );<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>lua_getglobal(L, "rotation" );<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>gPictureInfo.rotation = (float )lua_tonumber(L, -1 );<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>lua_close(L);<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">}<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">q里我们使用的所有变量全部都?span lang="EN-US">global</span>的变量,所以调用v来非常方ѝ上面的主要内容是?span lang="EN-US">Lua C API</span>的用,׃其本w较为复杂,q里一下也讲不清楚?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">基本程是创建新?span lang="EN-US">Lua state,</span>然后?span lang="EN-US">luaL_dofile </span>执行我们需要的配置文gQ然后调?span lang="EN-US">lua_getglobal </span>加适当的{换来获取我们需要的配置倹{这里的例子是一个字W串和一个QҎ?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">具体昄的效果和其他代码参考《数?span lang="EN-US">/</span>配置 的存储方?span lang="EN-US"> Json</span>??span lang="EN-US">JsonCpp</span>库用ؓ例》就好了Q这里不重复描述了?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">数组<span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span></span><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">?span lang="EN-US">Lua</span>中数l也通过<span lang="EN-US">table</span>的Ş式来表示Q这里我仅仅介绍怎么?span lang="EN-US">lua</span>文g中获取配|,具体的显C等东西也不再篏qC?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span></span><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">q里Q我们徏立一个包含数l的<span lang="EN-US">lua</span>文g作ؓ配置Q?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">data = { <o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>{<span style="mso-spacerun: yes">  </span>name="dragon.png" , rotation=180<span style="mso-spacerun: yes">  </span>} ,<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>{<span style="mso-spacerun: yes">  </span>name="dragon.png" , rotation=0<span style="mso-spacerun: yes">  </span>} <o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">} <o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">下面看怎么配|读取出来:<span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">using<span style="mso-spacerun: yes">  </span>namespace<span style="mso-spacerun: yes">  </span>std;<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">struct<span style="mso-spacerun: yes">   </span>PictureInfo {<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>string name;<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>float<span style="mso-spacerun: yes">  </span>rotation;<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">};<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">vector gPictureInfoVec;<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">void<span style="mso-spacerun: yes">   </span>PictureInit() {<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>lua_State *L = luaL_newstate();<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>if ( luaL_dofile(L, "picture.lua" ) != 0 ) {<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>printf("Error happen." );<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>// handle the error. <o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>exit(1 );<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>}<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>// get the table <o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>lua_getglobal(L, "data" );<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>assert(lua_istable(L, -1 ) == 1 );<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>/*<span style="mso-spacerun: yes">  </span>table is in the stack at index 't' */ <o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>lua_pushnil(L);<span style="mso-spacerun: yes">  </span>/*<span style="mso-spacerun: yes">  </span>first key */ <o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>while<span style="mso-spacerun: yes">  </span>(lua_next(L, -2 ) != 0 ) {<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>PictureInfo info;<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>/* 'key' (at index -2) and 'value' (at index -1) */ <o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>// push the key to stack for getting the value <o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>lua_pushstring(L, "name" );<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>// now the table is in the -2 and key in the top(-1) <o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>lua_gettable(L, -2 );<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>assert(lua_isstring(L, -1 ));<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>info.name = lua_tostring(L, -1 );<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>lua_pop(L, 1 );<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>// push the key to stack for getting the value <o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>lua_pushstring(L, "rotation" );<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>// now the table is in the -2 and key in the top(-1) <o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>lua_gettable(L, -2 );<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>assert(lua_isnumber(L, -1 ));<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>info.rotation = lua_tonumber(L, -1 );<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>gPictureInfoVec.push_back(info);<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>/*<span style="mso-spacerun: yes">  </span>removes the key we pushed and the 'value' of the global table ;<span style="mso-spacerun: yes">   </span>keeps 'key' for next iteration */ <o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>lua_pop(L, 2 );<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>}<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span>lua_close(L);<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">}<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">代码中的注释解释的已l够详细了,但是因ؓ<span lang="EN-US">Lua </span>?span lang="EN-US">API</span>的确不是太容易理解,q里也无法一下讲清楚Q所以还是先了解<span lang="EN-US">Lua </span>?span lang="EN-US"> API</span>为好Q具体的<span lang="EN-US">API</span>的意义可以参考参考手册?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">上面仅仅使用?span lang="EN-US">Lua API</span>遍历数组以及?span lang="EN-US">table</span>中获取元素的Ҏ?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">假如仅仅只有上面q些Q是看不出用<span lang="EN-US">lua</span>作ؓ配置的好处的Q用<span lang="EN-US">lua</span>做配|的好处在于可以利用<span lang="EN-US">lua</span>的特性实现配|段之间的承以及完整的q算功能?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">比如_完全不用修改上面的读取配|的代码Q我们仅仅修攚w|,看看?span lang="EN-US">Lua</span>中用上q功能:<span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">data1 = {<span style="mso-spacerun: yes">  </span>name="dragon.png" , rotation=180<span style="mso-spacerun: yes">  </span>} <o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">data2 = {<span style="mso-spacerun: yes">  </span>name = data1.name, rotation = data1.rotation / 2 } <o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">data = { <o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span>data1, data2<o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US">} <o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">此时Q?span lang="EN-US">data2</span>的数据完全依赖于<span lang="EN-US">data1</span>的数据,当有M东西需要修改的时候,你只需要修改一个地方,对于配置来说Q?span lang="EN-US">don't repeat yourself</span>也是很有意义的。最最重要的的是,在配|中能够q行计算那是非常强大Q即使完全不?span lang="EN-US">lua</span>的函数功能与代码的交互,仅仅通过<span lang="EN-US">lua</span>的计,也可以完成游戏中所?span lang="EN-US">sprite</span>的布局。。。。。?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">结<span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span></span><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">相对于?span lang="EN-US">XML,Json</span>Q?span lang="EN-US">Lua</span>作ؓ配置l对是最最强大的。。。。。。作Z个完整的语言Q它拥有你想在配|中实现的一切功能。但是缺点也是很明显的:<span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">  </span><span style="mso-spacerun: yes">  </span></span><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">首先Q速度上,<span lang="EN-US">Lua</span>需要解释运行,可能明显跟不?span lang="EN-US">XML</span>或?span lang="EN-US">Json</span>的解析速度。但是,配置的解析读取可以都攑֜初始化阶D,所以配|不是太多的时候,也不太q难以接受,另外Q还可以?span lang="EN-US">lua</span>的配|完全作为开发期的一U机制用,在发布后完全转化?span lang="EN-US">2</span>q制数据?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span></span><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">其次Q?span lang="EN-US">Lua</span>的配|读取需要手动调?span lang="EN-US">Lua</span>?span lang="EN-US">API</span>来完成,相对?span lang="EN-US">XML</span>Q?span lang="EN-US">Json</span>那种有很方便的库的情况,使用上还是麻烦一些,特别?span lang="EN-US">Lua</span>?span lang="EN-US">API</span>的用ƈ不是那么单易懂,不算太直观。但是,q也不是不可以克服的Q完全可以自己写个小型的库将<span lang="EN-US">Lua</span>?span lang="EN-US">API</span>装hQŞ成一个类?span lang="EN-US">JsonCpp</span>那样的库Q用<span lang="EN-US">Map</span>来表CZ切。这个可能需要限制一?span lang="EN-US">Lua</span>语法的时候,或者在解析的时候进行取舍,比如函数啥的可能需要过滤掉Q不焉׃太像?span lang="EN-US">Lua</span>作ؓ配置使用了?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span></span><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">q有Q?span lang="EN-US">Lua</span>的配|的生成更加是没?span lang="EN-US">XML,Json</span>那样有完善库支持来的方便Q这Ҏ时没有办法克服,写一个自动生?span lang="EN-US">Lua</span>配置文g的库感觉q不是太q容易的事情。而且Q自动生成时Q?span lang="EN-US">lua</span>的优势其实ƈ没有余地发挥出来Q那L话,q不如?span lang="EN-US">Json</span>?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><span style="mso-spacerun: yes">    </span></span><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">ȝ来说Q假如是没有工具Q需要大量手写配|的话,?span lang="EN-US">Lua</span>?span lang="EN-US">API</span>q行一层简单的装Q然后?span lang="EN-US">Lua</span>来做配置Q那q是件很爽的事情Q需要手写的时候,你才会感觉到一个完整语a作ؓ配置l你带来的好处?span lang="EN-US"><o:p></o:p></span></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-bidi-font-family: arial">本文来自<span lang="EN-US">CSDN</span>博客Q{载请标明出处Q?span lang="EN-US">http://blog.csdn.net/vagrxie/archive/<?xml:namespace prefix = st1 /><st1:chsdate w:st="on" isrocdate="False" islunardate="False" day="23" month="7" year="2010">2010/07/23</st1:chsdate>/5758521.aspx<o:p></o:p></span></span></p><img src ="http://m.shnenglu.com/woaidongmao/aggbug/137601.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/woaidongmao/" target="_blank">肥仔</a> 2010-12-28 13:06 <a href="http://m.shnenglu.com/woaidongmao/archive/2010/12/28/137601.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一个C++dLua配置文g的例?/title><link>http://m.shnenglu.com/woaidongmao/archive/2010/12/28/137600.html</link><dc:creator>肥仔</dc:creator><author>肥仔</author><pubDate>Tue, 28 Dec 2010 05:05:00 GMT</pubDate><guid>http://m.shnenglu.com/woaidongmao/archive/2010/12/28/137600.html</guid><wfw:comment>http://m.shnenglu.com/woaidongmao/comments/137600.html</wfw:comment><comments>http://m.shnenglu.com/woaidongmao/archive/2010/12/28/137600.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/woaidongmao/comments/commentRss/137600.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/woaidongmao/services/trackbacks/137600.html</trackback:ping><description><![CDATA[<p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt" lang="EN-US">//LuaEx.h</span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt">文g<span lang="EN-US"><br><br>#pragma once<br>#include <Windows.h><br>extern "C"<br>{<br>#include "lua/lua.h"<br>#include "lua/lualib.h"<br>#include "lua/lauxlib.h"<br>};<br><br>class LuaEx<br>{<br>public:<br>LuaEx(void);<br>~LuaEx(void);<br>bool LoadFile(LPCSTR str); //</span>载入<span lang="EN-US">lua</span>文g<span lang="EN-US"><br>LPSTR LoadString(LPCSTR str); //</span>d字符?span lang="EN-US"><br>int LoadInteger(LPCSTR str); //</span>d整Ş<span lang="EN-US"><br>double LoadDouble(LPCSTR str); //</span>d点?span lang="EN-US"><br>bool LoadBoolean(LPCSTR str); //</span>d布尔?span lang="EN-US"><br><br>private:<br>lua_State *L; //lua</span>指针<span lang="EN-US"><br>};<br><br>//LuaEx.cpp</span>文g<span lang="EN-US"><br><br>#include ".\luaex.h"<br>#pragma comment(lib, ".\\lua\\lua.lib")<br><br>LuaEx::LuaEx(void)<br>{<br>L = lua_open();<br>luaL_openlibs(L);<br>}<br><br>LuaEx::~LuaEx(void)<br>{<br>lua_close(L);<br>}<br><br>bool LuaEx::LoadFile(LPCSTR str)<br>{<br>if(luaL_dofile(L, str))<br>{<br>return false;<br>}<br>return true;<br>}<br><br>LPSTR LuaEx::LoadString(LPCSTR str)<br>{<br>lua_getglobal(L, str);<br>if (lua_isstring(L, -1))<br>{<br>return (LPSTR)lua_tostring(L, -1);<br>}<br>return NULL;<br>}<br><br>int LuaEx::LoadInteger(LPCSTR str)<br>{<br>lua_getglobal(L, str);<br>if (lua_isnumber(L, -1))<br>{<br>return (int)lua_tointeger(L, -1);<br>}<br>return NULL;<br>}<br><br>double LuaEx::LoadDouble(LPCSTR str)<br>{<br>lua_getglobal(L, str);<br>if (lua_isnumber(L, -1))<br>{<br>return (double)lua_tonumber(L, -1);<br>}<br>return 0.0;<br>}<br><br>bool LuaEx::LoadBoolean(LPCSTR str)<br>{<br>lua_getglobal(L, str);<br>if (lua_isboolean(L, -1))<br>{<br>return (bool)lua_toboolean(L, -1);<br>}<br>return false;<br>}<br><br><br></span>在要使用配置文g的地方实例化一?span lang="EN-US">LuaEx</span>cd可以了?span lang="EN-US"><br></span>先调?span lang="EN-US">LoadFile</span>载入文gQ参Cؓ文g路径。文件格式可以按照如下方式:<span lang="EN-US"><br><br>title = "</span>游戏<span lang="EN-US">"<br>width = 640<br>height = 480<br>isWindowed = true;<br>useSound = false;<br>hideMouse = false;<br><br></span>l尾分号可加可不加,是写一?span lang="EN-US">lua</span>脚本Q但是只包含变量不包含方法?span lang="EN-US"><br></span>之后可以读取其中内容了。例?span lang="EN-US"><br>LoadString("title"); //</span>表示载入变量名ؓ<span lang="EN-US">title</span>的变量的倹{?span lang="EN-US"><br></span>本类中所有函数的参数都是字符丌Ӏ?/span><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt; mso-bidi-font-family: 宋体" lang="EN-US"><?xml:namespace prefix = o /><o:p></o:p></span></p> <p style="line-height: 150%" class="MsoNormal"><span style="line-height: 150%; font-family: 宋体; color: black; font-size: 12pt; mso-bidi-font-family: arial" lang="EN-US"><o:p> </o:p></span></p><img src ="http://m.shnenglu.com/woaidongmao/aggbug/137600.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/woaidongmao/" target="_blank">肥仔</a> 2010-12-28 13:05 <a href="http://m.shnenglu.com/woaidongmao/archive/2010/12/28/137600.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>c++调用lua例子http://m.shnenglu.com/woaidongmao/archive/2010/12/27/137556.html肥仔肥仔Mon, 27 Dec 2010 04:57:00 GMThttp://m.shnenglu.com/woaidongmao/archive/2010/12/27/137556.htmlhttp://m.shnenglu.com/woaidongmao/comments/137556.htmlhttp://m.shnenglu.com/woaidongmao/archive/2010/12/27/137556.html#Feedback0http://m.shnenglu.com/woaidongmao/comments/commentRss/137556.htmlhttp://m.shnenglu.com/woaidongmao/services/trackbacks/137556.html 

http://blog.csdn.net/bhwst/archive/2010/07/29/5774746.aspx

 

VC要编译LUA文g必须先配|VC~程环境。。我用的是VC6.0Qlua 5.1.4?

首先lua?lua.h" Q?lualib.h"Q?"lauxlib.h" q三个放在vcE序include文g夹下

然后lua的lua5.1.lib攑֜lib文g夹下OK?

下面看看我写的一个小例子Q?

a.cpp

view plaincopy to clipboardprint?

#include "windows.h"

extern "C"{

#include "lua.h"

#include "lualib.h"

#include "lauxlib.h"

}

#pragma comment(lib,"lua5.1.lib")

lua_State * L;

static int clib(lua_State *L) //llua调用的c函数必须定义成static int XXX(lua_State *L)

{

char path[MAX_PATH];

GetCurrentDirectory(MAX_PATH,path);

lua_pushstring(L,path);

return 1; //Z么要q回1Q这是有依据的,该函数把l果压入了栈Qlua调用该函数将从栈?

//?个结?

}

int main ( int argc, char *argv[] )

{

int sum;

//创徏一个指向lua解释器的指针

L = luaL_newstate();

//加蝲lua标准?

luaL_openlibs(L);

//注册C++函数

lua_register(L,"clib",clib);

//加蝲脚本

luaL_dofile(L,"4.lua");

//调用函数

lua_getglobal(L,"run");

//q行函数q把l果压入?

lua_pcall(L,0,0,0);

//关闭q放资?

lua_close(L);

return 0;

}

#include "windows.h"

extern "C"{

#include "lua.h"

#include "lualib.h"

#include "lauxlib.h"

}

#pragma comment(lib,"lua5.1.lib")

lua_State * L;

static int clib(lua_State *L) //llua调用的c函数必须定义成static int XXX(lua_State *L)

{

char path[MAX_PATH];

GetCurrentDirectory(MAX_PATH,path);

lua_pushstring(L,path);

return 1; //Z么要q回1Q这是有依据的,该函数把l果压入了栈Qlua调用该函数将从栈?

//?个结?

}

int main ( int argc, char *argv[] )

{

int sum;

//创徏一个指向lua解释器的指针

L = luaL_newstate();

//加蝲lua标准?

luaL_openlibs(L);

//注册C++函数

lua_register(L,"clib",clib);

//加蝲脚本

luaL_dofile(L,"4.lua");

//调用函数

lua_getglobal(L,"run");

//q行函数q把l果压入?

lua_pcall(L,0,0,0);

//关闭q放资?

lua_close(L);

return 0;

}

4.lua文g

view plaincopy to clipboardprint?

function run()

print("call running from c")

print(clib())

end

function run()

print("call running from c")

print(clib())

end

q个例子实现了C与lua的相互调用。。另外,q可以把我前面一lua调用C dll的内Ҏ合在一赗?

本文来自CSDN博客Q{载请标明出处Qhttp://blog.csdn.net/bhwst/archive/2010/07/29/5774746.aspx



肥仔 2010-12-27 12:57 发表评论
]]>
Lua调用C DLLhttp://m.shnenglu.com/woaidongmao/archive/2010/12/27/137555.html肥仔肥仔Mon, 27 Dec 2010 04:56:00 GMThttp://m.shnenglu.com/woaidongmao/archive/2010/12/27/137555.htmlhttp://m.shnenglu.com/woaidongmao/comments/137555.htmlhttp://m.shnenglu.com/woaidongmao/archive/2010/12/27/137555.html#Feedback0http://m.shnenglu.com/woaidongmao/comments/commentRss/137555.htmlhttp://m.shnenglu.com/woaidongmao/services/trackbacks/137555.htmlhttp://blog.csdn.net/bhwst/archive/2010/07/22/5756441.aspx



肥仔 2010-12-27 12:56 发表评论
]]>
lua5.1.dll ?lua51.dllhttp://m.shnenglu.com/woaidongmao/archive/2010/12/27/137552.html肥仔肥仔Mon, 27 Dec 2010 04:37:00 GMThttp://m.shnenglu.com/woaidongmao/archive/2010/12/27/137552.htmlhttp://m.shnenglu.com/woaidongmao/comments/137552.htmlhttp://m.shnenglu.com/woaidongmao/archive/2010/12/27/137552.html#Feedback1http://m.shnenglu.com/woaidongmao/comments/commentRss/137552.htmlhttp://m.shnenglu.com/woaidongmao/services/trackbacks/137552.html今天下蝲lua的Windows二进制发布luabinaries 的时候,发现luabinaries的发布包含两个dll,lua5.1.dll,lua51.dllQ其中lua5.1.dll大小合适,lua51.dll只有11KQ感觉不像是个有用的lua dllQ对此比较不解,查看了luabinaries的文,
文档中如此解释:
The LuaBinaries DLL packages have a dll proxy called "lua51.dll". It can be used to replace other "lua51.dll" released by other distributions. It will simply forward calls to the "lua5.1.dll". There is no compiled source code involved in the forwarding.
也就是说Qlua51.dll完全是一个lua5.1.dll的代理类Q没有Q何实际的代码Q仅仅是做一个到lua5.1.dll的forwarding?br>    不过文解释了lua51.dll是什么,却没有解释ؓ啥会需要一个这L东西呢?我还从来没有见过cM的情c要说有一点点cM的情늚话,也是以前做反外挂的时候,知道可以通过替换现有的dllQƈ且完全模拟原来dll的接口,q将不需要hack的函数全部forwarding到原来的dll中。对于lua怎么会需要这L功能呢?
于是我google了一下,发现了原因:
The standard DLL name "lua51.dll" has been selected more than
three years ago. Around twice every year someone comes along and
thinks "Oh, we absolutely need a dot in the DLL version number".

Alas, Windows does not like an extra dot in there. Many things
break when you have an extra dot in DLL names. Depends on the
version of the OS, on the specific system call, on the library or
tool used ... it's hopeless. So please let's forget about it.

--Mike
来自lua-users.org的一个帖??br>原来?q前有h定了一个lua51.dll名字的动态库Qƈ且,有h觉得我们非常需要在5?之间加一个点Q不然lua?.1版岂不是看成lua?1版了Q?br>ҎQ有一步提Z疑问 Q认个问题怎么q么久了竟然没有Z复?
接着有h回答了:
But who's duty is to resolve the issue?

It's clearly not an issue of Lua as a language. It's just a consequence of (a very popular) LuaBinaries once releasing lua5.1.dll that became a de facto binary standard, then authors of many third-party Lua libraries were releasing binary packages compatible with LuaBinaries.
Note: I'm not blaming LuaBinaries; that was just one unfortunate decision that is difficult to be undone. --
Shmuel

我们知道了:
LuaBinaries做出了一个错误的军_Q但是已l发布了Q很多第3方的库也发布了,q且依赖于LuaBinaries的这个lua51.dllQ于是:
that was just one unfortunate decision that is difficult to be undone.
那仅仅是一个过d下,现在难以撤销的不q决定。。。。。。。。。。。?

    有的东西存在了,即是不合理的存在,因ؓ它存在了一D|间了Q因为惯性,它还会存在在那里。编E中Q这U情늻常出现。突然让我想起上个项目中Q大家经常对目中蹩脚代码存在原因的解释Q?#8220;历史原因”?br>    对于代码来说Q即使大安知道可以重构Q但是重构是有代LQ很多时候大家就妥协在历史原因当中?br>    对于语言来说QC++是对历史进行最大妥协而生的语言Q大安承认Q假如当qC++不兼容C的话QC++Ҏ得不到现在q样的流行程度,也都承认Q因为C++兼容CQ(常常被称为历史的包袱QC++在语a的优程度上损失了太多?br>    惌v国内某个大牛有过cM的感慨,“现在每做一个设计决定的时候都非常心Q因为那可能会被使用非常非常长的旉Q当它还能正常工作的时候,甚至不会有h惌去重写它“
    呵呵Q仅仅将q个有趣的事件作为编E中的一个轶事来看吧?

本文来自CSDN博客Q{载请标明出处Qhttp://blog.csdn.net/vagrxie/archive/2010/07/23/5756804.aspx



肥仔 2010-12-27 12:37 发表评论
]]>
在C++中应用Google Chrome脚本引擎&mdash;&mdash;V8http://m.shnenglu.com/woaidongmao/archive/2010/05/17/115578.html肥仔肥仔Mon, 17 May 2010 04:25:00 GMThttp://m.shnenglu.com/woaidongmao/archive/2010/05/17/115578.htmlhttp://m.shnenglu.com/woaidongmao/comments/115578.htmlhttp://m.shnenglu.com/woaidongmao/archive/2010/05/17/115578.html#Feedback1http://m.shnenglu.com/woaidongmao/comments/commentRss/115578.htmlhttp://m.shnenglu.com/woaidongmao/services/trackbacks/115578.html阅读全文

肥仔 2010-05-17 12:25 发表评论
]]>
脚本引擎PK: SpiderMonkey vs V8(?http://m.shnenglu.com/woaidongmao/archive/2010/05/17/115577.html肥仔肥仔Mon, 17 May 2010 04:20:00 GMThttp://m.shnenglu.com/woaidongmao/archive/2010/05/17/115577.htmlhttp://m.shnenglu.com/woaidongmao/comments/115577.htmlhttp://m.shnenglu.com/woaidongmao/archive/2010/05/17/115577.html#Feedback0http://m.shnenglu.com/woaidongmao/comments/commentRss/115577.htmlhttp://m.shnenglu.com/woaidongmao/services/trackbacks/115577.html阅读全文

肥仔 2010-05-17 12:20 发表评论
]]>
脚本引擎PK: SpiderMonkey vs V8(一)http://m.shnenglu.com/woaidongmao/archive/2010/05/17/115576.html肥仔肥仔Mon, 17 May 2010 04:15:00 GMThttp://m.shnenglu.com/woaidongmao/archive/2010/05/17/115576.htmlhttp://m.shnenglu.com/woaidongmao/comments/115576.htmlhttp://m.shnenglu.com/woaidongmao/archive/2010/05/17/115576.html#Feedback0http://m.shnenglu.com/woaidongmao/comments/commentRss/115576.htmlhttp://m.shnenglu.com/woaidongmao/services/trackbacks/115576.html阅读全文

肥仔 2010-05-17 12:15 发表评论
]]>
功能强大的JavaScript引擎--SpiderMonkeyhttp://m.shnenglu.com/woaidongmao/archive/2010/05/17/115575.html肥仔肥仔Mon, 17 May 2010 04:13:00 GMThttp://m.shnenglu.com/woaidongmao/archive/2010/05/17/115575.htmlhttp://m.shnenglu.com/woaidongmao/comments/115575.htmlhttp://m.shnenglu.com/woaidongmao/archive/2010/05/17/115575.html#Feedback0http://m.shnenglu.com/woaidongmao/comments/commentRss/115575.htmlhttp://m.shnenglu.com/woaidongmao/services/trackbacks/115575.html阅读全文

肥仔 2010-05-17 12:13 发表评论
]]>
使用 Lua ~写可嵌入式脚本http://m.shnenglu.com/woaidongmao/archive/2009/08/26/94449.html肥仔肥仔Wed, 26 Aug 2009 05:27:00 GMThttp://m.shnenglu.com/woaidongmao/archive/2009/08/26/94449.htmlhttp://m.shnenglu.com/woaidongmao/comments/94449.htmlhttp://m.shnenglu.com/woaidongmao/archive/2009/08/26/94449.html#Feedback0http://m.shnenglu.com/woaidongmao/comments/commentRss/94449.htmlhttp://m.shnenglu.com/woaidongmao/services/trackbacks/94449.html阅读全文

肥仔 2009-08-26 13:27 发表评论
]]>
91ɫۺϾþѷ| Ĺ˾Ʒþ޾ƷA뾫Ʒ | ŷƷž99þڹۿ| þþþƷþþþþ | þ99Ʒþþþþ| ȫɫƴɫƬѾþþ| AVþþƷ| Ʒþþþþþþ| þñѵӰˬˬˬ| þҹ³˿ƬҹƷ| ݺɫþþۺ| 97þù޾Ʒ| Ʒѿþþ㽶 | 999Ʒþþþþ| ɫۺϾþ88ɫۺ | þþþƷһ| 97ȾþƵƷ99| ˺ݺۺϾþ88| ɫþþۺ| þþþAV| 99ȾþֻоƷ68| ٸۺϾþĻ| þݹֻƬ | ĻþþƷ1| þþƷA㽶| þþþþϸApp| þùƷþùƷ| þǿdŮվ| һþƵ| ձһƷþþþӰԺ| þ99ڹ| þAVӰ| vvaþ| 91鶹Ʒ91þþþþ| þ99Ʒþþþþ| þþþ޾Ʒһ| ˾þó˳ۺ222| þۺϳDž| þþþùɫAVѹۿ| ƷȾþþø| þþƷƷް|