draw_states.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #include "draw_states.h"
  2. #include "fxlib.h"
  3. #include "fixed.h"
  4. #include <stdio.h>
  5. // static functions
  6. static void fillBackground(Game_Data *data);
  7. static void drawPlayer(Game_Data *data, int player_angle);
  8. static void drawPolygon(Game_Data *data, int nb_lines, Line_Transition line_transition);
  9. static void drawDiagonals(Game_Data *data, int nb_lines, Line_Transition line_transition);
  10. static void drawHud(Game_Data *data);
  11. static void drawChrono(Game_Data *data);
  12. static void drawStep(Game_Data *data);
  13. static unsigned int length_of_print_string(unsigned char* txt);
  14. static void drawTopLeftCornerText(unsigned char* txt, unsigned char isReversed);
  15. static void drawTopRightCornerText(unsigned char* txt, unsigned char isReversed);
  16. static void drawBottomLeftCornerText(unsigned char* txt, unsigned char isReversed);
  17. static void drawBottomRightCornerText(unsigned char* txt, unsigned char isReversed);
  18. 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};
  19. static const unsigned char hex_border_top_left[8] = {0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0xC0, 0x80, 0x80};
  20. static const unsigned char hex_border_top_right[8] ={0x1F, 0x1F, 0x0F, 0x0F, 0x07, 0x07, 0x03, 0x03};
  21. static const unsigned char hex_border_bottom_left[8] = {0x80, 0x80, 0xC0, 0xC0, 0xE0, 0xE0, 0xF0, 0xF0};
  22. static const unsigned char hex_border_bottom_right[8] ={0x03, 0x03, 0x07, 0x07, 0x0F, 0x0F, 0x1F, 0x1F};
  23. static const unsigned char hex_border_top_left_rev[8] = {0x0F, 0x0F, 0x1F, 0x1F, 0x3F, 0x3F, 0x7F, 0x7F};
  24. static const unsigned char hex_border_top_right_rev[8] ={0xE0, 0xE0, 0xF0, 0xF0, 0xF8, 0xF8, 0xFC, 0xFC};
  25. static const unsigned char hex_border_bottom_left_rev[8] = {0x7F, 0x7F, 0x3F, 0x3F, 0x1F, 0x1F, 0x0F, 0x0F};
  26. static const unsigned char hex_border_bottom_right_rev[8] ={0xFC, 0xFC, 0xF8, 0xF8, 0xF0, 0xF0, 0xE0, 0xE0};
  27. void draw_game(Game_Data *data)
  28. {
  29. fillBackground(data);
  30. // draw the player and the lines
  31. drawPlayer(data, data->player_angle);
  32. drawPolygon(data, data->nb_lines, data->line_transition);
  33. drawDiagonals(data, data->nb_lines, data->line_transition);
  34. // showing the walls
  35. if(data->list != NULL)
  36. drawWalls(data->list, data, data->nb_lines, data->line_transition);
  37. drawHud(data);
  38. }
  39. void draw_title(Game_Data *data)
  40. {
  41. fillBackground(data);
  42. ML_bmp_or(title_spr, 12, (64-26)/2, 62, 26);
  43. drawPolygon(data, data->nb_lines, data->line_transition);
  44. drawDiagonals(data, data->nb_lines, data->line_transition);
  45. drawBottomLeftCornerText("Press Shift", data->are_colors_reversed);
  46. drawBottomRightCornerText("By Eiyeron", data->are_colors_reversed);
  47. }
  48. void draw_menu(Game_Data *data)
  49. {
  50. char buffer[20];
  51. sprintf(buffer, "%i", data->cam.angle);
  52. fillBackground(data);
  53. drawPolygon(data, data->nb_lines, data->line_transition);
  54. drawDiagonals(data, data->nb_lines, data->line_transition);
  55. drawTopRightCornerText(data->entry_difficulties[data->current_entry - 1], data->are_colors_reversed);
  56. if(data->current_entry > 3) {
  57. // Hyper Mode
  58. drawBottomRightCornerText("Hyper Mode", data->are_colors_reversed);
  59. }
  60. }
  61. void draw_game_over(Game_Data *data)
  62. {
  63. }
  64. static void fillBackground(Game_Data *data) {
  65. if(data->are_colors_reversed) {
  66. int i, end, *pointer_long, vram;
  67. char *pointer_byte;
  68. vram = (int)ML_vram_adress();
  69. end = 4-vram&3;
  70. pointer_byte = (char*)vram;
  71. for(i=0 ; i<end ; i++) pointer_byte[i] = 0xFF;
  72. pointer_long = (int*) (vram+end);
  73. for(i=0 ; i<255 ; i++) pointer_long[i] = 0xFFFFFFFF;
  74. pointer_byte += 1020+end;
  75. end = vram&3;
  76. for(i=0 ; i<end ; i++) pointer_byte[i] = 0xFF;
  77. } else {
  78. ML_clear_vram();
  79. }
  80. }
  81. static void drawChrono(Game_Data *data) {
  82. unsigned char time_text[32] = "";
  83. sprintf(time_text, "%.2f", data->chrono_time);
  84. if(data->chrono_time < 60) {
  85. // Drawing the text with the little progress bar. As I offset the text,
  86. // I need to remake the whole routine.
  87. ML_Color drawing_color = data->are_colors_reversed ? WHITE : BLACK;
  88. unsigned int text_color = data->are_colors_reversed ? MINI_OVER : MINI_REV;
  89. unsigned short length_of_time, length_of_time_line;
  90. length_of_time = strlen(time_text);
  91. length_of_time_line = 4 * length_of_time;
  92. PrintMini(0, 0, time_text, text_color);
  93. ML_horizontal_line(6, 0, (data->chrono_time/60.) * (length_of_time_line - 2), drawing_color);
  94. ML_horizontal_line(7, 0, length_of_time_line - 1, drawing_color);
  95. if(!data->are_colors_reversed) {
  96. ML_bmp_8_or(hex_border_top_left, length_of_time_line, 0);
  97. } else {
  98. ML_bmp_8_and(hex_border_top_left_rev, length_of_time_line, 0);
  99. }
  100. }
  101. else {
  102. drawTopLeftCornerText(time_text, data->are_colors_reversed);
  103. }
  104. }
  105. static void drawStep(Game_Data *data) {
  106. unsigned char *step_text[6] = {
  107. "Point",
  108. "Line",
  109. "Triangle",
  110. "Square",
  111. "Pentagon",
  112. "Hexagon"
  113. };
  114. unsigned short step_time[5] = {
  115. 10,
  116. 20,
  117. 30,
  118. 45,
  119. 60
  120. };
  121. unsigned short current_step, i;
  122. current_step = 5;
  123. for(i = 0; i < 5; i++) {
  124. if(data->chrono_time < step_time[i]) {
  125. current_step = i;
  126. break;
  127. }
  128. }
  129. drawTopRightCornerText(step_text[current_step], data->are_colors_reversed);
  130. }
  131. static void drawHud(Game_Data *data) {
  132. drawChrono(data);
  133. drawStep(data);
  134. }
  135. static void drawPolygon(Game_Data *data, int nb_lines, Line_Transition line_transition) {
  136. int x[32];
  137. int y[32];
  138. int i = 0;
  139. int angle = 0;
  140. Camera* cam= &data->cam;
  141. ML_Color drawing_color = data->are_colors_reversed ? WHITE : BLACK;
  142. float tmp_angle = 0.0;
  143. float transition_angle = 0.0;
  144. float delta_angle = 0.0;
  145. if(line_transition.delta_nb_lines == 1)
  146. nb_lines ++;
  147. if(line_transition.counter_start != 0)
  148. transition_angle = (360.0 / (float)nb_lines) * ((float)line_transition.counter / (float)line_transition.counter_start);
  149. delta_angle = 360.0/nb_lines;
  150. do
  151. {
  152. // TODO : use fixed for only two trig calls?
  153. x[i] = (8. + cam->zoom)*cos(PI * (tmp_angle + cam->angle)/180.) + cam->cX;
  154. y[i] = (8. + cam->zoom)*sin(PI * (tmp_angle + cam->angle)/180.) + cam->cY;
  155. i++;
  156. switch(line_transition.delta_nb_lines)
  157. {
  158. case 0:
  159. tmp_angle += delta_angle;
  160. break;
  161. case 1:
  162. if(i < nb_lines)
  163. tmp_angle += (360 - (delta_angle - transition_angle)) / (nb_lines - 1);
  164. else
  165. tmp_angle += delta_angle - transition_angle;
  166. break;
  167. case -1:
  168. if(i < nb_lines)
  169. tmp_angle += (360 - transition_angle) / (nb_lines - 1);
  170. else
  171. tmp_angle = transition_angle;
  172. break;
  173. }
  174. }while(i <= nb_lines);
  175. // draw the aforementionned circle, depending on the camera's center
  176. // ML_filled_circle(cam.cX, cam.cY, 6, BLACK);
  177. ML_polygone(x, y, nb_lines, drawing_color);
  178. // draw the player. At such a low scale, it was impossible to draw a rotating triangle, so its a radius 1 circle instead.
  179. // TODO : Replace it for a quick sprite blit, or unwrapped ML_pixel procedure.
  180. }
  181. // draws the player
  182. // at first, was supposed to draw an hexagon in the center, plus a triangle to show the player,
  183. // but the hexagon was not visible, and it was a pixel mess, so we're showing a circle instead.
  184. // there is still for code to calculate the vertices of the hexagon, in case we want to change that again
  185. static void drawPlayer(Game_Data *data, int player_angle)
  186. {
  187. ML_Color drawing_color = data->are_colors_reversed ? WHITE : BLACK;
  188. ML_filled_circle((9. + data->cam.zoom)*cos( PI*(player_angle + data->cam.angle)/180) + data->cam.cX, (9. + data->cam.zoom)*sin( PI*(player_angle+data->cam.angle)/180) + data->cam.cY, 1, drawing_color);
  189. }
  190. // draws one of the three rotating lines
  191. static void drawDiagonals(Game_Data *data, int nb_lines, Line_Transition line_transition)
  192. {
  193. fix tmp_angle = FIX(data->cam.angle);
  194. int i = 0;
  195. fix x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  196. fix delta_angle = 0;
  197. fix coeff = 0;
  198. fix transition_angle = 0;
  199. ML_Color drawing_color = data->are_colors_reversed ? WHITE : BLACK;
  200. delta_angle = fdiv(FIX(360), FIX(nb_lines));
  201. if(line_transition.delta_nb_lines == 1)
  202. nb_lines ++;
  203. if(line_transition.counter_start != 0)
  204. coeff = fdiv(FIX(line_transition.counter), FIX(line_transition.counter_start));
  205. transition_angle = fmul(delta_angle, coeff);
  206. do{
  207. x1 = fmul(FIX(9) + ftofix(data->cam.zoom), fcos(tmp_angle));
  208. y1 = fmul(FIX(9) + ftofix(data->cam.zoom), fsin(tmp_angle));
  209. x2 = fmul(fcos(tmp_angle), FIX(128));
  210. y2 = fmul(fsin(tmp_angle), FIX(128));
  211. ML_line(UNFIX(x1) + data->cam.cX, UNFIX(y1) + data->cam.cY, UNFIX(x2) + data->cam.cX, UNFIX(y2) + data->cam.cY, drawing_color);
  212. i++;
  213. switch(line_transition.delta_nb_lines){
  214. case 0:
  215. tmp_angle += fdiv(FIX(360), FIX(nb_lines));
  216. break;
  217. case 1:
  218. if(i < nb_lines - 1)
  219. {
  220. tmp_angle += fdiv(FIX(360) - (delta_angle - transition_angle), FIX(nb_lines - 1));
  221. }else{
  222. tmp_angle += delta_angle - transition_angle;
  223. }
  224. break;
  225. case -1:
  226. if(i < nb_lines - 1)
  227. {
  228. tmp_angle += fdiv(FIX(360) - transition_angle, FIX(nb_lines - 1));
  229. }else{
  230. tmp_angle += transition_angle;
  231. }
  232. break;
  233. }
  234. if(tmp_angle >= FIX(360)) tmp_angle = tmp_angle - FIX(359);
  235. }while(i < nb_lines);
  236. }
  237. static unsigned int length_of_print_string(unsigned char* txt) {
  238. // TODO : define
  239. unsigned int text_length = 0;
  240. unsigned int i;
  241. unsigned int current_char_length = 0;
  242. for(i = 0; i < strlen(txt); i++) {
  243. switch(txt[i]) {
  244. // upper case
  245. case 'K':
  246. case 'M':
  247. case 'N':
  248. case 'Q':
  249. case 'W':
  250. current_char_length = 6;
  251. break;
  252. // lower case
  253. case 'i':
  254. current_char_length = 2;
  255. break;
  256. case 'n':
  257. case 'r':
  258. current_char_length = 5;
  259. break;
  260. case 'm':
  261. case 'w':
  262. current_char_length = 6;
  263. break;
  264. //
  265. //
  266. default:
  267. current_char_length = 4;
  268. break;
  269. }
  270. text_length += current_char_length;
  271. }
  272. return text_length;
  273. }
  274. static void drawTopLeftCornerText(unsigned char* txt, unsigned char isReversed) {
  275. ML_Color drawing_color = isReversed ? WHITE : BLACK;
  276. unsigned int text_color = isReversed ? MINI_OVER : MINI_REV;
  277. unsigned int text_length = length_of_print_string(txt);
  278. if(!isReversed) {
  279. ML_bmp_8_or(hex_border_top_left, text_length, 0);
  280. } else {
  281. ML_bmp_8_and(hex_border_top_left_rev, text_length, 0);
  282. }
  283. PrintMini(0, 1, txt, text_color);
  284. ML_horizontal_line(7, 0, text_length, drawing_color);
  285. }
  286. static void drawTopRightCornerText(unsigned char* txt, unsigned char isReversed) {
  287. ML_Color drawing_color = isReversed ? WHITE : BLACK;
  288. unsigned int text_color = isReversed ? MINI_OVER : MINI_REV;
  289. int text_length = length_of_print_string(txt);
  290. int xPosition = 128 - text_length;
  291. if(!isReversed) {
  292. ML_bmp_8_or(hex_border_top_right, xPosition - 8, 0);
  293. } else {
  294. ML_bmp_8_and(hex_border_top_right_rev, xPosition - 8, 0);
  295. }
  296. PrintMini(xPosition, 1, txt, text_color);
  297. ML_horizontal_line(7, xPosition, 127, drawing_color);
  298. }
  299. static void drawBottomLeftCornerText(unsigned char* txt, unsigned char isReversed) {
  300. ML_Color drawing_color = isReversed ? WHITE : BLACK;
  301. unsigned int text_length = length_of_print_string(txt);
  302. unsigned int text_color = isReversed ? MINI_OVER : MINI_REV;
  303. if(!isReversed) {
  304. ML_bmp_8_or(hex_border_bottom_left, text_length - 1, 56);
  305. } else {
  306. ML_bmp_8_and(hex_border_bottom_left_rev, text_length - 1, 56);
  307. }
  308. PrintMini(0, 57, txt, text_color);
  309. ML_horizontal_line(63, 0, text_length, drawing_color);
  310. }
  311. static void drawBottomRightCornerText(unsigned char* txt, unsigned char isReversed) {
  312. ML_Color drawing_color = isReversed ? WHITE : BLACK;
  313. unsigned int text_color = isReversed ? MINI_OVER : MINI_REV;
  314. int text_length = length_of_print_string(txt);
  315. int xPosition = 128 - text_length;
  316. if(!isReversed) {
  317. ML_bmp_8_or(hex_border_bottom_right, xPosition - 8, 56);
  318. } else {
  319. ML_bmp_8_and(hex_border_bottom_right_rev, xPosition - 8, 56);
  320. }
  321. ML_horizontal_line(63, xPosition, 127, drawing_color);
  322. PrintMini(xPosition, 57, txt, text_color);
  323. }