textures_background_scrolling.c 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include "raylib.h"
  16. //------------------------------------------------------------------------------------
  17. // Program main entry point
  18. //------------------------------------------------------------------------------------
  19. int main(void)
  20. {
  21. // Initialization
  22. //--------------------------------------------------------------------------------------
  23. const int screenWidth = 800;
  24. const int screenHeight = 450;
  25. InitWindow(screenWidth, screenHeight, "raylib [textures] example - background scrolling");
  26. // NOTE: Be careful, background width must be equal or bigger than screen width
  27. // if not, texture should be draw more than two times for scrolling effect
  28. Texture2D background = LoadTexture("resources/cyberpunk_street_background.png");
  29. Texture2D midground = LoadTexture("resources/cyberpunk_street_midground.png");
  30. Texture2D foreground = LoadTexture("resources/cyberpunk_street_foreground.png");
  31. float scrollingBack = 0.0f;
  32. float scrollingMid = 0.0f;
  33. float scrollingFore = 0.0f;
  34. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  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. // NOTE: Texture is scaled twice its size, so it sould be considered on scrolling
  45. if (scrollingBack <= -background.width*2) scrollingBack = 0;
  46. if (scrollingMid <= -midground.width*2) scrollingMid = 0;
  47. if (scrollingFore <= -foreground.width*2) scrollingFore = 0;
  48. //----------------------------------------------------------------------------------
  49. // Draw
  50. //----------------------------------------------------------------------------------
  51. BeginDrawing();
  52. ClearBackground(GetColor(0x052c46ff));
  53. // Draw background image twice
  54. // NOTE: Texture is scaled twice its size
  55. DrawTextureEx(background, (Vector2){ scrollingBack, 20 }, 0.0f, 2.0f, WHITE);
  56. DrawTextureEx(background, (Vector2){ background.width*2 + scrollingBack, 20 }, 0.0f, 2.0f, WHITE);
  57. // Draw midground image twice
  58. DrawTextureEx(midground, (Vector2){ scrollingMid, 20 }, 0.0f, 2.0f, WHITE);
  59. DrawTextureEx(midground, (Vector2){ midground.width*2 + scrollingMid, 20 }, 0.0f, 2.0f, WHITE);
  60. // Draw foreground image twice
  61. DrawTextureEx(foreground, (Vector2){ scrollingFore, 70 }, 0.0f, 2.0f, WHITE);
  62. DrawTextureEx(foreground, (Vector2){ foreground.width*2 + scrollingFore, 70 }, 0.0f, 2.0f, WHITE);
  63. DrawText("BACKGROUND SCROLLING & PARALLAX", 10, 10, 20, RED);
  64. DrawText("(c) Cyberpunk Street Environment by Luis Zuno (@ansimuz)", screenWidth - 330, screenHeight - 20, 10, RAYWHITE);
  65. EndDrawing();
  66. //----------------------------------------------------------------------------------
  67. }
  68. // De-Initialization
  69. //--------------------------------------------------------------------------------------
  70. UnloadTexture(background); // Unload background texture
  71. UnloadTexture(midground); // Unload midground texture
  72. UnloadTexture(foreground); // Unload foreground texture
  73. CloseWindow(); // Close window and OpenGL context
  74. //--------------------------------------------------------------------------------------
  75. return 0;
  76. }