sprite.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "./common.h"
  2. #include "r3d.h"
  3. #include "raymath.h"
  4. /* === Resources === */
  5. static Camera3D camera = { 0 };
  6. static R3D_Mesh plane = { 0 };
  7. static R3D_Material material = { 0 };
  8. static Texture2D texture = { 0 };
  9. static R3D_Sprite sprite = { 0 };
  10. /* === Bird Data === */
  11. float birdDirX = 1.0f;
  12. Vector3 birdPos = { 0.0f, 0.5f, 0.0f };
  13. /* === Examples === */
  14. const char* Init(void)
  15. {
  16. /* --- Initialize R3D with screen resolution --- */
  17. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  18. SetTargetFPS(60);
  19. /* --- Generate a large plane to act as the ground --- */
  20. plane = R3D_GenMeshPlane(1000, 1000, 1, 1, true);
  21. material = R3D_GetDefaultMaterial();
  22. /* --- Load a sprite sheet texture and set its filter --- */
  23. texture = LoadTexture(RESOURCES_PATH "spritesheet.png");
  24. SetTextureFilter(texture, TEXTURE_FILTER_BILINEAR);
  25. /* --- Create a sprite from the loaded texture --- */
  26. sprite = R3D_LoadSprite(texture, 4, 1);
  27. /* --- Setup a spotlight in the scene --- */
  28. R3D_Light light = R3D_CreateLight(R3D_LIGHT_SPOT);
  29. {
  30. R3D_LightLookAt(light, (Vector3) { 0, 10, 10 }, (Vector3) { 0 });
  31. R3D_SetLightRange(light, 64.0f);
  32. R3D_EnableShadow(light, 1024);
  33. R3D_SetLightActive(light, true);
  34. }
  35. /* --- Setup the camera --- */
  36. camera = (Camera3D){
  37. .position = (Vector3) { 0, 2, 5 },
  38. .target = (Vector3) { 0, 0, 0 },
  39. .up = (Vector3) { 0, 1, 0 },
  40. .fovy = 60,
  41. };
  42. return "[r3d] - Sprite example";
  43. }
  44. void Update(float delta)
  45. {
  46. R3D_UpdateSprite(&sprite, 10 * delta);
  47. Vector3 birdPosPrev = birdPos;
  48. birdPos.x = 2.0f * sinf(GetTime());
  49. birdPos.y = 1.0f + cosf(GetTime() * 4.0f) * 0.5f;
  50. birdDirX = (birdPos.x - birdPosPrev.x >= 0) ? 1 : -1;
  51. }
  52. void Draw(void)
  53. {
  54. R3D_Begin(camera);
  55. R3D_DrawMesh(&plane, &material, MatrixTranslate(0, -0.5f, 0));
  56. R3D_DrawSpriteEx(&sprite, birdPos, (Vector2) { birdDirX, 1.0f }, 0.0f);
  57. R3D_End();
  58. }
  59. void Close(void)
  60. {
  61. R3D_UnloadSprite(&sprite);
  62. R3D_UnloadMesh(&plane);
  63. UnloadTexture(texture);
  64. R3D_Close();
  65. }