sprite.c 3.2 KB

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