glplatform.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. #ifndef GL_WIN_H
  2. #define GL_WIN_H
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #ifdef _WIN32
  6. #include <Windows.h>
  7. #else
  8. #include <X11/Xlib.h>
  9. #endif
  10. struct glplatform_win;
  11. typedef intptr_t glplatform_gl_context_t;
  12. /*
  13. * int glplatform_epoll_fd;
  14. *
  15. * epoll file descriptor used by glplatform for event waiting. Can be used to
  16. * integrate glplatform into a different event loop.
  17. *
  18. */
  19. extern int glplatform_epoll_fd;
  20. /*
  21. * struct glplatform_win_callbacks
  22. *
  23. * Callbacks for windows created by glplatform
  24. *
  25. */
  26. struct glplatform_win_callbacks {
  27. /*
  28. * on_create(win)
  29. *
  30. * Called when the window is created.
  31. *
  32. * win - Window
  33. *
  34. */
  35. void (*on_create)(struct glplatform_win *win);
  36. /*
  37. * on_resize(win)
  38. *
  39. * Called when window is resized. The new window
  40. * size can be read from the window structure.
  41. *
  42. * win - Window
  43. *
  44. */
  45. void (*on_resize)(struct glplatform_win *win);
  46. /*
  47. * on_expose(win)
  48. *
  49. * Called when a part of the window was hidden and is
  50. * now exposed.
  51. *
  52. * win - Window
  53. *
  54. */
  55. void (*on_expose)(struct glplatform_win *win);
  56. /*
  57. *
  58. * on_mouse_button_up(win, button_num, mouse_x, mouse_y)
  59. *
  60. * Called when a mouse button is released
  61. *
  62. * win - Window
  63. *
  64. * button_num - Button released
  65. *
  66. * mouse_x,mouse_y - Mouse position
  67. *
  68. */
  69. void (*on_mouse_button_up)(struct glplatform_win *, int button_num, int mouse_x, int mouse_y);
  70. /*
  71. *
  72. * on_mouse_button_down(win, button_num, mouse_x, mouse_y)
  73. *
  74. * Called when a mouse button is pressed
  75. *
  76. * win - Window
  77. *
  78. * button_num - Button pressed
  79. *
  80. * mouse_x,mouse_y - Mouse position
  81. *
  82. */
  83. void (*on_mouse_button_down)(struct glplatform_win *, int button_num, int mouse_x, int mouse_y);
  84. /*
  85. *
  86. * on_mouse_button_move(win, button_num, mouse_x, mouse_y)
  87. *
  88. * Called when the mouse moves in the window area
  89. *
  90. * win - Window
  91. *
  92. * mouse_x,mouse_y - Mouse position
  93. *
  94. */
  95. void (*on_mouse_move)(struct glplatform_win *, int mouse_x, int mouse_y);
  96. /*
  97. *
  98. * on_mouse_wheel(win, button_num, mouse_x, mouse_y, delta)
  99. *
  100. * Called when the mouse wheel changes position
  101. *
  102. * win - window
  103. *
  104. * mouse_x,mouse_y - Mouse position
  105. *
  106. * delta - Direction the mouse wheel moved.
  107. *
  108. * +1: Mouse wheel moved up
  109. *
  110. * -1: Mouse wheel moved down
  111. */
  112. void (*on_mouse_wheel)(struct glplatform_win *, int mouse_x, int mouse_y, int delta);
  113. /*
  114. * on_key_down(win, key)
  115. *
  116. * Called when a key is pressed
  117. *
  118. * win - Window
  119. *
  120. * key - ASCII value of key pressed
  121. *
  122. */
  123. void (*on_key_down)(struct glplatform_win *, int key);
  124. /*
  125. * on_key_down(win, key)
  126. *
  127. * Called when a key is released
  128. *
  129. * win - Window
  130. *
  131. * key - ASCII code of key released
  132. *
  133. */
  134. void (*on_key_up)(struct glplatform_win *, int key);
  135. /*
  136. * on_destroy(win)
  137. *
  138. * Called when the window's close button has been pressed
  139. *
  140. * win - Window
  141. *
  142. */
  143. void (*on_destroy)(struct glplatform_win *);
  144. /*
  145. * on_fd_event(win, fd, event)
  146. *
  147. * Called when the window's close button has been pressed
  148. *
  149. * win - Window
  150. *
  151. * fd - File descriptor that changed status
  152. *
  153. * event - Event mask returned from epoll(). See 'man epoll_ctl' for full descriptions.
  154. *
  155. * EPOLLIN: File descriptor has become readable
  156. *
  157. * EPOLLOUT: File descriptor has become writable
  158. *
  159. * EPOLLERR: File descriptor encountered an error
  160. *
  161. * EPOLLRDHUP: Peer closed connection
  162. *
  163. * user_data - User data value passed to glplatform_fd_bind()
  164. *
  165. */
  166. void (*on_fd_event)(struct glplatform_win *, int fd, uint32_t event, intptr_t user_data);
  167. #ifndef _WIN32
  168. /*
  169. * on_x_event(event)
  170. *
  171. * Called for all X event's processed. Can be used to process events
  172. * not processed by glplatform.
  173. *
  174. * win - Window
  175. *
  176. * event - X event
  177. *
  178. */
  179. void (*on_x_event)(struct glplatform_win *, XEvent *event);
  180. #endif
  181. };
  182. struct glplatform_fbformat {
  183. int color_bits;
  184. int alpha_bits;
  185. int stencil_bits;
  186. int depth_bits;
  187. int accum_bits;
  188. };
  189. struct glplatform_win {
  190. #ifdef _WIN32
  191. int pixel_format;
  192. HDC hdc;
  193. HWND hwnd;
  194. #else
  195. uint32_t window; //Window xid
  196. void *fb_config; //GLXFBConfig
  197. uint32_t glx_window; //GLXWindow
  198. int x_state_mask;
  199. #endif
  200. struct glplatform_fbformat fbformat;
  201. struct glplatform_win_callbacks callbacks;
  202. int width;
  203. int height;
  204. struct glplatform_win *next;
  205. struct glplatform_win **pprev;
  206. };
  207. #ifndef _WIN32
  208. struct glplatform_thread_state {
  209. void *display;
  210. void *context;
  211. uint32_t read_draw;
  212. uint32_t write_draw;
  213. };
  214. #endif
  215. /*
  216. * glplatform_init()
  217. *
  218. * Performs one time initializtion of gplatform library. Must be called
  219. * before any other glplatform calls are made.
  220. *
  221. */
  222. bool glplatform_init();
  223. /*
  224. *
  225. * glplatform_shutdown()
  226. *
  227. * Free any resources allocated by glplatform. No glplatform calls
  228. * should be made after glplatform_shutdown()
  229. *
  230. */
  231. void glplatform_shutdown();
  232. /*
  233. * glplatform_create_window()
  234. *
  235. * Create an OpenGL enabled window.
  236. *
  237. * title - Title of the window
  238. *
  239. * callbacks - Functions to call when window events occur
  240. *
  241. * width - Desired width of window
  242. *
  243. * height - Desired height of window
  244. *
  245. */
  246. struct glplatform_win *glplatform_create_window(const char *title,
  247. const struct glplatform_win_callbacks *callbacks,
  248. const struct glplatform_fbformat *fbformat,
  249. int width,
  250. int height);
  251. /*
  252. * glplatform_destroy_window
  253. *
  254. * Destroy a previously created window. The window structure will
  255. * be deallocated and no further callbacks will be issued for
  256. * it.
  257. *
  258. */
  259. void glplatform_destroy_window(struct glplatform_win *win);
  260. /*
  261. * glplatform_get_thread_state()
  262. *
  263. * Store OpenGL related thread state. Should be called
  264. * before calling glplatform_make_current() if the current
  265. * thread state is not known and will need to be restored.
  266. *
  267. */
  268. void glplatform_get_thread_state(struct glplatform_thread_state *state);
  269. /*
  270. * glplatform_get_thread_state()
  271. *
  272. * Restore OpenGL thread state retrieved by
  273. * glplatform_get_thread_state()
  274. *
  275. */
  276. void glplatform_set_thread_state(const struct glplatform_thread_state *state);
  277. /*
  278. * glplatform_get_events()
  279. *
  280. * Collect events into glplatform's internal queue. If 'block' is set to true
  281. * then wait until an event occurs.
  282. *
  283. * Returns non-zero if events have been queued. Returns zero if no events are queued
  284. * and negative on error.
  285. *
  286. */
  287. int glplatform_get_events(bool block);
  288. /*
  289. * glplatform_make_current()
  290. *
  291. * Make 'context' current for this thread. All subsequent OpenGL calls
  292. * in the current thread will execute in this context.
  293. *
  294. */
  295. void glplatform_make_current(struct glplatform_win *win, glplatform_gl_context_t context);
  296. /*
  297. * glplatform_process_events()
  298. *
  299. * Issue callbacks for events queued by glplatform_get_events()
  300. *
  301. * Returns true if at least one window is still exists
  302. *
  303. */
  304. bool glplatform_process_events();
  305. /*
  306. * glplatform_fd_bind()
  307. *
  308. * Causes glplatform to call win->on_fd_event() whenever the
  309. * supplied file descriptor's read, write, or error status
  310. * changes.
  311. *
  312. * fd - File descriptor to bind
  313. *
  314. * win - Window to bind to the file descriptor
  315. *
  316. * user_data - Data to pass to the on_fd_event() callback
  317. *
  318. */
  319. void glplatform_fd_bind(int fd, struct glplatform_win *win, intptr_t user_data);
  320. /*
  321. * glplatform_fd_unbind()
  322. *
  323. * Causes glplatform to stop calling win->on_fd_event()
  324. * for the supplied file descriptor.
  325. *
  326. */
  327. void glplatform_fd_unbind(int fd);
  328. /*
  329. * glplatform_create_context()
  330. *
  331. * Attempts to create a core-profile OpenGL context that is backwards
  332. * compatible with the supplied version number.
  333. *
  334. */
  335. glplatform_gl_context_t glplatform_create_context(struct glplatform_win *win, int maj_ver, int min_ver);
  336. enum glplatform_win_types {
  337. GLWIN_POPUP,
  338. GLWIN_NORMAL,
  339. GLWIN_DIALOG,
  340. GLWIN_TOOLBAR,
  341. GLWIN_UTILITY
  342. };
  343. /*
  344. * glplatform_set_win_type()
  345. *
  346. * Set a window's type which determines it's border and stacking behavior.
  347. *
  348. */
  349. void glplatform_set_win_type(struct glplatform_win *win, enum glplatform_win_types type);
  350. /*
  351. * glplatform_set_win_transient_for()
  352. *
  353. * Make this window a 'transient' for the window with the specified xid.
  354. * A glplatform window's xid is stored in the 'window' field of it's
  355. * glplatform_win structure.
  356. *
  357. */
  358. void glplatform_set_win_transient_for(struct glplatform_win *win, intptr_t xid);
  359. /*
  360. * glplatform_swap_buffers()
  361. *
  362. * Swap a windows backbuffer to the front.
  363. *
  364. */
  365. void glplatform_swap_buffers(struct glplatform_win *win);
  366. /*
  367. * glplatform_show_window()
  368. *
  369. * Make a window visible. Windows are hidden at creation time.
  370. *
  371. */
  372. void glplatform_show_window(struct glplatform_win *win);
  373. /*
  374. * glplatform_fullscreen_win()
  375. *
  376. * Change a window's fullscreen status. Windows are initially
  377. * not fullscreen.
  378. *
  379. */
  380. void glplatform_fullscreen_win(struct glplatform_win *win, bool fullscreen);
  381. /*
  382. * glplatform_is_button_pressed()
  383. *
  384. * Returns true if specified mouse button number is pressed. The left
  385. * button is numbered 0. The middle button is numbered 1. The right
  386. * button is numbered 2.
  387. *
  388. */
  389. bool glplatform_is_button_pressed(struct glplatform_win *win, int button_no);
  390. /*
  391. * glplatform_is_shift_pressed()
  392. *
  393. * Returns true if the shift key is being held
  394. *
  395. */
  396. bool glplatform_is_shift_pressed(struct glplatform_win *win);
  397. /*
  398. * glplatform_is_control_pressed()
  399. *
  400. * Returns true if the control key is being held
  401. *
  402. */
  403. bool glplatform_is_control_pressed(struct glplatform_win *win);
  404. #endif