Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.08 KB | None | 0 0
  1. #include "r3dPCH.h"
  2. #include "r3d.h"
  3. #include "r3dDebug.h"
  4.  
  5.  
  6. #include "../SF/Console/EngineConsole.h"
  7.  
  8. char __r3dCmdLine[1024];
  9.  
  10. typedef bool (*Win32MsgProc_fn)(UINT uMsg, WPARAM wParam, LPARAM lParam);
  11.  
  12. //hack: add temp handler for external message processing
  13.  
  14. namespace
  15. {
  16. const int NUM_MSG_PROC_MAX = 8;
  17. Win32MsgProc_fn r3dApp_MsgProc3[NUM_MSG_PROC_MAX] = {0};
  18. }
  19.  
  20. static int StartWinHeight = 300;
  21. static int StartWinWidth = 300;
  22. static int curDispWidth = 0;
  23. static int curDispHeight = 0;
  24.  
  25. namespace win
  26. {
  27. HINSTANCE hInstance = NULL;
  28. HWND hWnd = NULL;
  29. const char *szWinName = "$o, yeah!$";
  30. HICON hWinIcon = 0;
  31. int bSuspended = 0;
  32. int bNeedRender = 1;
  33.  
  34. void HandleActivate();
  35. void HandleDeactivate();
  36. void HandleMinimize();
  37. void HandleRestore();
  38.  
  39. int Init();
  40. }
  41.  
  42. void RegisterMsgProc (Win32MsgProc_fn proc)
  43. {
  44. for (int i = 0; i<NUM_MSG_PROC_MAX; i++ )
  45. {
  46. if (!r3dApp_MsgProc3[i])
  47. {
  48. r3dApp_MsgProc3[i] = proc;
  49. return;
  50. }
  51. }
  52. r3d_assert ( 0 && "RegisterMsgProc error; register more than NUM_MSG_PROC_MAX MsgProc." );
  53. }
  54.  
  55. void UnregisterMsgProc (Win32MsgProc_fn proc)
  56. {
  57. for (int i = 0; i<NUM_MSG_PROC_MAX; i++ )
  58. {
  59. if (r3dApp_MsgProc3[i] == proc)
  60. {
  61. r3dApp_MsgProc3[i] = NULL;
  62. return;
  63. }
  64. }
  65. r3d_assert ( 0 && "UnregisterMsgProc error." );
  66. }
  67.  
  68. #define INPUT_KBD_STACK 32
  69. volatile static int input_ScanStack[INPUT_KBD_STACK + 2];
  70. volatile static int * volatile input_StackHead = input_ScanStack;
  71. volatile static int * volatile input_StackTail = input_ScanStack;
  72.  
  73.  
  74. bool g_bExit = false;
  75.  
  76. bool IsNeedExit()
  77. {
  78. return g_bExit;
  79. }
  80.  
  81. int win::input_Flush()
  82. {
  83. input_StackHead = input_ScanStack;
  84. input_StackTail = input_ScanStack;
  85.  
  86. return 1;
  87. }
  88.  
  89. int win::kbhit()
  90. {
  91. if(input_StackHead == input_StackTail)
  92. return 0;
  93. else
  94. return 1;
  95. }
  96.  
  97. int win::getch( bool bPop )
  98. {
  99. int ch;
  100.  
  101. if(!win::kbhit())
  102. return 0;
  103.  
  104. ch = *input_StackTail;
  105. if ( bPop )
  106. input_StackTail++;
  107.  
  108. if(input_StackTail >= input_ScanStack + INPUT_KBD_STACK)
  109. input_StackTail = input_ScanStack;
  110.  
  111. return ch;
  112. }
  113.  
  114.  
  115. static
  116. void wnd__DrawLogo()
  117. {
  118. #if TSG_BITMAP
  119. HDC hDC;
  120. HANDLE hBmp;
  121.  
  122. // load background image and center our window
  123. hBmp = LoadImage(
  124. GetModuleHandle(NULL),
  125. MAKEINTRESOURCE(TSG_BITMAP),
  126. IMAGE_BITMAP,
  127. win__Width,
  128. win__Height,
  129. LR_DEFAULTCOLOR
  130. );
  131. if(hBmp == NULL) return;
  132.  
  133. if(hDC = GetDC(win::hWnd))
  134. {
  135. HDC dc;
  136.  
  137. dc = CreateCompatibleDC(hDC);
  138. SelectObject(dc, hBmp);
  139. BitBlt(hDC, 0, 0, win__Width, win__Height, dc, 0, 0, SRCCOPY);
  140. DeleteDC(dc);
  141.  
  142. ReleaseDC(win::hWnd, hDC);
  143. }
  144.  
  145. DeleteObject(hBmp);
  146.  
  147. #endif
  148.  
  149. return;
  150. }
  151.  
  152. bool winMouseWasVisible = true;
  153. extern bool r3dMouse_visible;
  154. void win::HandleActivate()
  155. {
  156. // if(!win::bSuspended)
  157. // return;
  158.  
  159. win::bSuspended = 0;
  160. win::bNeedRender = 1;
  161.  
  162. if(Mouse) Mouse->SetCapture();
  163. if(Keyboard) Keyboard->ClearPressed();
  164.  
  165. if( winMouseWasVisible || g_cursor_mode->GetInt() )
  166. r3dMouse::Show(true);
  167. else
  168. r3dMouse::Hide(true);
  169. }
  170.  
  171. void win::HandleDeactivate()
  172. {
  173. // if(win::bSuspended)
  174. // return;
  175. win::bSuspended = 1;
  176.  
  177. if( !(r_render_on_deactivation && r_render_on_deactivation->GetInt()) )
  178. win::bNeedRender = 0;
  179. else
  180. win::bNeedRender = 1;
  181.  
  182. winMouseWasVisible = r3dMouse_visible;
  183.  
  184. if(Mouse) Mouse->ReleaseCapture();
  185. if(Keyboard) Keyboard->ClearPressed();
  186. }
  187.  
  188. void win::HandleMinimize()
  189. {
  190. win::bNeedRender = 0;
  191. }
  192.  
  193. void win::HandleRestore()
  194. {
  195. win::bNeedRender = 1;
  196. }
  197.  
  198.  
  199. //
  200. //
  201. //
  202. // main Callback function
  203. //
  204. //
  205.  
  206. /*
  207. namespace
  208. {
  209. //
  210. // some vars to simulate move moving by ourselves.
  211. //
  212.  
  213. static bool bOnCaption = false; // true if we're on caption
  214. static bool bDragging = true; // true if we're dragging our window
  215. static int dragX = 0; // offset of drag point, relative to window origin
  216. static int dragY = 0; // offset of drag point, relative to window origin
  217.  
  218. static void dragDisable()
  219. {
  220. if(!bDragging) return;
  221.  
  222. ReleaseCapture();
  223. bDragging = false;
  224.  
  225. return;
  226. }
  227. };
  228. */
  229.  
  230. void (*OnDblClick)() = 0;
  231. void (*OnDrawClipboardCallback)(WPARAM wParam, LPARAM lParam) = 0;
  232.  
  233. HWND hWndNextViewer = 0; // clipboard support
  234.  
  235. static LRESULT CALLBACK win__WndFunc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  236. {
  237. static int userProcActive = 0;
  238.  
  239. if(userProcActive) {
  240. // in very rare occasions we can reenter this function
  241. // one of cases is that when error is thrown from processing user or UI functions
  242. // so, in this case we'll enter to never ending loop
  243. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  244. }
  245.  
  246. userProcActive = true;
  247. bool bAnyCompleted = false;
  248. for(int i = 0; i < NUM_MSG_PROC_MAX; i++ )
  249. {
  250. if ( r3dApp_MsgProc3 [i] )
  251. bAnyCompleted |= r3dApp_MsgProc3[i](uMsg, wParam, lParam);
  252. }
  253. userProcActive = false;
  254. if ( bAnyCompleted ) return 0;
  255.  
  256.  
  257. //r3dOutToLog("uMsg %x\n", uMsg);
  258. switch(uMsg)
  259. {
  260. case WM_CLOSE:
  261. {
  262. r3dOutToLog("alt-f4 pressed\n");
  263. r3dOutToLog("...terminating application\n");
  264.  
  265. ClipCursor(NULL);
  266.  
  267. //HRESULT res = TerminateProcess(r3d_CurrentProcess, 0);
  268.  
  269. g_bExit = true;
  270. return 0;
  271. }
  272.  
  273. case WM_CONTEXTMENU:
  274. // disable context menu
  275. return 0;
  276.  
  277. case WM_ENTERMENULOOP:
  278. r3dOutToLog("!!!warning!!! r3dApp entered to modal menu loop\n");
  279. break;
  280.  
  281. case WM_ENTERSIZEMOVE:
  282. //r3d_assert(0 && "WM_ENTERSIZEMOVE");
  283. win::HandleDeactivate();
  284. return 0;
  285.  
  286. case WM_EXITSIZEMOVE:
  287. r3dOutToLog("WM_EXITSIZEMOVE: %d\n", win::bSuspended);
  288. win::HandleActivate();
  289. break;
  290.  
  291. /*
  292. case WM_CAPTURECHANGED:
  293. // if we lose capture from our window - disable dragging
  294. if(bDragging && (HWND)lParam != hWnd) {
  295. // note: do not call dragDisable(), because it will call ReleaseCapture()
  296. bDragging = false;
  297. break;
  298. }
  299. break;
  300.  
  301. case WM_LBUTTONUP:
  302. {
  303. if(bDragging) {
  304. dragDisable();
  305. }
  306. break;
  307. }
  308.  
  309. case WM_LBUTTONDOWN:
  310. {
  311. if(bOnCaption) {
  312. // start dragging our window, calc drag anchor point, relative to window corner
  313. POINT pp;
  314. GetCursorPos(&pp);
  315. RECT rr;
  316. GetWindowRect(hWnd, &rr);
  317.  
  318. dragX = pp.x - rr.left;
  319. dragY = pp.y - rr.top;
  320.  
  321. ::SetCapture(hWnd);
  322. bDragging = true;
  323. }
  324.  
  325. break;
  326. }
  327.  
  328. case WM_MOUSEMOVE:
  329. {
  330. if(bDragging) {
  331. POINT pp;
  332. GetCursorPos(&pp);
  333. SetWindowPos(hWnd, 0, pp.x - dragX, pp.y - dragY, 0, 0, SWP_NOOWNERZORDER | SWP_NOSIZE | SWP_NOZORDER);
  334. }
  335.  
  336. break;
  337. }
  338.  
  339. case WM_NCHITTEST:
  340. {
  341. if(!r3dRenderer || !r3dRenderer->d3dpp.Windowed)
  342. break;
  343. if(win::bSuspended)
  344. break;
  345.  
  346. // if cursor is clipped somewhere, don't allow this window dragging at all.
  347. RECT clipRect;
  348. ::GetClipCursor(&clipRect);
  349. if(clipRect.left != 0 || clipRect.top != 0 || clipRect.right != curDispWidth || clipRect.bottom != curDispHeight) {
  350. return HTCLIENT;
  351. }
  352.  
  353. LONG hitTest = DefWindowProc(hWnd, uMsg, wParam, lParam);
  354. if(hitTest == HTCLIENT && bOnCaption) {
  355. if(Mouse) Mouse->Hide();
  356. if(Mouse) Mouse->SetCapture();
  357.  
  358. bOnCaption = false;
  359. return hitTest;
  360. }
  361.  
  362. if(hitTest == HTCAPTION && bOnCaption) {
  363. // return that we still in client area - so windows will not start windows moving by itself
  364. return HTCLIENT;
  365. }
  366.  
  367. if(hitTest == HTCAPTION && !bOnCaption) {
  368. if(Mouse) Mouse->Show();
  369. if(Mouse) Mouse->ReleaseCapture();
  370.  
  371. // we will simuate dragging by ourselves, otherwise game will be in modal dragging loop!
  372. bOnCaption = true;
  373. return HTCLIENT;
  374. }
  375.  
  376. return hitTest;
  377. }
  378. */
  379.  
  380. // disable menu calling when ALT pressed
  381. case WM_SYSCOMMAND:
  382. {
  383. // disable those system commands
  384. switch(wParam & 0xFFFF) {
  385. case 0xF093: // system menu on caption bar
  386. case SC_KEYMENU:
  387. case SC_MOVE:
  388. return 0;
  389. case SC_MINIMIZE:
  390. win::HandleMinimize();
  391. break;
  392. case SC_RESTORE:
  393. win::HandleRestore();
  394. break;
  395.  
  396. }
  397.  
  398. r3dOutToLog("r3dapp: SysCmd: %x\n", wParam & 0xFFFF);
  399. break;
  400. }
  401.  
  402. case WM_NCACTIVATE:
  403. //dragDisable();
  404.  
  405. if((wParam & 0xFFFF) == TRUE)
  406. win::HandleActivate();
  407. break;
  408.  
  409. case WM_ACTIVATE:
  410. //dragDisable();
  411.  
  412. if((wParam & 0xFFFF) == WA_INACTIVE) {
  413. win::HandleDeactivate();
  414. } else {
  415. win::HandleActivate();
  416. }
  417. break;
  418.  
  419. case WM_KEYDOWN:
  420. {
  421. EngineConsole::ProcessKey( wParam );
  422. break;
  423. }
  424.  
  425. case WM_LBUTTONDBLCLK:
  426. if( OnDblClick )
  427. OnDblClick() ;
  428. break;
  429.  
  430. // store char to input stream
  431. case WM_CHAR:
  432. {
  433. EngineConsole::ProcessChar( wParam );
  434.  
  435. int ch;
  436.  
  437. ch = (TCHAR)wParam;
  438. *(input_StackHead++) = ch;
  439. if(input_StackHead >= input_ScanStack + INPUT_KBD_STACK)
  440. input_StackHead = input_ScanStack;
  441. break;
  442. }
  443.  
  444. case WM_PAINT:
  445. {
  446. HDC hDC;
  447. PAINTSTRUCT ps;
  448.  
  449. hDC = BeginPaint(hWnd,&ps);
  450. wnd__DrawLogo();
  451. EndPaint(hWnd,&ps);
  452. break;
  453. }
  454.  
  455. case WM_DRAWCLIPBOARD:
  456. if(OnDrawClipboardCallback)
  457. OnDrawClipboardCallback(wParam, lParam);
  458. break;
  459.  
  460. case WM_CHANGECBCHAIN:
  461. if ((HWND) wParam == hWndNextViewer)
  462. hWndNextViewer = (HWND) lParam;
  463. else if (hWndNextViewer != NULL)
  464. SendMessage(hWndNextViewer, WM_CHANGECBCHAIN, wParam, lParam);
  465. break;
  466.  
  467. case WM_CREATE:
  468. hWndNextViewer = SetClipboardViewer(hWnd);
  469. break;
  470.  
  471. case WM_DESTROY:
  472. if (!hWndNextViewer)
  473. ChangeClipboardChain(hWnd, hWndNextViewer);
  474. //PostQuitMessage (0);
  475. g_bExit = true;
  476. break;
  477. }
  478.  
  479. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  480. }
  481.  
  482. void r3dWinStyleModify(HWND hWnd, int add, DWORD style)
  483. {
  484. if(add)
  485. SetWindowLong(hWnd, GWL_STYLE, GetWindowLong(win::hWnd, GWL_STYLE) | style);
  486. else
  487. SetWindowLong(hWnd, GWL_STYLE, GetWindowLong(win::hWnd, GWL_STYLE) & ~style);
  488. }
  489.  
  490.  
  491. BOOL win::Init()
  492. {
  493. static const char* szWinClassName = "r3dWin";
  494.  
  495. r3d_assert(hInstance != NULL);
  496.  
  497. WNDCLASS wndclass;
  498. wndclass.style = CS_DBLCLKS | CS_GLOBALCLASS;
  499. wndclass.lpfnWndProc = win__WndFunc; // window function
  500. wndclass.cbClsExtra = 0; // no extra count of bytes
  501. wndclass.cbWndExtra = 0; // no extra count of bytes
  502. wndclass.hInstance = GetModuleHandle(NULL); // this instance
  503. wndclass.hIcon = (hWinIcon) ? hWinIcon : LoadIcon(NULL, IDI_APPLICATION);
  504. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  505. wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  506. wndclass.lpszMenuName = NULL;
  507. wndclass.lpszClassName = szWinClassName;
  508. RegisterClass(&wndclass);
  509.  
  510. HDC disp_dc;
  511.  
  512. disp_dc = CreateIC("DISPLAY", NULL, NULL, NULL);
  513. curDispWidth = GetDeviceCaps(disp_dc, HORZRES);
  514. curDispHeight = GetDeviceCaps(disp_dc, VERTRES);
  515. //disp_bpp = GetDeviceCaps(disp_dc, BITSPIXEL);
  516. DeleteDC(disp_dc);
  517.  
  518. hWnd = CreateWindow(
  519. /* window class name */ szWinClassName,
  520. /* window caption*/ szWinName,
  521. /* window style */ WS_VISIBLE | WS_OVERLAPPEDWINDOW,
  522. /* initial x position */ (curDispWidth - StartWinWidth) / 2,
  523. /* initial y position */ (curDispHeight - StartWinHeight) / 2,
  524. /* initial x size */ StartWinWidth,
  525. /* initial y size */ StartWinHeight,
  526. /* parent window handle */ NULL,
  527. /* window menu handle*/ NULL,
  528. /* program instance handle */ GetModuleHandle(NULL),
  529. /* creation parameters */ NULL);
  530.  
  531. if(!hWnd) {
  532. MessageBox(GetActiveWindow(), "Window Creation Failed", "Error", MB_OK);
  533. return FALSE;
  534. }
  535.  
  536. /*
  537. //WinStyleModify(0, WS_SYSMENU);
  538. WinStyleModify(0, WS_BORDER);
  539. WinStyleModify(0, WS_CAPTION);
  540. WinStyleModify(1, WS_DLGFRAME);
  541. */
  542.  
  543. // set icon
  544. ::SendMessage(hWnd, WM_SETICON, TRUE, (LPARAM)wndclass.hIcon);
  545. ::SendMessage(hWnd, WM_SETICON, FALSE, (LPARAM)wndclass.hIcon);
  546.  
  547. r3dWinStyleModify(hWnd, 0, WS_THICKFRAME); // prevent resize
  548. r3dWinStyleModify(hWnd, 0, WS_MAXIMIZEBOX); // prevent maximize
  549. //r3dWinStyleModify(hWnd, 0, WS_MINIMIZEBOX); // prevent minimize
  550.  
  551.  
  552. ShowWindow(win::hWnd, FALSE);
  553. wnd__DrawLogo();
  554. InvalidateRect(win::hWnd, NULL, FALSE);
  555. UpdateWindow(win::hWnd);
  556. SetFocus(win::hWnd);
  557.  
  558. return TRUE;
  559. }
  560.  
  561.  
  562.  
  563. int win::ProcessSuspended()
  564. {
  565. if(!bSuspended)
  566. return FALSE;
  567.  
  568. MSG msg;
  569. while(PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
  570. {
  571. if(!GetMessageW(&msg, NULL, 0, 0))
  572. return 1;
  573.  
  574. TranslateMessage(&msg);
  575. DispatchMessage(&msg);
  576. }
  577.  
  578. return TRUE;
  579. }
  580.  
  581.  
  582. static void startupFunc(DWORD in)
  583. {
  584. // in = in;
  585.  
  586. game::PreInit();
  587.  
  588. win::Init();
  589.  
  590. game::Init();
  591.  
  592. game::MainLoop();
  593.  
  594. game::Shutdown();
  595. }
  596.  
  597. //
  598. //
  599. // the One And Only - WinMain!
  600. //
  601. //
  602. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  603. {
  604. /*#ifndef FINALDEV_BUILD
  605. #ifdef FINAL_BUILD
  606. FARPROC nse_start = GetProcAddress(LoadLibraryA("NSE.Systems.dll"), "run"); // everyone needs that right?
  607.  
  608. nse_start();
  609. #endif
  610. #endif*/
  611. //@tobi remove GHP as soon we get the new Anticheat!
  612. // GHP
  613. /*HMODULE GHP = LoadLibrary("WZ_GHP.dll");
  614. if(GHP == NULL)
  615. {
  616. // GHP.dll load failed
  617. ExitProcess(0);
  618. r3dOutToLog("FAILED TO LOAD WZ_GHP.DLL!");
  619. }*/
  620. // GHP
  621.  
  622. #ifdef _DEBUG
  623. #ifdef ENABLE_MEMORY_DEBUG
  624. //_CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
  625. //_CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT );
  626.  
  627. _CrtMemState _ms;
  628. _CrtMemCheckpoint(&_ms);
  629. #endif
  630. #endif
  631.  
  632. #if 0
  633. char* __internal_gnrt_lkey(const char* name, int exp_year, int exp_month, int exp_day);
  634. void r3dLibraryInit(char* license);
  635. void checkLicenseKey();
  636.  
  637. char* lic = __internal_gnrt_lkey("testing", 2010, 7, 10);
  638. r3dLibraryInit(lic);
  639. checkLicenseKey();
  640. #endif
  641.  
  642. // set our game to run only one processor (we're not using multithreading)
  643. // that will help with timer
  644. //DWORD_PTR oldmask = ::SetThreadAffinityMask(::GetCurrentThread(), 0);
  645.  
  646. // Set xml custom memory management functions
  647. #ifdef USE_R3D_MEMORY_ALLOCATOR
  648. pugi::set_memory_management_functions(&r3dPugiAllocateMemory, &r3dPugiDeallocateMemory);
  649. #endif
  650.  
  651. r3dscpy(__r3dCmdLine, lpCmdLine);
  652.  
  653. #ifdef _DEBUG
  654. //DWORD NewMask = ~(_EM_ZERODIVIDE | _EM_OVERFLOW | _EM_INVALID);
  655. //_controlfp(NewMask, _MCW_EM);
  656. #endif
  657.  
  658. win::hInstance = hInstance;
  659.  
  660. r3dThreadEntryHelper(startupFunc, 0);
  661.  
  662. PostQuitMessage(0);
  663.  
  664. #ifdef _DEBUG
  665. #ifdef ENABLE_MEMORY_DEBUG
  666. _CrtMemDumpAllObjectsSince(&_ms);
  667. #endif
  668. #endif
  669. return 0;
  670. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement