sprite.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <r3d/r3d.h>
  2. #include <raymath.h>
  3. #ifndef RESOURCES_PATH
  4. # define RESOURCES_PATH "./"
  5. #endif
  6. static void GetTexCoordScaleOffset(Vector2* uvScale, Vector2* uvOffset, int xFrameCount, int yFrameCount, float currentFrame);
  7. int main(void)
  8. {
  9. // Initialize window
  10. InitWindow(800, 450, "[r3d] - Sprite example");
  11. SetTargetFPS(60);
  12. // Initialize R3D
  13. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  14. // Set background/ambient color
  15. R3D_ENVIRONMENT_SET(background.color, (Color){102, 191, 255, 255});
  16. R3D_ENVIRONMENT_SET(ambient.color, (Color){10, 19, 25, 255});
  17. // Create ground mesh and material
  18. R3D_Mesh meshGround = R3D_GenMeshPlane(200, 200, 1, 1);
  19. R3D_Material matGround = R3D_GetDefaultMaterial();
  20. matGround.albedo.color = GREEN;
  21. // Create sprite mesh and material
  22. R3D_Mesh meshSprite = R3D_GenMeshQuad(1.0f, 1.0f, 1, 1, (Vector3){0,0,1});
  23. meshSprite.shadowCastMode = R3D_SHADOW_CAST_ON_DOUBLE_SIDED;
  24. R3D_Material matSprite = R3D_GetDefaultMaterial();
  25. matSprite.billboardMode = R3D_BILLBOARD_Y_AXIS;
  26. matSprite.albedo.texture = LoadTexture(RESOURCES_PATH "spritesheet.png");
  27. // Setup spotlight
  28. R3D_Light light = R3D_CreateLight(R3D_LIGHT_SPOT);
  29. R3D_LightLookAt(light, (Vector3){0,10,10}, (Vector3){0});
  30. R3D_SetLightRange(light, 64.0f);
  31. R3D_EnableShadow(light, 1024);
  32. R3D_SetLightActive(light, true);
  33. // Setup camera
  34. Camera3D camera = {
  35. .position = {0, 2, 5},
  36. .target = {0, 0.5f, 0},
  37. .up = {0, 1, 0},
  38. .fovy = 45
  39. };
  40. // Bird data
  41. Vector3 birdPos = {0, 0.5f, 0};
  42. float birdDirX = 1.0f;
  43. // Main loop
  44. while (!WindowShouldClose())
  45. {
  46. // Update bird position
  47. Vector3 birdPrev = birdPos;
  48. birdPos.x = 2.0f * sinf((float)GetTime());
  49. birdPos.y = 1.0f + cosf((float)GetTime() * 4.0f) * 0.5f;
  50. birdDirX = (birdPos.x - birdPrev.x >= 0.0f) ? 1.0f : -1.0f;
  51. // Update sprite UVs
  52. // We multiply by the sign of the X direction to invert the uvScale.x
  53. float currentFrame = 10.0f * GetTime();
  54. GetTexCoordScaleOffset(&matSprite.uvScale, &matSprite.uvOffset, 4 * birdDirX, 1, currentFrame);
  55. BeginDrawing();
  56. ClearBackground(RAYWHITE);
  57. // Draw scene
  58. R3D_Begin(camera);
  59. R3D_DrawMesh(&meshGround, &matGround, MatrixTranslate(0, -0.5f, 0));
  60. R3D_DrawMesh(&meshSprite, &matSprite, MatrixTranslate(birdPos.x, birdPos.y, 0));
  61. R3D_End();
  62. EndDrawing();
  63. }
  64. // Cleanup
  65. R3D_UnloadMaterial(&matSprite);
  66. R3D_UnloadMesh(&meshSprite);
  67. R3D_UnloadMesh(&meshGround);
  68. R3D_Close();
  69. CloseWindow();
  70. return 0;
  71. }
  72. void GetTexCoordScaleOffset(Vector2* uvScale, Vector2* uvOffset, int xFrameCount, int yFrameCount, float currentFrame)
  73. {
  74. uvScale->x = 1.0f / xFrameCount;
  75. uvScale->y = 1.0f / yFrameCount;
  76. int frameIndex = (int)(currentFrame + 0.5f) % (xFrameCount * yFrameCount);
  77. int frameX = frameIndex % xFrameCount;
  78. int frameY = frameIndex / xFrameCount;
  79. uvOffset->x = frameX * uvScale->x;
  80. uvOffset->y = frameY * uvScale->y;
  81. }