city.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. Example program for small3dlib -- a GTA-like game demo.
  3. author: Miloslav Ciz
  4. license: CC0 1.0
  5. */
  6. #include "Pokitto.h"
  7. #include <stdio.h>
  8. Pokitto::Core pokitto;
  9. #define S3L_FLAT 0
  10. #define S3L_STRICT_NEAR_CULLING 0
  11. #define S3L_PERSPECTIVE_CORRECTION 2
  12. #define S3L_SORT 0
  13. #define S3L_STENCIL_BUFFER 0
  14. #define S3L_Z_BUFFER 2
  15. #define S3L_PIXEL_FUNCTION drawPixel
  16. #define S3L_RESOLUTION_X 110
  17. #define S3L_RESOLUTION_Y 88
  18. #include "small3dlib.h"
  19. #include "cityModel.h"
  20. #include "cityTexture.h"
  21. #include "carModel.h"
  22. #define MAX_VELOCITY 700
  23. #define ACCELERATION 600
  24. #define TURN_SPEED 200
  25. #define FRICTION 600
  26. S3L_Model3D models[2];
  27. const uint8_t collisionMap[8 * 10] =
  28. {
  29. 1,1,1,1,1,1,1,1,
  30. 1,1,1,1,0,0,0,1,
  31. 1,1,1,1,0,1,0,1,
  32. 2,2,1,0,0,0,0,3,
  33. 1,2,1,0,1,1,3,1,
  34. 2,0,0,0,1,1,3,3,
  35. 1,0,1,0,0,1,1,1,
  36. 1,0,0,0,1,1,1,1,
  37. 1,1,1,1,1,1,1,1,
  38. 1,1,1,1,1,1,1,1
  39. };
  40. S3L_Scene scene;
  41. inline uint8_t sampleTexture(int32_t u, int32_t v)
  42. {
  43. uint32_t index = v * CITY_TEXTURE_WIDTH + u;
  44. return cityTexture[index];
  45. }
  46. uint32_t previousTriangle = -1;
  47. S3L_Vec4 uv0, uv1, uv2;
  48. void drawPixel(S3L_PixelInfo *p)
  49. {
  50. if (p->triangleID != previousTriangle)
  51. {
  52. const S3L_Index *uvIndices;
  53. const S3L_Unit *uvs;
  54. if (p->modelIndex == 0)
  55. {
  56. uvIndices = cityUVIndices;
  57. uvs = cityUVs;
  58. }
  59. else
  60. {
  61. uvIndices = carUVIndices;
  62. uvs = carUVs;
  63. }
  64. S3L_getIndexedTriangleValues(p->triangleIndex,uvIndices,uvs,2,&uv0,&uv1,&uv2);
  65. previousTriangle = p->triangleID;
  66. }
  67. S3L_Unit uv[2];
  68. uv[0] = S3L_interpolateBarycentric(uv0.x,uv1.x,uv2.x,p->barycentric);
  69. uv[1] = S3L_interpolateBarycentric(uv0.y,uv1.y,uv2.y,p->barycentric);
  70. uint8_t *buf = pokitto.display.screenbuffer;
  71. buf += p->y * 110;
  72. buf += p->x;
  73. *buf = sampleTexture(uv[0] >> 2,uv[1] >> 2);
  74. }
  75. static inline void draw()
  76. {
  77. S3L_newFrame();
  78. S3L_drawScene(scene);
  79. }
  80. static inline uint8_t collision(S3L_Vec4 worldPosition)
  81. {
  82. worldPosition.x /= S3L_FRACTIONS_PER_UNIT;
  83. worldPosition.z /= -S3L_FRACTIONS_PER_UNIT;
  84. uint16_t index = worldPosition.z * 8 + worldPosition.x;
  85. return collisionMap[index];
  86. }
  87. static inline void handleCollision(S3L_Vec4 *pos, S3L_Vec4 previousPos)
  88. {
  89. S3L_Vec4 newPos = *pos;
  90. newPos.x = previousPos.x;
  91. if (collision(newPos))
  92. {
  93. newPos = *pos;
  94. newPos.z = previousPos.z;
  95. if (collision(newPos))
  96. newPos = previousPos;
  97. }
  98. *pos = newPos;
  99. }
  100. int main()
  101. {
  102. pokitto.begin();
  103. // pokitto.display.persistence = 1;
  104. pokitto.setFrameRate(60);
  105. pokitto.display.load565Palette(cityPalette);
  106. pokitto.display.bgcolor = 78;
  107. cityModelInit();
  108. carModelInit();
  109. models[0] = cityModel;
  110. models[1] = carModel;
  111. S3L_initScene(models,2,&scene);
  112. S3L_setTransform3D(1909,16,-3317,0,-510,0,512,512,512,&(models[1].transform));
  113. S3L_Vec4 carDirection;
  114. S3L_initVec4(&carDirection);
  115. scene.camera.transform.translation.y = S3L_FRACTIONS_PER_UNIT / 2;
  116. scene.camera.transform.rotation.x = -S3L_FRACTIONS_PER_UNIT / 16;
  117. int16_t velocity = 0;
  118. int16_t previousTime = 0;
  119. while (pokitto.isRunning())
  120. {
  121. if (pokitto.update())
  122. {
  123. models[1].transform.rotation.y += models[1].transform.rotation.z; // overturn the car for the rendering
  124. draw();
  125. models[1].transform.rotation.y -= models[1].transform.rotation.z; // turn the car back for the physics
  126. int16_t timeNow = pokitto.getTime();
  127. int16_t frameDiffMs = timeNow - previousTime;
  128. previousTime = timeNow;
  129. int16_t step = (velocity * frameDiffMs) / 1024;
  130. int16_t stepFriction = (FRICTION * frameDiffMs) / 1024;
  131. int16_t stepRotation = TURN_SPEED * frameDiffMs * S3L_max(0,velocity - 200) / (MAX_VELOCITY * 1024);
  132. int16_t stepVelocity = S3L_nonZero((ACCELERATION * frameDiffMs) / 1024);
  133. if (stepRotation == 0 && S3L_abs(velocity) >= 200)
  134. stepRotation = 10;
  135. if (velocity < 0)
  136. stepRotation *= -1;
  137. if (pokitto.leftBtn())
  138. {
  139. models[1].transform.rotation.y += stepRotation;
  140. models[1].transform.rotation.z =
  141. S3L_min(S3L_abs(velocity) / 64, models[1].transform.rotation.z + 1);
  142. }
  143. else if (pokitto.rightBtn())
  144. {
  145. models[1].transform.rotation.y -= stepRotation;
  146. models[1].transform.rotation.z =
  147. S3L_max(-S3L_abs(velocity) / 64, models[1].transform.rotation.z - 1);
  148. }
  149. else
  150. models[1].transform.rotation.z = (models[1].transform.rotation.z * 3) / 4;
  151. S3L_rotationToDirections(models[1].transform.rotation,S3L_FRACTIONS_PER_UNIT,&carDirection,0,0);
  152. S3L_Vec4 previousCarPos = models[1].transform.translation;
  153. S3L_Vec4 previousCamPos = scene.camera.transform.translation;
  154. int16_t friction = 0;
  155. if (pokitto.upBtn())
  156. velocity = S3L_min(MAX_VELOCITY,velocity + (velocity < 0 ? (2 * stepVelocity) : stepVelocity));
  157. else if (pokitto.downBtn())
  158. velocity = S3L_max(-MAX_VELOCITY,velocity - (velocity > 0 ? (2 * stepVelocity) : stepVelocity));
  159. else
  160. friction = 1;
  161. models[1].transform.translation.x += (carDirection.x * step) / S3L_FRACTIONS_PER_UNIT;
  162. models[1].transform.translation.z += (carDirection.z * step) / S3L_FRACTIONS_PER_UNIT;
  163. uint8_t coll = collision(models[1].transform.translation);
  164. if (coll != 0)
  165. {
  166. if (coll == 1)
  167. {
  168. handleCollision(&(models[1].transform.translation),previousCarPos);
  169. friction = 2;
  170. }
  171. else if (coll == 2)
  172. {
  173. // teleport the car
  174. models[1].transform.translation.x += 5 * S3L_FRACTIONS_PER_UNIT;
  175. models[1].transform.translation.z += 2 * S3L_FRACTIONS_PER_UNIT;
  176. }
  177. else
  178. {
  179. // teleport the car
  180. models[1].transform.translation.x -= 5 * S3L_FRACTIONS_PER_UNIT;
  181. models[1].transform.translation.z -= 2 * S3L_FRACTIONS_PER_UNIT;
  182. }
  183. }
  184. if (velocity > 0)
  185. velocity = S3L_max(0,velocity - stepFriction * friction);
  186. else
  187. velocity = S3L_min(0,velocity + stepFriction * friction);
  188. S3L_Unit cameraDistance =
  189. S3L_interpolate(S3L_FRACTIONS_PER_UNIT / 2,(3 * S3L_FRACTIONS_PER_UNIT) / 4,S3L_abs(velocity),MAX_VELOCITY);
  190. scene.camera.transform.translation.x =
  191. scene.models[1].transform.translation.x - (carDirection.x * cameraDistance) / S3L_FRACTIONS_PER_UNIT;
  192. scene.camera.transform.translation.z =
  193. scene.models[1].transform.translation.z - (carDirection.z * cameraDistance) / S3L_FRACTIONS_PER_UNIT;
  194. scene.camera.transform.rotation.y = models[1].transform.rotation.y;
  195. }
  196. }
  197. return 0;
  198. }