raylib/ports/core/core_render_texture
Licence This example has its own licence, rather than Odin's: raylib/ports/LICENSE
These are ported Raylib examples, and as such, they are subject to Raylib's license.
Copyright (c) 2013-2024 Ramon Santamaria (@raysan5)
The rest of the licence
This software is provided "as-is", without any express or implied warranty. In no event
will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, including commercial
applications, and to alter it and redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you
wrote the original software. If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented
as being the original software.
3. This notice may not be removed or altered from any source distribution.
Code
core_render_texture.odin ¶103 linesSource
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
16package raylib_examples
17
18import rl "vendor:raylib"
19
20//------------------------------------------------------------------------------------
21// Program main entry point
22//------------------------------------------------------------------------------------
23main :: proc() {
24 // Initialization
25 //---------------------------------------------------------
26 SCREEN_WIDTH :: 800
27 SCREEN_HEIGHT :: 450
28
29 rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [core] example - render texture")
30
31 // Define a render texture to render
32 render_texture_width: i32 = 300
33 render_texture_height: i32 = 300
34 target: rl.RenderTexture2D = rl.LoadRenderTexture(render_texture_width, render_texture_height)
35
36 ball_position := rl.Vector2 {f32(render_texture_width)/2, f32(render_texture_height)/2}
37 ball_speed := rl.Vector2 {5.0, 4.0}
38 ball_radius: i32 = 20
39
40 rotation: f32
41
42 rl.SetTargetFPS(60) // Set our game to run at 60 frames-per-second
43 //----------------------------------------------------------
44
45 // Main game loop
46 for !rl.WindowShouldClose() { // Detect window close button or ESC key
47 // Update
48 //-----------------------------------------------------
49 // Ball movement logic
50 ball_position.x += ball_speed.x
51 ball_position.y += ball_speed.y
52
53 // Check walls collision for bouncing
54 if ball_position.x >= f32(render_texture_width - ball_radius) || ball_position.x <= f32(ball_radius) {
55 ball_speed.x *= -1
56 }
57 if ball_position.y >= f32(render_texture_height - ball_radius) || ball_position.y <= f32(ball_radius) {
58 ball_speed.y *= -1
59 }
60
61 // Render texture rotation
62 rotation += 0.5
63 //-----------------------------------------------------
64
65 // Draw
66 //-----------------------------------------------------
67 // Draw our scene to the render texture
68 rl.BeginTextureMode(target)
69
70 rl.ClearBackground(rl.SKYBLUE)
71
72 rl.DrawCircleV(ball_position, f32(ball_radius), rl.MAROON)
73
74 rl.EndTextureMode()
75
76 // Draw render texture to main framebuffer
77 rl.BeginDrawing()
78
79 rl.ClearBackground(rl.RAYWHITE)
80
81 // Draw our render texture with rotation applied
82 // NOTE: We set the origin of the texture to the center of the render texture
83 texture_width := f32(target.texture.width)
84 texture_height := f32(target.texture.height)
85 rl.DrawTexturePro(target.texture,
86 {0, 0, texture_width, -texture_height},
87 {SCREEN_WIDTH/2, SCREEN_HEIGHT/2, texture_width, -texture_height},
88 {texture_width/2, texture_height/2}, rotation, rl.WHITE)
89
90 rl.DrawText("DRAWING BOUNCING BALL INSIDE RENDER TEXTURE!", 10, SCREEN_HEIGHT - 40, 20, rl.BLACK)
91
92
93 rl.DrawFPS(10, 10)
94
95 rl.EndDrawing()
96 //-----------------------------------------------------
97 }
98
99 // De-Initialization
100 //---------------------------------------------------------
101 rl.CloseWindow() // Close window and OpenGL context
102 //----------------------------------------------------------
103}
Declarations Used 21
- vendor:raylib BeginDrawing · BeginTextureMode · BLACK · ClearBackground · CloseWindow · DrawCircleV · DrawFPS · DrawText · DrawTexturePro · EndDrawing · EndTextureMode · InitWindow · LoadRenderTexture · MAROON · RAYWHITE · RenderTexture2D · SetTargetFPS · SKYBLUE · Vector2 · WHITE · WindowShouldClose