draw_states.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. static unsigned int length_of_print_string(unsigned char* txt);
  13. static void drawTopLeftCornerText(unsigned char* txt);
  14. static void drawTopRightCornerText(unsigned char* txt);
  15. static void drawBottomLeftCornerText(unsigned char* txt);
  16. static void drawBottomRightCornerText(unsigned char* txt);
  17. static const unsigned char title_spr[] = {0x07, 0xF6, 0x1B, 0xFD, 0xFE, 0xFE, 0x00, 0x00, 0x0F, 0xF6, 0x1B, 0xFD, 0xFE, 0xFF, 0x00, 0x00, 0x0C, 0x06, 0x1B, 0x0D, 0x80, 0xC3, 0x00, 0x00, 0x0F, 0xF6, 0x1B, 0x0D, 0xFE, 0xC3, 0x00, 0x00, 0x0F, 0xF6, 0x1B, 0xFD, 0xFE, 0xFE, 0x00, 0x00, 0x00, 0x36, 0x1B, 0xF9, 0x80, 0xFF, 0x00, 0x00, 0x0F, 0xF7, 0xFB, 0x01, 0xFE, 0xC3, 0x00, 0x00, 0x0F, 0xE3, 0xFB, 0x00, 0xFE, 0xC3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC3, 0x7F, 0xB0, 0xCF, 0xEF, 0xF3, 0xFB, 0xF8, 0xC3, 0x7F, 0xB0, 0xDF, 0xEF, 0xF7, 0xFB, 0xFC, 0xC3, 0x60, 0x30, 0xD8, 0x6C, 0x06, 0x1B, 0x0C, 0xFF, 0x7F, 0xBF, 0xD8, 0x6C, 0x36, 0x1B, 0x0C, 0xFF, 0x7F, 0x9F, 0x9F, 0xEC, 0x36, 0x1B, 0x0C, 0xC3, 0x60, 0x30, 0xDF, 0xEC, 0x36, 0x1B, 0x0C, 0xC3, 0x7F, 0xB0, 0xD8, 0x6F, 0xF7, 0xFB, 0x0C, 0xC3, 0x3F, 0xB0, 0xD8, 0x6F, 0xE7, 0xF3, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x9F, 0xCF, 0xEF, 0xF3, 0xF8, 0x00, 0x00, 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xF8, 0x00, 0x00, 0x60, 0x30, 0xD8, 0x01, 0x86, 0x18, 0x00, 0x00, 0x60, 0x30, 0xDF, 0xE1, 0x86, 0x18, 0x00, 0x00, 0x60, 0x3F, 0xDF, 0xE1, 0x86, 0x18, 0x00, 0x00, 0x60, 0x3F, 0xC0, 0x61, 0x86, 0x18, 0x00, 0x00, 0x7F, 0xB0, 0xDF, 0xEF, 0xF7, 0xF8, 0x00, 0x00, 0x3F, 0xB0, 0xDF, 0xCF, 0xF7, 0xF0, 0x00};
  18. static const unsigned char hex_border_top_left[8] = {0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0xC0, 0x80, 0x80};
  19. static const unsigned char hex_border_top_right[8] ={0x1F, 0x1F, 0x0F, 0x0F, 0x07, 0x07, 0x03, 0x03};
  20. static const unsigned char hex_border_bottom_left[8] = {0x80, 0x80, 0xC0, 0xC0, 0xE0, 0xE0, 0xF0, 0xF0};
  21. static const unsigned char hex_border_bottom_right[8] ={0x03, 0x03, 0x07, 0x07, 0x0F, 0x0F, 0x1F, 0x1F};
  22. void draw_game(Game_Data *data)
  23. {
  24. //draw the player and the lines
  25. drawPlayer(&(data->cam), data->player_angle);
  26. drawPolygon(&(data->cam), data->nb_lines, data->line_transition);
  27. drawDiagonals(data->cam, data->nb_lines, data->line_transition);
  28. //showing the walls
  29. if(data->list != NULL)
  30. drawWalls(data->list, &(data->cam), data->nb_lines, data->line_transition);
  31. drawHud(data);
  32. }
  33. void draw_title(Game_Data *data)
  34. {
  35. ML_bmp_or(title_spr, 12, (64-26)/2, 62, 26);
  36. drawPolygon(&(data->cam), data->nb_lines, data->line_transition);
  37. drawDiagonals(data->cam, data->nb_lines, data->line_transition);
  38. drawBottomLeftCornerText("Press Shift");
  39. drawBottomRightCornerText("By Eiyeron");
  40. }
  41. void draw_menu(Game_Data *data)
  42. {
  43. drawPolygon(&(data->cam), data->nb_lines, data->line_transition);
  44. drawDiagonals(data->cam, data->nb_lines, data->line_transition);
  45. drawTopRightCornerText("WIP Menu. Forget that.");
  46. }
  47. void draw_game_over(Game_Data *data)
  48. {
  49. }
  50. static void drawChrono(Game_Data *data) {
  51. unsigned char time_text[8] = "";
  52. sprintf(time_text, "%.2f", data->chrono_time);
  53. if(data->chrono_time < 60) {
  54. unsigned short length_of_time, length_of_time_line;
  55. length_of_time = strlen(time_text);
  56. length_of_time_line = 4 * length_of_time;
  57. PrintMini(0, 0, time_text, MINI_REV);
  58. ML_horizontal_line(6, 0, (data->chrono_time/60.) * (length_of_time_line - 2), BLACK);
  59. ML_horizontal_line(7, 0, length_of_time_line - 1, BLACK);
  60. ML_bmp_8_or(hex_border_top_left, length_of_time_line, 0);
  61. }
  62. else {
  63. drawTopLeftCornerText(time_text);
  64. }
  65. }
  66. static void drawStep(Game_Data *data) {
  67. unsigned char *step_text[6] = {
  68. "Point",
  69. "Line",
  70. "Triangle",
  71. "Square",
  72. "Pentagon",
  73. "Hexagon"
  74. };
  75. unsigned short step_time[5] = {
  76. 10,
  77. 20,
  78. 30,
  79. 45,
  80. 60
  81. };
  82. unsigned short current_step, i;
  83. current_step = 5;
  84. for(i = 0; i < 5; i++) {
  85. if(data->chrono_time < step_time[i]) {
  86. current_step = i;
  87. break;
  88. }
  89. }
  90. drawTopRightCornerText(step_text[current_step]);
  91. }
  92. static void drawHud(Game_Data *data) {
  93. drawChrono(data);
  94. drawStep(data);
  95. }
  96. static void drawPolygon(Camera *cam, int nb_lines, Line_Transition line_transition) {
  97. int x[32];
  98. int y[32];
  99. int i = 0;
  100. int angle = 0;
  101. float tmp_angle = 0.0;
  102. float transition_angle = 0.0;
  103. float delta_angle = 0.0;
  104. if(line_transition.delta_nb_lines == 1)
  105. nb_lines ++;
  106. if(line_transition.counter_start != 0)
  107. transition_angle = (360.0 / (float)nb_lines) * ((float)line_transition.counter / (float)line_transition.counter_start);
  108. delta_angle = 360.0/nb_lines;
  109. do
  110. {
  111. // TODO : use fixed for only two trig calls?
  112. x[i] = (8. + cam->zoom)*cos(PI * (tmp_angle + cam->angle)/180.) + cam->cX;
  113. y[i] = (8. + cam->zoom)*sin(PI * (tmp_angle + cam->angle)/180.) + cam->cY;
  114. i++;
  115. switch(line_transition.delta_nb_lines)
  116. {
  117. case 0:
  118. tmp_angle += delta_angle;
  119. break;
  120. case 1:
  121. if(i < nb_lines)
  122. tmp_angle += (360 - (delta_angle - transition_angle)) / (nb_lines - 1);
  123. else
  124. tmp_angle += delta_angle - transition_angle;
  125. break;
  126. case -1:
  127. if(i < nb_lines)
  128. tmp_angle += (360 - transition_angle) / (nb_lines - 1);
  129. else
  130. tmp_angle = transition_angle;
  131. break;
  132. }
  133. }while(i <= nb_lines);
  134. //draw the aforementionned circle, depending on the camera's center
  135. //ML_filled_circle(cam.cX, cam.cY, 6, BLACK);
  136. ML_polygone(x, y, nb_lines, BLACK);
  137. //draw the player. At such a low scale, it was impossible to draw a rotating triangle, so its a radius 1 circle instead.
  138. // TODO : Replace it for a quick sprite blit, or unwrapped ML_pixel procedure.
  139. }
  140. //draws the player
  141. //at first, was supposed to draw an hexagon in the center, plus a triangle to show the player,
  142. //but the hexagon was not visible, and it was a pixel mess, so we're showing a circle instead.
  143. //there is still for code to calculate the vertices of the hexagon, in case we want to change that again
  144. static void drawPlayer(Camera *cam, int player_angle)
  145. {
  146. 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);
  147. }
  148. //draws one of the three rotating lines
  149. static void drawDiagonals(Camera cam, int nb_lines, Line_Transition line_transition)
  150. {
  151. fix tmp_angle = FIX(cam.angle);
  152. int i = 0;
  153. fix x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  154. fix delta_angle = 0;
  155. fix coeff = 0;
  156. fix transition_angle = 0;
  157. delta_angle = fdiv(FIX(360), FIX(nb_lines));
  158. if(line_transition.delta_nb_lines == 1)
  159. nb_lines ++;
  160. if(line_transition.counter_start != 0)
  161. coeff = fdiv(FIX(line_transition.counter), FIX(line_transition.counter_start));
  162. transition_angle = fmul(delta_angle, coeff);
  163. do{
  164. x1 = fmul(FIX(9) + ftofix(cam.zoom), fcos(tmp_angle));
  165. y1 = fmul(FIX(9) + ftofix(cam.zoom), fsin(tmp_angle));
  166. x2 = fmul(fcos(tmp_angle), FIX(128));
  167. y2 = fmul(fsin(tmp_angle), FIX(128));
  168. ML_line(UNFIX(x1) + cam.cX, UNFIX(y1) + cam.cY, UNFIX(x2) + cam.cX, UNFIX(y2) + cam.cY, BLACK);
  169. i++;
  170. switch(line_transition.delta_nb_lines){
  171. case 0:
  172. tmp_angle += fdiv(FIX(360), FIX(nb_lines));
  173. break;
  174. case 1:
  175. if(i < nb_lines - 1)
  176. {
  177. tmp_angle += fdiv(FIX(360) - (delta_angle - transition_angle), FIX(nb_lines - 1));
  178. }else{
  179. tmp_angle += delta_angle - transition_angle;
  180. }
  181. break;
  182. case -1:
  183. if(i < nb_lines - 1)
  184. {
  185. tmp_angle += fdiv(FIX(360) - transition_angle, FIX(nb_lines - 1));
  186. }else{
  187. tmp_angle += transition_angle;
  188. }
  189. break;
  190. }
  191. if(tmp_angle >= FIX(360)) tmp_angle = tmp_angle - FIX(359);
  192. }while(i < nb_lines);
  193. }
  194. static unsigned int length_of_print_string(unsigned char* txt) {
  195. // TODO : define
  196. unsigned int text_length = 0;
  197. unsigned int i;
  198. unsigned int current_char_length = 0;
  199. for(i = 0; i < strlen(txt); i++) {
  200. switch(txt[i]) {
  201. // upper case
  202. case 'K':
  203. case 'M':
  204. case 'N':
  205. case 'Q':
  206. case 'W':
  207. current_char_length = 6;
  208. break;
  209. // lower case
  210. case 'i':
  211. current_char_length = 2;
  212. break;
  213. case 'n':
  214. case 'r':
  215. current_char_length = 5;
  216. break;
  217. case 'm':
  218. case 'w':
  219. current_char_length = 6;
  220. break;
  221. //
  222. //
  223. default:
  224. current_char_length = 4;
  225. break;
  226. }
  227. text_length += current_char_length;
  228. }
  229. return text_length;
  230. }
  231. static void drawTopLeftCornerText(unsigned char* txt) {
  232. unsigned int text_length = length_of_print_string(txt);
  233. ML_bmp_8_or(hex_border_top_left, text_length, 0);
  234. ML_horizontal_line(7, 0, text_length, BLACK);
  235. PrintMini(0, 1, txt, MINI_REV);
  236. }
  237. static void drawTopRightCornerText(unsigned char* txt) {
  238. int text_length = length_of_print_string(txt);
  239. int xPosition = 128 - text_length;
  240. ML_bmp_8_or(hex_border_top_right, xPosition - 8, 0);
  241. ML_horizontal_line(7, xPosition, 127, BLACK);
  242. PrintMini(xPosition, 1, txt, MINI_REV);
  243. }
  244. static void drawBottomLeftCornerText(unsigned char* txt) {
  245. unsigned int text_length = length_of_print_string(txt);
  246. ML_bmp_8_or(hex_border_bottom_left, text_length - 1, 56);
  247. ML_horizontal_line(63, 0, text_length, BLACK);
  248. PrintMini(0, 57, txt, MINI_REV);
  249. }
  250. static void drawBottomRightCornerText(unsigned char* txt) {
  251. int text_length = length_of_print_string(txt);
  252. int xPosition = 128 - text_length;
  253. ML_bmp_8_or(hex_border_bottom_right, xPosition - 8, 56);
  254. ML_horizontal_line(63, xPosition, 127, BLACK);
  255. PrintMini(xPosition, 57, txt, MINI_REV);
  256. }