hexagon.c 5.5 KB

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