Back to index

nux  3.0.0
GraphicsDisplayWin.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #ifndef GRAPHICSDISPLAYWIN_H
00024 #define GRAPHICSDISPLAYWIN_H
00025 
00026 #include "Gfx_Interface.h"
00027 #include "GLTimer.h"
00028 #include "GLDeviceObjects.h"
00029 #include "GLRenderStates.h"
00030 
00031 #include <d2d1.h>
00032 #include <d2d1helper.h>
00033 #include <dwrite.h>
00034 #include "Wincodec.h"
00035 
00036 
00037 namespace nux
00038 {
00039 
00040   class Event;
00041   class MainFBO;
00042   class GpuDevice;
00043   class GraphicsEngine;
00044   class IOpenGLFrameBufferObject;
00045 
00046   enum WindowStyle
00047   {
00048     WINDOWSTYLE_NORMAL,
00049     WINDOWSTYLE_PANEL,
00050     WINDOWSTYLE_DIALOG,
00051     WINDOWSTYLE_TOOL,
00052     WINDOWSTYLE_NOBORDER,
00053   };
00054 
00055 #define NUX_THREADMSG                           (WM_APP+0)
00056 #define NUX_THREADMSG_START_RENDERING           (WM_APP+1)  // Connection established // start at WM_APP
00057 #define NUX_THREADMSG_CHILD_WINDOW_TERMINATED   (WM_APP+2)  // General failure - Wait Connection failed
00058 #define NUX_THREADMSG_THREAD_TERMINATED         (WM_APP+3)  // Set wParam = Thread ID, lParam = 0
00059 
00060 // This will become GLWindow
00061   class GraphicsDisplay : public GraphicSystem
00062   {
00063     friend class GraphicsEngine;
00064 
00065   private:
00066 
00067     // WIN32 system variables
00068     HGLRC       opengl_rendering_context_;  
00069     HDC         device_context_;            
00070     HWND        wnd_handle_;                     
00071     HWND        m_ParentWindow;
00072 
00073     char m_WindowClassName[256];
00074     GLuint      m_PixelFormat;      // Holds The Results After Searching For A Match
00075     DWORD       m_dwExStyle;        // Window Extended Style
00076     DWORD       m_dwStyle;          // Window Style
00077     NString     m_WindowTitle;
00078 
00079     static HGLRC sMainGLRC;         // Used as the first argument of wglShareLists to make all successive OpenGL  context share their objects
00080     static HDC   sMainDC;           // Used as the first argument of wglShareLists to make all successive OpenGL  context share their objects
00081 
00082     ID2D1Factory    *d2d_factory_;
00083     IDWriteFactory  *dw_factory_;
00084     IWICImagingFactory *wic_factory_;
00085 
00086     // size, position
00087     Size  m_ViewportSize;
00088     Size m_WindowSize;
00089 
00090     // surface attibute;
00091     bool m_fullscreen;
00092     unsigned int m_ScreenBitDepth;
00093 
00094     // verify that the interface is properly created
00095     bool m_GfxInterfaceCreated;
00096 
00097     // Device information
00098     void GetDisplayInfo();
00099     int m_index_of_current_mode;
00100 
00101     bool m_is_window_minimized;
00102 
00103     HCURSOR cursor_;
00104 
00105     static int Win32KeySymToINL(int Keysym);
00106     static int Win32VKToNuxKey(int vk);
00107   public:
00108 
00109     // Device
00110     int m_num_device_modes;
00111 
00112     // Event object
00113     Event *event_;
00114 
00115     // Creation
00116     bool IsGfxInterfaceCreated();
00117 
00119 
00127     bool CreateOpenGLWindow(
00128       const char *WindowTitle,
00129       unsigned int WindowWidth,
00130       unsigned int WindowHeight,
00131       WindowStyle Style,
00132       const GraphicsDisplay *Parent,
00133       bool FullscreenFlag = false,
00134       bool create_rendering_data = true);
00135 
00137 
00142     bool CreateFromOpenGLWindow(HWND WindowHandle, HDC WindowDCHandle, HGLRC OpenGLRenderingContext);
00143 
00144     void DestroyOpenGLWindow();
00145 
00146     void SetWindowTitle(const char *Title);
00147     void SetWindowSize(int width, int height);
00148     void SetViewPort(int x, int y, int width, int height);
00149     Point GetMouseScreenCoord();
00150     Point GetMouseWindowCoord();
00151     Point GetWindowCoord();
00152     Rect GetWindowGeometry();
00153     Rect GetNCWindowGeometry();
00154     void MakeGLContextCurrent(bool b = true);
00155     void SwapBuffer(bool glswap = true);
00156 
00157     // Event methods
00158     void GetSystemEvent(Event *evt);
00159     Event &GetCurrentEvent();
00160 
00161     bool isWindowMinimized() const
00162     {
00163       return m_is_window_minimized;
00164     }
00165 
00166     void ShowWindow();
00167     void HideWindow();
00168     bool IsWindowVisible();
00169 
00170     void EnterMaximizeWindow();
00171     void ExitMaximizeWindow();
00172 
00173     HWND GetWindowHandle() const
00174     {
00175       return wnd_handle_;
00176     }
00177     HWND GetParentWindowHandle() const
00178     {
00179       return m_ParentWindow;
00180     }
00181     HDC GetWindowHDC() const
00182     {
00183       return device_context_;
00184     }
00185     bool IsChildWindow() const
00186     {
00187       return m_ParentWindow != 0;
00188     }
00189 
00190     ID2D1Factory* GetDirect2DFactory();
00191     IDWriteFactory* GetDirectWriteFactory();
00192     IWICImagingFactory* GetWICFactory();
00193 
00194     // Return true if VSync swap control is available
00195     bool HasVSyncSwapControl() const;
00196     void EnableVSyncSwapControl();
00197     void DisableVSyncSwapControl();
00198 
00199     // m_FrameRate
00200     float GetFrameTime() const;
00201     void ResetFrameTime();
00202 
00203     GraphicsEngine *GetGraphicsEngine() const;
00204     
00205     GpuDevice *GetGpuDevice() const;
00206 
00207     // Dialog
00208     bool StartOpenFileDialog(FileDialogOption &fdo);
00209     bool StartSaveFileDialog(FileDialogOption &fdo);
00210     bool StartColorDialog(ColorDialogOption &cdo);
00211 
00212 
00213     void GetWindowSize(int &w, int &h);
00214     int GetWindowWidth();
00215     int GetWindowHeight();
00216 
00218 
00222     void ResetWindowSize();
00223 
00224     bool HasFrameBufferSupport();
00225     void SetWindowCursor(HCURSOR cursor);
00226     HCURSOR GetWindowCursor() const;
00227 
00228     void ProcessForeignWin32Event(HWND hWnd, MSG msg, WPARAM wParam, LPARAM lParam, Event *event);
00229     LRESULT ProcessWin32Event(HWND hWnd, unsigned int uMsg, WPARAM wParam, LPARAM lParam);
00230 
00232 
00241     void PauseThreadGraphicsRendering();
00242     bool IsPauseThreadGraphicsRendering() const;
00243 
00244     // Pointer and keyboard grab API
00245     typedef void(*GrabReleaseCallback) (bool replaced, void *user_data);
00246 
00247     bool GrabPointer   (GrabReleaseCallback callback, void *data, bool replace_existing);
00248     bool UngrabPointer(void *data);
00249     bool PointerIsGrabbed();
00250 
00251     bool GrabKeyboard   (GrabReleaseCallback callback, void *data, bool replace_existing);
00252     bool UngrabKeyboard(void *data);
00253     bool KeyboardIsGrabbed();
00254 
00255     void * KeyboardGrabData() { return _global_keyboard_grab_data; }
00256     void * PointerGrabData() { return _global_pointer_grab_data; }
00257 
00258   private:
00259     void InitGlobalGrabWindow();
00260 
00261     bool m_PauseGraphicsRendering;
00262     GLTimer m_Timer;
00263     float m_FrameTime;
00264     GpuDevice *m_DeviceFactory;
00265     GraphicsEngine *m_GraphicsContext;
00266     WindowStyle m_Style;
00267 
00268     HWND                _global_grab_window;
00269 
00270     void               *_global_pointer_grab_data;
00271     bool                _global_pointer_grab_active;
00272     GrabReleaseCallback _global_pointer_grab_callback;
00273 
00274     void               *_global_keyboard_grab_data;
00275     bool                _global_keyboard_grab_active;
00276     GrabReleaseCallback _global_keyboard_grab_callback;
00277 
00278 
00279   public:
00280     ~GraphicsDisplay();
00281     GLEWContext *GetGLEWContext()
00282     {
00283       return &m_GLEWContext;
00284     }
00285     WGLEWContext *GetWGLEWContext()
00286     {
00287       return &m_WGLEWContext;
00288     }
00289 
00290   private:
00291     GraphicsDisplay();
00292     GraphicsDisplay(const GraphicsDisplay &);
00293     // Does not make sense for a singleton. This is a self assignment.
00294     GraphicsDisplay &operator= (const GraphicsDisplay &);
00295 
00296 
00297     GLEWContext m_GLEWContext;
00298     WGLEWContext m_WGLEWContext;
00299     friend class DisplayAccessController;
00300   };
00301 
00302   LRESULT CALLBACK WndProcManager(HWND    hWnd,          // Handle For This Window
00303                                    unsigned int   uMsg,           // Message For This Window
00304                                    WPARAM  wParam,         // Additional Message Information
00305                                    LPARAM  lParam);        // Additional Message Information
00306 
00307 }
00308 
00309 #endif //GRAPHICSDISPLAYWIN_H
00310