sprite.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  17. SetTargetFPS(60);
  18. plane = R3D_GenMeshPlane(1000, 1000, 1, 1, true);
  19. material = R3D_GetDefaultMaterial();
  20. texture = LoadTexture(RESOURCES_PATH "spritesheet.png");
  21. sprite = R3D_LoadSprite(texture, 4, 1);
  22. camera = (Camera3D){
  23. .position = (Vector3) { 0, 2, 5 },
  24. .target = (Vector3) { 0, 0, 0 },
  25. .up = (Vector3) { 0, 1, 0 },
  26. .fovy = 60,
  27. };
  28. R3D_Light light = R3D_CreateLight(R3D_LIGHT_SPOT);
  29. {
  30. R3D_LightLookAt(light, (Vector3) { 0, 10, 10 }, (Vector3) { 0 });
  31. R3D_SetLightActive(light, true);
  32. }
  33. return "[r3d] - Sprite example";
  34. }
  35. void Update(float delta)
  36. {
  37. R3D_UpdateSprite(&sprite, 10 * delta);
  38. Vector3 birdPosPrev = birdPos;
  39. birdPos.x = 2.0f * sinf(GetTime());
  40. birdPos.y = 1.0f + cosf(GetTime() * 4.0f) * 0.5f;
  41. birdDirX = (birdPos.x - birdPosPrev.x >= 0) ? 1 : -1;
  42. }
  43. void Draw(void)
  44. {
  45. R3D_Begin(camera);
  46. R3D_DrawMesh(&plane, &material, MatrixTranslate(0, -0.5f, 0));
  47. R3D_DrawSpriteEx(&sprite, birdPos, (Vector2) { birdDirX, 1.0f }, 0.0f);
  48. R3D_End();
  49. }
  50. void Close(void)
  51. {
  52. R3D_UnloadSprite(&sprite);
  53. R3D_UnloadMesh(&plane);
  54. UnloadTexture(texture);
  55. R3D_Close();
  56. }