test_sdl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. #include <SDL2/SDL.h>
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #define S3L_RESOLUTION_X 640
  5. #define S3L_RESOLUTION_Y 480
  6. #define S3L_PIXEL_FUNCTION drawPixel
  7. #define S3L_FLAT 0
  8. #define S3L_NEAR_CROSS_STRATEGY 1
  9. #define S3L_PERSPECTIVE_CORRECTION 2
  10. #define S3L_SORT 0
  11. #define S3L_STENCIL_BUFFER 0
  12. #define S3L_Z_BUFFER 1
  13. #define FPS 30
  14. #define MSPF (1000 / (FPS))
  15. #include "small3dlib.h"
  16. #include "tinyphysicsengine.h"
  17. #define PIXELS_SIZE (S3L_RESOLUTION_X * S3L_RESOLUTION_Y * 4)
  18. uint8_t pixels[PIXELS_SIZE];
  19. void draw2DPoint(int x, int y, int r, int g, int b)
  20. {
  21. if (x < 1 || x > S3L_RESOLUTION_X - 1 ||
  22. y < 1 || y > S3L_RESOLUTION_Y - 1)
  23. return;
  24. uint32_t index = ((y - 1) * S3L_RESOLUTION_X + x) * 4;
  25. #define d pixels[index] = 0; pixels[index + 1] = b; pixels[index + 2] = g; pixels[index + 3] = r;
  26. d
  27. index += S3L_RESOLUTION_X * 4 - 4;
  28. d
  29. index += 4;
  30. d
  31. index += 4;
  32. d
  33. index += S3L_RESOLUTION_X * 4 - 4;
  34. d
  35. #undef d
  36. }
  37. void drawPixel(S3L_PixelInfo *p)
  38. {
  39. uint32_t index = (p->y * S3L_RESOLUTION_X + p->x) * 4;
  40. pixels[index + 1] = p->triangleIndex * 16;
  41. pixels[index + 2] = 255 - p->triangleIndex * 16;
  42. }
  43. S3L_Unit cubeVertices[] = { S3L_CUBE_VERTICES(S3L_FRACTIONS_PER_UNIT) };
  44. S3L_Index cubeTriangles[] = { S3L_CUBE_TRIANGLES };
  45. #define CYLINDER_VERTEX_COUNT 20
  46. const S3L_Unit cylinderVertices[CYLINDER_VERTEX_COUNT * 3] = {
  47. 0, -256, 512, // 0
  48. 0, 256, 512, // 3
  49. 300, -256, 414, // 6
  50. 300, 256, 414, // 9
  51. 486, -256, 158, // 12
  52. 486, 256, 158, // 15
  53. 486, -256, -158, // 18
  54. 486, 256, -158, // 21
  55. 300, -256, -414, // 24
  56. 300, 256, -414, // 27
  57. 0, -256, -512, // 30
  58. 0, 256, -512, // 33
  59. -300, -256, -414, // 36
  60. -300, 256, -414, // 39
  61. -486, -256, -158, // 42
  62. -486, 256, -158, // 45
  63. -486, -256, 158, // 48
  64. -486, 256, 158, // 51
  65. -300, -256, 414, // 54
  66. -300, 256, 414 // 57
  67. }; // cylinderVertices
  68. #define CYLINDER_TRIANGLE_COUNT 36
  69. const S3L_Index cylinderTriangleIndices[CYLINDER_TRIANGLE_COUNT * 3] = {
  70. 1, 2, 0, // 0
  71. 3, 4, 2, // 3
  72. 5, 6, 4, // 6
  73. 7, 8, 6, // 9
  74. 9, 10, 8, // 12
  75. 11, 12, 10, // 15
  76. 13, 14, 12, // 18
  77. 15, 16, 14, // 21
  78. 17, 7, 5, // 24
  79. 17, 18, 16, // 27
  80. 19, 0, 18, // 30
  81. 6, 14, 18, // 33
  82. 1, 3, 2, // 36
  83. 3, 5, 4, // 39
  84. 5, 7, 6, // 42
  85. 7, 9, 8, // 45
  86. 9, 11, 10, // 48
  87. 11, 13, 12, // 51
  88. 13, 15, 14, // 54
  89. 15, 17, 16, // 57
  90. 5, 3, 17, // 60
  91. 1, 19, 17, // 63
  92. 17, 15, 13, // 66
  93. 13, 11, 17, // 69
  94. 9, 7, 17, // 72
  95. 3, 1, 17, // 75
  96. 17, 11, 9, // 78
  97. 17, 19, 18, // 81
  98. 19, 1, 0, // 84
  99. 18, 0, 2, // 87
  100. 2, 4, 6, // 90
  101. 6, 8, 10, // 93
  102. 10, 12, 14, // 96
  103. 14, 16, 18, // 99
  104. 18, 2, 6, // 102
  105. 6, 10, 14 // 105
  106. }; // cylinderTriangleIndices
  107. #define SPHERE_VERTEX_COUNT 42
  108. const S3L_Unit sphereVertices[SPHERE_VERTEX_COUNT * 3] = {
  109. 0, -512, 0, // 0
  110. 370, -228, -269, // 3
  111. -141, -228, -435, // 6
  112. -457, -228, 0, // 9
  113. -141, -228, 435, // 12
  114. 370, -228, 269, // 15
  115. 141, 228, -435, // 18
  116. -370, 228, -269, // 21
  117. -370, 228, 269, // 24
  118. 141, 228, 435, // 27
  119. 457, 228, 0, // 30
  120. 0, 512, 0, // 33
  121. -83, -435, -255, // 36
  122. 217, -435, -158, // 39
  123. 134, -269, -414, // 42
  124. 435, -269, 0, // 45
  125. 217, -435, 158, // 48
  126. -269, -435, 0, // 51
  127. -352, -269, -255, // 54
  128. -83, -435, 255, // 57
  129. -352, -269, 255, // 60
  130. 134, -269, 414, // 63
  131. 486, 0, -158, // 66
  132. 486, 0, 158, // 69
  133. 0, 0, -512, // 72
  134. 300, 0, -414, // 75
  135. -486, 0, -158, // 78
  136. -300, 0, -414, // 81
  137. -300, 0, 414, // 84
  138. -486, 0, 158, // 87
  139. 300, 0, 414, // 90
  140. 0, 0, 512, // 93
  141. 352, 269, -255, // 96
  142. -134, 269, -414, // 99
  143. -435, 269, 0, // 102
  144. -134, 269, 414, // 105
  145. 352, 269, 255, // 108
  146. 83, 435, -255, // 111
  147. 269, 435, 0, // 114
  148. -217, 435, -158, // 117
  149. -217, 435, 158, // 120
  150. 83, 435, 255 // 123
  151. }; // sphereVertices
  152. #define SPHERE_TRIANGLE_COUNT 80
  153. const S3L_Index sphereTriangleIndices[SPHERE_TRIANGLE_COUNT * 3] = {
  154. 0, 13, 12, // 0
  155. 1, 13, 15, // 3
  156. 0, 12, 17, // 6
  157. 0, 17, 19, // 9
  158. 0, 19, 16, // 12
  159. 1, 15, 22, // 15
  160. 2, 14, 24, // 18
  161. 3, 18, 26, // 21
  162. 4, 20, 28, // 24
  163. 5, 21, 30, // 27
  164. 1, 22, 25, // 30
  165. 2, 24, 27, // 33
  166. 3, 26, 29, // 36
  167. 4, 28, 31, // 39
  168. 5, 30, 23, // 42
  169. 6, 32, 37, // 45
  170. 7, 33, 39, // 48
  171. 8, 34, 40, // 51
  172. 9, 35, 41, // 54
  173. 10, 36, 38, // 57
  174. 38, 41, 11, // 60
  175. 38, 36, 41, // 63
  176. 36, 9, 41, // 66
  177. 41, 40, 11, // 69
  178. 41, 35, 40, // 72
  179. 35, 8, 40, // 75
  180. 40, 39, 11, // 78
  181. 40, 34, 39, // 81
  182. 34, 7, 39, // 84
  183. 39, 37, 11, // 87
  184. 39, 33, 37, // 90
  185. 33, 6, 37, // 93
  186. 37, 38, 11, // 96
  187. 37, 32, 38, // 99
  188. 32, 10, 38, // 102
  189. 23, 36, 10, // 105
  190. 23, 30, 36, // 108
  191. 30, 9, 36, // 111
  192. 31, 35, 9, // 114
  193. 31, 28, 35, // 117
  194. 28, 8, 35, // 120
  195. 29, 34, 8, // 123
  196. 29, 26, 34, // 126
  197. 26, 7, 34, // 129
  198. 27, 33, 7, // 132
  199. 27, 24, 33, // 135
  200. 24, 6, 33, // 138
  201. 25, 32, 6, // 141
  202. 25, 22, 32, // 144
  203. 22, 10, 32, // 147
  204. 30, 31, 9, // 150
  205. 30, 21, 31, // 153
  206. 21, 4, 31, // 156
  207. 28, 29, 8, // 159
  208. 28, 20, 29, // 162
  209. 20, 3, 29, // 165
  210. 26, 27, 7, // 168
  211. 26, 18, 27, // 171
  212. 18, 2, 27, // 174
  213. 24, 25, 6, // 177
  214. 24, 14, 25, // 180
  215. 14, 1, 25, // 183
  216. 22, 23, 10, // 186
  217. 22, 15, 23, // 189
  218. 15, 5, 23, // 192
  219. 16, 21, 5, // 195
  220. 16, 19, 21, // 198
  221. 19, 4, 21, // 201
  222. 19, 20, 4, // 204
  223. 19, 17, 20, // 207
  224. 17, 3, 20, // 210
  225. 17, 18, 3, // 213
  226. 17, 12, 18, // 216
  227. 12, 2, 18, // 219
  228. 15, 16, 5, // 222
  229. 15, 13, 16, // 225
  230. 13, 0, 16, // 228
  231. 12, 14, 2, // 231
  232. 12, 13, 14, // 234
  233. 13, 1, 14 // 237
  234. }; // sphereTriangleIndices
  235. typedef struct
  236. {
  237. S3L_Mat4 matrix;
  238. S3L_Mat4 scaleMatrix;
  239. } BodyModelExtra;
  240. int bodyCount = 0;
  241. S3L_Scene scene;
  242. S3L_Model3D models[1024];
  243. TPE_World world;
  244. TPE_Body bodies[1024];
  245. BodyModelExtra extra[1024];
  246. void addBody(uint8_t shape, TPE_Unit mass, TPE_Unit param1, TPE_Unit param2, TPE_Unit param3)
  247. {
  248. BodyModelExtra e;
  249. TPE_Body b;
  250. S3L_Model3D m;
  251. TPE_bodyInit(&b);
  252. b.shape = shape;
  253. b.mass = mass;
  254. b.shapeParams[0] = param1;
  255. b.shapeParams[1] = param2;
  256. b.shapeParams[2] = param3;
  257. TPE_bodyRecomputeBounds(&b);
  258. const S3L_Unit *v;
  259. const S3L_Index *t;
  260. S3L_Index vc, tc;
  261. S3L_Unit sx = S3L_FRACTIONS_PER_UNIT, sy = S3L_FRACTIONS_PER_UNIT, sz = S3L_FRACTIONS_PER_UNIT;
  262. switch (shape)
  263. {
  264. case TPE_SHAPE_CYLINDER:
  265. case TPE_SHAPE_CAPSULE:
  266. v = cylinderVertices;
  267. t = cylinderTriangleIndices;
  268. vc = CYLINDER_VERTEX_COUNT;
  269. tc = CYLINDER_TRIANGLE_COUNT;
  270. sx = param1; sy = param2; sz = param1;
  271. break;
  272. case TPE_SHAPE_SPHERE:
  273. default:
  274. v = sphereVertices;
  275. t = sphereTriangleIndices;
  276. vc = SPHERE_VERTEX_COUNT;
  277. tc = SPHERE_TRIANGLE_COUNT;
  278. sx = param1; sy = param1; sz = param1;
  279. break;
  280. case TPE_SHAPE_CUBOID:
  281. v = cubeVertices;
  282. t = cubeTriangles;
  283. vc = S3L_CUBE_VERTEX_COUNT;
  284. tc = S3L_CUBE_TRIANGLE_COUNT;
  285. sx = param1; sy = param2; sz = param3;
  286. break;
  287. }
  288. S3L_initModel3D(v,vc,t,tc,&m);
  289. S3L_makeScaleMatrix(sx,sy,sz,e.scaleMatrix);
  290. S3L_initMat4(e.matrix);
  291. extra[bodyCount] = e;
  292. m.customTransformMatrix = &(extra[bodyCount].matrix);
  293. bodies[bodyCount] = b;
  294. models[bodyCount] = m;
  295. scene.modelCount++;
  296. world.bodyCount++;
  297. bodyCount++;
  298. }
  299. void updateBodies()
  300. {
  301. for (int i = 0; i < bodyCount; ++i)
  302. {
  303. BodyModelExtra *e = &(extra[i]);
  304. S3L_Mat4 m;
  305. TPE_bodyGetTransformMatrix(&(bodies[i]),m);
  306. S3L_copyMat4(e->scaleMatrix,e->matrix);
  307. S3L_mat4Xmat4(e->matrix,m);
  308. }
  309. }
  310. int main()
  311. {
  312. SDL_Window *window = SDL_CreateWindow("test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, S3L_RESOLUTION_X, S3L_RESOLUTION_Y, SDL_WINDOW_SHOWN);
  313. SDL_Renderer *renderer = SDL_CreateRenderer(window,-1,0);
  314. SDL_Texture *textureSDL = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBX8888, SDL_TEXTUREACCESS_STATIC, S3L_RESOLUTION_X, S3L_RESOLUTION_Y);
  315. SDL_Surface *screenSurface = SDL_GetWindowSurface(window);
  316. SDL_Event event;
  317. int running = 1;
  318. S3L_initScene(models,0,&scene);
  319. TPE_worldInit(&world);
  320. world.bodies = bodies;
  321. TPE_Unit frame = 0;
  322. //-------
  323. scene.camera.transform.translation.z = -8 * S3L_FRACTIONS_PER_UNIT;
  324. scene.camera.transform.translation.y = 2 * S3L_FRACTIONS_PER_UNIT;
  325. addBody(TPE_SHAPE_CUBOID,4024,1000,2000,3000);
  326. addBody(TPE_SHAPE_CUBOID,TPE_INFINITY,5000,1000,5000);
  327. addBody(TPE_SHAPE_CUBOID,4024,1000,1000,1500);
  328. bodies[0].position = TPE_vec4(0,3000,0,0);
  329. bodies[1].position = TPE_vec4(0,-1000,0,0);
  330. bodies[0].velocity = TPE_vec4(0,0,0,0);
  331. bodies[2].position.x = 2000;
  332. bodies[2].position.y = 2000;
  333. TPE_Vec4 qqq;
  334. TPE_rotationToQuaternion(TPE_vec4(0,100,255,0),50,&qqq);
  335. TPE_bodySetOrientation(&(bodies[0]),qqq);
  336. int collided = 0;
  337. int time;
  338. while (running)
  339. {
  340. time = SDL_GetTicks();
  341. // TPE_worldApplyGravityCenter(&world,TPE_vec4(0,0,0,0),4);
  342. TPE_worldApplyGravityDown(&world,4);
  343. for (uint32_t i = 0; i < PIXELS_SIZE; ++i)
  344. pixels[i] = 0;
  345. S3L_newFrame();
  346. TPE_worldStepBodies(&world);
  347. updateBodies();
  348. S3L_drawScene(scene);
  349. TPE_Vec4 p, n;
  350. TPE_worldResolveCollisionNaive(&world);
  351. SDL_UpdateTexture(textureSDL,NULL,pixels,S3L_RESOLUTION_X * sizeof(uint32_t));
  352. while (SDL_PollEvent(&event))
  353. {
  354. if (event.type == SDL_QUIT)
  355. running = 0;
  356. else if (event.type == SDL_KEYDOWN)
  357. {
  358. if (event.key.keysym.scancode == SDL_SCANCODE_Q || event.key.keysym.scancode == SDL_SCANCODE_ESCAPE)
  359. running = 0;
  360. }
  361. }
  362. const uint8_t *state = SDL_GetKeyboardState(NULL);
  363. S3L_Vec4 camF, camR;
  364. #define SHIFT_STEP 50
  365. #define ROT_STEP 5
  366. S3L_rotationToDirections(scene.camera.transform.rotation,SHIFT_STEP,&camF,&camR,0);
  367. if (state[SDL_SCANCODE_LSHIFT])
  368. {
  369. if (state[SDL_SCANCODE_UP])
  370. S3L_vec3Add(&scene.camera.transform.translation,camF);
  371. else if (state[SDL_SCANCODE_DOWN])
  372. S3L_vec3Sub(&scene.camera.transform.translation,camF);
  373. else if (state[SDL_SCANCODE_LEFT])
  374. S3L_vec3Sub(&scene.camera.transform.translation,camR);
  375. else if (state[SDL_SCANCODE_RIGHT])
  376. S3L_vec3Add(&scene.camera.transform.translation,camR);
  377. }
  378. else
  379. {
  380. if (state[SDL_SCANCODE_UP])
  381. scene.camera.transform.rotation.x += ROT_STEP;
  382. else if (state[SDL_SCANCODE_DOWN])
  383. scene.camera.transform.rotation.x -= ROT_STEP;
  384. else if (state[SDL_SCANCODE_LEFT])
  385. scene.camera.transform.rotation.y += ROT_STEP;
  386. else if (state[SDL_SCANCODE_RIGHT])
  387. scene.camera.transform.rotation.y -= ROT_STEP;
  388. }
  389. #define SHIFT_STEP 50
  390. if (state[SDL_SCANCODE_L])
  391. bodies[1].position.x += SHIFT_STEP;
  392. else if (state[SDL_SCANCODE_J])
  393. bodies[1].position.x -= SHIFT_STEP;
  394. else if (state[SDL_SCANCODE_I])
  395. bodies[1].position.z += SHIFT_STEP;
  396. else if (state[SDL_SCANCODE_K])
  397. bodies[1].position.z -= SHIFT_STEP;
  398. else if (state[SDL_SCANCODE_N])
  399. bodies[1].position.y += SHIFT_STEP;
  400. else if (state[SDL_SCANCODE_M])
  401. bodies[1].position.y -= SHIFT_STEP;
  402. if (state[SDL_SCANCODE_P])
  403. scene.camera.transform.translation.y += SHIFT_STEP;
  404. else if (state[SDL_SCANCODE_O])
  405. scene.camera.transform.translation.y -= SHIFT_STEP;
  406. #undef SHIFT_STEP
  407. SDL_RenderClear(renderer);
  408. SDL_RenderCopy(renderer,textureSDL,NULL,NULL);
  409. SDL_RenderPresent(renderer);
  410. time = time + MSPF - SDL_GetTicks();
  411. if (time > 1)
  412. usleep(time * 1000);
  413. frame++;
  414. }
  415. return 0;
  416. }