draw_states.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include "draw_states.h"
  2. #include "fxlib.h"
  3. #include <stdio.h>
  4. // static functions
  5. static void drawPlayer(Camera *cam, int player_angle, int nb_lines, Line_Transition line_transition);
  6. static void drawDiagonals(Camera cam, int nb_lines, Line_Transition line_transition);
  7. static void drawHud(Game_Data *data);
  8. static void drawChrono(Game_Data *data);
  9. static void drawStep(Game_Data *data);
  10. void draw_game(Game_Data *data)
  11. {
  12. //draw the player and the lines
  13. drawPlayer(&(data->cam), data->player_angle, data->nb_lines, data->line_transition);
  14. drawDiagonals(data->cam, data->nb_lines, data->line_transition);
  15. drawHud(data);
  16. //showing the walls
  17. if(data->list != NULL)
  18. drawWalls(data->list, &(data->cam), data->nb_lines, data->line_transition);
  19. }
  20. void draw_title(Game_Data *data)
  21. {
  22. }
  23. void draw_menu(Game_Data *data)
  24. {
  25. }
  26. void draw_game_over(Game_Data *data)
  27. {
  28. }
  29. static void drawChrono(Game_Data *data) {
  30. unsigned char time_text[8] = "";
  31. static const unsigned char time_hud_border[8] = {0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0xC0, 0x80, 0x80};
  32. unsigned short length_of_time, length_of_time_line;
  33. sprintf(time_text, "%.2f", data->chrono_time);
  34. length_of_time = strlen(time_text);
  35. length_of_time_line = 4 * length_of_time;
  36. if(data->chrono_time < 60) {
  37. PrintMini(0, 0, time_text, MINI_REV);
  38. ML_horizontal_line(6, 0, (data->chrono_time/60.) * (length_of_time_line - 2), BLACK);
  39. ML_horizontal_line(7, 0, length_of_time_line - 1, BLACK);
  40. }
  41. else {
  42. PrintMini(0, 1, time_text, MINI_REV);
  43. ML_horizontal_line(6, 0, length_of_time_line - 1, BLACK);
  44. ML_horizontal_line(7, 0, length_of_time_line - 1, BLACK);
  45. }
  46. ML_bmp_8_or(time_hud_border, length_of_time_line, 0);
  47. }
  48. static void drawStep(Game_Data *data) {
  49. unsigned char *step_text[6] = {
  50. "Point",
  51. "Line",
  52. "Triangle",
  53. "Square",
  54. "Pentagon",
  55. "Hexagon"
  56. };
  57. unsigned short step_time[5] = {
  58. 10,
  59. 20,
  60. 30,
  61. 45,
  62. 60
  63. };
  64. unsigned char step_hud_border[8] ={0x1F, 0x1F, 0x0F, 0x0F, 0x07, 0x07, 0x01, 0x01};
  65. unsigned short length_of_step, current_step, i;
  66. current_step = 5;
  67. for(i = 0; i < 5; i++) {
  68. if(data->chrono_time < step_time[i]) {
  69. current_step = i;
  70. break;
  71. }
  72. }
  73. length_of_step = 4 * strlen(step_text[current_step]) + 1;
  74. // Little patch because the font is not fixed width and 'n' chars are annoying me.
  75. ML_vertical_line(125,0,5, BLACK);
  76. ML_vertical_line(126,0,5, BLACK);
  77. ML_vertical_line(127,0,5, BLACK);
  78. PrintMini(127 - length_of_step, 1, step_text[current_step], MINI_REV);
  79. ML_bmp_8_or(step_hud_border, 127 - length_of_step - 8, 0);
  80. ML_horizontal_line(6, 127 - length_of_step - 2, 127, BLACK);
  81. ML_horizontal_line(7, 127 - length_of_step - 2, 127, BLACK);
  82. }
  83. static void drawHud(Game_Data *data) {
  84. drawChrono(data);
  85. drawStep(data);
  86. }
  87. //draws the player
  88. //at first, was supposed to draw an hexagon in the center, plus a triangle to show the player,
  89. //but the hexagon was not visible, and it was a pixel mess, so we're showing a circle instead.
  90. //there is still for code to calculate the vertices of the hexagon, in case we want to change that again
  91. static void drawPlayer(Camera *cam, int player_angle, int nb_lines, Line_Transition line_transition)
  92. {
  93. int x[32];
  94. int y[32];
  95. int i = 0;
  96. int angle = 0;
  97. float tmp_angle = 0.0;
  98. float transition_angle = 0.0;
  99. float delta_angle = 0.0;
  100. if(line_transition.delta_nb_lines == 1)
  101. nb_lines ++;
  102. if(line_transition.counter_start != 0)
  103. transition_angle = (360.0 / (float)nb_lines) * ((float)line_transition.counter / (float)line_transition.counter_start);
  104. delta_angle = 360.0/nb_lines;
  105. do
  106. {
  107. x[i] = (8. + cam->zoom)*cos(PI * (tmp_angle + cam->angle)/180.) + cam->cX;
  108. y[i] = (8. + cam->zoom)*sin(PI * (tmp_angle + cam->angle)/180.) + cam->cY;
  109. i++;
  110. switch(line_transition.delta_nb_lines)
  111. {
  112. case 0:
  113. tmp_angle += delta_angle;
  114. break;
  115. case 1:
  116. if(i < nb_lines)
  117. tmp_angle += (360 - (delta_angle - transition_angle)) / (nb_lines - 1);
  118. else
  119. tmp_angle += delta_angle - transition_angle;
  120. break;
  121. case -1:
  122. if(i < nb_lines)
  123. tmp_angle += (360 - transition_angle) / (nb_lines - 1);
  124. else
  125. tmp_angle = transition_angle;
  126. break;
  127. }
  128. }while(i <= nb_lines);
  129. //draw the aforementionned circle, depending on the camera's center
  130. //ML_filled_circle(cam.cX, cam.cY, 6, BLACK);
  131. ML_polygone(x, y, nb_lines, BLACK);
  132. //draw the player. At such a low scale, it was impossible to draw a rotating triangle, so its a radius 1 circle instead.
  133. 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);
  134. }
  135. //draws one of the three rotating lines
  136. static void drawDiagonals(Camera cam, int nb_lines, Line_Transition line_transition)
  137. {
  138. int tmp_angle = cam.angle;
  139. float tmp_angle_rad = 0.0f;
  140. int i = 0;
  141. float x1 = 0.0f, y1 = 0.0f, x2 = 0.0f, y2 = 0.0f;
  142. float delta_angle = 0.0;
  143. float coeff = 0.0;
  144. float transition_angle = 0.0;
  145. delta_angle = 360.0 / nb_lines;
  146. if(line_transition.delta_nb_lines == 1)
  147. nb_lines ++;
  148. if(line_transition.counter_start != 0)
  149. coeff = (float)line_transition.counter / (float)line_transition.counter_start;
  150. transition_angle = delta_angle * coeff;
  151. do{
  152. tmp_angle_rad = tmp_angle * PI / 180.0f;
  153. x1 = (9.0f + cam.zoom) * cos(tmp_angle_rad);
  154. y1 = (9.0f + cam.zoom) * sin(tmp_angle_rad);
  155. x2 = 64.0f * cos(tmp_angle_rad);
  156. y2 = 64.0f * sin(tmp_angle_rad);
  157. ML_line(x1 + cam.cX, y1 + cam.cY, x2 + cam.cX, y2 + cam.cY, BLACK);
  158. i++;
  159. switch(line_transition.delta_nb_lines){
  160. case 0:
  161. tmp_angle += 360/nb_lines;
  162. break;
  163. case 1:
  164. if(i < nb_lines - 1)
  165. {
  166. tmp_angle += (360 - (delta_angle - transition_angle)) / (nb_lines - 1);
  167. }else{
  168. tmp_angle += delta_angle - transition_angle;
  169. }
  170. break;
  171. case -1:
  172. if(i < nb_lines - 1)
  173. {
  174. tmp_angle += (360 - transition_angle) / (nb_lines - 1);
  175. }else{
  176. tmp_angle += transition_angle;
  177. }
  178. break;
  179. }
  180. if(tmp_angle >= 360)tmp_angle = tmp_angle - 359;
  181. }while(i < nb_lines);
  182. }