hexagon.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include "fxlib.h"
  2. #include "MonochromeLib.h"
  3. #include "math.h"
  4. #include "ECode.h"
  5. #include "stdlib.h"
  6. #include "struct.h"
  7. #include "wall.h"
  8. #include "syscall.h"
  9. #define FPS 60
  10. //duration of a frame
  11. #define FRAME_TIME 1/FPS
  12. //oriented angle between the player and the horizontal axis
  13. int player_angle=0;
  14. Camera cam = {64, 32, 0, 0, 0};
  15. //linked list of obstacles
  16. //at the moment, there is only a list, but at term, we should be using the lists from the Line struct. (And thus remove the "line" member from the Wall struct
  17. Wall *list = NULL;
  18. //delta angle: defines the 'speed' of the camera
  19. //function prototypes
  20. //draws the player
  21. void drawPlayer();
  22. //draws one of the lines (from 0 to 2)
  23. void drawDiagonal(int nb);
  24. void updateCamera(Camera *cam, unsigned int delta_time);
  25. int AddIn_main(int isAppli, unsigned short OptionNum)
  26. {
  27. unsigned int start_time = RTC_GetTicks(); //1 tick == 1/128 second
  28. unsigned int last_time = 0;
  29. unsigned int current_time = RTC_GetTicks();
  30. //variables for fps calculation
  31. unsigned int fps = 0, frame = 0, tempsOrigine = RTC_GetTicks();
  32. //char string to display the fps
  33. unsigned char fps_text[8] = {0};
  34. //rand initialisation
  35. srand(RTC_GetTicks());
  36. //list = addWall(list, 128, 8, 1, 1);
  37. while(KeyUp(K_EXIT)){ //main loop
  38. last_time = current_time;
  39. current_time = RTC_GetTicks();
  40. //fps
  41. if(RTC_GetTicks() - tempsOrigine >= 32 )//if 1/4 seconds elapsed
  42. {
  43. fps = frame*4;
  44. frame = 0;
  45. tempsOrigine = RTC_GetTicks();
  46. }
  47. frame++;
  48. if(list != NULL){ //if the linked list is not empty
  49. update(list, current_time - last_time); //update the linked list
  50. if(isColliding(list, player_angle) == true) //if the player and a wall collide
  51. {
  52. PrintMini(50, 0, "TOUCHE", MINI_OVER); //death handling
  53. }
  54. list = removeWall(list, 0); //remove every wall whose distance to the center equals zero
  55. }
  56. //level generation
  57. //TODO: something else than pure randomness
  58. if(rand() % 40 == 1)//
  59. {
  60. list = addWall(list, 128, 8, 1, rand()%6);
  61. }else if(rand() % 70 == 1)
  62. {
  63. int emptyRow = rand()%6;
  64. int i = 0;
  65. for(i = 0; i < 6; i++)
  66. if(i != emptyRow)
  67. list = addWall(list, 128, 15, 1, i);
  68. }
  69. //draw the player and the lines
  70. drawPlayer();
  71. drawDiagonal(1);
  72. drawDiagonal(2);
  73. drawDiagonal(3);
  74. //printing debug information
  75. intToStr(fps_text, fps);
  76. PrintMini(0, 0, fps_text, MINI_OVER);
  77. intToStr(fps_text, player_angle);
  78. PrintMini(116, 0, fps_text, MINI_OVER);
  79. //showing the walls
  80. if(list != NULL)
  81. show(list, &cam);
  82. //updating the screen
  83. ML_display_vram();
  84. ML_clear_vram();
  85. //player input
  86. //TODO: no magic values
  87. if(KeyDown(K_LEFT))
  88. {
  89. player_angle-=15;
  90. }
  91. if(KeyDown(K_RIGHT))
  92. {
  93. player_angle+=15;
  94. }
  95. updateCamera(&cam, current_time - last_time);
  96. //manually change the rotation speed
  97. //TODO: automatize this
  98. /* if(KeyDown(K_PLUS))
  99. {
  100. dAngle += 0.2;
  101. }
  102. if(KeyDown(K_MINUS))
  103. {
  104. dAngle -= 0.2;
  105. }
  106. */
  107. //the angle must not be greater than 360
  108. if(player_angle < 0)
  109. player_angle = 360 + player_angle;
  110. player_angle = player_angle % 360;
  111. if(player_angle > 360)
  112. {
  113. while(KeyUp(K_EXE))
  114. {}
  115. }
  116. //while
  117. //pause
  118. Sleep(1/0.06);
  119. }
  120. return 1;
  121. }
  122. void updateCamera(Camera *cam, unsigned int delta_time){
  123. cam->speed = 1; //to change
  124. cam->angle += cam->speed * delta_time / 2.;
  125. if(cam->angle >= 360)
  126. cam->angle -= 359;
  127. }
  128. //draws the player
  129. //at first, was supposed to draw an hexagon in the center, plus a triangle to show the player,
  130. //but the hexagon was not visible, and it was a pixel mess, so we're showing a circle instead.
  131. //there is still for code to calculate the vertices of the hexagon, in case we want to change that again
  132. void drawPlayer()
  133. {
  134. int x[6];
  135. int y[6];
  136. int i = 0;
  137. for(i = 0; i<6; ++i)
  138. {
  139. int angle = i *60;
  140. x[i] = (8. + cam.zoom)*cos(PI * (angle + cam.angle)/180.) + cam.cX;
  141. y[i] = (8. + cam.zoom)*sin(PI * (angle + cam.angle)/180.) + cam.cY;
  142. }
  143. //draw the aforementionned circle, depending on the camera's center
  144. //ML_filled_circle(cam.cX, cam.cY, 6, BLACK);
  145. ML_polygone(x, y, 6, BLACK);
  146. //draw the player. At such a low scale, it was impossible to draw a rotating triangle, so its a radius 1 circle instead.
  147. ML_filled_circle((9. + cam.zoom)*cos( PI*(player_angle + cam.angle)/180) + cam.cX, (9. + cam.zoom)*sin( PI*(player_angle+cam.angle)/180) + cam.cY, 1, BLACK);
  148. }
  149. //draws one of the three rotating lines
  150. void drawDiagonal(int nb)
  151. {
  152. int tmp_angle = 0;
  153. float coeff = 0;
  154. int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  155. //defining the angle between the line and the horizontal axis
  156. if(nb == 1)
  157. tmp_angle = cam.angle + 60;
  158. else if(nb == 2)
  159. tmp_angle = cam.angle;
  160. else if(nb == 3)
  161. tmp_angle = 300 + cam.angle;
  162. if(tmp_angle >= 360)tmp_angle = tmp_angle - 359;
  163. //defining the slope coefficient
  164. coeff = sin(PI*tmp_angle / 180)/cos(PI * tmp_angle / 180);
  165. //ML needs four coordinates in order to draw a line
  166. x1 = -32/coeff;
  167. y1 = -32;
  168. x2 = 32/coeff;
  169. y2 = 32;
  170. if(abs(x1) > 64){
  171. x1 = -64;
  172. y1 = -64*coeff;
  173. x2 = 64;
  174. y2 = 64*coeff;
  175. }
  176. //drawing the line
  177. ML_line(x1 + cam.cX, y1 + cam.cY, x2 + cam.cX, y2 + cam.cY, BLACK);
  178. }
  179. #pragma section _BR_Size
  180. unsigned long BR_Size;
  181. #pragma section
  182. #pragma section _TOP
  183. int InitializeSystem(int isAppli, unsigned short OptionNum)
  184. {
  185. return INIT_ADDIN_APPLICATION(isAppli, OptionNum);
  186. }
  187. #pragma section