core_render_texture.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - render texture
  4. *
  5. * Example complexity rating: [★☆☆☆] 1/4
  6. *
  7. * Example originally created with raylib 5.6-dev, last time updated with raylib 5.6-dev
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2025 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. //------------------------------------------------------------------------------------
  17. // Program main entry point
  18. //------------------------------------------------------------------------------------
  19. int main(void)
  20. {
  21. // Initialization
  22. //---------------------------------------------------------
  23. const int screenWidth = 800;
  24. const int screenHeight = 450;
  25. InitWindow(screenWidth, screenHeight, "raylib [core] example - render texture");
  26. // Define a render texture to render
  27. int renderTextureWidth = 300;
  28. int renderTextureHeight = 300;
  29. RenderTexture2D target = LoadRenderTexture(renderTextureWidth, renderTextureHeight);
  30. Vector2 ballPosition = { renderTextureWidth/2.0f, renderTextureHeight/2.0f };
  31. Vector2 ballSpeed = { 5.0f, 4.0f };
  32. int ballRadius = 20;
  33. float rotation = 0.0f;
  34. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  35. //----------------------------------------------------------
  36. // Main game loop
  37. while (!WindowShouldClose()) // Detect window close button or ESC key
  38. {
  39. // Update
  40. //-----------------------------------------------------
  41. // Ball movement logic
  42. ballPosition.x += ballSpeed.x;
  43. ballPosition.y += ballSpeed.y;
  44. // Check walls collision for bouncing
  45. if ((ballPosition.x >= (renderTextureWidth - ballRadius)) || (ballPosition.x <= ballRadius)) ballSpeed.x *= -1.0f;
  46. if ((ballPosition.y >= (renderTextureHeight - ballRadius)) || (ballPosition.y <= ballRadius)) ballSpeed.y *= -1.0f;
  47. // Render texture rotation
  48. rotation += 0.5f;
  49. //-----------------------------------------------------
  50. // Draw
  51. //-----------------------------------------------------
  52. // Draw our scene to the render texture
  53. BeginTextureMode(target);
  54. ClearBackground(SKYBLUE);
  55. DrawRectangle(0, 0, 20, 20, RED);
  56. DrawCircleV(ballPosition, (float)ballRadius, MAROON);
  57. EndTextureMode();
  58. // Draw render texture to main framebuffer
  59. BeginDrawing();
  60. ClearBackground(RAYWHITE);
  61. // Draw our render texture with rotation applied
  62. // NOTE 1: We set the origin of the texture to the center of the render texture
  63. // NOTE 2: We flip vertically the texture setting negative source rectangle height
  64. DrawTexturePro(target.texture,
  65. (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height },
  66. (Rectangle){ screenWidth/2.0f, screenHeight/2.0f, (float)target.texture.width, (float)target.texture.height },
  67. (Vector2){ target.texture.width/2.0f, target.texture.height/2.0f }, rotation, WHITE);
  68. DrawText("DRAWING BOUNCING BALL INSIDE RENDER TEXTURE!", 10, screenHeight - 40, 20, BLACK);
  69. DrawFPS(10, 10);
  70. EndDrawing();
  71. //-----------------------------------------------------
  72. }
  73. // De-Initialization
  74. //---------------------------------------------------------
  75. CloseWindow(); // Close window and OpenGL context
  76. //----------------------------------------------------------
  77. return 0;
  78. }