main2.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #define SAF_PROGRAM_NAME "tpe1"
  2. #define SAF_PLATFORM_POKITTO
  3. #define SAF_SETTING_ENABLE_SOUND 0
  4. #define SAF_SETTING_ENABLE_SAVES 0
  5. #define S3L_RESOLUTION_X SAF_SCREEN_WIDTH
  6. #define S3L_RESOLUTION_Y SAF_SCREEN_HEIGHT
  7. #define S3L_PIXEL_FUNCTION s3l_drawPixel
  8. #include "saf.h"
  9. #define S3L_Z_BUFFER 0
  10. #define S3L_SORT 1
  11. #define S3L_FLAT 1
  12. #define S3L_NEAR_CROSS_STRATEGY 1
  13. #define ROOM_SIZE (30 * TPE_F)
  14. #define TPE_RESHAPE_ITERATIONS 2
  15. #define TPE_APPROXIMATE_NET_SPEED 1
  16. #include "../tinyphysicsengine.h"
  17. #include "../programs/small3dlib.h"
  18. TPE_World tpe_world;
  19. TPE_Joint tpe_joints[30];
  20. TPE_Connection tpe_connections[35];
  21. TPE_Body tpe_bodies[4];
  22. static const S3L_Unit s3l_cubeVertices[S3L_CUBE_VERTEX_COUNT * 3] = { S3L_CUBE_VERTICES(S3L_FRACTIONS_PER_UNIT) };
  23. static const S3L_Index s3l_cubeTriangles[S3L_CUBE_TRIANGLE_COUNT * 3] = { S3L_CUBE_TRIANGLES };
  24. S3L_Model3D s3l_models[3];
  25. S3L_Scene s3l_scene;
  26. uint8_t debugDraw = 0;
  27. void tpe_debugDrawPixel(uint16_t x, uint16_t y, uint8_t color)
  28. {
  29. SAF_drawPixel(x,y,(color + 2) * 32 + color);
  30. }
  31. TPE_Vec3 tpe_environmentDistance(TPE_Vec3 p, TPE_Unit maxD)
  32. {
  33. return TPE_envAABoxInside(p,TPE_vec3(0,0,0),
  34. TPE_vec3(ROOM_SIZE,ROOM_SIZE,ROOM_SIZE));
  35. }
  36. static uint8_t sideColors[12] =
  37. { 32, 32, 64, 64, 96, 96, 128, 128, 160, 160, 192, 192 };
  38. uint8_t colorAdd;
  39. void s3l_drawPixel(S3L_PixelInfo *p)
  40. {
  41. SAF_drawPixel(p->x,p->y,sideColors[p->triangleIndex] + colorAdd);
  42. }
  43. void SAF_init(void)
  44. {
  45. S3L_model3DInit(s3l_cubeVertices,S3L_CUBE_VERTEX_COUNT,s3l_cubeTriangles,S3L_CUBE_TRIANGLE_COUNT,&s3l_models[0]);
  46. s3l_models[0].transform.scale.x = 3 * S3L_FRACTIONS_PER_UNIT;
  47. s3l_models[0].transform.scale.y = s3l_models[0].transform.scale.x;
  48. s3l_models[0].transform.scale.z = s3l_models[0].transform.scale.x;
  49. s3l_models[1] = s3l_models[0];
  50. s3l_models[2] = s3l_models[0];
  51. s3l_models[2].transform.scale.x = ROOM_SIZE;
  52. s3l_models[2].transform.scale.y = ROOM_SIZE;
  53. s3l_models[2].transform.scale.z = ROOM_SIZE;
  54. s3l_models[2].config.backfaceCulling = 1;
  55. s3l_models[2].triangleCount -= 2;
  56. S3L_sceneInit(s3l_models,2,&s3l_scene);
  57. s3l_scene.camera.transform.rotation.x -= TPE_F / 16;
  58. TPE_makeBox(tpe_joints,tpe_connections,
  59. 2 * TPE_F,2 * TPE_F,2 * TPE_F,TPE_F);
  60. TPE_bodyInit(&tpe_bodies[0],tpe_joints,8,tpe_connections,16,TPE_F);
  61. TPE_makeBox(tpe_joints + 8,tpe_connections + 16,
  62. 2 * TPE_F,2 * TPE_F,2 * TPE_F,TPE_F);
  63. TPE_bodyInit(&tpe_bodies[1],tpe_joints + 8,8,tpe_connections + 16,16,TPE_F);
  64. TPE_bodyMoveBy(&tpe_bodies[1],TPE_vec3(4 * TPE_F,0,0));
  65. TPE_worldInit(&tpe_world,tpe_bodies,2,tpe_environmentDistance);
  66. }
  67. uint8_t SAF_loop(void)
  68. {
  69. SAF_clearScreen(SAF_COLOR_GRAY);
  70. TPE_worldStep(&tpe_world);
  71. for (uint8_t i = 0; i < tpe_world.bodyCount; ++i)
  72. TPE_bodyApplyGravity(&tpe_world.bodies[i],TPE_F / 50);
  73. for (uint8_t i = 0; i < 2; ++i)
  74. {
  75. s3l_models[i].transform.translation.x =
  76. (tpe_bodies[i].joints[0].position.x +
  77. tpe_bodies[i].joints[7].position.x) / 2;
  78. s3l_models[i].transform.translation.y =
  79. (tpe_bodies[i].joints[0].position.y +
  80. tpe_bodies[i].joints[7].position.y) / 2;
  81. s3l_models[i].transform.translation.z =
  82. (tpe_bodies[i].joints[0].position.z +
  83. tpe_bodies[i].joints[7].position.z) / 2;
  84. TPE_Vec3 orient = TPE_bodyGetRotation(&tpe_bodies[i],0,1,2);
  85. s3l_models[i].transform.rotation.x = orient.x;
  86. s3l_models[i].transform.rotation.y = orient.y;
  87. s3l_models[i].transform.rotation.z = orient.z;
  88. }
  89. S3L_newFrame();
  90. colorAdd = 19;
  91. s3l_scene.models = s3l_models + 2;
  92. s3l_scene.modelCount = 1;
  93. S3L_drawScene(s3l_scene);
  94. colorAdd = 0;
  95. S3L_newFrame();
  96. s3l_scene.models = s3l_models;
  97. s3l_scene.modelCount = 2;
  98. S3L_drawScene(s3l_scene);
  99. if (SAF_buttonJustPressed(SAF_BUTTON_C))
  100. debugDraw = !debugDraw;
  101. if (debugDraw)
  102. TPE_worldDebugDraw(&tpe_world,tpe_debugDrawPixel,
  103. TPE_vec3(
  104. s3l_scene.camera.transform.translation.x,
  105. s3l_scene.camera.transform.translation.y,
  106. s3l_scene.camera.transform.translation.z),
  107. TPE_vec3(s3l_scene.camera.transform.rotation.x,s3l_scene.camera.transform.rotation.y,0),
  108. TPE_vec3(SAF_SCREEN_WIDTH,SAF_SCREEN_HEIGHT,TPE_F),4,TPE_F);
  109. s3l_scene.camera.transform.translation = s3l_models[0].transform.translation;
  110. s3l_scene.camera.transform.translation.y += 3 * TPE_F;
  111. s3l_scene.camera.transform.translation.z -= 5 * TPE_F;
  112. #define ACC 20
  113. if (SAF_buttonPressed(SAF_BUTTON_LEFT))
  114. TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3(-1 * ACC,0,0));
  115. else if (SAF_buttonPressed(SAF_BUTTON_RIGHT))
  116. TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3(ACC,0,0));
  117. else if (SAF_buttonPressed(SAF_BUTTON_UP))
  118. TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3(0,0,ACC));
  119. else if (SAF_buttonPressed(SAF_BUTTON_DOWN))
  120. TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3(0,0,-1 * ACC));
  121. if (SAF_buttonJustPressed(SAF_BUTTON_A))
  122. TPE_bodyAccelerate(&tpe_world.bodies[0],TPE_vec3(0,20 * ACC,0));
  123. #undef ACC
  124. return 1;
  125. }