mirror of
https://github.com/raylib-cs/raylib-cs
synced 2025-07-02 19:13:43 -04:00
Update/merge examples back into main repo (#192)
This commit is contained in:
116
Examples/Textures/BackgroundScrolling.cs
Normal file
116
Examples/Textures/BackgroundScrolling.cs
Normal file
@ -0,0 +1,116 @@
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - Background scrolling
|
||||
*
|
||||
* This example has been created using raylib 2.0 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
*
|
||||
* Copyright (c) 2019 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Textures;
|
||||
|
||||
public class BackgroundScrolling
|
||||
{
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - background scrolling");
|
||||
|
||||
// NOTE: Be careful, background width must be equal or bigger than screen width
|
||||
// if not, texture should be draw more than two times for scrolling effect
|
||||
Texture2D background = LoadTexture("resources/cyberpunk_street_background.png");
|
||||
Texture2D midground = LoadTexture("resources/cyberpunk_street_midground.png");
|
||||
Texture2D foreground = LoadTexture("resources/cyberpunk_street_foreground.png");
|
||||
|
||||
float scrollingBack = 0.0f;
|
||||
float scrollingMid = 0.0f;
|
||||
float scrollingFore = 0.0f;
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
scrollingBack -= 0.1f;
|
||||
scrollingMid -= 0.5f;
|
||||
scrollingFore -= 1.0f;
|
||||
|
||||
// NOTE: Texture is scaled twice its size, so it sould be considered on scrolling
|
||||
if (scrollingBack <= -background.Width * 2)
|
||||
{
|
||||
scrollingBack = 0;
|
||||
}
|
||||
if (scrollingMid <= -midground.Width * 2)
|
||||
{
|
||||
scrollingMid = 0;
|
||||
}
|
||||
if (scrollingFore <= -foreground.Width * 2)
|
||||
{
|
||||
scrollingFore = 0;
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
ClearBackground(GetColor(0x052c46ff));
|
||||
|
||||
// Draw background image twice
|
||||
// NOTE: Texture is scaled twice its size
|
||||
DrawTextureEx(background, new Vector2(scrollingBack, 20), 0.0f, 2.0f, Color.WHITE);
|
||||
DrawTextureEx(
|
||||
background,
|
||||
new Vector2(background.Width * 2 + scrollingBack, 20),
|
||||
0.0f,
|
||||
2.0f,
|
||||
Color.WHITE
|
||||
);
|
||||
|
||||
// Draw midground image twice
|
||||
DrawTextureEx(midground, new Vector2(scrollingMid, 20), 0.0f, 2.0f, Color.WHITE);
|
||||
DrawTextureEx(midground, new Vector2(midground.Width * 2 + scrollingMid, 20), 0.0f, 2.0f, Color.WHITE);
|
||||
|
||||
// Draw foreground image twice
|
||||
DrawTextureEx(foreground, new Vector2(scrollingFore, 70), 0.0f, 2.0f, Color.WHITE);
|
||||
DrawTextureEx(
|
||||
foreground,
|
||||
new Vector2(foreground.Width * 2 + scrollingFore, 70),
|
||||
0.0f,
|
||||
2.0f,
|
||||
Color.WHITE
|
||||
);
|
||||
|
||||
DrawText("BACKGROUND SCROLLING & PARALLAX", 10, 10, 20, Color.RED);
|
||||
|
||||
int x = screenWidth - 330;
|
||||
int y = screenHeight - 20;
|
||||
DrawText("(c) Cyberpunk Street Environment by Luis Zuno (@ansimuz)", x, y, 10, Color.RAYWHITE);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadTexture(background);
|
||||
UnloadTexture(midground);
|
||||
UnloadTexture(foreground);
|
||||
|
||||
CloseWindow();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user