npWebGamePlugin.mm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // Timer which ticks/renders Torque3D
  26. static CFRunLoopTimerRef gTimer = NULL;
  27. static bool gHidden = false;
  28. static void timer_callback(CFRunLoopRef timer, void *info)
  29. {
  30. if (gHidden)
  31. return;
  32. if (!torque_enginetick())
  33. {
  34. //TODO: undefined when get quit from Torque 3D under Safari
  35. }
  36. }
  37. NPWebGamePlugin* NPWebGamePlugin::sInstance = NULL;
  38. NPWebGamePlugin::NPWebGamePlugin(NPP aInstance)
  39. {
  40. mOpen = FALSE;
  41. mInstance = aInstance;
  42. sInstance = this;
  43. }
  44. NPWebGamePlugin::~NPWebGamePlugin()
  45. {
  46. Close();
  47. sInstance = NULL;
  48. }
  49. NPBool NPWebGamePlugin::Open(NPWindow* aWindow)
  50. {
  51. gHidden = false;
  52. WebCommon::ChangeToGameDirectory();
  53. if (mOpen || WebCommon::gTorque3DModule)
  54. {
  55. NP_CGContext *npcontext = reinterpret_cast<NP_CGContext *> (aWindow->window);
  56. NSWindow* browserWindow = [[NSWindow alloc] initWithWindowRef:npcontext->window];
  57. torque_setsafariwindow( browserWindow, aWindow->clipRect.left, aWindow->clipRect.top, aWindow->clipRect.right - aWindow->clipRect.left, aWindow->clipRect.bottom - aWindow->clipRect.top );
  58. [browserWindow release];
  59. mOpen = TRUE;
  60. return TRUE;
  61. }
  62. if (!aWindow)
  63. return FALSE;
  64. mOpen = true;
  65. NP_CGContext *npcontext = reinterpret_cast<NP_CGContext *> (aWindow->window);
  66. // You may want to resize the browser window or set minimum sizes here for your web content
  67. NSWindow* browserWindow = [[NSWindow alloc] initWithWindowRef:npcontext->window];
  68. if (!browserWindow)
  69. {
  70. WebCommon::MessageBox( 0, "Web plugin unable to get browser window", "Error");
  71. return false;
  72. }
  73. if (!WebCommon::InitTorque3D(browserWindow, aWindow->clipRect.left, aWindow->clipRect.top, aWindow->clipRect.right - aWindow->clipRect.left, aWindow->clipRect.bottom - aWindow->clipRect.top))
  74. return false;
  75. //setup high speed timer to tick Torque 3D
  76. CFAllocatorRef allocator = kCFAllocatorDefault;
  77. CFAbsoluteTime fireDate = CFAbsoluteTimeGetCurrent() + .001;
  78. CFTimeInterval interval = .001;
  79. CFOptionFlags flags = 0;
  80. CFIndex order = 0;
  81. CFRunLoopTimerCallBack callback = (CFRunLoopTimerCallBack)timer_callback;
  82. CFRunLoopTimerContext context = { 0, NULL, NULL, NULL, NULL };
  83. gTimer = CFRunLoopTimerCreate(allocator, fireDate, interval, flags, order, callback, &context);
  84. CFRunLoopAddTimer(CFRunLoopGetCurrent(), gTimer, kCFRunLoopDefaultMode);
  85. [browserWindow release];
  86. return mOpen;
  87. }
  88. void NPWebGamePlugin::Close()
  89. {
  90. if (!mOpen)
  91. return;
  92. if (WebCommon::gTorque3DModule)
  93. {
  94. gHidden = true;
  95. torque_setsafariwindow(NULL, 0, 0, 0, 0);
  96. torque_reset();
  97. }
  98. //WebCommon::ShutdownTorque3D();
  99. mOpen = false;
  100. }
  101. NPBool NPWebGamePlugin::IsOpen()
  102. {
  103. return mOpen;
  104. }