hexagon.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. Wall *list = NULL;
  19. //delta angle: defines the 'speed' of the player
  20. float dAngle = 10;
  21. //function prototypes
  22. //draws the player
  23. void drawPlayer();
  24. //draws one of the lines (from 0 to 2)
  25. void drawDiagonal(int nb);
  26. int AddIn_main(int isAppli, unsigned short OptionNum)
  27. {
  28. //variables for fps calculation
  29. unsigned int fps = 0, frame = 0, tempsOrigine = RTC_GetTicks();
  30. //char string to display the fps
  31. unsigned char fps_text[8] = {0};
  32. //rand initialisation
  33. srand(RTC_GetTicks());
  34. //list = addWall(list, 128, 8, 1, 1);
  35. while(KeyUp(K_EXIT)){
  36. //fps
  37. int current_frame_time = RTC_GetTicks();
  38. if(RTC_GetTicks() - tempsOrigine >= 32 )//if 1/4 seconds elapsed
  39. {
  40. fps = frame*4;
  41. frame = 0;
  42. tempsOrigine = RTC_GetTicks();
  43. }
  44. frame++;
  45. //rotating the camera
  46. cam.angle = (int) (cam.angle + dAngle);
  47. if(cam.angle >= 360)cam.angle = cam.angle - 359;
  48. if(list != NULL){ //if the linked list is not empty
  49. update(list); //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. //manually change the rotation speed
  96. //TODO: automatize this
  97. if(KeyDown(K_PLUS))
  98. {
  99. dAngle += 0.2;
  100. }
  101. if(KeyDown(K_MINUS))
  102. {
  103. dAngle -= 0.2;
  104. }
  105. //the angle must not be greater than 360
  106. if(player_angle < 0)
  107. player_angle = 360 + player_angle;
  108. player_angle = player_angle % 360;
  109. if(player_angle > 360)
  110. {
  111. while(KeyUp(K_EXE))
  112. {}
  113. }
  114. //while
  115. //pause
  116. Sleep(1/0.06);
  117. }
  118. return 1;
  119. }
  120. //draws the player
  121. //at first, was supposed to draw an hexagon in the center, plus a triangle to show the player,
  122. //but the hexagon was not visible, and it was a pixel mess, so we're showing a circle instead.
  123. //there is still for code to calculate the vertices of the hexagon, in case we want to change that again
  124. void drawPlayer()
  125. {
  126. /* int x[6];
  127. int y[6];
  128. int i = 0;
  129. for(i = 0; i++; i != 6)
  130. {
  131. int angle = i *60;
  132. x[i] = 16*cos(PI*angle/180) + cam.cX;
  133. y[i] = 16*sin(PI*angle/180) + cam.cY;
  134. }
  135. */
  136. //draw the aforementionned circle, depending on the camera's center
  137. ML_filled_circle(cam.cX, cam.cY, 6, BLACK);
  138. //draw the player. At such a low scale, it was impossible to draw a rotating triangle, so its a radius 1 circle instead.
  139. 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);
  140. }
  141. //draws one of the three rotating lines
  142. void drawDiagonal(int nb)
  143. {
  144. int tmp_angle = 0;
  145. float coeff = 0;
  146. int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  147. //defining the angle between the line and the horizontal axis
  148. if(nb == 1)
  149. tmp_angle = cam.angle + 60;
  150. else if(nb == 2)
  151. tmp_angle = cam.angle;
  152. else if(nb == 3)
  153. tmp_angle = 300 + cam.angle;
  154. if(tmp_angle >= 360)tmp_angle = tmp_angle - 359;
  155. //defining the slope coefficient
  156. coeff = sin(PI*tmp_angle / 180)/cos(PI * tmp_angle / 180);
  157. //ML needs four coordinates in order to draw a line
  158. x1 = -32/coeff;
  159. y1 = -32;
  160. x2 = 32/coeff;
  161. y2 = 32;
  162. if(abs(x1) > 64){
  163. x1 = -64;
  164. y1 = -64*coeff;
  165. x2 = 64;
  166. y2 = 64*coeff;
  167. }
  168. //drawing the line
  169. ML_line(x1 + cam.cX, y1 + cam.cY, x2 + cam.cX, y2 + cam.cY, BLACK);
  170. }
  171. #pragma section _BR_Size
  172. unsigned long BR_Size;
  173. #pragma section
  174. #pragma section _TOP
  175. int InitializeSystem(int isAppli, unsigned short OptionNum)
  176. {
  177. return INIT_ADDIN_APPLICATION(isAppli, OptionNum);
  178. }
  179. #pragma section