cplusplus
cplusplus

 

Introduction

A drawing application allows users to create and manipulate graphical elements such as lines, shapes, and drawings.
This basic drawing app written in C++ allows users to draw on a window using mouse events. It serves as a great introduction
to handling graphical user interfaces (GUIs) and user input through the mouse.

Objective

The objective of this program is to create a simple drawing application where the user can click and drag the mouse
to draw freehand on the screen. The program uses the SFML library (Simple and Fast Multimedia Library) to
create the graphical interface and handle the mouse input.

Code: Basic Drawing App in C++

#include <SFML/Graphics.hpp>

int main() {
    // Create a window for the drawing app
    sf::RenderWindow window(sf::VideoMode(800, 600), "Basic Drawing App");

    // Create a vector to store the points the user draws
    std::vector points;

    // Create a clock to manage the frame rate
    sf::Clock clock;

    // Main application loop
    while (window.isOpen()) {
        sf::Event event;
        
        // Event handling
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }

        // If the left mouse button is pressed, add the current mouse position to the points vector
        if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
            sf::Vector2i mousePos = sf::Mouse::getPosition(window);
            points.push_back(sf::Vertex(sf::Vector2f(mousePos), sf::Color::Black));
        }

        // Clear the screen with a white color
        window.clear(sf::Color::White);

        // Draw the points (the drawing)
        window.draw(&points[0], points.size(), sf::Points);

        // Display everything that was drawn on the window
        window.display();
    }

    return 0;
}

Explanation of the Program

This basic drawing app utilizes the SFML library to create a graphical window where users can draw with
the mouse. Below is a breakdown of the program’s structure:

  • Window Creation: The window is created with a size of 800×600 pixels using the sf::RenderWindow class.
  • Mouse Event Handling: The program listens for mouse events using sf::Mouse::isButtonPressed
    to check if the left mouse button is pressed. When pressed, the program adds the current mouse position to the points vector.
  • Drawing: The points vector stores each point that the user draws. These points are drawn
    using the sf::Vertex object, and are rendered with the window.draw function.
  • Window Display: After drawing, the screen is updated with the window.display() function,
    which shows the newly drawn points.

How to Run the Program

To run this drawing application, follow these steps:

  1. Install the SFML library on your system. You can follow the installation guide on the official SFML website:
    SFML Downloads.
  2. Write the above C++ code in a file, e.g., DrawingApp.cpp.
  3. Compile the program using a C++ compiler that supports SFML. For example, using g++:
    g++ DrawingApp.cpp -o DrawingApp -lsfml-graphics -lsfml-window -lsfml-system
  4. Run the compiled program:
    ./DrawingApp

After running the program, a window will open. You can draw by holding down the left mouse button and moving the mouse.

© 2024 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)