Advertisement
Guest User

firebreath openGL Test Plugin Example

a guest
Jul 10th, 2011
2,509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.33 KB | None | 0 0
  1. /**********************************************************\
  2.  
  3.   Auto-generated threadedOpenGLTestPlugin.cpp
  4.  
  5.   This file contains the auto-generated main plugin object
  6.   implementation for the threadedOpenGLTestPlugin project
  7.  
  8.   Example from Kai Rathmann and Eamon Woortman
  9.  
  10. \**********************************************************/
  11.  
  12. #include <sstream>
  13.  
  14. #include <stdio.h>
  15.  
  16. #include "threadedOpenGLTestPluginAPI.h"
  17.  
  18. #include "threadedOpenGLTestPlugin.h"
  19.  
  20. #include "DOM/Window.h"
  21. #include "URI.h"
  22.  
  23. #include "PluginEvents/KeyCodes.h"
  24.  
  25. #ifdef FB_WIN
  26. #include "PluginWindowWin.h"
  27. #include "PluginWindowlessWin.h"
  28. #ifdef HAS_LEAKFINDER
  29. #define XML_LEAK_FINDER
  30. #include "LeakFinder/LeakFinder.h"
  31. #endif
  32. #endif
  33.  
  34. #ifdef HAS_LEAKFINDER
  35. boost::scoped_ptr<LeakFinderXmlOutput> FBTestPlugin::pOut;
  36. #endif
  37.  
  38. //openGL stuff
  39. //opengl thread stuff
  40. #include <windows.h>
  41. #include <strsafe.h>
  42. #include <stdio.h>
  43.  
  44. HANDLE openGLThreadHandle;
  45. DWORD dwThreadID;
  46.  
  47. float theta = 0.0f;
  48. #include "win_common.h"
  49. #include "logging.h"
  50. #include <gl\gl.h>
  51. HWND hWnd;
  52. HDC hDC;
  53. HGLRC hRC;
  54. GLfloat     rtri = 0.1f;   
  55. bool run = true;
  56.  
  57. FB::PluginWindowWin* pluginWindowWin;
  58. FB::PluginWindow* pluginWindow;
  59.  
  60. #include <tchar.h>
  61. #include <stdio.h>
  62. //long timeSinceLastFrame;
  63.  
  64. #ifndef __PLUGIN_KEYS__
  65. #define __PLUGIN_KEYS__
  66.     bool    pluginKeys[256];
  67. #endif
  68.  
  69. LPTHREAD_START_ROUTINE threadedOpenGLTestPlugin::drawThreaded( LPVOID lpParam )
  70. {
  71.     EnableOpenGL( pluginWindowWin->getHWND(), &hDC, &hRC );
  72.     SetFocus(pluginWindowWin->getHWND());  
  73.     //FB::
  74.     FBLOG_INFO("BrowserHost", "Logging to HTML: " << "Bla!");
  75.     static int fps = 1;
  76.     static double start = 0, diff, wait;
  77.     wait = 1 / fps;
  78.  
  79.    
  80.  
  81.     //return 0;
  82.     while(run)
  83.     {
  84.        
  85.         //lets check for keyboard input
  86.         if(pluginKeys[FB::FBKeyCode::FBKEY_F1]==true)
  87.         {
  88.             pluginKeys[FB::FBKeyCode::FBKEY_F1]=false;
  89.             rtri = 0.1f;
  90.             //run = false; //stop refreshing
  91.         }
  92.         if(pluginKeys[FB::FBKeyCode::FBKEY_F2]==true)
  93.         {
  94.             pluginKeys[FB::FBKeyCode::FBKEY_F2]=false;
  95.             rtri -= 0.1f;
  96.         }
  97.         if(pluginKeys[FB::FBKeyCode::FBKEY_F3]==true)
  98.         {
  99.             pluginKeys[FB::FBKeyCode::FBKEY_F3]=false;
  100.             rtri += 0.1f;
  101.         }
  102.         if(pluginKeys[FB::FBKeyCode::FBKEY_F4]==true)
  103.         {
  104.             pluginKeys[FB::FBKeyCode::FBKEY_F4]=false;
  105.             rtri = 0.0f;
  106.         }
  107.         try
  108.         {
  109.         FB::Rect pos = pluginWindow->getWindowPosition();
  110.         #if FB_WIN
  111.             //HDC hDC;
  112.             //FB::PluginWindowlessWin *wndLess = dynamic_cast<FB::PluginWindowlessWin*>(pluginWindow);
  113.             //FB::PluginWindowWin *wnd = dynamic_cast<FB::PluginWindowWin*>(pluginWindow);
  114.             PAINTSTRUCT ps;
  115.             /*if (wndLess) {
  116.                 hDC = wndLess->getHDC();
  117.             } else if (wnd) {*/
  118.             if(pluginWindowWin){
  119.                
  120.                 //if(!hDC)
  121.                     hDC = BeginPaint(pluginWindowWin->getHWND(), &ps);
  122.  
  123.                 pos.right -= pos.left;
  124.                 pos.left = 0;
  125.                 pos.bottom -= pos.top;
  126.                 pos.top = 0;
  127.  
  128.                
  129.                 glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
  130.                     glClear( GL_COLOR_BUFFER_BIT );
  131.  
  132.                     glPushMatrix();
  133.                     glRotatef(rtri,0.0f,1.0f,0.0f);
  134.                     glBegin( GL_TRIANGLES );
  135.            
  136.                     glColor3f( 1.0f, 0.0f, 0.0f ); glVertex2f( 0.0f, 1.0f );
  137.                     glColor3f( 0.0f, 1.0f, 0.0f ); glVertex2f( 0.87f, -0.5f );
  138.                     glColor3f( 0.0f, 0.0f, 1.0f ); glVertex2f( -0.87f, -0.5f );
  139.                     glEnd();
  140.            
  141.                     glBegin(GL_QUADS);                      // Draw A Quad
  142.                         glColor3f( 1.0f, 0.0f, 0.0f ); glVertex3f(-0.5f, 0.5f, 0.0f);               // Top Left
  143.                         glColor3f( 0.0f, 1.0f, 0.0f );glVertex3f( 0.5f, 0.5f, 0.0f);                // Top Right
  144.                         glColor3f( 0.0f, 0.0f, 1.0f ); glVertex3f( 0.5f,-0.5f, 0.0f);               // Bottom Right
  145.                         glColor3f( 0.0f, 0.0f, 0.0f ); glVertex3f(-0.5f,-0.5f, 0.0f);               // Bottom Left
  146.                     glEnd();                            // Done Drawing The Quad
  147.                     glPopMatrix();
  148.                     glRotatef(rtri,0.0f,1.0f,0.0f);
  149.                    
  150.                    
  151.                     SwapBuffers( hDC );
  152.    
  153.  
  154.                 //me end
  155.                
  156.  
  157.             }
  158.             //rtri+=0.1f;
  159.             ::SetTextAlign(hDC, TA_CENTER|TA_BASELINE);
  160.             LPCTSTR pszText = _T("FireBreath Plugin!\n:-)");
  161.  
  162.             ::TextOut(hDC, pos.left + (pos.right - pos.left) / 2, pos.top + (pos.bottom - pos.top) / 2, pszText, lstrlen(pszText));
  163.            
  164.             if (pluginWindowWin) {
  165.                 // Release the device context
  166.                 EndPaint(pluginWindowWin->getHWND(), &ps);
  167.             }
  168.         #endif
  169.         //return true;
  170.         }catch(...)
  171.         {
  172.             return 0;
  173.         }
  174.            
  175.        
  176.         //doesnt work
  177.         /*
  178.             diff = GetTickCount() - start;
  179.             if (diff < wait) {
  180.                 Sleep(wait - diff);
  181.             }
  182.             start = GetTickCount();*/
  183.         Sleep(10);
  184.  
  185.        
  186.     }//end of while run
  187.     return 0;
  188. }
  189.  
  190. void threadedOpenGLTestPlugin::EnableOpenGL(HWND handleWnd, HDC * hdc, HGLRC * hRC)
  191. {
  192.    
  193.     PIXELFORMATDESCRIPTOR pfd;
  194.     int format;
  195.  
  196.     // get the device context (DC)
  197.     *hdc = GetDC( handleWnd );
  198.  
  199.     // set the pixel format for the DC
  200.     ZeroMemory( &pfd, sizeof( pfd ) );
  201.     pfd.nSize = sizeof( pfd );
  202.     pfd.nVersion = 1;
  203.     pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  204.     pfd.iPixelType = PFD_TYPE_RGBA;
  205.     pfd.cColorBits = 24;
  206.     pfd.cDepthBits = 16;
  207.     pfd.iLayerType = PFD_MAIN_PLANE;
  208.     format = ChoosePixelFormat( *hdc, &pfd );
  209.     SetPixelFormat( *hdc, format, &pfd );
  210.  
  211.     // create and enable the render context (RC)
  212.     *hRC = wglCreateContext( *hdc );
  213.     wglMakeCurrent( *hdc, *hRC );
  214.  
  215.     hDC = *hdc;
  216. }
  217. // Disable OpenGL
  218.  
  219. void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC)
  220. {
  221.     wglMakeCurrent( NULL, NULL );
  222.     wglDeleteContext( hRC );
  223.     ReleaseDC( hWnd, hDC );
  224. }
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232. ///////////////////////////////////////////////////////////////////////////////
  233. /// @fn threadedOpenGLTestPlugin::StaticInitialize()
  234. ///
  235. /// @brief  Called from PluginFactory::globalPluginInitialize()
  236. ///
  237. /// @see FB::FactoryBase::globalPluginInitialize
  238. ///////////////////////////////////////////////////////////////////////////////
  239. void threadedOpenGLTestPlugin::StaticInitialize()
  240. {
  241.     // Place one-time initialization stuff here; As of FireBreath 1.4 this should only
  242.     // be called once per process
  243. }
  244.  
  245. ///////////////////////////////////////////////////////////////////////////////
  246. /// @fn threadedOpenGLTestPlugin::StaticInitialize()
  247. ///
  248. /// @brief  Called from PluginFactory::globalPluginDeinitialize()
  249. ///
  250. /// @see FB::FactoryBase::globalPluginDeinitialize
  251. ///////////////////////////////////////////////////////////////////////////////
  252. void threadedOpenGLTestPlugin::StaticDeinitialize()
  253. {
  254.     // Place one-time deinitialization stuff here. As of FireBreath 1.4 this should
  255.     // always be called just before the plugin library is unloaded
  256. }
  257.  
  258. ///////////////////////////////////////////////////////////////////////////////
  259. /// @brief  threadedOpenGLTestPlugin constructor.  Note that your API is not available
  260. ///         at this point, nor the window.  For best results wait to use
  261. ///         the JSAPI object until the onPluginReady method is called
  262. ///////////////////////////////////////////////////////////////////////////////
  263. threadedOpenGLTestPlugin::threadedOpenGLTestPlugin()
  264. {
  265. }
  266.  
  267. ///////////////////////////////////////////////////////////////////////////////
  268. /// @brief  threadedOpenGLTestPlugin destructor.
  269. ///////////////////////////////////////////////////////////////////////////////
  270. threadedOpenGLTestPlugin::~threadedOpenGLTestPlugin()
  271. {
  272.     // This is optional, but if you reset m_api (the shared_ptr to your JSAPI
  273.     // root object) and tell the host to free the retained JSAPI objects then
  274.     // unless you are holding another shared_ptr reference to your JSAPI object
  275.     // they will be released here.
  276.     releaseRootJSAPI();
  277.     m_host->freeRetainedObjects();
  278. }
  279.  
  280. void threadedOpenGLTestPlugin::onPluginReady()
  281. {
  282.     // When this is called, the BrowserHost is attached, the JSAPI object is
  283.     // created, and we are ready to interact with the page and such.  The
  284.     // PluginWindow may or may not have already fire the AttachedEvent at
  285.     // this point.
  286. }
  287.  
  288. void threadedOpenGLTestPlugin::shutdown()
  289. {
  290.     // This will be called when it is time for the plugin to shut down;
  291.     // any threads or anything else that may hold a shared_ptr to this
  292.     // object should be released here so that this object can be safely
  293.     // destroyed. This is the last point that shared_from_this and weak_ptr
  294.     // references to this object will be valid
  295. }
  296.  
  297. ///////////////////////////////////////////////////////////////////////////////
  298. /// @brief  Creates an instance of the JSAPI object that provides your main
  299. ///         Javascript interface.
  300. ///
  301. /// Note that m_host is your BrowserHost and shared_ptr returns a
  302. /// FB::PluginCorePtr, which can be used to provide a
  303. /// boost::weak_ptr<threadedOpenGLTestPlugin> for your JSAPI class.
  304. ///
  305. /// Be very careful where you hold a shared_ptr to your plugin class from,
  306. /// as it could prevent your plugin class from getting destroyed properly.
  307. ///////////////////////////////////////////////////////////////////////////////
  308. FB::JSAPIPtr threadedOpenGLTestPlugin::createJSAPI()
  309. {
  310.     // m_host is the BrowserHost
  311.     return boost::make_shared<threadedOpenGLTestPluginAPI>(FB::ptr_cast<threadedOpenGLTestPlugin>(shared_from_this()), m_host);
  312. }
  313.  
  314. bool threadedOpenGLTestPlugin::onMouseDown(FB::MouseDownEvent *evt, FB::PluginWindow *)
  315. {
  316.     //printf("Mouse down at: %d, %d\n", evt->m_x, evt->m_y);
  317.     return false;
  318. }
  319.  
  320. bool threadedOpenGLTestPlugin::onMouseUp(FB::MouseUpEvent *evt, FB::PluginWindow *)
  321. {
  322.     //printf("Mouse up at: %d, %d\n", evt->m_x, evt->m_y);
  323.     return false;
  324. }
  325. bool threadedOpenGLTestPlugin::onKeyDown(FB::KeyDownEvent* evt, FB::PluginWindow* window)
  326. {
  327.     //set the key to true
  328.     pluginKeys[evt->m_key_code] = true;
  329.     return true;
  330. }
  331. bool threadedOpenGLTestPlugin::onMouseMove(FB::MouseMoveEvent *evt, FB::PluginWindow *)
  332. {
  333.     //printf("Mouse move at: %d, %d\n", evt->m_x, evt->m_y);
  334.     return false;
  335. }
  336.  
  337. bool threadedOpenGLTestPlugin::onWindowAttached(FB::AttachedEvent *evt, FB::PluginWindow* window)
  338. {
  339.     pluginWindow = window;
  340.     FB::PluginWindowlessWin *wndLess = dynamic_cast<FB::PluginWindowlessWin*>(window);
  341.     pluginWindowWin = dynamic_cast<FB::PluginWindowWin*>(window);
  342.  
  343.     boost::thread t(boost::bind(&threadedOpenGLTestPlugin::drawThreaded,  this));
  344.     return false;
  345. }
  346.  
  347. bool threadedOpenGLTestPlugin::onWindowDetached(FB::DetachedEvent *evt, FB::PluginWindow *)
  348. {
  349.     // The window is about to be detached; act appropriately
  350.  
  351.     return false;
  352. }
  353.  
  354. bool threadedOpenGLTestPlugin::draw( FB::RefreshEvent *evt, FB::PluginWindow* win )
  355. {
  356.     //Refresh Events... nothing todo since the opengl is running in it's own thread
  357.     return true;
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement