1 /*=========================================================================
2 
3 Program:   Visualization Toolkit
4 Module:    vtkSDL2OpenGL2RenderWindow.h
5 
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 /**
16  * @class   vtkSDL2OpenGL2RenderWindow
17  * @brief   OpenGL rendering window
18  *
19  * vtkSDL2OpenGL2RenderWindow is a concrete implementation of the abstract
20  * class vtkRenderWindow. vtkSDL2OpenGL2Renderer interfaces to the standard
21  * OpenGL graphics library using SDL2
22  */
23 
24 #ifndef vtkSDL2OpenGLRenderWindow_h
25 #define vtkSDL2OpenGLRenderWindow_h
26 
27 #include "vtkOpenGLRenderWindow.h"
28 #include "vtkRenderingOpenGL2Module.h" // For export macro
29 #include <SDL2/SDL.h>                  // for ivars
30 #include <stack>                       // for ivar
31 
32 class VTKRENDERINGOPENGL2_EXPORT vtkSDL2OpenGLRenderWindow : public vtkOpenGLRenderWindow
33 {
34 public:
35   static vtkSDL2OpenGLRenderWindow* New();
36   vtkTypeMacro(vtkSDL2OpenGLRenderWindow, vtkOpenGLRenderWindow);
37   void PrintSelf(ostream& os, vtkIndent indent) override;
38 
39   /**
40    * Initialize the rendering window.  This will setup all system-specific
41    * resources.  This method and Finalize() must be symmetric and it
42    * should be possible to call them multiple times, even changing WindowId
43    * in-between.  This is what WindowRemap does.
44    */
45   void Initialize(void) override;
46 
47   /**
48    * Finalize the rendering window.  This will shutdown all system-specific
49    * resources.  After having called this, it should be possible to destroy
50    * a window that was used for a SetWindowId() call without any ill effects.
51    */
52   void Finalize(void) override;
53 
54   /**
55    * Change the window to fill the entire screen.
56    */
57   void SetFullScreen(vtkTypeBool) override;
58 
59   /**
60    * Show or not Show the window
61    */
62   void SetShowWindow(bool val) override;
63 
64   ///@{
65   /**
66    * Set the size of the window in pixels.
67    */
68   void SetSize(int, int) override;
SetSize(int a[2])69   void SetSize(int a[2]) override { vtkOpenGLRenderWindow::SetSize(a); }
70   ///@}
71 
72   /**
73    * Get the current size of the window in pixels.
74    */
75   int* GetSize() VTK_SIZEHINT(2) override;
76 
77   ///@{
78   /**
79    * Set the position of the window.
80    */
81   void SetPosition(int, int) override;
SetPosition(int a[2])82   void SetPosition(int a[2]) override { vtkOpenGLRenderWindow::SetPosition(a); }
83   ///@}
84 
85   /**
86    * Get the current size of the screen in pixels.
87    */
88   int* GetScreenSize() VTK_SIZEHINT(2) override;
89 
90   /**
91    * Get the position in screen coordinates of the window.
92    */
93   int* GetPosition() VTK_SIZEHINT(2) override;
94 
95   /**
96    * Set the name of the window. This appears at the top of the window
97    * normally.
98    */
99   void SetWindowName(const char*) override;
100 
GetGenericDisplayId()101   void* GetGenericDisplayId() override { return (void*)this->ContextId; }
GetGenericWindowId()102   void* GetGenericWindowId() override { return (void*)this->WindowId; }
GetGenericDrawable()103   void* GetGenericDrawable() override { return (void*)this->WindowId; }
104 
105   /**
106    * Make this windows OpenGL context the current context.
107    */
108   void MakeCurrent() override;
109 
110   /**
111    * Release the current context.
112    */
113   void ReleaseCurrent() override;
114 
115   /**
116    * Tells if this window is the current OpenGL context for the calling thread.
117    */
118   bool IsCurrent() override;
119 
120   /**
121    * Clean up device contexts, rendering contexts, etc.
122    */
123   void Clean();
124 
125   /**
126    * A termination method performed at the end of the rendering process
127    * to do things like swapping buffers (if necessary) or similar actions.
128    */
129   void Frame() override;
130 
131   ///@{
132   /**
133    * Ability to push and pop this window's context
134    * as the current context. The idea being to
135    * if needed make this window's context current
136    * and when done releasing resources restore
137    * the prior context
138    */
139   void PushContext() override;
140   void PopContext() override;
141   ///@}
142 
143   /**
144    * Set the number of vertical syncs required between frames.
145    * A value of 0 means swap buffers as quickly as possible
146    * regardless of the vertical refresh. A value of 1 means swap
147    * buffers in sync with the vertical refresh to eliminate tearing.
148    * A value of -1 means use a value of 1 unless we missed a frame
149    * in which case swap immediately. Returns true if the call
150    * succeeded.
151    */
152   bool SetSwapControl(int i) override;
153 
154   /**
155    * Get the size of the color buffer.
156    * Returns 0 if not able to determine otherwise sets R G B and A into buffer.
157    */
158   int GetColorBufferSizes(int* rgba) override;
159 
160   ///@{
161   /**
162    * Hide or Show the mouse cursor, it is nice to be able to hide the
163    * default cursor if you want VTK to display a 3D cursor instead.
164    */
165   void HideCursor() override;
166   void ShowCursor() override;
167   ///@}
168 
169 protected:
170   vtkSDL2OpenGLRenderWindow();
171   ~vtkSDL2OpenGLRenderWindow() override;
172 
173   SDL_Window* WindowId;
174   SDL_GLContext ContextId;
175   std::stack<SDL_GLContext> ContextStack;
176   std::stack<SDL_Window*> WindowStack;
177   int ScreenSize[2];
178   static const std::string DEFAULT_BASE_WINDOW_NAME;
179 
180   void CleanUpRenderers();
181   void CreateAWindow() override;
182   void DestroyWindow() override;
183 
184 private:
185   vtkSDL2OpenGLRenderWindow(const vtkSDL2OpenGLRenderWindow&) = delete;
186   void operator=(const vtkSDL2OpenGLRenderWindow&) = delete;
187 };
188 
189 #endif
190