CEGUI version 0.6
為了便于找到問題,需要打開應用程序的控制臺,并且要知道寫的lua腳本中哪行語法錯了(畢竟不是天天寫lua,語法錯了很正常)
1.Q:打開應用程序的控制臺
A:
/SUBSYSTEM:{CONSOLE|EFI_APPLICATION|EFI_BOOT_SERVICE_DRIVER|
EFI_ROM|EFI_RUNTIME_DRIVER|NATIVE|POSIX|WINDOWS|WINDOWSCE}
[,major[.minor]]
win32 字符模式應用程序,此種類型的應用程序在運行的時候會產生一個類似DOS
窗口的控制臺窗口,如果在應用程序的主函數為main()或者wmain(),在默認情況下
該應用程序就是一個控制臺應用程序
和CPU具體架構相關的一個參數選項,并不常用,在這里暫不詳細介紹.
如果對此有興趣的可以訪問intel主頁來查看相關內容
設備驅動器選項,如果/DRIVER:WDM選項被設定的話,該鏈接選項(NATIVE)就為默認選項
在windows NT 種運行在POSIX子系統上的應用程序
該類型的應用程序不產生console窗口,該類型的應用程序的窗口由用戶自己創建,簡而言之
就是一個標準的Win32 application,其入口地址為WinMain()函數或者wWinMain()函數的地址
如果你在應用程序種定義的主函數為WinMain或者wWinMain,在默認情況下該應用程序就是一個
Win32 Application !
運行在windows CE上的應用程序
主版本號和次版本號,該選項為可選,該選項為0~65535之間的十進制整數
CONSOLE,可以在VC開發環境的project->setting->link->project option中看到!
我們知道用VC編寫的程序,運行的時候是需要 C\C++運行庫支持的.當我們運行一個C/C++程序的時候
鏈接器會首先尋找應用程序的啟動函數,例如:
如果你建立了一個console程序的話,編譯器得鏈接開關會是以下這種形式
/subsystem:"console" /entry:"mainCRTStartup" (ANSI)
/subsystem:"console" /entry:"wmainCRTStartuup" (UNICODE)
/subsystem:"windows" /entry:"WinMain" (ANSI)
/sbusystem:"windows" /entry:"wWinMain" (UINCODE)
上面的subsystem和entry并不需要都設置,如果你只設置了/subsystem:"console"
的話,那么默認的entry開關在默認情況下應為/entry:"mainCRTStartup"
反之,如果你在應用程序中定義了main函數的話,默認情況下,你的/subsystem開關
應該為/system:"console"
在默認情況下/subsystem 和/entry開關是匹配的,也就是
console對應mainCRTStartup或者wmainCRTStartup
windows對應WinMain或者wWinMain
例如我們可以這樣改動
int main(int argc, char* argv[])
{
MessageBox(NULL, "hello", "Notice", MB_OK);
return 0;
}
上面是在代碼中使用#pragma指令來設置,還有一種就是直接在開發環境的
project->setting->link->project option中手工改動!2. 生成具有console窗口的Win32窗口程序(不使用MFC)
{
HINSTANCE hPreInstance = NULL;
TCHAR szCmdLine[1024];
szCmdLine[0] = 0;
LPTSTR lpCmdLine = szCmdLine;
for ( int i = 1; i < argc; i++ )
{
if ( i > 1 )
{
_tcscpy(lpCmdLine, _T(" "));
lpCmdLine = lpCmdLine + _tcslen(_T(" "));
}
_tcscpy(lpCmdLine, argv[i]);
lpCmdLine = lpCmdLine + _tcslen(argv[i]);
}
lpCmdLine = szCmdLine;
int nCmdShow = SW_SHOWNORMAL;
return 0;
}3.生成具有console窗口的MFC窗口應用程序
LPTSTR lpCmdLine, int nCmdShow)
{
ASSERT(hPrevInstance == NULL);
CWinThread* pThread = AfxGetThread();
CWinApp* pApp = AfxGetApp();
if (!AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow))
goto InitFailure;
if (pApp != NULL && !pApp->InitApplication())
goto InitFailure;
if (!pThread->InitInstance())
{
if (pThread->m_pMainWnd != NULL)
{
TRACE(traceAppMsg, 0, "Warning: Destroying non-NULL m_pMainWnd\n");
pThread->m_pMainWnd->DestroyWindow();
}
nReturnCode = pThread->ExitInstance();
goto InitFailure;
}
nReturnCode = pThread->Run();
#ifdef _DEBUG
// Check for missing AfxLockTempMap calls
if (AfxGetModuleThreadState()->m_nTempMapLock != 0)
{
TRACE(traceAppMsg, 0, "Warning: Temp map lock count non-zero (%ld).\n",
AfxGetModuleThreadState()->m_nTempMapLock);
}
AfxLockTempMaps();
AfxUnlockTempMaps(-1);
#endif
return nReturnCode;
}
第二種方法是顯式加載mfc71d.dll或是mfc71.dll,并調用其中的AfxWinMain函數。不過,這兩個動態鏈接庫都是使用NONAME的方式來導出函數的,因此只能通過函數序號的方式來調用AfxWinMain函數。通過在IDA Pro中對這兩個動態鏈接庫進行反編譯,我們可以發現AfxWinMain在mfc71d.dll中的序號為1589,而在mfc71.dll中的序號為1207,下面即是使用動態鏈接庫的方式調用AfxWinMain的方法。
// typedef int __stdcall (*MYPROC)(HINSTANCE, HINSTANCE,LPTSTR, int);
typedef int (__stdcall *AFXWINMAIN_FUNC)(HINSTANCE, HINSTANCE,LPTSTR, int);
#define MFC_DLL_NAME _T("mfc71d.dll")
#define AFXWINMAIN_ORDINAL 1589
#else
#define MFC_DLL_NAME _T("mfc71.dll")
#define AFXWINMAIN_ORDINAL 1207
#endif
int _tmain()
{
char _afxInitAppState = (char)(AfxInitialize(FALSE, _MFC_VER), atexit(&_AfxTermAppState));
#else
char _afxInitAppState = (char)(AfxInitialize(FALSE, _MFC_VER));
#endif
AFXWINMAIN_FUNC ProcAdd;
int ret = 0;
{
ProcAdd = (AFXWINMAIN_FUNC) GetProcAddress(hinstLib, MAKEINTRESOURCE(AFXWINMAIN_ORDINAL));
{
HINSTANCE hPrevInstance = NULL;
LPTSTR lpCmdLine = NULL;
int nCmdShow = SW_SHOWNORMAL;
ret = (*ProcAdd)(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}
}
需要說明的是,在GetProcAddress函數中,我們需要調用MAKEINTRESOURCE來將函數序號進行轉化。 另外,對于AFXWINMAIN_FUNC的定義一定要加上__stdcall的調用約定,因為AfxWinMain是采用__stdcall方式來調用的。最后還有一點需要注意,我們需要使用AfxInitialize來注冊退出函數,否則程序將不能正確退出。
2.Q:要知道寫的lua腳本中哪行語法錯了,即lua語法檢查
A:
添加粗體的兩行就能在控制臺中看到出錯信息,并能定位到那一行
{
// load file
RawDataContainer raw;
System::getSingleton().getResourceProvider()->loadRawDataContainer(filename,
raw, resourceGroup.empty() ? d_defaultResourceGroup : resourceGroup);
// load code into lua
int top = lua_gettop(d_state);
int loaderr = luaL_loadbuffer(d_state, (char*)raw.getDataPtr(), raw.getSize(), filename.c_str());
System::getSingleton().getResourceProvider()->unloadRawDataContainer( raw );
if (loaderr)
{
String errMsg = lua_tostring(d_state,-1);
lua_settop(d_state,top);
#ifdef _DEBUG
::printf_s("[LuaScriptModule]: %s\n", errMsg.c_str());
#endif
throw ScriptException("Unable to execute Lua script file: '"+filename+"'\n\n"+errMsg+"\n");
}
// call it
if (lua_pcall(d_state,0,0,0))
{
String errMsg = lua_tostring(d_state,-1);
lua_settop(d_state,top);
#ifdef _DEBUG
::printf_s("[LuaScriptModule]: %s\n", errMsg.c_str());
#endif
throw ScriptException("Unable to execute Lua script file: '"+filename+"'\n\n"+errMsg+"\n");
}
lua_settop(d_state,top); // just in case :P
}
3.Q:用字符集Unicode生成lua51_d.lib調用GetModuleFileName為什么會出錯?
char buff[MAX_PATH + 1];
char *lb;
DWORD nsize = sizeof(buff)/sizeof(char);
DWORD n = GetModuleFileName(NULL, buff, nsize);
if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL)
luaL_error(L, "unable to get ModuleFileName");
else {
*lb = '\0';
luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff);
lua_remove(L, -2); /* remove original string */
}
}
A: 因為調用了GetModuleFileNameW,寬字符版本,而buff是char,buff里面就是d0 :0 /0,類似如此
#define GetModuleFileName GetModuleFileNameW
#else
#define GetModuleFileName GetModuleFileNameA
#endif // !UNICODE
4. Q: CEGUI里面的lua如何使用?
A:
CEGUI::LuaScriptModule* pScriptMod = new LuaScriptModule();
System::getSingleton().setScriptingModule(pScriptMod);
// 執行lua時
System::getSingleton().executeScriptFile("Sample_Text.lua");
// 退出程序時
CEGUI::ScriptModule* pScriptModule = System::getSingleton().getScriptingModule();
delete pScriptModule;
pScriptModule = NULL;
5.貼lua代碼,以后看起來方便
我改了下CEGUI的這個例子 Sample_TextDemo
print輸出信息到控制臺
Sample_Text.lua
local guiSystem = CEGUI.System:getSingleton()
local schemeMgr = CEGUI.SchemeManager:getSingleton()
local winMgr = CEGUI.WindowManager:getSingleton();
local fontMgr = CEGUI.FontManager:getSingleton();
-- load scheme and set up defaults
schemeMgr:loadScheme("TaharezLook.scheme");
guiSystem:setDefaultMouseCursor("TaharezLook", "MouseArrow");
-- We need a font
if(false == fontMgr:isFontPresent("DejaVuSans-10")) then
fontMgr:createFont("DejaVuSans-10.font");
end
-- Font defaulting
if(fontMgr:isFontPresent("DejaVuSans-10")) then
guiSystem:setDefaultFont("DejaVuSans-10");
elseif(fontMgr:isFontPresent("Commonwealth-10")) then
guiSystem:setDefaultFont("Commonwealth-10");
end
-- load an image to use as a background
CEGUI.ImagesetManager:getSingleton():createImagesetFromImageFile("BackgroundImage", "GPN-2000-001437.tga");
-- here we will use a StaticImage as the root, then we can use it to place a background image
local background = winMgr:createWindow("TaharezLook/StaticImage", "background_wnd");
-- set position and size
local xx = CEGUI.UDim(0,0)
local yy = CEGUI.UDim(0,0)
local zz = CEGUI.UVector2(xx,yy)
background:setPosition(zz);
background:setSize(CEGUI.UVector2(CEGUI.UDim(1,0), CEGUI.UDim(1,0)));
-- disable frame and standard background
background:setProperty("FrameEnabled", "false");
background:setProperty("BackgroundEnabled", "false");
-- set the background image
background:setProperty("Image", "set:BackgroundImage image:full_image");
-- install this as the root GUI sheet
guiSystem:setGUISheet(background);
-- Load our layout as a basic
background:addChildWindow (winMgr:loadWindowLayout ("TextDemo.layout"));
print("Sample_Text OK")



