npWebGamePlugin.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include <string>
  23. #include <vector>
  24. #include "npWebGamePlugin.h"
  25. #include "../../common/webCommon.h"
  26. NPWebGamePlugin* NPWebGamePlugin::sInstance = NULL;
  27. // we use a timer to update the Torque 3D game loop (tick) and handle rendering
  28. VOID CALLBACK MyTimerProc( HWND hwnd, // handle to window for timer messages
  29. UINT message, // WM_TIMER message
  30. UINT idTimer, // timer identifier
  31. DWORD dwTime) // current system time
  32. {
  33. static bool reentrant = false;
  34. if (!reentrant)
  35. {
  36. reentrant = true;
  37. torque_enginetick();
  38. reentrant = false;
  39. }
  40. }
  41. // custom window proc for our plugin's rendering window
  42. static LRESULT CALLBACK NPWebGamePluginWinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  43. {
  44. NPWebGamePlugin* plugin = (NPWebGamePlugin*)GetWindowLongPtr(hWnd, GWL_USERDATA);
  45. if (plugin)
  46. {
  47. switch (msg)
  48. {
  49. case WM_MOUSEACTIVATE:
  50. break;
  51. case WM_SIZE:
  52. // handle resize of browser (sub)window updating our Torque 3D child window accordingly
  53. int width = (int) LOWORD( lParam );
  54. int height = (int) HIWORD( lParam );
  55. torque_resizewindow(width,height);
  56. break;
  57. }
  58. return CallWindowProc((WNDPROC)plugin->mOriginalWinProc, hWnd, msg, wParam, lParam);
  59. }
  60. else
  61. {
  62. return DefWindowProc(hWnd, msg, wParam, lParam);
  63. }
  64. }
  65. // DLL Entry Point
  66. extern "C" BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
  67. {
  68. WebCommon::gPluginModule = (HMODULE) hInstance;
  69. return TRUE;
  70. }
  71. NPWebGamePlugin::NPWebGamePlugin(NPP aInstance)
  72. {
  73. mOpen = FALSE;
  74. mInstance = aInstance;
  75. sInstance = this;
  76. mOriginalWinProc = NULL;
  77. mHwnd = NULL;
  78. }
  79. NPWebGamePlugin::~NPWebGamePlugin()
  80. {
  81. Close();
  82. sInstance = NULL;
  83. }
  84. NPBool NPWebGamePlugin::Open(NPWindow* aWindow)
  85. {
  86. if (mOpen)
  87. {
  88. return TRUE; //firefox tries to open 2x
  89. }
  90. if (!aWindow)
  91. return FALSE;
  92. void* platformWindow = NULL;
  93. mHwnd = (HWND)aWindow->window;
  94. if (!mHwnd)
  95. return FALSE;
  96. platformWindow = mHwnd;
  97. // replace our plugin window proc with a custom one (for handling resizing,etc)
  98. mOriginalWinProc = SetWindowLongPtr(mHwnd, GWLP_WNDPROC, (LONG_PTR)NPWebGamePluginWinProc);
  99. LONG lStyle = GetWindowLong(mHwnd, GWL_STYLE);
  100. SetWindowLong(mHwnd, GWL_STYLE, lStyle | WS_CLIPCHILDREN);
  101. SetWindowLongPtr(mHwnd, GWL_USERDATA, (LONG_PTR)this);
  102. // load up the Torque 3D shared library and initialize it
  103. if (!WebCommon::InitTorque3D(platformWindow))
  104. return false;
  105. mOpen = true;
  106. // fire up our tick/update timer
  107. SetTimer( mHwnd, 1, // timer identifier
  108. 1, // 1 millisecond
  109. (TIMERPROC) MyTimerProc); // timer callback
  110. return mOpen;
  111. }
  112. void NPWebGamePlugin::Close()
  113. {
  114. if (!mOpen)
  115. return;
  116. if (mOriginalWinProc)
  117. {
  118. // restore original window proc
  119. SetWindowLongPtr(mHwnd, GWLP_WNDPROC, mOriginalWinProc);
  120. mOriginalWinProc = NULL;
  121. }
  122. if (mHwnd)
  123. {
  124. // no more ticks please
  125. KillTimer( mHwnd, 1);
  126. }
  127. mHwnd = NULL;
  128. // shutdown and unload the Torque 3D DLL
  129. WebCommon::ShutdownTorque3D();
  130. mOpen = false;
  131. }
  132. NPBool NPWebGamePlugin::IsOpen()
  133. {
  134. return mOpen;
  135. }