OpenGLFramebuffer_Multi.h
1 /*
2 
3 Copyright (c) 2018, Johnathan Corkery. (jcorkery@umich.edu)
4 All rights reserved.
5 
6 This file is part of the Dynacoe project (https://github.com/jcorks/Dynacoe)
7 Dynacoe was released under the MIT License, as detailed below.
8 
9 
10 
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is furnished
16 to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall
19 be included in all copies or substantial portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 
29 
30 */
31 
32 #if(defined DC_BACKENDS_OPENGLFRAMEBUFFER_X11 || defined DC_BACKENDS_OPENGLFRAMEBUFFER_WIN32)
33 #ifndef H_DC_DISPLAY_GLFRAME
34 #define H_DC_DISPLAY_GLFRAME
35 
36 /*
37  Display_GL3
38 
39  A display signified by a Window with an OpenGL context
40 
41  */
42 
43 
44 #ifdef DC_SUBSYSTEM_WIN32
45  #ifndef GLEW_STATIC
46  #define GLEW_STATIC
47  #endif
48  #ifdef _MBCS
49  #include <GL/glew.h>
50  #else
51  #include <glew.h>
52  #endif
53 
54  #include <windows.h>
55  #include <gl/gl.h>
56 
57 #endif
58 #ifdef DC_SUBSYSTEM_X11
59  #include <X11/X.h>
60  #include <X11/Xlib.h>
61  #ifndef GLEW_STATIC
62  #define GLEW_STATIC
63  #endif
64  #include <GL/glew.h>
65  #include <GL/glx.h>
66  typedef Display X11Display;
67 #endif
68 
69 #include <vector>
70 #include <stack>
71 #include <Dynacoe/Backends/Display/Display.h>
72 
73 
74 namespace Dynacoe {
75 
76 
77 class OpenGLFBDisplay : public Dynacoe::Display {
78  public:
79  OpenGLFBDisplay();
80  ~OpenGLFBDisplay();
81 
82 
83  std::string Name();
84  std::string Version();
85  bool Valid();
86 
87 
88  void Resize(int, int);
89  void SetPosition(int, int);
90  void Fullscreen(bool);
91  void Hide(bool);
92  bool HasInputFocus();
93  void LockClientResize(bool);
94  void LockClientPosition(bool);
95  void SetViewPolicy(ViewPolicy);
96 
97  int Width();
98  int Height();
99  int X();
100  int Y();
101 
102  void SetName(const std::string &);
103  void AddResizeCallback(ResizeCallback *);
104  void RemoveResizeCallback(ResizeCallback *);
105  void AddCloseCallback(CloseCallback *);
106  void RemoveCloseCallback(CloseCallback *);
107 
108  bool IsCapable(Dynacoe::Display::Capability);
109  void Update();
110  void AttachSource(Dynacoe::Framebuffer *);
111  std::vector<Dynacoe::Framebuffer::Type> SupportedFramebuffers();
112  Dynacoe::Framebuffer * GetSource();
113 
114 
115  void * GetSystemHandle();
116  DisplayHandleType GetSystemHandleType();
117  void * GetLastSystemEvent();
118  DisplayEventType GetSystemEventType();
119 
120  bool Dump(const std::string &);
121  void QueueDump(const std::string &, int delay);
122 
123  private:
124 
125 
126  std::vector<Display::ResizeCallback *> resizeCBs;
127  std::vector<Display::CloseCallback *> closeCBs;
128 
129  bool valid;
130  int screenTex; // openGL texture to be handled
131  uint32_t flags;
132  int viewX;
133  int viewY;
134  int viewW;
135  int viewH;
136 
137  char * framebufferPixels;
138  GLuint framebufferImage;
139  Dynacoe::Framebuffer * framebuffer;
140  GLint framebufferW, framebufferH;
141 
142  bool realize();
143  bool spawnWindow(const char *, int, int);
144 
145  bool queryCapabilities();
146  void setupDisplayProgram();
147  void drawFrame(int, int);
148  Display::ViewPolicy policy;
149 
150  GLfloat transformResult[16];
151  unsigned int winH, winW;
152 
153  #ifdef DC_SUBSYSTEM_WIN32
154  bool createContext(HWND h);
155  HWND handle;
156  HDC deviceHandle;
157  HGLRC GLcontext;
158  std::vector<MSG> lastMsgs;
159 
160  bool needSizeUpdate;
161  static LRESULT CALLBACK WindowCallback(HWND, UINT, WPARAM, LPARAM);
162  void UpdateSize();
163  #endif
164 
165  #ifdef DC_SUBSYSTEM_X11
166  bool createContext();
167  X11Display *dpy;
168  XVisualInfo *vi;
169  Colormap cmap;
170  XSetWindowAttributes swa;
171  Window win;
172  Window root;
173  GLXContext glc;
174  XWindowAttributes gwa;
175  XEvent xev;
176  std::vector<XEvent> lastEvents;
177 
178  int winX, winY;
179 
180  void updateDims();
181  #endif
182 
183  GLint programHandle;
184  GLuint vao;
185  GLuint vbo;
186 
187  enum ProgramBindPoint {
188  VertexPosition = 0,
189  VertexColor = 1,
190  VertexUVs = 2,
191  };
192  std::stack<std::string> dumpQueue;
193  std::stack<int> dumpWait;
194  time_t cycleTime;
195 
196 };
197 
198 struct DisplayVertex {
199  GLfloat
200  x, y, z, w,
201  texX, texY;
202 };
203 }
204 
205 
206 #endif
207 #endif
208 
Contains a visual state.
Definition: Framebuffer.h:49
Capability
The standard functional capabilities of a Display.
Definition: Display.h:86
ViewPolicy
Controls how the Display displays Rendered data.
Definition: Display.h:96
Horizontal axis.
Definition: AssetID.h:37
Vertical axis.
Backend that controls how data is displayed to the user.
Definition: Display.h:53