S3Client.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:8k
源码类别:

模拟服务器

开发平台:

C/C++

  1.  // S3Client.cpp : Defines the entry point for the application.
  2. //
  3. #include "KWin32.h"
  4. #include "KCore.h"
  5. #include "S3Client.h"
  6. #include "KWin32Wnd.h"
  7. #include "../../Represent/iRepresent/iRepresentShell.h"
  8. #include "Ui/UiShell.h"
  9. #include "NetConnect/NetConnectAgent.h"
  10. #include "TextCtrlCmd/TextCtrlCmd.h"
  11. #include "KPakList.h"
  12. #include "Ui/Elem/TextPic.h"
  13. #include "Ui/Elem/UiCursor.h"
  14. #include "Ui/Elem/SpecialFuncs.h"
  15. #include "Ui/FilterTextLib.h"
  16. #include "Ui/ChatFilter.h"
  17. #include "Ui/uibase.h"
  18. #include "ErrorCode.h"
  19. #define ClientVersion
  20. KMyApp MyApp;
  21. HINSTANCE hInst;
  22. KPakList g_PakList;
  23. CFilterTextLib g_libFilterText;
  24. CChatFilter g_ChatFilter;
  25. #define QUIT_QUESTION_ID "22"
  26. #define GAME_TITLE "23"
  27. #define REPRESENT_MODULE_2 "Represent2.dll"
  28. #define REPRESENT_MODULE_3 "Represent3.dll"
  29. #define CREATE_REPRESENT_SHELL_FUN "CreateRepresentShell"
  30. #define GAME_FPS 18
  31. //Represent模块接口的指针
  32. struct iRepresentShell* g_pRepresentShell = NULL;
  33. struct IInlinePicEngineSink* g_pIInlinePicSink = NULL;
  34. iCoreShell* g_pCoreShell = NULL;
  35. KMusic* g_pMusic = NULL;
  36. #define DYNAMIC_LINK_REPRESENT_LIBRARY
  37. #ifdef DYNAMIC_LINK_REPRESENT_LIBRARY
  38. static HMODULE l_hRepresentModule = NULL;
  39. int g_bRepresent3 = false;
  40. #endif
  41. int g_bScreen = true;
  42. char g_szGameName[32] = "剑侠情缘·网络版";
  43. KClientCallback g_ClientCallback;
  44. #define SCREEN_WIDTH 800
  45. #define SCREEN_HEIGHT 600
  46. /*
  47.  * Add this macro by liupeng on 2003.3.20
  48.  * This macro is helper that can judge some legal character
  49.  */
  50. #define _private_IS_SPACE(c)   ((c) == ' ' || (c) == 'r' || (c) == 'n' || (c) == 't' || (c) == 'x')
  51. #define IS_SPACE(c) _private_IS_SPACE(c)
  52. int APIENTRY WinMain(HINSTANCE hInstance,
  53.                      HINSTANCE hPrevInstance,
  54.                      LPSTR     lpCmdLine,
  55.                      int       nCmdShow)
  56. {
  57.   // TODO: Place code here.
  58. /*
  59.  * Add this funtion by liupeng on 2003.3.20
  60.  * We can find some error when start a console tracer
  61.  */
  62. #ifdef TRUE
  63. bool bOpenTracer = false;
  64.     while( lpCmdLine[0] == '-' || lpCmdLine[0] == '/' )
  65.     {
  66.         lpCmdLine++;
  67.         switch ( *lpCmdLine++ )
  68.         {
  69. case 'c':
  70.         case 'C':
  71.             bOpenTracer = true;
  72.             break;
  73.         }
  74.         while( IS_SPACE( *lpCmdLine ) )
  75.         {
  76.             lpCmdLine++;
  77.         }
  78.     }
  79. if ( bOpenTracer ) 
  80. {
  81. AllocConsole();
  82. }
  83. #endif // End of this function
  84. hInst = hInstance;
  85. if (MyApp.Init(hInstance))
  86. MyApp.Run();
  87. #ifdef TRUE
  88. if ( bOpenTracer )
  89. {
  90. FreeConsole();
  91. }
  92. #endif
  93. Error_Box();
  94. return 0;
  95. }
  96. KMyApp::KMyApp()
  97. {
  98. m_pInlinePicSink = NULL;
  99. }
  100. BOOL InitRepresentShell(BOOL bFullScreen, int nWidth, int nHeight)
  101. {
  102. Error_SetErrorString(g_bRepresent3 ? REPRESENT_MODULE_3 : REPRESENT_MODULE_2);
  103. if (g_pRepresentShell == NULL)
  104. {
  105. #ifdef DYNAMIC_LINK_REPRESENT_LIBRARY
  106. if (l_hRepresentModule == NULL &&
  107. (l_hRepresentModule = LoadLibrary(g_bRepresent3 ? REPRESENT_MODULE_3 : REPRESENT_MODULE_2)) == NULL)
  108. {
  109. Error_SetErrorCode(ERR_T_LOAD_MODULE_FAILED);
  110. return FALSE;
  111. }
  112. fnCreateRepresentShell pCreate = (fnCreateRepresentShell)GetProcAddress(
  113. l_hRepresentModule, CREATE_REPRESENT_SHELL_FUN);
  114. if (pCreate == NULL || 
  115. (g_pRepresentShell = pCreate()) == NULL)
  116. {
  117. Error_SetErrorCode((pCreate == NULL) ? ERR_T_MODULE_UNCORRECT : ERR_T_MODULE_INIT_FAILED);
  118. return FALSE;
  119. }
  120. #else
  121. g_pRepresentShell = CreateRepresentShell();
  122. #endif
  123. }
  124. if(g_pRepresentShell->Create(nWidth, nHeight, bFullScreen != 0))
  125. {
  126. return TRUE;
  127. }
  128. else
  129. {
  130. Error_SetErrorCode(g_bRepresent3 ? ERR_T_REPRESENT3_INIT_FAILED : ERR_T_REPRESENT2_INIT_FAILED);
  131. return FALSE;
  132. }
  133. }
  134. BOOL KMyApp::GameInit()
  135. {
  136. Error_SetErrorString("KMyApp::GameInit");
  137.     #ifdef KUI_USE_HARDWARE_MOUSE
  138.     ShowMouse(TRUE);
  139.     
  140.     #else   // KUI_USE_HARDWARE_MOUSE
  141.     ShowMouse(FALSE);
  142.     
  143.     #endif
  144. g_SetRootPath(NULL);
  145. g_SetFilePath("\");
  146. KIniFile* pSetting = g_UiBase.GetCommConfigFile();
  147. if (pSetting)
  148. {
  149. pSetting->GetString("Main", "GameName", "剑侠情缘·网络版", g_szGameName, sizeof(g_szGameName));
  150.         SetWindowText(g_GetMainHWnd(), g_szGameName);
  151. }
  152. #ifdef _DEBUG
  153. g_FindDebugWindow("#32770","DebugWin");
  154. #endif
  155. KIniFile IniFile;
  156. if (!IniFile.Load("\config.ini"))
  157. {
  158. Error_SetErrorCode(ERR_T_FILE_NO_FOUND);
  159. Error_SetErrorString("\config.ini");
  160. return FALSE;
  161. }
  162. #ifdef _DEBUG
  163. BOOL bCursor = FALSE;
  164. if (IniFile.GetInteger("Client", "ShowCursor", 0, &bCursor))
  165. ShowMouse(TRUE);
  166. #endif
  167. IniFile.GetInteger("Client", "FullScreen", FALSE, &g_bScreen);
  168. #ifdef DYNAMIC_LINK_REPRESENT_LIBRARY
  169. IniFile.GetInteger("Client", "Represent", 2, &g_bRepresent3);
  170. g_bRepresent3 = (g_bRepresent3 == 3);
  171. #endif
  172. g_PakList.Open("\config.ini");
  173. char szPath[MAX_PATH];
  174. if (IniFile.GetString("Client", "CapPath", "", szPath, sizeof(szPath)))
  175. {
  176. if (szPath[0])
  177. SetScrPicPath(szPath);
  178. }
  179. IniFile.Clear();
  180. if (!g_libFilterText.Initialize()
  181. || !g_ChatFilter.Initialize())
  182. return FALSE;
  183. if (!InitRepresentShell(g_bScreen, SCREEN_WIDTH, SCREEN_HEIGHT))
  184. {
  185. return FALSE;
  186. }
  187. if (!UiInit())
  188. {
  189. Error_SetErrorCode(ERR_T_MODULE_INIT_FAILED);
  190. Error_SetErrorString("UiInit");
  191. return FALSE;
  192. }
  193. //[wxb 2003-6-23]
  194. m_pInlinePicSink = new KInlinePicSink;
  195.     if (m_pInlinePicSink)
  196. {
  197. m_pInlinePicSink->Init(g_pRepresentShell);
  198. _ASSERT(NULL == g_pIInlinePicSink);
  199. g_pIInlinePicSink = m_pInlinePicSink;
  200. }
  201. UiSetScreenSize(SCREEN_WIDTH, SCREEN_HEIGHT);
  202. UiPaint(0);
  203. // init dsound
  204. m_Sound.Init();
  205. SetMultiGame(TRUE);
  206. if ((g_pCoreShell = CoreGetShell()) == NULL)
  207. {
  208. Error_SetErrorCode(ERR_T_MODULE_INIT_FAILED);
  209. Error_SetErrorString("CoreGetShell");
  210. return false;
  211. }
  212. g_pCoreShell->SetRepresentShell(g_pRepresentShell);
  213. g_pCoreShell->SetMusicInterface((KMusic*)&m_Music);
  214. g_pCoreShell->SetCallDataChangedNofify(&g_ClientCallback);
  215. g_pCoreShell->SetRepresentAreaSize(SCREEN_WIDTH, SCREEN_HEIGHT);
  216. g_pMusic = &m_Music;
  217. if (g_NetConnectAgent.Initialize() == 0)
  218. {
  219. Error_SetErrorCode(ERR_T_MODULE_INIT_FAILED);
  220. Error_SetErrorString("NetConnectAgent");
  221. return FALSE;
  222. }
  223. m_GameCounter = 0;
  224. m_Timer.Start();
  225. SetMouseHoverTime(400);
  226. if(UiStart())
  227. {
  228. return TRUE;
  229. }
  230. else
  231. {
  232. Error_SetErrorCode(ERR_T_MODULE_INIT_FAILED);
  233. Error_SetErrorString("UiStart");
  234. return FALSE;
  235. }
  236. }
  237. BOOL KMyApp::GameExit()
  238. {
  239. if (m_pInlinePicSink)
  240. {
  241. //[wxb 2003-6-23]
  242. m_pInlinePicSink->UnInit();
  243. delete m_pInlinePicSink;
  244. m_pInlinePicSink = NULL;
  245. g_pIInlinePicSink = NULL;
  246. }
  247. UiExit();
  248. g_pMusic = NULL;
  249. if (g_pCoreShell)
  250. {
  251. g_pCoreShell->SetRepresentShell(NULL);
  252. g_pCoreShell->SetClient(NULL);
  253. g_pCoreShell->SetMusicInterface(NULL);
  254. g_pCoreShell->Release();
  255. g_pCoreShell = NULL;
  256. }
  257. if (g_pRepresentShell)
  258. {
  259. g_pRepresentShell->Release();
  260. g_pRepresentShell = NULL;
  261. }
  262. g_NetConnectAgent.Exit();
  263. m_Music.Close();
  264. m_Sound.Exit();
  265. #ifdef DYNAMIC_LINK_REPRESENT_LIBRARY
  266. if (l_hRepresentModule)
  267. {
  268. FreeLibrary(l_hRepresentModule);
  269. l_hRepresentModule = NULL;
  270. }
  271. #endif
  272. ::ShowCursor(TRUE);
  273. g_ChatFilter.Uninitialize();
  274. g_libFilterText.Uninitialize();
  275. return TRUE;
  276. }
  277. BOOL KMyApp::GameLoop()
  278. {
  279. static int nGameFps = 0;
  280. g_NetConnectAgent.Breathe();
  281. if (m_GameCounter * 1000 <= m_Timer.GetElapse() * GAME_FPS)
  282. {
  283. if (g_pCoreShell->Breathe() && UiHeartBeat())
  284. {
  285. m_GameCounter++;
  286. int nElapse = m_Timer.GetElapse();
  287. if (nElapse)
  288. nGameFps = m_GameCounter * 1000 / nElapse;
  289. }
  290. else
  291. {
  292. return false;
  293. }
  294. }
  295. if (m_GameCounter * 1000 >= m_Timer.GetElapse() * GAME_FPS)
  296. {
  297. UiPaint(nGameFps);
  298. Sleep(1);
  299. }
  300. else if ((m_GameCounter % 8) == 0)
  301. {
  302. Sleep(1);
  303. }
  304. return true;
  305. }
  306. int KMyApp::HandleInput(UINT uMsg, WPARAM wParam, LPARAM lParam)
  307. {
  308. int nRet = 0;
  309. if (uMsg != WM_CLOSE)
  310. {
  311. UiProcessInput(uMsg, wParam, lParam);
  312. }
  313. else if (g_bScreen == false && UiIsAlreadyQuit() == false)
  314. {
  315. KIniFile* pSetting = g_UiBase.GetCommConfigFile();
  316. if (pSetting)
  317. {
  318. char szMsg[128], szTitle[64];
  319. pSetting->GetString("InfoString", QUIT_QUESTION_ID, "", szMsg, sizeof(szMsg));
  320. pSetting->GetString("InfoString", GAME_TITLE, "", szTitle, sizeof(szTitle));
  321. if (szMsg[0] && szTitle[0])
  322. {
  323. nRet = (MessageBox(g_GetMainHWnd(), szMsg, szTitle,
  324. MB_YESNO | MB_ICONQUESTION) != IDYES);
  325. }
  326. }
  327. }
  328. return nRet;
  329. }