javascript_main.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*************************************************************************/
  2. /* javascript_main.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* 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 */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include <GL/glut.h>
  30. #include "os_javascript.h"
  31. #include "main/main.h"
  32. #include "io/resource_loader.h"
  33. #include "os/keyboard.h"
  34. OS_JavaScript *os=NULL;
  35. static void _gfx_init(void *ud,bool gl2,int w, int h,bool fs) {
  36. glutInitWindowSize(w, h);
  37. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  38. glutCreateWindow("godot");
  39. }
  40. static void _glut_skey(bool pressed,int key) {
  41. InputEvent ev;
  42. ev.type=InputEvent::KEY;
  43. ev.key.pressed=pressed;
  44. switch(key) {
  45. case GLUT_KEY_F1: ev.key.scancode=KEY_F1; break;
  46. case GLUT_KEY_F2: ev.key.scancode=KEY_F2; break;
  47. case GLUT_KEY_F3: ev.key.scancode=KEY_F3; break;
  48. case GLUT_KEY_F4: ev.key.scancode=KEY_F4; break;
  49. case GLUT_KEY_F5: ev.key.scancode=KEY_F5; break;
  50. case GLUT_KEY_F6: ev.key.scancode=KEY_F6; break;
  51. case GLUT_KEY_F7: ev.key.scancode=KEY_F7; break;
  52. case GLUT_KEY_F8: ev.key.scancode=KEY_F8; break;
  53. case GLUT_KEY_F9: ev.key.scancode=KEY_F9; break;
  54. case GLUT_KEY_F10: ev.key.scancode=KEY_F10; break;
  55. case GLUT_KEY_F11: ev.key.scancode=KEY_F11; break;
  56. case GLUT_KEY_F12: ev.key.scancode=KEY_F12; break;
  57. case GLUT_KEY_LEFT: ev.key.scancode=KEY_LEFT; break;
  58. case GLUT_KEY_UP: ev.key.scancode=KEY_UP; break;
  59. case GLUT_KEY_RIGHT: ev.key.scancode=KEY_RIGHT; break;
  60. case GLUT_KEY_DOWN: ev.key.scancode=KEY_DOWN; break;
  61. case GLUT_KEY_PAGE_UP: ev.key.scancode=KEY_PAGEUP; break;
  62. case GLUT_KEY_PAGE_DOWN: ev.key.scancode=KEY_PAGEDOWN; break;
  63. case GLUT_KEY_HOME: ev.key.scancode=KEY_HOME; break;
  64. case GLUT_KEY_END: ev.key.scancode=KEY_END; break;
  65. case GLUT_KEY_INSERT: ev.key.scancode=KEY_INSERT; break;
  66. }
  67. uint32_t m = glutGetModifiers();
  68. ev.key.mod.alt=(m&GLUT_ACTIVE_ALT)!=0;
  69. ev.key.mod.shift=(m&GLUT_ACTIVE_SHIFT)!=0;
  70. ev.key.mod.control=(m&GLUT_ACTIVE_CTRL)!=0;
  71. os->push_input(ev);
  72. }
  73. static void _glut_skey_up(int key, int x, int y) {
  74. _glut_skey(false,key);
  75. }
  76. static void _glut_skey_down(int key, int x, int y) {
  77. _glut_skey(true,key);
  78. }
  79. static void _glut_key(bool pressed,unsigned char key) {
  80. InputEvent ev;
  81. ev.type=InputEvent::KEY;
  82. ev.key.pressed=pressed;
  83. switch(key) {
  84. case '\n': ev.key.scancode=KEY_RETURN; break;
  85. case 0x1b: ev.key.scancode=KEY_ESCAPE; break;
  86. case 8: ev.key.scancode=KEY_BACKSPACE; break;
  87. case 0x7f: ev.key.scancode=KEY_DELETE; break;
  88. case 0x20: ev.key.scancode=KEY_SPACE; ev.key.unicode=key; break;
  89. default: {
  90. ev.key.unicode=key;
  91. }
  92. }
  93. uint32_t m = glutGetModifiers();
  94. ev.key.mod.alt=(m&GLUT_ACTIVE_ALT)!=0;
  95. ev.key.mod.shift=(m&GLUT_ACTIVE_SHIFT)!=0;
  96. ev.key.mod.control=(m&GLUT_ACTIVE_CTRL)!=0;
  97. os->push_input(ev);
  98. }
  99. static void _glut_key_up(unsigned char key, int x, int y) {
  100. _glut_key(false,key);
  101. }
  102. static void _glut_key_down(unsigned char key, int x, int y) {
  103. _glut_key(true,key);
  104. }
  105. static uint32_t _mouse_button_mask=0;
  106. static void _glut_mouse_button(int button, int state, int x, int y) {
  107. InputEvent ev;
  108. ev.type=InputEvent::MOUSE_BUTTON;
  109. switch(button) {
  110. case GLUT_LEFT_BUTTON: ev.mouse_button.button_index=BUTTON_LEFT; break;
  111. case GLUT_MIDDLE_BUTTON: ev.mouse_button.button_index=BUTTON_MIDDLE; break;
  112. case GLUT_RIGHT_BUTTON: ev.mouse_button.button_index=BUTTON_RIGHT; break;
  113. case 3: ev.mouse_button.button_index=BUTTON_WHEEL_UP; break;
  114. case 4: ev.mouse_button.button_index=BUTTON_WHEEL_DOWN; break;
  115. }
  116. ev.mouse_button.pressed=state==GLUT_DOWN;
  117. ev.mouse_button.x=x;
  118. ev.mouse_button.y=y;
  119. ev.mouse_button.global_x=x;
  120. ev.mouse_button.global_y=y;
  121. if (ev.mouse_button.button_index<4) {
  122. if (ev.mouse_button.pressed) {
  123. _mouse_button_mask|=1<<ev.mouse_button.button_index;
  124. } else {
  125. _mouse_button_mask&=~(1<<ev.mouse_button.button_index);
  126. }
  127. }
  128. uint32_t m = glutGetModifiers();
  129. ev.mouse_button.mod.alt=(m&GLUT_ACTIVE_ALT)!=0;
  130. ev.mouse_button.mod.shift=(m&GLUT_ACTIVE_SHIFT)!=0;
  131. ev.mouse_button.mod.control=(m&GLUT_ACTIVE_CTRL)!=0;
  132. os->push_input(ev);
  133. }
  134. static int _glut_prev_x=0;
  135. static int _glut_prev_y=0;
  136. static void _glut_mouse_motion(int x, int y) {
  137. InputEvent ev;
  138. ev.type=InputEvent::MOUSE_MOTION;
  139. ev.mouse_motion.button_mask=_mouse_button_mask;
  140. ev.mouse_motion.x=x;
  141. ev.mouse_motion.y=y;
  142. ev.mouse_motion.global_x=x;
  143. ev.mouse_motion.global_y=y;
  144. ev.mouse_motion.relative_x=x-_glut_prev_x;
  145. ev.mouse_motion.relative_y=y-_glut_prev_y;
  146. _glut_prev_x=x;
  147. _glut_prev_y=y;
  148. uint32_t m = glutGetModifiers();
  149. ev.mouse_motion.mod.alt=(m&GLUT_ACTIVE_ALT)!=0;
  150. ev.mouse_motion.mod.shift=(m&GLUT_ACTIVE_SHIFT)!=0;
  151. ev.mouse_motion.mod.control=(m&GLUT_ACTIVE_CTRL)!=0;
  152. os->push_input(ev);
  153. }
  154. static void _gfx_idle() {
  155. glutPostRedisplay();
  156. }
  157. static void _godot_draw(void) {
  158. os->main_loop_iterate();
  159. glutSwapBuffers();
  160. }
  161. int main(int argc, char *argv[]) {
  162. /* Initialize the window */
  163. printf("let it go!\n");
  164. glutInit(&argc, argv);
  165. os = new OS_JavaScript(_gfx_init,NULL,NULL,NULL,NULL);
  166. #if 0
  167. char *args[]={"-test","gui","-v",NULL};
  168. Error err = Main::setup("apk",3,args);
  169. #else
  170. // char *args[]={"-v",NULL};//
  171. // Error err = Main::setup("",1,args);
  172. Error err = Main::setup("",0,NULL);
  173. #endif
  174. ResourceLoader::set_abort_on_missing_resources(false); //ease up compatibility
  175. Main::start();
  176. glutSpecialUpFunc(_glut_skey_up);
  177. glutSpecialFunc(_glut_skey_down);
  178. glutKeyboardUpFunc(_glut_key_up);
  179. glutKeyboardFunc(_glut_key_down);
  180. glutMouseFunc(_glut_mouse_button);
  181. glutMotionFunc(_glut_mouse_motion);
  182. glutMotionFunc(_glut_mouse_motion);
  183. glutPassiveMotionFunc(_glut_mouse_motion);
  184. /* Set up glut callback functions */
  185. glutIdleFunc (_gfx_idle);
  186. // glutReshapeFunc(gears_reshape);
  187. glutDisplayFunc(_godot_draw);
  188. //glutSpecialFunc(gears_special);
  189. os->main_loop_begin();
  190. glutMainLoop();
  191. return 0;
  192. }
  193. /*
  194. *
  195. *09] <azakai|2__> reduz: yes, define TOTAL_MEMORY on Module. for example var Module = { TOTAL_MEMORY: 12345.. }; before the main
  196. *
  197. */