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 thepoints
vector. - Drawing: The
points
vector stores each point that the user draws. These points are drawn
using thesf::Vertex
object, and are rendered with thewindow.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:
- Install the SFML library on your system. You can follow the installation guide on the official SFML website:
SFML Downloads. - Write the above C++ code in a file, e.g.,
DrawingApp.cpp
. - 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
- 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.