Tests About Natural Selection In Virtual Environment
c
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

event.c 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Copyright (C) 2011 Weber Yann, Schuck Clement
  3. This file is part of Tansive.
  4. Tansive is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Tansive is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Tansive. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include"event.h"
  16. /*
  17. Thread function for SDL events handling
  18. */
  19. void* event_catcher_loop(void* arg)
  20. {
  21. t_param_thread_event *param = (t_param_thread_event*)arg;
  22. *(param->again) = 1;
  23. SDL_Event event;
  24. while(*(param->again))
  25. {
  26. bzero(&event,sizeof(SDL_Event));
  27. SDL_PollEvent(&event);
  28. switch(event.type)
  29. {
  30. case SDL_QUIT:
  31. pthread_mutex_lock(param->mutex);
  32. *(param->again) = 0;
  33. pthread_mutex_unlock(param->mutex);
  34. break;
  35. case SDL_MOUSEMOTION:
  36. pthread_mutex_lock(param->mutex);
  37. *(param->mx) = event.motion.x;
  38. *(param->my) = event.motion.y;
  39. pthread_mutex_unlock(param->mutex);
  40. break;
  41. case SDL_MOUSEBUTTONUP:
  42. pthread_mutex_lock(param->mutex);
  43. if (event.button.button == SDL_BUTTON_RIGHT || event.button.button == SDL_BUTTON_WHEELDOWN)
  44. {
  45. if(*(param->zoom) < 3)
  46. {
  47. *(param->x) = *(param->x_resol)/2;
  48. *(param->y) = *(param->y_resol)/2;
  49. }
  50. else
  51. {
  52. *(param->x) -= (*(param->x_resol)/ *(param->zoom))/2;
  53. *(param->y) -= (*(param->y_resol)/ *(param->zoom))/2;
  54. *(param->x) += event.button.x/ *(param->zoom);
  55. *(param->y) += event.button.y/ *(param->zoom);
  56. }
  57. *(param->zoom) = (*(param->zoom)>1?*(param->zoom)-1:1);
  58. }
  59. else if (event.button.button == SDL_BUTTON_LEFT || event.button.button == SDL_BUTTON_WHEELUP)
  60. {
  61. *(param->x) -= (*(param->x_resol)/ *(param->zoom))/2;
  62. *(param->y) -= (*(param->y_resol)/ *(param->zoom))/2;
  63. *(param->x) += event.button.x/ *(param->zoom);
  64. *(param->y) += event.button.y/ *(param->zoom);
  65. *(param->zoom)+=1;
  66. }
  67. pthread_mutex_unlock(param->mutex);
  68. break;
  69. case SDL_KEYDOWN:
  70. pthread_mutex_lock(param->mutex);
  71. switch(event.key.keysym.sym)
  72. {
  73. case SDLK_ESCAPE:
  74. *(param->again) = 0;
  75. break;
  76. case SDLK_UP:
  77. *(param->y)-=1;
  78. *(param->y) = *(param->y)<0?0:*(param->y);
  79. break;
  80. case SDLK_DOWN:
  81. *(param->y)+=1;
  82. *(param->y) = *(param->y)>*(param->y_resol)-1?*(param->y_resol)-1:*(param->y);
  83. break;
  84. case SDLK_RIGHT:
  85. *(param->x)+=1;
  86. *(param->x) = *(param->x)>*(param->x_resol)-1?*(param->x_resol)-1:*(param->x);
  87. break;
  88. case SDLK_LEFT:
  89. *(param->x)-=1;
  90. *(param->x) = *(param->x)<0?0:*(param->x);
  91. break;
  92. }
  93. pthread_mutex_unlock(param->mutex);
  94. break;
  95. }
  96. }
  97. exit(0);
  98. }