You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
4.1 KiB

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