PageRenderTime 23ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/engine.c

https://github.com/ocTpoyxuu/albion
C | 268 lines | 204 code | 55 blank | 9 comment | 23 complexity | dd00821bee489914bd57db5c4c8ad3f6 MD5 | raw file
  1. #include <sys/time.h>
  2. #include "engine.h"
  3. // Drawing stuff
  4. int window;
  5. int swap = 0;
  6. // Timing stuff
  7. struct timeval
  8. time,
  9. tmptime,
  10. drawframebase,
  11. controlframebase,
  12. secbit = {1, 0}; // 1 second time bit
  13. int frameps = 0, drawps = 0;
  14. // I/O stuff
  15. char keys[256],
  16. skeys[256],
  17. btn[5];
  18. // Handlers stuff
  19. typedef struct tagHANDLERS {
  20. void (* updateWorld)(float);
  21. void (* resizeWorld)(void);
  22. int (* drawWorld)(void);
  23. void (* destroyWorld)(void);
  24. void (* keyboard)(unsigned char, int, int);
  25. void (* keyboardup)(unsigned char, int, int);
  26. void (* special)(int, int, int);
  27. void (* specialup)(int, int, int);
  28. void (* mouse)(int, int, int, int);
  29. void (* motion)(int, int);
  30. } Handlers;
  31. Handlers handlers = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
  32. void eUpdateWorldFunc(void (*f)(float))
  33. {
  34. handlers.updateWorld = f;
  35. }
  36. void eResizeWorldFunc(void (*f)(void))
  37. {
  38. handlers.resizeWorld = f;
  39. }
  40. void eDrawWorldFunc(int (*f)(void))
  41. {
  42. handlers.drawWorld = f;
  43. }
  44. void eDestroyWorldFunc(void (*f)(void))
  45. {
  46. handlers.destroyWorld = f;
  47. }
  48. void eKeyboardFunc(void (*f)(unsigned char key, int x, int y))
  49. {
  50. handlers.keyboard = f;
  51. }
  52. void eKeyboardUpFunc(void (*f)(unsigned char key, int x, int y))
  53. {
  54. handlers.keyboardup = f;
  55. }
  56. void eSpecialKeyboardFunc(void (*f)(int key, int x, int y))
  57. {
  58. handlers.special = f;
  59. }
  60. void eSpecialKeyboardUpFunc(void (*f)(int key, int x, int y))
  61. {
  62. handlers.specialup = f;
  63. }
  64. void eMouse(void (*f)(int button, int state, int x, int y))
  65. {
  66. handlers.mouse = f;
  67. }
  68. void eMotion(void (*f)(int x, int y))
  69. {
  70. handlers.motion = f;
  71. }
  72. void Redraw()
  73. {
  74. swap = 1;
  75. }
  76. int DrawScene()
  77. {
  78. int ret = 0;
  79. if (handlers.drawWorld != NULL)
  80. ret = (*(handlers.drawWorld))();
  81. return ret;
  82. }
  83. void callDestroyWorld()
  84. {
  85. if (handlers.destroyWorld != NULL)
  86. (*(handlers.destroyWorld))();
  87. }
  88. void DrawGLScene()
  89. {
  90. // Counters
  91. frameps++;
  92. if (swap)
  93. drawps++;
  94. gettimeofday(&time, NULL);
  95. timersub(&time, &drawframebase, &tmptime);
  96. if (timercmp(&tmptime, &secbit, >=))
  97. {
  98. app.fps = frameps*(float)(secbit.tv_sec * 1000000 + secbit.tv_usec) / (tmptime.tv_sec * 1000000 + tmptime.tv_usec);
  99. app.dps = drawps*(float)(secbit.tv_sec * 1000000 + secbit.tv_usec) / (tmptime.tv_sec * 1000000 + tmptime.tv_usec);
  100. frameps = 0;
  101. drawps = 0;
  102. drawframebase = time;
  103. printf("FPS %f DPS %f\n", app.fps, app.dps);
  104. }
  105. if (swap)
  106. {
  107. swap = DrawScene();
  108. glutSwapBuffers();
  109. }
  110. }
  111. void UpdateScene(int value)
  112. {
  113. float dt;
  114. gettimeofday(&time, NULL);
  115. timersub(&time, &controlframebase, &tmptime);
  116. dt = (float)(tmptime.tv_sec*1000000 + tmptime.tv_usec)/(1000000);
  117. // Checking keys
  118. if (keys[K_ESCAPE])
  119. {
  120. glutDestroyWindow(window);
  121. exit(0);
  122. }
  123. // Updating world
  124. if (handlers.updateWorld != NULL)
  125. (*(handlers.updateWorld))(dt);
  126. controlframebase = time;
  127. value++;
  128. if (value == app.ctlfrate)
  129. value = 0;
  130. glutTimerFunc(1000/app.ctlfrate, UpdateScene, value);
  131. }
  132. void ReSizeGLScene(int Width, int Height)
  133. {
  134. if (Height == 0)
  135. Height = 1;
  136. app.portw = Width;
  137. app.porth = Height;
  138. glViewport(0, 0, app.portw, app.porth);
  139. glMatrixMode(GL_PROJECTION);
  140. glLoadIdentity();
  141. gluOrtho2D(0, app.portw, 0, app.porth);
  142. glMatrixMode(GL_MODELVIEW);
  143. if (handlers.resizeWorld)
  144. (*(handlers.resizeWorld))();
  145. swap = 1;
  146. }
  147. void keyboardDown(unsigned char key, int x, int y)
  148. {
  149. keys[key] = 1;
  150. if (handlers.keyboard)
  151. (*(handlers.keyboard))(key, x, y);
  152. }
  153. void keyboardUp(unsigned char key, int x, int y)
  154. {
  155. keys[key] = 0;
  156. if (handlers.keyboardup)
  157. (*(handlers.keyboardup))(key, x, y);
  158. }
  159. void specialKeyDown(int key, int x, int y)
  160. {
  161. skeys[key] = 1;
  162. if (handlers.special)
  163. (*(handlers.special))(key, x, y);
  164. }
  165. void specialKeyUp(int key, int x, int y)
  166. {
  167. skeys[key] = 0;
  168. if (handlers.specialup)
  169. (*(handlers.specialup))(key, x, y);
  170. }
  171. void GLMouse(int button, int state, int x, int y)
  172. {
  173. /*printf("Mouse event at x %d y %d with state %x button %x", x, y, state, button);*/
  174. btn[button] = (state == GLUT_DOWN ? 1 : 0);
  175. /*printf(" btn{%d, %d, %d, %d, %d}\n", btn[0], btn[1], btn[2], btn[3], btn[4]);*/
  176. if (handlers.mouse)
  177. (*(handlers.mouse))(button, state, x, y);
  178. }
  179. void GLMotion(int x, int y)
  180. {
  181. if (handlers.motion)
  182. (*(handlers.motion))(x, y);
  183. }
  184. void InitEngine(int * argc, char ** argv)
  185. {
  186. glutInit(argc, argv);
  187. glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
  188. glutInitWindowSize(app.winw, app.winh);
  189. glutInitWindowPosition(app.winl, app.wint);
  190. window = glutCreateWindow(AppName);
  191. glutDisplayFunc(DrawGLScene);
  192. glutTimerFunc(1000/app.ctlfrate, UpdateScene, 0);
  193. glutIdleFunc(DrawGLScene);
  194. glutReshapeFunc(ReSizeGLScene);
  195. glutKeyboardUpFunc(keyboardUp);
  196. glutKeyboardFunc(keyboardDown);
  197. glutSpecialFunc(specialKeyDown);
  198. glutSpecialUpFunc(specialKeyUp);
  199. glutMouseFunc(GLMouse);
  200. glutMotionFunc(GLMotion);
  201. if (app.fullscreen)
  202. glutFullScreen();
  203. ReSizeGLScene(app.winw, app.winh);
  204. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  205. }
  206. void RunEngine()
  207. {
  208. frameps = 0;
  209. drawps = 0;
  210. gettimeofday(&controlframebase, NULL);
  211. gettimeofday(&drawframebase, NULL);
  212. glutMainLoop();
  213. }