Guest User

Untitled

a guest
Apr 6th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. #ifdef __linux__
  2.  
  3. #include "Window.hpp"
  4.  
  5. #include <iostream>
  6. #include <X11/X.h>
  7. #include <X11/Xlib.h>
  8. #include <X11/keysym.h>
  9. #include <GL/glx.h>
  10. #include <GL/glu.h>
  11.  
  12. Display* _display;
  13. Window _window;
  14. GLint _attributes[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None};
  15. XSetWindowAttributes _wattributes;
  16. Colormap _cmap;
  17. XVisualInfo *_vinfo;
  18. GLXContext _context;
  19. Atom _close;
  20. XEvent _event;
  21.  
  22.  
  23. namespace ege{
  24.  
  25. EGE_Window::EGE_Window(){
  26. _width = 640;
  27. _height = 480;
  28. _x = 1;
  29. _y = 1;
  30. _title = (char *)"EGE";
  31. _r = 0.0;
  32. _g = 0.0;
  33. _b = 0.0;
  34. _minWidth = 100;
  35. _minHeight = 100;
  36. _maxWidth = 2000;
  37. _maxHeight = 2000;
  38. }
  39.  
  40. EGE_Window::~EGE_Window(){
  41.  
  42. }
  43.  
  44. void EGE_Window::create(){
  45. if(!(_display = XOpenDisplay(NULL))){ std::cout << "Error opening display" << std::endl; }
  46.  
  47. int _screen = DefaultScreen(_display);
  48. _vinfo = glXChooseVisual(_display, _screen, _attributes);
  49. _cmap = XCreateColormap(_display, RootWindow(_display, _screen), _vinfo->visual, AllocNone);
  50. _wattributes.colormap = _cmap;
  51. _wattributes.event_mask = ExposureMask | FocusChangeMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask | ExposureMask | KeyPressMask | KeyReleaseMask | StructureNotifyMask;
  52.  
  53. if(!(_window = XCreateWindow(_display, RootWindow(_display, _screen), _x, _y, _width, _height, 0, _vinfo->depth, InputOutput, _vinfo->visual, CWColormap | CWEventMask, &_wattributes))){ std::cout << "Error creating window" << std::endl; }
  54. XMapWindow(_display, _window);
  55. XStoreName(_display, _window, _title);
  56. _close = XInternAtom(_display, "WM_DELETE_WINDOW", False);
  57. XSetWMProtocols(_display, _window, &_close, 1);
  58. _context = glXCreateContext(_display, _vinfo, NULL, GL_TRUE);
  59. glXMakeCurrent(_display, _window, _context);
  60.  
  61. XFlush(_display);
  62.  
  63. glClearColor(_r, _b, _g, 1.0);
  64. glClearDepth(1.0);
  65. glDepthFunc(GL_LESS);
  66. glEnable(GL_DEPTH_TEST);
  67. glShadeModel(GL_SMOOTH);
  68. glMatrixMode(GL_PROJECTION);
  69. glLoadIdentity();
  70. gluOrtho2D(0, _width, 0, _height);
  71. glMatrixMode(GL_MODELVIEW);
  72. }
  73.  
  74. void EGE_Window::create(const char *title, int x, int y, int width, int height){
  75. _x = x;
  76. _y = y;
  77. _width = width;
  78. _height = height;
  79. _title = (char *)title;
  80.  
  81. if(!(_display = XOpenDisplay(NULL))){ std::cout << "Error opening display" << std::endl; }
  82.  
  83. int _screen = DefaultScreen(_display);
  84. _vinfo = glXChooseVisual(_display, _screen, _attributes);
  85. _cmap = XCreateColormap(_display, RootWindow(_display, _screen), _vinfo->visual, AllocNone);
  86. _wattributes.colormap = _cmap;
  87. _wattributes.event_mask = FocusChangeMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask | ExposureMask | KeyPressMask | KeyReleaseMask | StructureNotifyMask;
  88.  
  89. if(!(_window = XCreateWindow(_display, RootWindow(_display, _screen), _x, _y, _width, _height, 0, _vinfo->depth, InputOutput, _vinfo->visual, CWColormap | CWEventMask, &_wattributes))){ std::cout << "Error creating window" << std::endl; }
  90. XMapWindow(_display, _window);
  91. XStoreName(_display, _window, _title);
  92. _close = XInternAtom(_display, "WM_DELETE_WINDOW", False);
  93. XSetWMProtocols(_display, _window, &_close, 0);
  94.  
  95. _context = glXCreateContext(_display, _vinfo, NULL, GL_TRUE);
  96. glXMakeCurrent(_display, _window, _context);
  97.  
  98. XFlush(_display);
  99.  
  100. glClearColor(_r, _b, _g, 1.0);
  101. glClearDepth(1.0);
  102. glDepthFunc(GL_LESS);
  103. glEnable(GL_DEPTH_TEST);
  104. glShadeModel(GL_SMOOTH);
  105. glMatrixMode(GL_PROJECTION);
  106. glLoadIdentity();
  107. gluOrtho2D(0, _width, 0, _height);
  108. glMatrixMode(GL_MODELVIEW);
  109. }
  110.  
  111. void EGE_Window::destroy(){
  112. XDestroyWindow(_display, _window);
  113. XCloseDisplay(_display);
  114. }
  115.  
  116. void EGE_Window::setTitle(const char *title){
  117. _title = (char *)title;
  118. XStoreName(_display, _window, _title);
  119. }
  120.  
  121. void EGE_Window::update(){
  122. glFinish();
  123. glXSwapBuffers(_display, _window);
  124. glLoadIdentity();
  125. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  126. }
  127.  
  128. void EGE_Window::grabMouse(bool grab){
  129. /*if(grab)
  130.  
  131. else*/
  132. }
  133.  
  134. void EGE_Window::setBackgroundColor(float r, float g, float b){
  135. _r = r;
  136. _g = g;
  137. _b = b;
  138. }
  139.  
  140. void EGE_Window::setMinimumSize(int minWidth, int minHeight){
  141. if(minWidth < 1) minWidth = 1;
  142. if(minHeight < 1) minHeight = 1;
  143.  
  144. _minWidth = minWidth;
  145. _minHeight = minHeight;
  146. }
  147.  
  148. void EGE_Window::setMaximumSize(int maxWidth, int maxHeight){
  149. _maxWidth = maxWidth;
  150. _maxHeight = maxHeight;
  151. }
  152.  
  153. bool EGE_Window::pollEvent(EGE_Event *event){
  154.  
  155. if(XPending(_display) > 0) {
  156. XNextEvent(_display, &_event);
  157.  
  158. switch(_event.type){
  159. case ClientMessage:
  160. if((Atom)_event.xclient.data.l[0] == _close){ event->type = EGE_QUIT; XResizeWindow(_display, _window, _width, _height); }
  161. break;
  162. case KeyPress:
  163. event->type = EGE_KEYDOWN;
  164.  
  165. char _key[1];
  166. KeySym _keysym;
  167. EGE_Key _rk;
  168.  
  169. if(XLookupString(&_event.xkey, _key, 25, &_keysym, NULL)){
  170. _rk = (EGE_Key)_key[0];
  171. } else {
  172. _rk = (EGE_Key)_event.xkey.keycode;
  173. }
  174.  
  175. event->key.keysym = _rk;
  176. break;
  177. case KeyRelease:
  178. event->type = EGE_KEYUP;
  179.  
  180. if(XLookupString(&_event.xkey, _key, 25, &_keysym, NULL)){
  181. _rk = (EGE_Key)_key[0];
  182. } else {
  183. _rk = (EGE_Key)_event.xkey.keycode;
  184. }
  185.  
  186. event->key.keysym = _rk;
  187. break;
  188. case ButtonPress:
  189. event->type = EGE_MOUSEBUTTON;
  190. event->mouse.type = EGE_MOUSEBUTTONDOWN;
  191. event->mouse.x = _event.xbutton.x;
  192. event->mouse.y = _event.xbutton.y;
  193.  
  194. switch(_event.xbutton.button){
  195. case Button1: event->mouse.button = 1; break;
  196. case Button2: event->mouse.button = 2; break;
  197. case Button3: event->mouse.button = 3; break;
  198. case Button4: event->mouse.button = 4; break;
  199. case Button5: event->mouse.button = 5; break;
  200. default: event->mouse.button = 0; break;
  201. }
  202. break;
  203. case ButtonRelease:
  204. event->type = EGE_MOUSEBUTTON;
  205. event->mouse.type = EGE_MOUSEBUTTONUP;
  206. event->mouse.x = _event.xbutton.x;
  207. event->mouse.y = _event.xbutton.y;
  208.  
  209. switch(_event.xbutton.button){
  210. case Button1: event->mouse.button = 1; break;
  211. case Button2: event->mouse.button = 2; break;
  212. case Button3: event->mouse.button = 3; break;
  213. case Button4: event->mouse.button = 4; break;
  214. case Button5: event->mouse.button = 5; break;
  215. default: event->mouse.button = 0; break;
  216. }
  217. break;
  218. case ResizeRequest:
  219. event->type = EGE_WINDOWEVENT;
  220. event->window.type = EGE_WINDOW_RESIZE;
  221. break;
  222. case Expose:
  223.  
  224. break;
  225. case ConfigureNotify:
  226.  
  227. break;
  228. }
  229. return true;
  230. }
  231.  
  232. return false;
  233. }
  234.  
  235. };
  236.  
  237. #endif /*__linux__*/
Advertisement
Add Comment
Please, Sign In to add comment