draw_states.c 6.4 KB

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