winapp.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #if defined(_WIN32)
  2. # define WIN32_LEAN_AND_MEAN
  3. # include <windows.h>
  4. #elif defined(__linux__)
  5. # include <X11/X.h>
  6. # include <X11/Xlib.h>
  7. # include <X11/Xutil.h>
  8. #endif
  9. #include <string>
  10. #include <GL/gl.h>
  11. #if defined(_WIN32)
  12. # include <GL/glu.h>
  13. #elif defined(__linux__)
  14. # include <GL/glx.h>
  15. #endif
  16. #if defined(_WIN32)
  17. # define WINCLASS "WinAppWnd"
  18. #endif
  19. #define SAFE_RELEASE(p) if (p) { p->Release(); p = NULL; }
  20. class WinApp
  21. {
  22. public:
  23. WinApp(int width, int height, std::string& window_name)
  24. {
  25. m_width = width;
  26. m_height = height;
  27. m_window_name = window_name;
  28. #if defined(_WIN32)
  29. m_hInstance = ::GetModuleHandle(NULL);
  30. #endif
  31. }
  32. virtual ~WinApp()
  33. {
  34. #if defined(_WIN32)
  35. ::UnregisterClass(WINCLASS, m_hInstance);
  36. #endif
  37. }
  38. int create()
  39. {
  40. #if defined(_WIN32)
  41. WNDCLASSEX wcex;
  42. wcex.cbSize = sizeof(WNDCLASSEX);
  43. wcex.style = CS_HREDRAW | CS_VREDRAW;
  44. wcex.lpfnWndProc = &WinApp::StaticWndProc;
  45. wcex.cbClsExtra = 0;
  46. wcex.cbWndExtra = 0;
  47. wcex.hInstance = m_hInstance;
  48. wcex.hIcon = LoadIcon(0, IDI_APPLICATION);
  49. wcex.hCursor = LoadCursor(0, IDC_ARROW);
  50. wcex.hbrBackground = 0;
  51. wcex.lpszMenuName = 0L;
  52. wcex.lpszClassName = WINCLASS;
  53. wcex.hIconSm = 0;
  54. ATOM wc = ::RegisterClassEx(&wcex);
  55. RECT rc = { 0, 0, m_width, m_height };
  56. ::AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, false);
  57. m_hWnd = ::CreateWindow(
  58. (LPCTSTR)wc, m_window_name.c_str(),
  59. WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
  60. rc.right - rc.left, rc.bottom - rc.top,
  61. NULL, NULL, m_hInstance, (void*)this);
  62. if (!m_hWnd)
  63. return -1;
  64. ::ShowWindow(m_hWnd, SW_SHOW);
  65. ::UpdateWindow(m_hWnd);
  66. ::SetFocus(m_hWnd);
  67. #elif defined(__linux__)
  68. m_display = XOpenDisplay(NULL);
  69. if (m_display == NULL)
  70. {
  71. return -1;
  72. }
  73. m_WM_DELETE_WINDOW = XInternAtom(m_display, "WM_DELETE_WINDOW", False);
  74. static GLint visual_attributes[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
  75. m_visual_info = glXChooseVisual(m_display, 0, visual_attributes);
  76. if (m_visual_info == NULL)
  77. {
  78. XCloseDisplay(m_display);
  79. return -2;
  80. }
  81. Window root = DefaultRootWindow(m_display);
  82. m_event_mask = ExposureMask | KeyPressMask;
  83. XSetWindowAttributes window_attributes;
  84. window_attributes.colormap = XCreateColormap(m_display, root, m_visual_info->visual, AllocNone);
  85. window_attributes.event_mask = m_event_mask;
  86. m_window = XCreateWindow(
  87. m_display, root, 0, 0, m_width, m_height, 0, m_visual_info->depth,
  88. InputOutput, m_visual_info->visual, CWColormap | CWEventMask, &window_attributes);
  89. XMapWindow(m_display, m_window);
  90. XSetWMProtocols(m_display, m_window, &m_WM_DELETE_WINDOW, 1);
  91. XStoreName(m_display, m_window, m_window_name.c_str());
  92. #endif
  93. return init();
  94. }
  95. virtual void cleanup()
  96. {
  97. #if defined(_WIN32)
  98. ::DestroyWindow(m_hWnd);
  99. #elif defined(__linux__)
  100. XDestroyWindow(m_display, m_window);
  101. XCloseDisplay(m_display);
  102. #endif
  103. }
  104. #if defined(_WIN32)
  105. virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) = 0;
  106. #endif
  107. int run()
  108. {
  109. #if defined(_WIN32)
  110. MSG msg;
  111. ::ZeroMemory(&msg, sizeof(msg));
  112. while (msg.message != WM_QUIT)
  113. {
  114. if (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
  115. {
  116. ::TranslateMessage(&msg);
  117. ::DispatchMessage(&msg);
  118. }
  119. else
  120. {
  121. idle();
  122. }
  123. }
  124. return static_cast<int>(msg.wParam);
  125. #elif defined(__linux__)
  126. m_end_loop = false;
  127. do {
  128. XEvent e;
  129. if (!XCheckWindowEvent(m_display, m_window, m_event_mask, &e) || !handle_event(e))
  130. {
  131. idle();
  132. }
  133. } while (!m_end_loop);
  134. return 0;
  135. #endif
  136. }
  137. protected:
  138. #if defined(_WIN32)
  139. static LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  140. {
  141. WinApp* pWnd;
  142. if (message == WM_NCCREATE)
  143. {
  144. LPCREATESTRUCT pCreateStruct = ((LPCREATESTRUCT)lParam);
  145. pWnd = (WinApp*)(pCreateStruct->lpCreateParams);
  146. ::SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)pWnd);
  147. }
  148. pWnd = GetObjectFromWindow(hWnd);
  149. if (pWnd)
  150. return pWnd->WndProc(hWnd, message, wParam, lParam);
  151. else
  152. return ::DefWindowProc(hWnd, message, wParam, lParam);
  153. }
  154. inline static WinApp* GetObjectFromWindow(HWND hWnd)
  155. {
  156. return (WinApp*)::GetWindowLongPtr(hWnd, GWLP_USERDATA);
  157. }
  158. #endif
  159. #if defined(__linux__)
  160. virtual int handle_event(XEvent& e) = 0;
  161. #endif
  162. virtual int init() = 0;
  163. virtual int render() = 0;
  164. virtual void idle() = 0;
  165. #if defined(_WIN32)
  166. HINSTANCE m_hInstance;
  167. HWND m_hWnd;
  168. #elif defined(__linux__)
  169. Display* m_display;
  170. XVisualInfo* m_visual_info;
  171. Window m_window;
  172. long m_event_mask;
  173. Atom m_WM_DELETE_WINDOW;
  174. bool m_end_loop;
  175. #endif
  176. int m_width;
  177. int m_height;
  178. std::string m_window_name;
  179. cv::TickMeter m_timer;
  180. };