tinySDLGraphicsWindow.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file tinySDLGraphicsWindow.cxx
  10. * @author drose
  11. * @date 2008-04-24
  12. */
  13. #include "pandabase.h"
  14. #ifdef HAVE_SDL
  15. #include "tinySDLGraphicsWindow.h"
  16. #include "tinyGraphicsStateGuardian.h"
  17. #include "config_tinydisplay.h"
  18. #include "tinySDLGraphicsPipe.h"
  19. #include "mouseButton.h"
  20. #include "keyboardButton.h"
  21. #include "graphicsPipe.h"
  22. TypeHandle TinySDLGraphicsWindow::_type_handle;
  23. /**
  24. *
  25. */
  26. TinySDLGraphicsWindow::
  27. TinySDLGraphicsWindow(GraphicsEngine *engine, GraphicsPipe *pipe,
  28. const std::string &name,
  29. const FrameBufferProperties &fb_prop,
  30. const WindowProperties &win_prop,
  31. int flags,
  32. GraphicsStateGuardian *gsg,
  33. GraphicsOutput *host) :
  34. GraphicsWindow(engine, pipe, name, fb_prop, win_prop, flags, gsg, host)
  35. {
  36. _screen = nullptr;
  37. _frame_buffer = nullptr;
  38. _pitch = 0;
  39. update_pixel_factor();
  40. PT(GraphicsWindowInputDevice) device =
  41. GraphicsWindowInputDevice::pointer_and_keyboard(this, "keyboard_mouse");
  42. add_input_device(device);
  43. _input = device.p();
  44. }
  45. /**
  46. *
  47. */
  48. TinySDLGraphicsWindow::
  49. ~TinySDLGraphicsWindow() {
  50. }
  51. /**
  52. * This function will be called within the draw thread before beginning
  53. * rendering for a given frame. It should do whatever setup is required, and
  54. * return true if the frame should be rendered, or false if it should be
  55. * skipped.
  56. */
  57. bool TinySDLGraphicsWindow::
  58. begin_frame(FrameMode mode, Thread *current_thread) {
  59. begin_frame_spam(mode);
  60. if (_gsg == nullptr) {
  61. return false;
  62. }
  63. TinyGraphicsStateGuardian *tinygsg;
  64. DCAST_INTO_R(tinygsg, _gsg, false);
  65. tinygsg->_current_frame_buffer = _frame_buffer;
  66. tinygsg->reset_if_new();
  67. _gsg->set_current_properties(&get_fb_properties());
  68. return _gsg->begin_frame(current_thread);
  69. }
  70. /**
  71. * This function will be called within the draw thread after rendering is
  72. * completed for a given frame. It should do whatever finalization is
  73. * required.
  74. */
  75. void TinySDLGraphicsWindow::
  76. end_frame(FrameMode mode, Thread *current_thread) {
  77. end_frame_spam(mode);
  78. nassertv(_gsg != nullptr);
  79. if (mode == FM_render) {
  80. // end_render_texture();
  81. copy_to_textures();
  82. }
  83. _gsg->end_frame(current_thread);
  84. if (mode == FM_render) {
  85. trigger_flip();
  86. }
  87. }
  88. /**
  89. * This function will be called within the draw thread after begin_flip() has
  90. * been called on all windows, to finish the exchange of the front and back
  91. * buffers.
  92. *
  93. * This should cause the window to wait for the flip, if necessary.
  94. */
  95. void TinySDLGraphicsWindow::
  96. end_flip() {
  97. if (!_flip_ready) {
  98. GraphicsWindow::end_flip();
  99. return;
  100. }
  101. int fb_xsize = get_fb_x_size();
  102. int fb_ysize = get_fb_y_size();
  103. if (fb_xsize == _frame_buffer->xsize) {
  104. // No zooming is necessary--copy directly to the screen.
  105. if (SDL_MUSTLOCK(_screen)) {
  106. if (SDL_LockSurface(_screen) < 0) {
  107. tinydisplay_cat.error()
  108. << "Can't lock screen: " << SDL_GetError() << "\n";
  109. }
  110. }
  111. ZB_copyFrameBuffer(_frame_buffer, _screen->pixels, _pitch);
  112. if (SDL_MUSTLOCK(_screen)) {
  113. SDL_UnlockSurface(_screen);
  114. }
  115. } else {
  116. // Copy to another surface, then scale it onto the screen.
  117. SDL_Surface *temp =
  118. SDL_CreateRGBSurfaceFrom(_frame_buffer->pbuf, _frame_buffer->xsize, _frame_buffer->ysize,
  119. 32, _frame_buffer->linesize, 0xff0000, 0x00ff00, 0x0000ff, 0xff000000);
  120. SDL_SetAlpha(temp, SDL_RLEACCEL, 0);
  121. SDL_BlitSurface(temp, nullptr, _screen, nullptr);
  122. SDL_FreeSurface(temp);
  123. }
  124. SDL_Flip(_screen);
  125. GraphicsWindow::end_flip();
  126. }
  127. /**
  128. * Do whatever processing is necessary to ensure that the window responds to
  129. * user events. Also, honor any requests recently made via
  130. * request_properties()
  131. *
  132. * This function is called only within the window thread.
  133. */
  134. void TinySDLGraphicsWindow::
  135. process_events() {
  136. GraphicsWindow::process_events();
  137. if (_screen == nullptr) {
  138. return;
  139. }
  140. WindowProperties properties;
  141. SDL_Event evt;
  142. ButtonHandle button;
  143. while (SDL_PollEvent(&evt)) {
  144. switch(evt.type) {
  145. case SDL_KEYDOWN:
  146. if (evt.key.keysym.unicode) {
  147. _input->keystroke(evt.key.keysym.unicode);
  148. }
  149. button = get_keyboard_button(evt.key.keysym.sym);
  150. if (button != ButtonHandle::none()) {
  151. _input->button_down(button);
  152. }
  153. break;
  154. case SDL_KEYUP:
  155. button = get_keyboard_button(evt.key.keysym.sym);
  156. if (button != ButtonHandle::none()) {
  157. _input->button_up(button);
  158. }
  159. break;
  160. case SDL_MOUSEBUTTONDOWN:
  161. button = get_mouse_button(evt.button.button);
  162. _input->set_pointer_in_window(evt.button.x, evt.button.y);
  163. _input->button_down(button);
  164. break;
  165. case SDL_MOUSEBUTTONUP:
  166. button = get_mouse_button(evt.button.button);
  167. _input->set_pointer_in_window(evt.button.x, evt.button.y);
  168. _input->button_up(button);
  169. break;
  170. case SDL_MOUSEMOTION:
  171. _input->set_pointer_in_window(evt.motion.x, evt.motion.y);
  172. break;
  173. case SDL_VIDEORESIZE:
  174. properties.set_size(evt.resize.w, evt.resize.h);
  175. system_changed_properties(properties);
  176. _screen = SDL_SetVideoMode(_properties.get_x_size(), _properties.get_y_size(), 32, _flags);
  177. ZB_resize(_frame_buffer, nullptr, _properties.get_x_size(), _properties.get_y_size());
  178. _pitch = _screen->pitch * 32 / _screen->format->BitsPerPixel;
  179. break;
  180. case SDL_QUIT:
  181. // The window was closed by the user.
  182. close_window();
  183. properties.set_open(false);
  184. system_changed_properties(properties);
  185. break;
  186. }
  187. }
  188. }
  189. /**
  190. * Applies the requested set of properties to the window, if possible, for
  191. * instance to request a change in size or minimization status.
  192. *
  193. * The window properties are applied immediately, rather than waiting until
  194. * the next frame. This implies that this method may *only* be called from
  195. * within the window thread.
  196. *
  197. * The return value is true if the properties are set, false if they are
  198. * ignored. This is mainly useful for derived classes to implement extensions
  199. * to this function.
  200. */
  201. void TinySDLGraphicsWindow::
  202. set_properties_now(WindowProperties &properties) {
  203. GraphicsWindow::set_properties_now(properties);
  204. if (!properties.is_any_specified()) {
  205. // The base class has already handled this case.
  206. return;
  207. }
  208. }
  209. /**
  210. * Returns true if a call to set_pixel_zoom() will be respected, false if it
  211. * will be ignored. If this returns false, then get_pixel_factor() will
  212. * always return 1.0, regardless of what value you specify for
  213. * set_pixel_zoom().
  214. *
  215. * This may return false if the underlying renderer doesn't support pixel
  216. * zooming, or if you have called this on a DisplayRegion that doesn't have
  217. * both set_clear_color() and set_clear_depth() enabled.
  218. */
  219. bool TinySDLGraphicsWindow::
  220. supports_pixel_zoom() const {
  221. return true;
  222. }
  223. /**
  224. * Closes the window right now. Called from the window thread.
  225. */
  226. void TinySDLGraphicsWindow::
  227. close_window() {
  228. GraphicsWindow::close_window();
  229. }
  230. /**
  231. * Opens the window right now. Called from the window thread. Returns true
  232. * if the window is successfully opened, or false if there was a problem.
  233. */
  234. bool TinySDLGraphicsWindow::
  235. open_window() {
  236. // GSG CreationInitialization
  237. TinyGraphicsStateGuardian *tinygsg;
  238. if (_gsg == 0) {
  239. // There is no old gsg. Create a new one.
  240. tinygsg = new TinyGraphicsStateGuardian(_engine, _pipe, nullptr);
  241. _gsg = tinygsg;
  242. } else {
  243. DCAST_INTO_R(tinygsg, _gsg, false);
  244. }
  245. _flags = SDL_SWSURFACE;
  246. if (_properties.get_fullscreen()) {
  247. _flags |= SDL_FULLSCREEN;
  248. }
  249. if (!_properties.get_fixed_size()) {
  250. _flags |= SDL_RESIZABLE;
  251. }
  252. if (_properties.get_undecorated()) {
  253. _flags |= SDL_NOFRAME;
  254. }
  255. _screen = SDL_SetVideoMode(_properties.get_x_size(), _properties.get_y_size(), 32, _flags);
  256. if (_screen == nullptr) {
  257. tinydisplay_cat.error()
  258. << "Video mode set failed.\n";
  259. return false;
  260. }
  261. create_frame_buffer();
  262. if (_frame_buffer == nullptr) {
  263. tinydisplay_cat.error()
  264. << "Could not create frame buffer.\n";
  265. return false;
  266. }
  267. tinygsg->_current_frame_buffer = _frame_buffer;
  268. // Now that we have made the context current to a window, we can reset the
  269. // GSG state if this is the first time it has been used. (We can't just
  270. // call reset() when we construct the GSG, because reset() requires having a
  271. // current context.)
  272. tinygsg->reset_if_new();
  273. return true;
  274. }
  275. /**
  276. * Creates a suitable frame buffer for the current window size.
  277. */
  278. void TinySDLGraphicsWindow::
  279. create_frame_buffer() {
  280. if (_frame_buffer != nullptr) {
  281. ZB_close(_frame_buffer);
  282. _frame_buffer = nullptr;
  283. }
  284. int mode;
  285. switch (_screen->format->BitsPerPixel) {
  286. case 8:
  287. tinydisplay_cat.error()
  288. << "SDL Palettes are currently not supported.\n";
  289. return;
  290. case 16:
  291. mode = ZB_MODE_5R6G5B;
  292. break;
  293. case 24:
  294. mode = ZB_MODE_RGB24;
  295. break;
  296. case 32:
  297. mode = ZB_MODE_RGBA;
  298. break;
  299. default:
  300. return;
  301. }
  302. _frame_buffer = ZB_open(_properties.get_x_size(), _properties.get_y_size(), mode, 0, 0, 0, 0);
  303. _pitch = _screen->pitch * 32 / _screen->format->BitsPerPixel;
  304. }
  305. /**
  306. * Maps from an SDL keysym to the corresponding Panda ButtonHandle.
  307. */
  308. ButtonHandle TinySDLGraphicsWindow::
  309. get_keyboard_button(SDLKey sym) {
  310. switch (sym) {
  311. case SDLK_BACKSPACE: return KeyboardButton::backspace();
  312. case SDLK_TAB: return KeyboardButton::tab();
  313. // case SDLK_CLEAR: return KeyboardButton::clear();
  314. case SDLK_RETURN: return KeyboardButton::enter();
  315. // case SDLK_PAUSE: return KeyboardButton::pause();
  316. case SDLK_ESCAPE: return KeyboardButton::escape();
  317. case SDLK_SPACE: return KeyboardButton::space();
  318. case SDLK_EXCLAIM: return KeyboardButton::ascii_key('!');
  319. case SDLK_QUOTEDBL: return KeyboardButton::ascii_key('"');
  320. case SDLK_HASH: return KeyboardButton::ascii_key('#');
  321. case SDLK_DOLLAR: return KeyboardButton::ascii_key('$');
  322. case SDLK_AMPERSAND: return KeyboardButton::ascii_key('&');
  323. case SDLK_QUOTE: return KeyboardButton::ascii_key('\'');
  324. case SDLK_LEFTPAREN: return KeyboardButton::ascii_key('(');
  325. case SDLK_RIGHTPAREN: return KeyboardButton::ascii_key(')');
  326. case SDLK_ASTERISK: return KeyboardButton::ascii_key('*');
  327. case SDLK_PLUS: return KeyboardButton::ascii_key('+');
  328. case SDLK_COMMA: return KeyboardButton::ascii_key(',');
  329. case SDLK_MINUS: return KeyboardButton::ascii_key('-');
  330. case SDLK_PERIOD: return KeyboardButton::ascii_key('.');
  331. case SDLK_SLASH: return KeyboardButton::ascii_key('/');
  332. case SDLK_0: return KeyboardButton::ascii_key('0');
  333. case SDLK_1: return KeyboardButton::ascii_key('1');
  334. case SDLK_2: return KeyboardButton::ascii_key('2');
  335. case SDLK_3: return KeyboardButton::ascii_key('3');
  336. case SDLK_4: return KeyboardButton::ascii_key('4');
  337. case SDLK_5: return KeyboardButton::ascii_key('5');
  338. case SDLK_6: return KeyboardButton::ascii_key('6');
  339. case SDLK_7: return KeyboardButton::ascii_key('7');
  340. case SDLK_8: return KeyboardButton::ascii_key('8');
  341. case SDLK_9: return KeyboardButton::ascii_key('9');
  342. case SDLK_COLON: return KeyboardButton::ascii_key(':');
  343. case SDLK_SEMICOLON: return KeyboardButton::ascii_key(';');
  344. case SDLK_LESS: return KeyboardButton::ascii_key('<');
  345. case SDLK_EQUALS: return KeyboardButton::ascii_key('=');
  346. case SDLK_GREATER: return KeyboardButton::ascii_key('>');
  347. case SDLK_QUESTION: return KeyboardButton::ascii_key('?');
  348. case SDLK_AT: return KeyboardButton::ascii_key('@');
  349. case SDLK_LEFTBRACKET: return KeyboardButton::ascii_key('[');
  350. case SDLK_BACKSLASH: return KeyboardButton::ascii_key('\\');
  351. case SDLK_RIGHTBRACKET: return KeyboardButton::ascii_key(']');
  352. case SDLK_CARET: return KeyboardButton::ascii_key('^');
  353. case SDLK_UNDERSCORE: return KeyboardButton::ascii_key('_');
  354. case SDLK_BACKQUOTE: return KeyboardButton::ascii_key('`');
  355. case SDLK_a: return KeyboardButton::ascii_key('a');
  356. case SDLK_b: return KeyboardButton::ascii_key('b');
  357. case SDLK_c: return KeyboardButton::ascii_key('c');
  358. case SDLK_d: return KeyboardButton::ascii_key('d');
  359. case SDLK_e: return KeyboardButton::ascii_key('e');
  360. case SDLK_f: return KeyboardButton::ascii_key('f');
  361. case SDLK_g: return KeyboardButton::ascii_key('g');
  362. case SDLK_h: return KeyboardButton::ascii_key('h');
  363. case SDLK_i: return KeyboardButton::ascii_key('i');
  364. case SDLK_j: return KeyboardButton::ascii_key('j');
  365. case SDLK_k: return KeyboardButton::ascii_key('k');
  366. case SDLK_l: return KeyboardButton::ascii_key('l');
  367. case SDLK_m: return KeyboardButton::ascii_key('m');
  368. case SDLK_n: return KeyboardButton::ascii_key('n');
  369. case SDLK_o: return KeyboardButton::ascii_key('o');
  370. case SDLK_p: return KeyboardButton::ascii_key('p');
  371. case SDLK_q: return KeyboardButton::ascii_key('q');
  372. case SDLK_r: return KeyboardButton::ascii_key('r');
  373. case SDLK_s: return KeyboardButton::ascii_key('s');
  374. case SDLK_t: return KeyboardButton::ascii_key('t');
  375. case SDLK_u: return KeyboardButton::ascii_key('u');
  376. case SDLK_v: return KeyboardButton::ascii_key('v');
  377. case SDLK_w: return KeyboardButton::ascii_key('w');
  378. case SDLK_x: return KeyboardButton::ascii_key('x');
  379. case SDLK_y: return KeyboardButton::ascii_key('y');
  380. case SDLK_z: return KeyboardButton::ascii_key('z');
  381. case SDLK_DELETE: return KeyboardButton::del();
  382. case SDLK_KP0: return KeyboardButton::ascii_key('0');
  383. case SDLK_KP1: return KeyboardButton::ascii_key('1');
  384. case SDLK_KP2: return KeyboardButton::ascii_key('2');
  385. case SDLK_KP3: return KeyboardButton::ascii_key('3');
  386. case SDLK_KP4: return KeyboardButton::ascii_key('4');
  387. case SDLK_KP5: return KeyboardButton::ascii_key('5');
  388. case SDLK_KP6: return KeyboardButton::ascii_key('6');
  389. case SDLK_KP7: return KeyboardButton::ascii_key('7');
  390. case SDLK_KP8: return KeyboardButton::ascii_key('8');
  391. case SDLK_KP9: return KeyboardButton::ascii_key('9');
  392. case SDLK_KP_PERIOD: return KeyboardButton::ascii_key('.');
  393. case SDLK_KP_DIVIDE: return KeyboardButton::ascii_key('/');
  394. case SDLK_KP_MULTIPLY: return KeyboardButton::ascii_key('*');
  395. case SDLK_KP_MINUS: return KeyboardButton::ascii_key('-');
  396. case SDLK_KP_PLUS: return KeyboardButton::ascii_key('+');
  397. case SDLK_KP_ENTER: return KeyboardButton::enter();
  398. case SDLK_KP_EQUALS: return KeyboardButton::ascii_key('=');
  399. case SDLK_UP: return KeyboardButton::up();
  400. case SDLK_DOWN: return KeyboardButton::down();
  401. case SDLK_RIGHT: return KeyboardButton::right();
  402. case SDLK_LEFT: return KeyboardButton::left();
  403. case SDLK_INSERT: return KeyboardButton::insert();
  404. case SDLK_HOME: return KeyboardButton::home();
  405. case SDLK_END: return KeyboardButton::end();
  406. case SDLK_PAGEUP: return KeyboardButton::page_up();
  407. case SDLK_PAGEDOWN: return KeyboardButton::page_down();
  408. case SDLK_F1: return KeyboardButton::f1();
  409. case SDLK_F2: return KeyboardButton::f2();
  410. case SDLK_F3: return KeyboardButton::f3();
  411. case SDLK_F4: return KeyboardButton::f4();
  412. case SDLK_F5: return KeyboardButton::f5();
  413. case SDLK_F6: return KeyboardButton::f6();
  414. case SDLK_F7: return KeyboardButton::f7();
  415. case SDLK_F8: return KeyboardButton::f8();
  416. case SDLK_F9: return KeyboardButton::f9();
  417. case SDLK_F10: return KeyboardButton::f10();
  418. case SDLK_F11: return KeyboardButton::f11();
  419. case SDLK_F12: return KeyboardButton::f12();
  420. case SDLK_F13: return KeyboardButton::f13();
  421. case SDLK_F14: return KeyboardButton::f14();
  422. case SDLK_F15: return KeyboardButton::f15();
  423. // case SDLK_NUMLOCK: return KeyboardButton::numlock(); case
  424. // SDLK_CAPSLOCK: return KeyboardButton::capslock(); case SDLK_SCROLLOCK:
  425. // return KeyboardButton::scrollock();
  426. case SDLK_RSHIFT: return KeyboardButton::rshift();
  427. case SDLK_LSHIFT: return KeyboardButton::lshift();
  428. case SDLK_RCTRL: return KeyboardButton::rcontrol();
  429. case SDLK_LCTRL: return KeyboardButton::lcontrol();
  430. case SDLK_RALT: return KeyboardButton::ralt();
  431. case SDLK_LALT: return KeyboardButton::lalt();
  432. case SDLK_RMETA: return KeyboardButton::ralt();
  433. case SDLK_LMETA: return KeyboardButton::lalt();
  434. // case SDLK_LSUPER: return KeyboardButton::left(); case SDLK_RSUPER:
  435. // return KeyboardButton::right(); case SDLK_MODE: return
  436. // KeyboardButton::mode();
  437. case SDLK_HELP: return KeyboardButton::help();
  438. // case SDLK_PRINT: return KeyboardButton::print-screen(); case
  439. // SDLK_SYSREQ: return KeyboardButton::SysRq(); case SDLK_BREAK: return
  440. // KeyboardButton::break(); case SDLK_MENU: return KeyboardButton::menu();
  441. // case SDLK_POWER: return KeyboardButton::power(); case SDLK_EURO: return
  442. // KeyboardButton::euro();
  443. }
  444. tinydisplay_cat.info()
  445. << "unhandled keyboard button " << sym << "\n";
  446. return ButtonHandle::none();
  447. }
  448. /**
  449. * Maps from an SDL mouse button index to the corresponding Panda
  450. * ButtonHandle.
  451. */
  452. ButtonHandle TinySDLGraphicsWindow::
  453. get_mouse_button(Uint8 button) {
  454. switch (button) {
  455. case SDL_BUTTON_LEFT:
  456. return MouseButton::one();
  457. case SDL_BUTTON_MIDDLE:
  458. return MouseButton::two();
  459. case SDL_BUTTON_RIGHT:
  460. return MouseButton::three();
  461. case SDL_BUTTON_WHEELUP:
  462. return MouseButton::wheel_up();
  463. case SDL_BUTTON_WHEELDOWN:
  464. return MouseButton::wheel_down();
  465. }
  466. tinydisplay_cat.info()
  467. << "unhandled mouse button " << button << "\n";
  468. return ButtonHandle::none();
  469. }
  470. #endif // HAVE_SDL