Advertisement
Guest User

Untitled

a guest
Feb 15th, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.04 KB | Source Code | 0 0
  1. using SFML.System;
  2. using SFML.Window;
  3. using SFML.Graphics;
  4. namespace sfml_dummy
  5. {
  6.     internal class Program
  7.     {
  8.         private static Image[] images = new Image[6];
  9.         private static int delta_frames = 0;
  10.         private static int current_image;
  11.         private static RenderWindow rw;
  12.         static void Main(string[] args)
  13.         {
  14.             rw = new RenderWindow(new VideoMode(800, 600), "SFML Loading Screen");
  15.             images[0] = new Image("1.png");
  16.             images[1] = new Image("2.png");
  17.             images[2] = new Image("3.png");
  18.             images[3] = new Image("4.png");
  19.             images[4] = new Image("5.png");
  20.             images[5] = new Image("6.png");
  21.             rw.Closed += (e, a) => rw.Close();
  22.             rw.SetFramerateLimit(60);
  23.             rw.Resized += Rw_Resized;
  24.             while (rw.IsOpen)
  25.             {
  26.                 delta_frames++;
  27.                 if (delta_frames == 60) delta_frames = 0;
  28.                 if (delta_frames == 0) current_image = new Random().Next(5);
  29.                 rw.DispatchEvents();
  30.                 rw.Clear();
  31.                 //rw.Draw(new Sprite(new Texture(images[current_image])));
  32.                 rw.Draw(new RectangleShape(new Vector2f(100, 100)) { Position = new Vector2f(800/2-50, 600/2-50)});
  33.                 rw.Display();
  34.             }
  35.         }
  36.  
  37.         private static void Rw_Resized(object o, SizeEventArgs a)
  38.         {
  39.             View v = rw.GetView();
  40.             rw.SetView(GetLetterboxView(v, 800, 600));
  41.         }
  42.  
  43.         public static View GetLetterboxView(View view, int windowWidth, int windowHeight)
  44.                 {
  45.                     // Compares the aspect ratio of the window to the aspect ratio of the view,
  46.                     // and sets the view's viewport accordingly in order to achieve a letterbox effect.
  47.                     // A new view (with a new viewport set) is returned.
  48.  
  49.                     float windowRatio = (float)windowWidth / windowHeight;
  50.                     float viewRatio = view.Size.X / view.Size.Y;
  51.                     float sizeX = 1;
  52.                     float sizeY = 1;
  53.                     float posX = 0;
  54.                     float posY = 0;
  55.  
  56.                     bool horizontalSpacing = true;
  57.                     if (windowRatio < viewRatio)
  58.                         horizontalSpacing = false;
  59.  
  60.                     // If horizontalSpacing is true, the black bars will appear on the left and right side.
  61.                     // Otherwise, the black bars will appear on the top and bottom.
  62.  
  63.                     if (horizontalSpacing)
  64.                     {
  65.                         sizeX = viewRatio / windowRatio;
  66.                         posX = (1 - sizeX) / 2f;
  67.                     }
  68.                     else
  69.                     {
  70.                         sizeY = windowRatio / viewRatio;
  71.                         posY = (1 - sizeY) / 2f;
  72.                     }
  73.  
  74.                     view.Viewport = new FloatRect(posX, posY, sizeX, sizeY);
  75.  
  76.                     return view;
  77.                 }
  78.     }
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement