hexagon.c 5.4 KB

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