main_test.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /**
  2. @file main_test.c
  3. This is a front end that serves as a basic automatic test of the game. At this
  4. point it's very simple and basically just checks if anything really
  5. substantial wasn't broken.
  6. This fronted tries to play the game and see if it behaves how expected. If you
  7. change anything substantial in the game, this test may start to fail and you
  8. may need to adjust it.
  9. by Miloslav Ciz (drummyfish), 2019
  10. Released under CC0 1.0 (https://creativecommons.org/publicdomain/zero/1.0/)
  11. plus a waiver of all other intellectual property. The goal of this work is
  12. be and remain completely in the public domain forever, available for any use
  13. whatsoever.
  14. */
  15. #include <stdio.h>
  16. #include <time.h>
  17. #define SFG_SCREEN_RESOLUTION_X 67
  18. #define SFG_SCREEN_RESOLUTION_Y 31
  19. #define SFG_BACKGROUND_BLUR 1
  20. #define SFG_DITHERED_SHADOW 1
  21. #define SFG_FPS 30
  22. #include "game.h"
  23. #include "sounds.h"
  24. uint8_t screen[SFG_SCREEN_RESOLUTION_X * SFG_SCREEN_RESOLUTION_Y];
  25. uint8_t keys[SFG_KEY_COUNT];
  26. uint32_t gameTime = 0;
  27. int8_t SFG_keyPressed(uint8_t key)
  28. {
  29. return keys[key];
  30. }
  31. void SFG_getMouseOffset(int16_t *x, int16_t *y)
  32. {
  33. }
  34. uint32_t SFG_getTimeMs()
  35. {
  36. return gameTime;
  37. }
  38. void SFG_sleepMs(uint16_t gameTimeMs)
  39. {
  40. }
  41. int aaa = 100;
  42. static inline void SFG_setPixel(uint16_t x, uint16_t y, uint8_t colorIndex)
  43. {
  44. screen[y * SFG_SCREEN_RESOLUTION_X + x] = colorIndex;
  45. }
  46. void SFG_playSound(uint8_t soundIndex, uint8_t volume)
  47. {
  48. }
  49. void SFG_setMusic(uint8_t value)
  50. {
  51. }
  52. void SFG_processEvent(uint8_t event, uint8_t data)
  53. {
  54. }
  55. void SFG_save(uint8_t data[SFG_SAVE_SIZE])
  56. {
  57. }
  58. uint8_t SFG_load(uint8_t data[SFG_SAVE_SIZE])
  59. {
  60. return 0;
  61. }
  62. void printTestHeading(const char *text)
  63. {
  64. printf("\n~~~~~ testing: %s ~~~~~\n\n",text);
  65. }
  66. const char colors[8] = " .,-;imX";
  67. void printScreen()
  68. {
  69. const char *c = screen;
  70. for (uint8_t y = 0; y < SFG_SCREEN_RESOLUTION_Y; ++y)
  71. {
  72. for (uint8_t x = 0; x < SFG_SCREEN_RESOLUTION_X; ++x)
  73. {
  74. putchar(*c != 7 ? colors[(*c) % 8] : '@');
  75. ++c;
  76. }
  77. putchar('\n');
  78. }
  79. }
  80. int main(void)
  81. {
  82. puts("===== TESTING ANARCH =====\n");
  83. puts("initializing");
  84. #define ASSERT(text,cond) { printf("checking \"%s\": ",text); if (cond) puts("OK"); else { puts("ERROR"); return 1; }}
  85. SFG_init();
  86. double msPerFrame = 0;
  87. ASSERT("frame == 0",SFG_game.frame == 0);
  88. {
  89. printTestHeading("music and sounds");
  90. static const uint8_t expectedSamples[] = { 1, 0, 0, 0, 0, 0, 255, 251, 80, 240, 240, 10, 0, 6, 4, 0 };
  91. uint16_t pos = 0;
  92. for (uint32_t i = 0; i < (SFG_TRACK_COUNT * SFG_TRACK_SAMPLES); ++i)
  93. {
  94. uint8_t sample = SFG_getNextMusicSample();
  95. if (i % 200000 == 0)
  96. {
  97. ASSERT("music sample", sample == expectedSamples[pos]);
  98. pos++;
  99. }
  100. }
  101. ASSERT("sfx sample",SFG_GET_SFX_SAMPLE(0,0) == 128);
  102. ASSERT("sfx sample",SFG_GET_SFX_SAMPLE(1,200) == 112);
  103. ASSERT("sfx sample",SFG_GET_SFX_SAMPLE(3,512) == 112);
  104. ASSERT("sfx sample",SFG_GET_SFX_SAMPLE(4,1000) == 128);
  105. }
  106. {
  107. printTestHeading("levels");
  108. SFG_TileDefinition t;
  109. uint8_t p;
  110. t = SFG_getMapTile(&SFG_level1,10,8,&p);
  111. ASSERT("level1 tile",SFG_TILE_FLOOR_HEIGHT(t) == 14 && SFG_TILE_CEILING_HEIGHT(t) == 0 && SFG_TILE_FLOOR_TEXTURE(t) == 4 && p == 0);
  112. t = SFG_getMapTile(&SFG_level3,9,20,&p);
  113. ASSERT("level3 tile",SFG_TILE_FLOOR_HEIGHT(t) == 17 && SFG_TILE_CEILING_HEIGHT(t) == 13 && SFG_TILE_FLOOR_TEXTURE(t) == 0 && p == 128);
  114. t = SFG_getMapTile(&SFG_level5,-9,0,&p);
  115. ASSERT("outside tile",SFG_TILE_FLOOR_HEIGHT(t) == 31 && SFG_TILE_CEILING_HEIGHT(t) == 0 && SFG_TILE_FLOOR_TEXTURE(t) == 7 && p == 0);
  116. }
  117. {
  118. printTestHeading("gameplay");
  119. for (uint8_t i = 0; i < SFG_KEY_COUNT; ++i)
  120. keys[i] = 0;
  121. #define STEP(ms) { printf("(fr %d, step %d ms) ",SFG_game.frame,ms); gameTime += ms; SFG_mainLoopBody(); }
  122. #define PRESS(k) { printf("(press %d) ",k); keys[k] = 1; }
  123. #define RELEASE(k) { printf("(release %d) ",k); keys[k] = 0; }
  124. #define TEST_PIXEL(x,y,v) { printf("(testing pixel %d %d)",x,y); uint8_t val = screen[y * SFG_SCREEN_RESOLUTION_X + y]; if (val != v) { printf("\nERROR: expcted %d, got %d\n",v,val); return 1; }}
  125. STEP(10)
  126. STEP(100)
  127. PRESS(SFG_KEY_DOWN) // select "exit"
  128. STEP(1000)
  129. RELEASE(SFG_KEY_DOWN)
  130. TEST_PIXEL(10,20,64)
  131. putchar('\n');
  132. ASSERT("menu item == exit",SFG_getMenuItem(SFG_game.selectedMenuItem) == SFG_MENU_ITEM_EXIT)
  133. PRESS(SFG_KEY_UP) // select "play"
  134. STEP(700)
  135. RELEASE(SFG_KEY_UP)
  136. PRESS(SFG_KEY_A) // confirm "play"
  137. STEP(100)
  138. TEST_PIXEL(30,21,0)
  139. RELEASE(SFG_KEY_A)
  140. STEP(100)
  141. PRESS(SFG_KEY_A) // skip intro
  142. STEP(2000)
  143. putchar('\n');
  144. ASSERT("state == playing",SFG_game.state == SFG_GAME_STATE_PLAYING)
  145. RELEASE(SFG_KEY_A)
  146. PRESS(SFG_KEY_RIGHT) // turn
  147. STEP(400)
  148. RELEASE(SFG_KEY_RIGHT)
  149. PRESS(SFG_KEY_UP) // take ammo
  150. STEP(400)
  151. putchar('\n');
  152. ASSERT("weapon == shotgun",SFG_player.weapon == SFG_WEAPON_SHOTGUN)
  153. RELEASE(SFG_KEY_UP)
  154. PRESS(SFG_KEY_LEFT) // turn back
  155. STEP(700)
  156. RELEASE(SFG_KEY_LEFT)
  157. PRESS(SFG_KEY_UP) // go to barrels
  158. STEP(1000)
  159. RELEASE(SFG_KEY_UP)
  160. PRESS(SFG_KEY_RIGHT)
  161. STEP(200)
  162. RELEASE(SFG_KEY_RIGHT)
  163. PRESS(SFG_KEY_A) // shoot barrels
  164. STEP(700)
  165. RELEASE(SFG_KEY_A)
  166. putchar('\n');
  167. ASSERT("health < 100",SFG_player.health < 100)
  168. PRESS(SFG_KEY_UP)
  169. STEP(720)
  170. RELEASE(SFG_KEY_UP)
  171. PRESS(SFG_KEY_LEFT)
  172. STEP(300)
  173. RELEASE(SFG_KEY_LEFT)
  174. PRESS(SFG_KEY_UP)
  175. STEP(700)
  176. RELEASE(SFG_KEY_UP)
  177. PRESS(SFG_KEY_RIGHT)
  178. STEP(700)
  179. RELEASE(SFG_KEY_RIGHT)
  180. PRESS(SFG_KEY_UP)
  181. STEP(850)
  182. RELEASE(SFG_KEY_UP)
  183. STEP(2500)
  184. PRESS(SFG_KEY_A) // shoot monster
  185. STEP(200)
  186. RELEASE(SFG_KEY_A) // shoot monster
  187. STEP(900)
  188. PRESS(SFG_KEY_LEFT)
  189. PRESS(SFG_KEY_NEXT_WEAPON) // switch to machine gun
  190. STEP(100)
  191. RELEASE(SFG_KEY_LEFT)
  192. RELEASE(SFG_KEY_NEXT_WEAPON)
  193. putchar('\n');
  194. ASSERT("weapon == machine gun",SFG_player.weapon == SFG_WEAPON_MACHINE_GUN)
  195. STEP(1000)
  196. PRESS(SFG_KEY_A) // shoot
  197. STEP(2000)
  198. putchar('\n');
  199. ASSERT("health == 74",SFG_player.health == 74)
  200. RELEASE(SFG_KEY_A)
  201. STEP(100)
  202. PRESS(SFG_KEY_LEFT)
  203. #define FRAMES 1000000
  204. printf("\nbenchmarking frame time on %d frames.\n",FRAMES);
  205. clock_t t1, t2;
  206. t1 = clock();
  207. STEP(FRAMES);
  208. t2 = clock();
  209. msPerFrame = (((double) (t2 - t1)) * 1000.0) / (CLOCKS_PER_SEC * FRAMES);
  210. RELEASE(SFG_KEY_LEFT)
  211. STEP(100)
  212. PRESS(SFG_KEY_C) // open menu
  213. PRESS(SFG_KEY_DOWN)
  214. STEP(200)
  215. RELEASE(SFG_KEY_C)
  216. putchar('\n');
  217. ASSERT("state == menu",SFG_game.state == SFG_GAME_STATE_MENU)
  218. STEP(1000)
  219. PRESS(SFG_KEY_A) // exit game
  220. STEP(100)
  221. putchar('\n');
  222. ASSERT("game exitted",SFG_mainLoopBody() == 0)
  223. putchar('\n');
  224. printScreen();
  225. #undef PRESS
  226. #undef RELEASE
  227. #undef STEP
  228. }
  229. puts("======================================\n\nDone.\nEverything seems OK.");
  230. printf("benchmarked ms per frame: %lf\n",msPerFrame);
  231. return 0;
  232. }