sprite.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "./common.h"
  2. /* === Resources === */
  3. static Camera3D camera = { 0 };
  4. static R3D_Mesh meshGround = { 0 };
  5. static R3D_Material matGround = { 0 };
  6. static R3D_Mesh meshSprite = { 0 };
  7. static R3D_Material matSprite = { 0 };
  8. /* === Bird Data === */
  9. float birdDirX = 1.0f;
  10. Vector3 birdPos = { 0.0f, 0.5f, 0.0f };
  11. /* === Sprite Helper === */
  12. void GetTexCoordScaleOffset(Vector2* uvScale, Vector2* uvOffset, int xFrameCount, int yFrameCount, float currentFrame)
  13. {
  14. uvScale->x = 1.0f / xFrameCount;
  15. uvScale->y = 1.0f / yFrameCount;
  16. int frameIndex = (int)(currentFrame + 0.5f) % (xFrameCount * yFrameCount);
  17. int frameX = frameIndex % xFrameCount;
  18. int frameY = frameIndex / xFrameCount;
  19. uvOffset->x = frameX * uvScale->x;
  20. uvOffset->y = frameY * uvScale->y;
  21. }
  22. /* === Examples === */
  23. const char* Init(void)
  24. {
  25. /* --- Initialize R3D with screen resolution --- */
  26. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  27. SetTargetFPS(60);
  28. /* --- Setup the ground mesh / material --- */
  29. meshGround = R3D_GenMeshPlane(200, 200, 1, 1);
  30. matGround = R3D_GetDefaultMaterial();
  31. matGround.albedo.color = GREEN;
  32. /* --- Setup the sprite mesh / material --- */
  33. meshSprite = R3D_GenMeshQuad(1.0f, 1.0f, 1, 1, (Vector3) { 0.0f, 0.0f, 1.0f });
  34. meshSprite.shadowCastMode = R3D_SHADOW_CAST_ON_DOUBLE_SIDED;
  35. matSprite = R3D_GetDefaultMaterial();
  36. matSprite.blendMode = R3D_BLEND_ALPHA;
  37. matSprite.billboardMode = R3D_BILLBOARD_Y_AXIS;
  38. matSprite.albedo.texture = LoadTexture(RESOURCES_PATH "spritesheet.png");
  39. /* --- Setup a spotlight in the scene --- */
  40. R3D_Light light = R3D_CreateLight(R3D_LIGHT_SPOT);
  41. {
  42. R3D_LightLookAt(light, (Vector3) { 0, 10, 10 }, (Vector3) { 0 });
  43. R3D_SetLightRange(light, 64.0f);
  44. R3D_EnableShadow(light, 1024);
  45. R3D_SetLightActive(light, true);
  46. }
  47. /* --- Setup the camera --- */
  48. camera = (Camera3D){
  49. .position = (Vector3) { 0, 2, 5 },
  50. .target = (Vector3) { 0, 0, 0 },
  51. .up = (Vector3) { 0, 1, 0 },
  52. .fovy = 60,
  53. };
  54. return "[r3d] - Sprite example";
  55. }
  56. void Update(float delta)
  57. {
  58. /* --- Update bird --- */
  59. Vector3 birdPosPrev = birdPos;
  60. birdPos.x = 2.0f * sinf((float)GetTime());
  61. birdPos.y = 1.0f + cosf((float)GetTime() * 4.0f) * 0.5f;
  62. birdDirX = (birdPos.x - birdPosPrev.x >= 0.0f) ? 1.0f : -1.0f;
  63. /* --- Update sprite --- */
  64. float currentFrame = 10.0f * GetTime();
  65. GetTexCoordScaleOffset(
  66. &matSprite.uvScale,
  67. &matSprite.uvOffset,
  68. 4 * birdDirX, // Multiply by the sign of the X direction to invert the uvScale.x
  69. 1,
  70. currentFrame
  71. );
  72. }
  73. void Draw(void)
  74. {
  75. R3D_Begin(camera);
  76. R3D_DrawMesh(&meshGround, &matGround, MatrixTranslate(0, -0.5f, 0));
  77. R3D_DrawMesh(&meshSprite, &matSprite, MatrixTranslate(birdPos.x, birdPos.y, 0.0f));
  78. R3D_End();
  79. }
  80. void Close(void)
  81. {
  82. R3D_UnloadMaterial(&matSprite);
  83. R3D_UnloadMesh(&meshSprite);
  84. R3D_UnloadMesh(&meshGround);
  85. R3D_Close();
  86. }