hexagon.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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};
  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 player
  19. float dAngle = 10;
  20. //function prototypes
  21. //draws the player
  22. void drawPlayer();
  23. //draws one of the lines (from 0 to 2)
  24. void drawDiagonal(int nb);
  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. //rotating the camera
  49. cam.angle = (int) (cam.angle + dAngle);
  50. if(cam.angle >= 360)cam.angle = cam.angle - 359;
  51. if(list != NULL){ //if the linked list is not empty
  52. update(list, current_time - last_time); //update the linked list
  53. if(isColliding(list, player_angle) == true) //if the player and a wall collide
  54. {
  55. PrintMini(50, 0, "TOUCHE", MINI_OVER); //death handling
  56. }
  57. list = removeWall(list, 0); //remove every wall whose distance to the center equals zero
  58. }
  59. //level generation
  60. //TODO: something else than pure randomness
  61. if(rand() % 40 == 1)//
  62. {
  63. list = addWall(list, 128, 8, 1, rand()%6);
  64. }else if(rand() % 70 == 1)
  65. {
  66. int emptyRow = rand()%6;
  67. int i = 0;
  68. for(i = 0; i < 6; i++)
  69. if(i != emptyRow)
  70. list = addWall(list, 128, 15, 1, i);
  71. }
  72. //draw the player and the lines
  73. drawPlayer();
  74. drawDiagonal(1);
  75. drawDiagonal(2);
  76. drawDiagonal(3);
  77. //printing debug information
  78. intToStr(fps_text, fps);
  79. PrintMini(0, 0, fps_text, MINI_OVER);
  80. intToStr(fps_text, player_angle);
  81. PrintMini(116, 0, fps_text, MINI_OVER);
  82. //showing the walls
  83. if(list != NULL)
  84. show(list, &cam);
  85. //updating the screen
  86. ML_display_vram();
  87. ML_clear_vram();
  88. //player input
  89. //TODO: no magic values
  90. if(KeyDown(K_LEFT))
  91. {
  92. player_angle-=15;
  93. }
  94. if(KeyDown(K_RIGHT))
  95. {
  96. player_angle+=15;
  97. }
  98. //manually change the rotation speed
  99. //TODO: automatize this
  100. if(KeyDown(K_PLUS))
  101. {
  102. dAngle += 0.2;
  103. }
  104. if(KeyDown(K_MINUS))
  105. {
  106. dAngle -= 0.2;
  107. }
  108. //the angle must not be greater than 360
  109. if(player_angle < 0)
  110. player_angle = 360 + player_angle;
  111. player_angle = player_angle % 360;
  112. if(player_angle > 360)
  113. {
  114. while(KeyUp(K_EXE))
  115. {}
  116. }
  117. //while
  118. //pause
  119. Sleep(1/0.06);
  120. }
  121. return 1;
  122. }
  123. //draws the player
  124. //at first, was supposed to draw an hexagon in the center, plus a triangle to show the player,
  125. //but the hexagon was not visible, and it was a pixel mess, so we're showing a circle instead.
  126. //there is still for code to calculate the vertices of the hexagon, in case we want to change that again
  127. void drawPlayer()
  128. {
  129. /* int x[6];
  130. int y[6];
  131. int i = 0;
  132. for(i = 0; i++; i != 6)
  133. {
  134. int angle = i *60;
  135. x[i] = 16*cos(PI*angle/180) + cam.cX;
  136. y[i] = 16*sin(PI*angle/180) + cam.cY;
  137. }
  138. */
  139. //draw the aforementionned circle, depending on the camera's center
  140. ML_filled_circle(cam.cX, cam.cY, 6, BLACK);
  141. //draw the player. At such a low scale, it was impossible to draw a rotating triangle, so its a radius 1 circle instead.
  142. ML_filled_circle(9*cos( PI*(player_angle + cam.angle)/180) + cam.cX, 9*sin( PI*(player_angle+cam.angle)/180) + cam.cY, 1, BLACK);
  143. }
  144. //draws one of the three rotating lines
  145. void drawDiagonal(int nb)
  146. {
  147. int tmp_angle = 0;
  148. float coeff = 0;
  149. int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  150. //defining the angle between the line and the horizontal axis
  151. if(nb == 1)
  152. tmp_angle = cam.angle + 60;
  153. else if(nb == 2)
  154. tmp_angle = cam.angle;
  155. else if(nb == 3)
  156. tmp_angle = 300 + cam.angle;
  157. if(tmp_angle >= 360)tmp_angle = tmp_angle - 359;
  158. //defining the slope coefficient
  159. coeff = sin(PI*tmp_angle / 180)/cos(PI * tmp_angle / 180);
  160. //ML needs four coordinates in order to draw a line
  161. x1 = -32/coeff;
  162. y1 = -32;
  163. x2 = 32/coeff;
  164. y2 = 32;
  165. if(abs(x1) > 64){
  166. x1 = -64;
  167. y1 = -64*coeff;
  168. x2 = 64;
  169. y2 = 64*coeff;
  170. }
  171. //drawing the line
  172. ML_line(x1 + cam.cX, y1 + cam.cY, x2 + cam.cX, y2 + cam.cY, BLACK);
  173. }
  174. #pragma section _BR_Size
  175. unsigned long BR_Size;
  176. #pragma section
  177. #pragma section _TOP
  178. int InitializeSystem(int isAppli, unsigned short OptionNum)
  179. {
  180. return INIT_ADDIN_APPLICATION(isAppli, OptionNum);
  181. }
  182. #pragma section