resize.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "./common.h"
  2. #include "r3d.h"
  3. #include <rlgl.h>
  4. /* === Resources === */
  5. static Camera3D camera = { 0 };
  6. static R3D_Mesh sphere = { 0 };
  7. static R3D_Material materials[5] = { 0 };
  8. /* === Example === */
  9. const char* Init(void)
  10. {
  11. /* --- Initialize R3D with its internal resolution --- */
  12. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  13. /* --- Some raylib setup --- */
  14. SetWindowState(FLAG_WINDOW_RESIZABLE);
  15. SetTargetFPS(60);
  16. /* --- Generate a sphere mesh and configure its material --- */
  17. sphere = R3D_GenMeshSphere(0.5f, 64, 64, true);
  18. for (int i = 0; i < 5; i++) {
  19. materials[i] = R3D_GetDefaultMaterial();
  20. materials[i].albedo.color = ColorFromHSV((float)i / 5 * 330, 1.0f, 1.0f);
  21. }
  22. /* --- Setup the scene lighting --- */
  23. R3D_Light light = R3D_CreateLight(R3D_LIGHT_DIR);
  24. {
  25. R3D_SetLightDirection(light, (Vector3) { 0, 0, -1 });
  26. R3D_SetLightActive(light, true);
  27. }
  28. /* --- Setup the camera --- */
  29. camera = (Camera3D){
  30. .position = (Vector3) { 0, 2, 2 },
  31. .target = (Vector3) { 0, 0, 0 },
  32. .up = (Vector3) { 0, 1, 0 },
  33. .fovy = 60,
  34. };
  35. return "[r3d] - Resize example";
  36. }
  37. void Update(float delta)
  38. {
  39. UpdateCamera(&camera, CAMERA_ORBITAL);
  40. if (IsKeyPressed(KEY_R)) {
  41. bool keep = R3D_HasState(R3D_FLAG_ASPECT_KEEP);
  42. if (keep) R3D_ClearState(R3D_FLAG_ASPECT_KEEP);
  43. else R3D_SetState(R3D_FLAG_ASPECT_KEEP);
  44. }
  45. if (IsKeyPressed(KEY_F)) {
  46. bool linear = R3D_HasState(R3D_FLAG_BLIT_LINEAR);
  47. if (linear) R3D_ClearState(R3D_FLAG_BLIT_LINEAR);
  48. else R3D_SetState(R3D_FLAG_BLIT_LINEAR);
  49. }
  50. }
  51. void Draw(void)
  52. {
  53. bool keep = R3D_HasState(R3D_FLAG_ASPECT_KEEP);
  54. bool linear = R3D_HasState(R3D_FLAG_BLIT_LINEAR);
  55. if (keep) {
  56. ClearBackground(BLACK);
  57. }
  58. R3D_Begin(camera);
  59. rlPushMatrix();
  60. for (int i = 0; i < 5; i++) {
  61. R3D_DrawMesh(&sphere, &materials[i], MatrixTranslate(i - 2, 0, 0));
  62. }
  63. rlPopMatrix();
  64. R3D_End();
  65. DrawText(TextFormat("Resize mode: %s", keep ? "KEEP" : "EXPAND"), 10, 10, 20, BLACK);
  66. DrawText(TextFormat("Filter mode: %s", linear ? "LINEAR" : "NEAREST"), 10, 40, 20, BLACK);
  67. }
  68. void Close(void)
  69. {
  70. R3D_UnloadMesh(&sphere);
  71. R3D_Close();
  72. }