Basic GUI Library Gaming Project in C++

Explanation

  1. Include SFML Library:
    • #include <SFML/Graphics.hpp>: Includes SFML’s graphics module, which provides functionality for creating and manipulating graphics.
  2. Create a Window:
    • sf::RenderWindow window(sf::VideoMode(800, 600), "Basic GUI with SFML");
      • Initializes a window with a resolution of 800×600 pixels and a title “Basic GUI with SFML”.
  3. Create and Configure Shapes:
    • Circle:
      • sf::CircleShape circle(50);: Creates a circle with a radius of 50 pixels.
      • circle.setFillColor(sf::Color::Green);: Sets the fill color to green.
      • circle.setPosition(375, 275);: Positions the circle in the center of the window.
    • Rectangle:
      • sf::RectangleShape rectangle(sf::Vector2f(200, 100));: Creates a rectangle with a width of 200 pixels and height of 100 pixels.
      • rectangle.setFillColor(sf::Color::Blue);: Sets the fill color to blue.
      • rectangle.setPosition(300, 250);: Positions the rectangle in the window.
  4. Main Loop:
    • while (window.isOpen()): Main event loop that continues while the window is open.
    • sf::Event event; and window.pollEvent(event): Polls events like window closing.
    • if (event.type == sf::Event::Closed): Checks if the window close event has occurred.
      • window.close();: Closes the window.
    • window.clear(sf::Color::Black);: Clears the window with a black color.
    • window.draw(circle); and window.draw(rectangle);: Draws the circle and rectangle shapes to the window.
    • window.display();: Displays the drawn content.

      Notes:

      • SFML Installation: Ensure that SFML is installed on your system and configured with your IDE or build system.
      • Graphics Module: This example uses the graphics module of SFML, but SFML also has modules for audio, window management, and networking, which can be used to extend your GUI or game.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top