draw_states.c 6.0 KB

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