1/*******************************************************************************************
2*
3* raylib [textures] example - Background scrolling
4*
5* This example has been created using raylib 2.0 (www.raylib.com)
6* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
7*
8* Copyright (c) 2019 Ramon Santamaria (@raysan5)
9*
10********************************************************************************************/
11
12#include "raylib.h"
13
14int main(void)
15{
16 // Initialization
17 //--------------------------------------------------------------------------------------
18 const int screenWidth = 800;
19 const int screenHeight = 450;
20
21 InitWindow(screenWidth, screenHeight, "raylib [textures] example - background scrolling");
22
23 // NOTE: Be careful, background width must be equal or bigger than screen width
24 // if not, texture should be draw more than two times for scrolling effect
25 Texture2D background = LoadTexture("resources/cyberpunk_street_background.png");
26 Texture2D midground = LoadTexture("resources/cyberpunk_street_midground.png");
27 Texture2D foreground = LoadTexture("resources/cyberpunk_street_foreground.png");
28
29 float scrollingBack = 0.0f;
30 float scrollingMid = 0.0f;
31 float scrollingFore = 0.0f;
32
33 SetTargetFPS(60); // Set our game to run at 60 frames-per-second
34 //--------------------------------------------------------------------------------------
35
36 // Main game loop
37 while (!WindowShouldClose()) // Detect window close button or ESC key
38 {
39 // Update
40 //----------------------------------------------------------------------------------
41 scrollingBack -= 0.1f;
42 scrollingMid -= 0.5f;
43 scrollingFore -= 1.0f;
44
45 // NOTE: Texture is scaled twice its size, so it sould be considered on scrolling
46 if (scrollingBack <= -background.width*2) scrollingBack = 0;
47 if (scrollingMid <= -midground.width*2) scrollingMid = 0;
48 if (scrollingFore <= -foreground.width*2) scrollingFore = 0;
49 //----------------------------------------------------------------------------------
50
51 // Draw
52 //----------------------------------------------------------------------------------
53 BeginDrawing();
54
55 ClearBackground(GetColor(0x052c46ff));
56
57 // Draw background image twice
58 // NOTE: Texture is scaled twice its size
59 DrawTextureEx(background, (Vector2){ scrollingBack, 20 }, 0.0f, 2.0f, WHITE);
60 DrawTextureEx(background, (Vector2){ background.width*2 + scrollingBack, 20 }, 0.0f, 2.0f, WHITE);
61
62 // Draw midground image twice
63 DrawTextureEx(midground, (Vector2){ scrollingMid, 20 }, 0.0f, 2.0f, WHITE);
64 DrawTextureEx(midground, (Vector2){ midground.width*2 + scrollingMid, 20 }, 0.0f, 2.0f, WHITE);
65
66 // Draw foreground image twice
67 DrawTextureEx(foreground, (Vector2){ scrollingFore, 70 }, 0.0f, 2.0f, WHITE);
68 DrawTextureEx(foreground, (Vector2){ foreground.width*2 + scrollingFore, 70 }, 0.0f, 2.0f, WHITE);
69
70 DrawText("BACKGROUND SCROLLING & PARALLAX", 10, 10, 20, RED);
71 DrawText("(c) Cyberpunk Street Environment by Luis Zuno (@ansimuz)", screenWidth - 330, screenHeight - 20, 10, RAYWHITE);
72
73 EndDrawing();
74 //----------------------------------------------------------------------------------
75 }
76
77 // De-Initialization
78 //--------------------------------------------------------------------------------------
79 UnloadTexture(background); // Unload background texture
80 UnloadTexture(midground); // Unload midground texture
81 UnloadTexture(foreground); // Unload foreground texture
82
83 CloseWindow(); // Close window and OpenGL context
84 //--------------------------------------------------------------------------------------
85
86 return 0;
87}