draw_states.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "draw_states.h"
  2. #include "fxlib.h"
  3. void draw_game(Game_Data *data)
  4. {
  5. //draw the player and the lines
  6. drawPlayer(&(data->cam), data->player_angle);
  7. drawDiagonal(1, data->cam);
  8. drawDiagonal(2, data->cam);
  9. drawDiagonal(3, data->cam);
  10. //showing the walls
  11. if(data->list != NULL)
  12. drawWalls(data->list, &(data->cam));
  13. //showing the HUD
  14. //drawGameHUD(data);
  15. }
  16. void draw_title(Game_Data *data)
  17. {
  18. }
  19. void draw_menu(Game_Data *data)
  20. {
  21. }
  22. void draw_game_over(Game_Data *data)
  23. {
  24. }
  25. int numDigits(unsigned int number)
  26. {
  27. int digits = 0;
  28. if (number <= 0) digits = 1; // remove this line if '-' counts as a digit
  29. while (number) {
  30. number /= 10;
  31. digits++;
  32. }
  33. return digits;
  34. }
  35. //draws HUD
  36. //Shows the time elapsed and the current level
  37. void drawGameHUD(Game_Data *data) {
  38. unsigned char timeBuffer[11] =""; //9 digits + . + \0
  39. unsigned long int delta = RTC_GetTIcks() - data->start_time;
  40. unsigned int centis = ((delta%128)/128.) *100; //calculate 1/100e second deciaml part
  41. unsigned int seconds = delta /128; //calculatr seconds
  42. unsigned char offset = 0; //offset touse to merge strings in the same array
  43. uintToStr(timeBuffer + offset, seconds);
  44. offset += numDigits(seconds);
  45. timeBuffer[offset++] ='.';
  46. uintToStr(timeBuffer + offset, centis);
  47. PrintMini(0, 8, timeBuffer, MINI_OVER);
  48. }
  49. //draws the player
  50. //at first, was supposed to draw an hexagon in the center, plus a triangle to show the player,
  51. //but the hexagon was not visible, and it was a pixel mess, so we're showing a circle instead.
  52. //there is still for code to calculate the vertices of the hexagon, in case we want to change that again
  53. void drawPlayer(Camera *cam, int player_angle)
  54. {
  55. int x[6];
  56. int y[6];
  57. int i = 0;
  58. int angle = 0;
  59. for(i = 0; i<6; ++i)
  60. {
  61. angle = i *60;
  62. x[i] = (8. + cam->zoom)*cos(PI * (angle + cam->angle)/180.) + cam->cX;
  63. y[i] = (8. + cam->zoom)*sin(PI * (angle + cam->angle)/180.) + cam->cY;
  64. }
  65. //draw the aforementionned circle, depending on the camera's center
  66. //ML_filled_circle(cam.cX, cam.cY, 6, BLACK);
  67. ML_polygone(x, y, 6, BLACK);
  68. //draw the player. At such a low scale, it was impossible to draw a rotating triangle, so its a radius 1 circle instead.
  69. 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);
  70. }
  71. //draws one of the three rotating lines
  72. void drawDiagonal(int nb, Camera cam)
  73. {
  74. int tmp_angle = 0;
  75. float coeff = 0;
  76. int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  77. //defining the angle between the line and the horizontal axis
  78. if(nb == 1)
  79. tmp_angle = cam.angle + 60;
  80. else if(nb == 2)
  81. tmp_angle = cam.angle;
  82. else if(nb == 3)
  83. tmp_angle = 300 + cam.angle;
  84. if(tmp_angle >= 360)tmp_angle = tmp_angle - 359;
  85. //defining the slope coefficient
  86. coeff = sin(PI*tmp_angle / 180)/cos(PI * tmp_angle / 180);
  87. //ML needs four coordinates in order to draw a line
  88. x1 = -32/coeff;
  89. y1 = -32;
  90. x2 = 32/coeff;
  91. y2 = 32;
  92. if(abs(x1) > 64){
  93. x1 = -64;
  94. y1 = -64*coeff;
  95. x2 = 64;
  96. y2 = 64*coeff;
  97. }
  98. //drawing the line
  99. ML_line(x1 + cam.cX, y1 + cam.cY, x2 + cam.cX, y2 + cam.cY, BLACK);
  100. }