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.

86 lines
3.8 KiB

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