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.

visu.c 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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"visu.h"
  16. void rasterCircleRGBA(SDL_Surface* surface,int x0, int y0, int radius, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  17. {
  18. int f = 1 - radius;
  19. int ddF_x = 1;
  20. int ddF_y = -2 * radius;
  21. int x = 0;
  22. int y = radius;
  23. setPixelRGBA(surface,x0, y0 + radius,r,g,b,a);
  24. setPixelRGBA(surface,x0, y0 - radius,r,g,b,a);
  25. setPixelRGBA(surface,x0 + radius, y0,r,g,b,a);
  26. setPixelRGBA(surface,x0 - radius, y0,r,g,b,a);
  27. while(x < y)
  28. {
  29. if(f >= 0)
  30. {
  31. y--;
  32. ddF_y += 2;
  33. f += ddF_y;
  34. }
  35. x++;
  36. ddF_x += 2;
  37. f += ddF_x;
  38. setPixelRGBA(surface,x0 + x, y0 + y,r,g,b,a);
  39. setPixelRGBA(surface,x0 - x, y0 + y,r,g,b,a);
  40. setPixelRGBA(surface,x0 + x, y0 - y,r,g,b,a);
  41. setPixelRGBA(surface,x0 - x, y0 - y,r,g,b,a);
  42. setPixelRGBA(surface,x0 + y, y0 + x,r,g,b,a);
  43. setPixelRGBA(surface,x0 - y, y0 + x,r,g,b,a);
  44. setPixelRGBA(surface,x0 + y, y0 - x,r,g,b,a);
  45. setPixelRGBA(surface,x0 - y, y0 - x,r,g,b,a);
  46. }
  47. }
  48. void rasterCircleRGB(SDL_Surface* surface,int x0, int y0, int radius, Uint8 r, Uint8 g, Uint8 b)
  49. {
  50. rasterCircleRGBA(surface, x0,y0,radius,r,g,b,255);
  51. }
  52. void setPixel(SDL_Surface* surface, int x, int y, Uint32 pixel)
  53. {
  54. if(x >= surface->w || y >= surface->h || x < 0 || y < 0)
  55. {
  56. #ifdef DEBUG
  57. fprintf(stderr, "Out of range\n\tx=%d\ty=%d\n",x,y);
  58. #endif
  59. }
  60. else
  61. {
  62. int BPP = surface->format->BytesPerPixel;
  63. Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * BPP;
  64. switch(BPP)
  65. {
  66. case 1:
  67. *p = pixel;
  68. break;
  69. case 2:
  70. *(Uint16 *)p = pixel;
  71. break;
  72. case 3:
  73. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  74. {
  75. p[0] = (pixel >> 16) & 0xff;
  76. p[1] = (pixel >> 8) & 0xff;
  77. p[2] = pixel & 0xff;
  78. }
  79. else
  80. {
  81. p[0] = pixel & 0xff;
  82. p[1] = (pixel >> 8) & 0xff;
  83. p[2] = (pixel >> 16) & 0xff;
  84. }
  85. break;
  86. case 4:
  87. *(Uint32 *)p = pixel;
  88. break;
  89. }
  90. }
  91. }
  92. void setPixelRGB(SDL_Surface* surface, int x, int y, Uint8 r, Uint8 g, Uint8 b)
  93. {
  94. Uint32 pixel = SDL_MapRGB(surface->format, r,g,b);
  95. setPixel(surface, x, y, pixel);
  96. }
  97. void setPixelRGBA(SDL_Surface* surface, int x, int y, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  98. {
  99. Uint32 pixel = SDL_MapRGBA(surface->format, r,g,b,a);
  100. setPixel(surface, x, y, pixel);
  101. }
  102. void setPixelRGBP(SDL_Surface* surface, int x, int y, Uint8 r, Uint8 g, Uint8 b, float p)
  103. {
  104. Uint8 a = (int)(p*255.0);
  105. Uint32 pixel = SDL_MapRGBA(surface->format, r,g,b,a);
  106. setPixel(surface, x, y, pixel);
  107. }
  108. void draw_map(SDL_Surface* screen, SDL_Surface* surface, t_map* mp)
  109. {
  110. int j,i;
  111. for(i=0;i<mp->x;i++)
  112. {
  113. for(j=0;j<mp->y;j++)
  114. {
  115. if(mp->map[i][j].type == MAP_ELT_VEGET)
  116. setPixelRGB(surface, i,j,0,255,0);
  117. else if(mp->map[i][j].type == MAP_ELT_CREAT)
  118. {
  119. rasterCircleRGB(surface, i, j, mp->map[i][j].creat->genome_tree->gnome.perception,255 ,255 , 0);
  120. setPixelRGB(surface, i,j,255,0,0);
  121. }
  122. }
  123. }
  124. }
  125. void draw_map_zoom(SDL_Surface* screen, SDL_Surface* surface, t_map* mp, int xc, int yc, int zoom)
  126. {
  127. int x1 = xc-(mp->x/zoom)/2, y1 = yc-(mp->y/zoom)/2, x2 = xc+(mp->x/zoom)/2, y2 = yc+(mp->y/zoom)/2;
  128. int i,j,k = 0,l = 0, m,n;
  129. if(x1<0)
  130. {
  131. x1*=-1;
  132. x2+=x1;
  133. x1=0;
  134. }
  135. else if(x2>mp->x-1)
  136. {
  137. x1-=x2-mp->x-1;
  138. x2=mp->x-1;
  139. }
  140. if(y1<0)
  141. {
  142. y1*=-1;
  143. y2+=y1;
  144. y1=0;
  145. }
  146. else if(y2>mp->y-1)
  147. {
  148. y1-=y2-mp->y-1;
  149. y2=mp->y-1;
  150. }
  151. l=0;
  152. for(j=y1;j<y2; j++)
  153. {
  154. k = 0;
  155. for(i=x1; i<x2; i++)
  156. {
  157. if(mp->map[i][j].type == MAP_ELT_VEGET)
  158. {
  159. /*for(n=l; n<l+zoom; n++)
  160. {
  161. for(m=k; m<k+zoom; m++)
  162. {
  163. setPixelRGB(surface,m,n,0,255,0);
  164. }
  165. }*/
  166. rasterCircleRGB(surface, k+zoom/2,l+zoom/2, zoom/2, 0,255,0);
  167. }
  168. else if(mp->map[i][j].type == MAP_ELT_CREAT)
  169. {
  170. /*for(n=l; n<l+zoom; n++)
  171. {
  172. for(m=k; m<k+zoom; m++)
  173. {
  174. setPixelRGB(surface,m,n,255,0,0);
  175. }
  176. }*/
  177. rasterCircleRGBA(surface,k+zoom/2, l+zoom/2, mp->map[i][j].creat->genome_tree->gnome.perception*zoom,255 ,255 , 0,128);
  178. rasterCircleRGB(surface, k+zoom/2,l+zoom/2, zoom/2, 255,0,0);
  179. }
  180. k+= zoom;
  181. }
  182. l+= zoom;
  183. }
  184. //SDL_BlitSurface(surface, NULL, screen, &bckgrndPos);
  185. }
  186. void draw_zoom_area(SDL_Surface* screen, SDL_Surface* surface, int xc, int yc, t_map* mp)
  187. {
  188. int zoom = 3;
  189. int x1 = xc-(mp->x/zoom)/2, y1 = yc-(mp->y/zoom)/2, x2 = xc+(mp->x/zoom)/2, y2 = yc+(mp->y/zoom)/2;
  190. int i,j;
  191. if(x1<0)
  192. {
  193. x1*=-1;
  194. x2+=x1;
  195. x1=0;
  196. }
  197. else if(x2>mp->x-1)
  198. {
  199. x1-=x2-mp->x-1;
  200. x2=mp->x-1;
  201. }
  202. if(y1<0)
  203. {
  204. y1*=-1;
  205. y2+=y1;
  206. y1=0;
  207. }
  208. else if(y2>mp->y-1)
  209. {
  210. y1-=y2-mp->y-1;
  211. y2=mp->y-1;
  212. }
  213. j=y1;
  214. for(i=x1; i<x2; i++)
  215. setPixelRGBP(surface,i,j,0,0,255,0.5);
  216. j=y2;
  217. for(i=x1; i<x2; i++)
  218. setPixelRGBP(surface,i,j,0,0,255,0.5);
  219. i=x1;
  220. for(j=y1;j<y2;j++)
  221. setPixelRGBP(surface,i,j,0,0,255,0.5);
  222. i=x2;
  223. for(j=y1;j<y2;j++)
  224. setPixelRGBP(surface,i,j,0,0,255,0.5);
  225. }