test.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**
  2. Simple testing program for SAF.
  3. by drummyfish, released under CC0 1.0, public domain
  4. */
  5. #define SAF_PROGRAM_NAME "Test"
  6. #define SAF_SETTING_BACKGROUND_COLOR 0xe0
  7. #define SAF_SETTING_FORCE_1BIT 0
  8. #define SAF_SETTING_1BIT_DITHER 0
  9. #define SAF_SETTING_FASTER_1BIT 2
  10. #define SAF_SETTING_ENABLE_SOUND 1
  11. #include "../saf.h"
  12. uint8_t saveValue = 0;
  13. void screen1()
  14. {
  15. SAF_clearScreen(SAF_COLOR_BLACK);
  16. SAF_drawText("SAF test!",2,2,SAF_COLOR_WHITE,1);
  17. uint8_t index = 0;
  18. for (uint8_t y = 0; y < 16; ++y)
  19. for (uint8_t x = 0; x < 16; ++x)
  20. {
  21. uint8_t color = ((x % 8) << 5) | ((y % 8) << 2) | (x / 8 + 2 * (y / 8));
  22. int8_t x2 = 2 + 2 * x;
  23. int8_t y2 = 12 + 2 * y;
  24. for (int8_t j = 0; j < 2; ++j)
  25. for (int8_t i = 0; i < 2; ++i)
  26. SAF_drawPixel(x2 + i,y2 + j,color);
  27. index++;
  28. }
  29. char btnChars[SAF_BUTTONS + 1] = "UDLRABC";
  30. char btnString[2] = "x";
  31. for (int8_t i = 0; i < SAF_BUTTONS; ++i)
  32. {
  33. btnString[0] = btnChars[i];
  34. uint8_t pressed = SAF_buttonPressed(i);
  35. int8_t x = 2 + i * 8;
  36. SAF_drawRect(x,48,7,8,pressed ? SAF_COLOR_WHITE : SAF_COLOR_GRAY_DARK,1);
  37. SAF_drawText(btnString,x + 2,50,pressed ? SAF_COLOR_BLACK : SAF_COLOR_WHITE,1);
  38. for (int8_t j = 0; j < (pressed / 32); ++j)
  39. SAF_drawPixel(x + j,58,SAF_COLOR_WHITE);
  40. }
  41. SAF_drawCircle(48,28,12,SAF_COLOR_WHITE,0);
  42. SAF_drawLine(48,28,48 + (SAF_sin(SAF_frame()) * 12) / 128,28 + (SAF_cos(SAF_frame()) * 12) / 128,SAF_COLOR_YELLOW);
  43. }
  44. void screen2()
  45. {
  46. SAF_clearScreen(SAF_COLOR_WHITE);
  47. SAF_drawRect(43,46,SAF_sin(((SAF_frame() * 2) % 256)) / 2,SAF_cos((SAF_frame() % 256)) / 2,SAF_COLOR_RED,1);
  48. SAF_drawCircle(-10,-20,SAF_frame() % 128,SAF_COLOR_BLUE,0);
  49. SAF_drawCircle(31,31,(SAF_frame() >> 1) % 32,SAF_COLOR_GREEN,0);
  50. SAF_drawText("random:\n b",1,1,SAF_COLOR_BLACK,1);
  51. uint8_t bit = (SAF_frame() >> 4) % 8;
  52. char numberStr[2];
  53. SAF_intToStr(bit,numberStr);
  54. SAF_drawText(numberStr,11,6,SAF_COLOR_BLACK,1);
  55. bit = 0x01 << bit;
  56. for (int8_t y = 0; y < 16; ++y)
  57. for (int8_t x = 0; x < 16; ++x)
  58. {
  59. uint8_t value = SAF_random();
  60. SAF_drawPixel(44 + x,11 + y,value);
  61. SAF_drawPixel(23 + x,11 + y,value < 128 ? SAF_COLOR_BLACK : SAF_COLOR_GRAY);
  62. value = value & bit;
  63. value = value ? SAF_COLOR_BLACK : SAF_COLOR_GRAY;
  64. SAF_drawPixel(2 + x,11 + y,value);
  65. }
  66. SAF_drawText("frame\ntime\nplat.",1,30,SAF_COLOR_BLACK,1);
  67. char numberStr2[32];
  68. SAF_intToStr(SAF_frame(),numberStr2);
  69. SAF_drawText(numberStr2,34,30,SAF_COLOR_BLACK,1);
  70. SAF_floatToStr(SAF_time() / 1000.0,numberStr2,3);
  71. SAF_drawText(numberStr2,34,35,SAF_COLOR_BLACK,1);
  72. SAF_drawText(SAF_PLATFORM_NAME,34,40,SAF_COLOR_BLACK,1);
  73. char allChars[10][11] =
  74. {
  75. " !\"#$%&'()",
  76. "*+-./01234",
  77. "56789:;<=>",
  78. "?@ABCDEFGH",
  79. "IJKLMNOPQR",
  80. "STUVWXYZ[]",
  81. "^,_@`abcde",
  82. "fghijklmno",
  83. "pqrstuvwxy",
  84. "z{|}~ "
  85. };
  86. SAF_drawText(allChars[(SAF_frame() >> 5) % 10],1,50,SAF_COLOR_BLACK,1);
  87. char num[] = "xxx";
  88. SAF_drawText(SAF_intToStr(saveValue,num),
  89. SAF_drawText("saved:",1,58,SAF_COLOR_BLACK,1),58,SAF_COLOR_BLACK,1);
  90. }
  91. uint8_t img[44] = {
  92. 0x07,0x06,0xff,0x00,0x00,0xff,0x00,0x00,0xff,0x00,0xed,0xed,0x00,0xed,0xe0,0x00,
  93. 0x00,0xed,0xe0,0xed,0xe0,0x60,0x00,0xff,0x00,0xe0,0xe0,0x60,0x00,0xff,0xff,0xff,
  94. 0x00,0x60,0x00,0xff,0xff,0xff,0xff,0xff,0x00,0xff,0xff,0xff};
  95. uint8_t imgCompressed[227] = {
  96. 0x13,0x20,0xe0,0x00,0x28,0x7c,0x07,0xfe,0x4c,0xff,0x71,0x70,0x95,0x6d,0x92,0x24,
  97. 0x48,0x49,0x60,0x41,0xd0,0x01,0x2d,0x01,0xc0,0x01,0x0f,0x08,0x0a,0x1c,0x01,0xa0,
  98. 0x01,0x08,0x3a,0x15,0x01,0x90,0x01,0x59,0x06,0x01,0xa0,0x06,0x05,0x27,0x05,0x06,
  99. 0xa0,0x06,0x15,0x27,0x15,0x06,0x90,0x06,0x15,0x27,0x15,0x06,0x70,0x11,0x38,0x0e,
  100. 0x08,0x2a,0x11,0x30,0x11,0x0f,0x08,0x7a,0x08,0x1c,0x11,0x00,0x01,0x3f,0x3b,0x38,
  101. 0x1b,0x2c,0x11,0x3d,0xa2,0x06,0x02,0x01,0x00,0x11,0x0d,0x16,0x0e,0x12,0x0e,0x08,
  102. 0x09,0x1e,0x08,0x06,0x11,0x30,0x01,0x02,0x69,0x18,0x11,0x70,0x06,0x05,0x47,0x05,
  103. 0x06,0x90,0x06,0x15,0x27,0x15,0x06,0xa0,0x16,0x25,0x16,0xd0,0x02,0x03,0x02,0x80,
  104. 0x34,0x20,0x02,0x03,0x02,0x10,0x43,0x00,0x14,0x10,0x14,0x10,0x02,0x0a,0x02,0x10,
  105. 0x13,0x10,0x13,0x14,0x10,0x14,0x10,0x02,0x0e,0x02,0x10,0x13,0x10,0x13,0x14,0x10,
  106. 0x14,0x10,0x01,0x02,0x01,0x10,0x43,0x00,0x54,0x10,0x01,0x02,0x01,0x10,0x13,0x10,
  107. 0x13,0x14,0x10,0x14,0x00,0x01,0x1c,0x0a,0x01,0x00,0x13,0x10,0x13,0x14,0x10,0x14,
  108. 0x00,0x01,0x22,0x01,0x00,0x43,0x80,0x01,0x02,0x01,0xf0,0x01,0x02,0x01,0xf0,0x01,
  109. 0x02,0x01,0xe0,0x01,0x1b,0x0c,0x01,0xb0,0x11,0x3b,0x0c,0x11,0x80,0x01,0x82,0x01,
  110. 0x70,0xa1,0x30};
  111. uint8_t img1Bit[25] = {
  112. 0x0c,0x0f,0xe9,0x7c,0x03,0x40,0x20,0x00,0x80,0x11,0xf8,0x96,0x9d,0x69,0xcf,0x3a,
  113. 0x01,0x90,0x1c,0xeb,0xc1,0xbe,0x07,0xe3,0xf0};
  114. uint8_t img1BitMask[25] = {
  115. 0x0c,0x0f,0x16,0x83,0xfc,0xbf,0xdf,0xff,0x7f,0xef,0xff,0x7f,0xe3,0xfe,0x3f,0xc7,
  116. 0xfe,0x7f,0xe3,0xfc,0x3f,0xc1,0xf8,0x1c,0x00};
  117. uint8_t imgLogo[2 + 8 * 8];
  118. int8_t trianglePoints[6] = {1,2,3,4,5,6};
  119. void screen3()
  120. {
  121. SAF_clearScreen(SAF_COLOR_YELLOW);
  122. int8_t s = SAF_sin(SAF_frame());
  123. int8_t c = SAF_cos(SAF_frame());
  124. int8_t a = 32 - s / 2, b = 32 + s / 2;
  125. SAF_drawLine(a,32 - 64,b,32 + 64,SAF_COLOR_RED);
  126. SAF_drawLine(32 - 64,b,32 + 64,a,SAF_COLOR_RED);
  127. uint8_t transform = SAF_TRANSFORM_NONE + (SAF_frame() >> 4) % 4;
  128. if ((SAF_frame() >> 3) % 2)
  129. transform |= SAF_TRANSFORM_FLIP;
  130. SAF_drawImage(img,
  131. 32 + s / 4 - img[0] / 2,
  132. 32 + c / 4 - img[1] / 2,
  133. transform | SAF_TRANSFORM_SCALE_3 | ((SAF_frame() & 0x10) ? 0 : SAF_TRANSFORM_INVERT),SAF_COLOR_WHITE);
  134. SAF_drawImageCompressed(imgCompressed,1,1,transform,SAF_COLOR_RED);
  135. SAF_drawImage(img,40,1,transform,SAF_COLOR_WHITE);
  136. SAF_drawImage1Bit(img1Bit,40,20,img1BitMask,255,0,transform);
  137. SAF_drawImage1Bit(img1Bit,50,45,0,255,0,SAF_TRANSFORM_NONE);
  138. SAF_drawImage1Bit(imgLogo,2,40,imgLogo,SAF_COLOR_BLUE,255,SAF_TRANSFORM_NONE);
  139. for (int i = 0; i < 6; ++i)
  140. {
  141. int8_t add = SAF_random() > 127 ? 1 : -1;
  142. if ((add > 0 && trianglePoints[i] < 16) || (add < 0 && trianglePoints[i] > -16))
  143. trianglePoints[i] += add;
  144. }
  145. for (uint8_t i = 0; i < 3; ++i)
  146. {
  147. uint8_t i1 = 2 * i, i2 = 2 * (i + 1) % 6;
  148. SAF_drawLine(
  149. trianglePoints[i1] + 25,
  150. trianglePoints[i1 + 1] + 40,
  151. trianglePoints[i2] + 25,
  152. trianglePoints[i2 + 1] + 40,
  153. SAF_COLOR_BLACK);
  154. }
  155. }
  156. uint8_t phase = 0;
  157. void SAF_init(void)
  158. {
  159. saveValue = SAF_load(0);
  160. saveValue++;
  161. SAF_save(0,saveValue);
  162. imgLogo[0] = 8;
  163. imgLogo[1] = 8;
  164. imgLogo[2] = (SAF_LOGO_IMAGE >> 56) & 0xff;
  165. imgLogo[3] = (SAF_LOGO_IMAGE >> 48) & 0xff;
  166. imgLogo[4] = (SAF_LOGO_IMAGE >> 40) & 0xff;
  167. imgLogo[5] = (SAF_LOGO_IMAGE >> 32) & 0xff;
  168. imgLogo[6] = (SAF_LOGO_IMAGE >> 24) & 0xff;
  169. imgLogo[7] = (SAF_LOGO_IMAGE >> 16) & 0xff;
  170. imgLogo[8] = (SAF_LOGO_IMAGE >> 8) & 0xff;
  171. imgLogo[9] = SAF_LOGO_IMAGE & 0xff;
  172. }
  173. uint8_t SAF_loop(void)
  174. {
  175. #if 0
  176. screen2();
  177. #else
  178. switch (phase)
  179. {
  180. case 0: screen1(); break;
  181. case 1: screen2(); break;
  182. case 2: screen3(); break;
  183. default: break;
  184. }
  185. uint32_t f = SAF_frame() % 64;
  186. if (f >= 24 && f < 35)
  187. {
  188. SAF_drawRect(3,56,45,6,SAF_COLOR_GRAY_DARK,1);
  189. uint8_t i = (SAF_frame() / 64) % SAF_SOUNDS;
  190. char s[8] = "sound x";
  191. s[6] = '0' + i;
  192. SAF_drawText(s,4,57,SAF_COLOR_RED,1);
  193. if (f == 24)
  194. SAF_playSound(i);
  195. }
  196. if (SAF_frame() != 0 && SAF_frame() % 128 == 0)
  197. {
  198. phase++;
  199. phase %= 3;
  200. }
  201. return 1;
  202. #endif
  203. }