raylib/ports/textures/textures_background_scrolling
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
textures_background_scrolling.odin ¶97 linesSource
1/*******************************************************************************************
2*
3* raylib [textures] example - background scrolling
4*
5* Example complexity rating: [★☆☆☆] 1/4
6*
7* Example originally created with raylib 2.0, last time updated with raylib 2.5
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) 2019-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 [textures] example - background scrolling")
30
31 // NOTE: Be careful, background width must be equal or bigger than screen width
32 // if not, texture should be draw more than two times for scrolling effect
33 background := rl.LoadTexture("resources/cyberpunk_street_background.png")
34 midground := rl.LoadTexture("resources/cyberpunk_street_midground.png")
35 foreground := rl.LoadTexture("resources/cyberpunk_street_foreground.png")
36
37 scrolling_back: f32
38 scrolling_mid: f32
39 scrolling_fore: f32
40
41 rl.SetTargetFPS(60) // Set our game to run at 60 frames-per-second
42 //--------------------------------------------------------------------------------------
43
44 // Main game loop
45 for !rl.WindowShouldClose() { // Detect window close button or ESC key
46 // Update
47 //----------------------------------------------------------------------------------
48 scrolling_back -= 0.1
49 scrolling_mid -= 0.5
50 scrolling_fore -= 1
51
52 // NOTE: Texture is scaled twice its size, so it sould be considered on scrolling
53 if scrolling_back <= f32(-background.width*2) {
54 scrolling_back = 0
55 }
56 if scrolling_mid <= f32(-midground.width*2) {
57 scrolling_mid = 0
58 }
59 if scrolling_fore <= f32(-foreground.width*2) {
60 scrolling_fore = 0
61 }
62 //----------------------------------------------------------------------------------
63
64 // Draw
65 //----------------------------------------------------------------------------------
66 rl.BeginDrawing()
67
68 rl.ClearBackground(rl.GetColor(0x052c46ff))
69
70 // Draw background image twice
71 // NOTE: Texture is scaled twice its size
72 rl.DrawTextureEx(background, {scrolling_back, 20}, 0, 2, rl.WHITE)
73 rl.DrawTextureEx(background, {f32(background.width*2) + scrolling_back, 20}, 0, 2, rl.WHITE)
74
75 // Draw midground image twice
76 rl.DrawTextureEx(midground, {scrolling_mid, 20}, 0, 2, rl.WHITE)
77 rl.DrawTextureEx(midground, {f32(midground.width*2) + scrolling_mid, 20}, 0, 2, rl.WHITE)
78
79 // Draw foreground image twice
80 rl.DrawTextureEx(foreground, {scrolling_fore, 70}, 0, 2, rl.WHITE)
81 rl.DrawTextureEx(foreground, {f32(foreground.width*2) + scrolling_fore, 70}, 0, 2, rl.WHITE)
82
83 rl.DrawText("BACKGROUND SCROLLING & PARALLAX", 10, 10, 20, rl.RED)
84 rl.DrawText("(c) Cyberpunk Street Environment by Luis Zuno (@ansimuz)", SCREEN_WIDTH - 330, SCREEN_HEIGHT - 20, 10, rl.RAYWHITE)
85
86 rl.EndDrawing()
87 //----------------------------------------------------------------------------------
88 }
89
90 // De-Initialization
91 //--------------------------------------------------------------------------------------
92 rl.UnloadTexture(background) // Unload background texture
93 rl.UnloadTexture(midground) // Unload midground texture
94 rl.UnloadTexture(foreground) // Unload foreground texture
95 rl.CloseWindow() // Close window and OpenGL context
96 //--------------------------------------------------------------------------------------
97}
Assets 3
- resources/cyberpunk_street_background.png
- resources/cyberpunk_street_foreground.png
- resources/cyberpunk_street_midground.png
Declarations Used 15
- vendor:raylib BeginDrawing · ClearBackground · CloseWindow · DrawText · DrawTextureEx · EndDrawing · GetColor · InitWindow · LoadTexture · RAYWHITE · RED · SetTargetFPS · UnloadTexture · WHITE · WindowShouldClose