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 @raysan5 (@ Santamaria")
13+ *
14+ ********************************************************************************************/
15+
16+ #include "raylib.h"
17+
18+ //------------------------------------------------------------------------------------
19+ // Program main entry point
20+ //------------------------------------------------------------------------------------
21+ int main (void )
22+ {
23+ // Initialization
24+ //---------------------------------------------------------
25+ const int screenWidth = 800 ;
26+ const int screenHeight = 450 ;
27+
28+ InitWindow (screenWidth , screenHeight , "raylib [core] example - render texture" );
29+
30+ // Define a render texture to render
31+ int renderTextureWidth = 300 ;
32+ int renderTextureHeight = 300 ;
33+ RenderTexture2D target = LoadRenderTexture (renderTextureWidth , renderTextureHeight );
34+
35+ Vector2 ballPosition = { renderTextureWidth /2.0f , renderTextureHeight /2.0f };
36+ Vector2 ballSpeed = { 5.0f , 4.0f };
37+ int ballRadius = 20 ;
38+
39+ float rotation = 0.0f ;
40+
41+ SetTargetFPS (60 ); // Set our game to run at 60 frames-per-second
42+ //----------------------------------------------------------
43+
44+ // Main game loop
45+ while (!WindowShouldClose ()) // Detect window close button or ESC key
46+ {
47+ // Update
48+ //-----------------------------------------------------
49+ // Ball movement logic
50+ ballPosition .x += ballSpeed .x ;
51+ ballPosition .y += ballSpeed .y ;
52+
53+ // Check walls collision for bouncing
54+ if ((ballPosition .x >= (renderTextureWidth - ballRadius )) || (ballPosition .x <= ballRadius )) ballSpeed .x *= -1.0f ;
55+ if ((ballPosition .y >= (renderTextureHeight - ballRadius )) || (ballPosition .y <= ballRadius )) ballSpeed .y *= -1.0f ;
56+
57+ // Render texture rotation
58+ rotation += 0.5f ;
59+ //-----------------------------------------------------
60+
61+ // Draw
62+ //-----------------------------------------------------
63+ // Draw our scene to the render texture
64+ BeginTextureMode (target );
65+
66+ ClearBackground (SKYBLUE );
67+
68+ DrawCircleV (ballPosition , (float )ballRadius , MAROON );
69+
70+ EndTextureMode ();
71+
72+ // Draw render texture to main framebuffer
73+ BeginDrawing ();
74+
75+ ClearBackground (RAYWHITE );
76+
77+ // Draw our render texture with rotation applied
78+ // NOTE: We set the origin of the texture to the center of the render texture
79+ DrawTexturePro (target .texture ,
80+ (Rectangle ){ 0 , 0 , target .texture .width , - target .texture .height },
81+ (Rectangle ){ screenWidth /2 , screenHeight /2 , target .texture .width , - target .texture .height },
82+ (Vector2 ){ target .texture .width /2 , target .texture .height /2 }, rotation , WHITE );
83+
84+ DrawText ("DRAWING BOUNCING BALL INSIDE RENDER TEXTURE!" , 10 , screenHeight - 40 , 20 , BLACK );
85+
86+
87+ DrawFPS (10 , 10 );
88+
89+ EndDrawing ();
90+ //-----------------------------------------------------
91+ }
92+
93+ // De-Initialization
94+ //---------------------------------------------------------
95+ CloseWindow (); // Close window and OpenGL context
96+ //----------------------------------------------------------
97+
98+ return 0 ;
99+ }
0 commit comments