CSS Cheat Sheet: Everything You Need to Know in One Place

CSS cheat-sheet Basic Selectors: * /* Universal selector */ element /* Selects all elements of a type */ .class /* Selects elements by class */ #id /* Selects elements by ID */ element , element /* Grouping selectors */ Box Model: element { margin : 10px ; /* Space outside the element */ padding : 10px ; /* Space inside the element */ border : 1px solid #000 ; /* Border around the element */ width : 100px ; /* Element width */ height : 100px ; /* Element height */ box-sizing : border-box; /* Includes padding/border in width and height */ } Text Styling: element { font-family : Arial, sans-serif; /* Font family */ font-size : 16px ; /* Font size */ font-weight : bold; /* Font weight (bold, normal) */ font-style : italic; /* Italic text */ text-align : center; /* Aligns text (lef...

How to Implement a Stopwatch Timer in C++ Using SFML

 

Stopwatch Timer in SFML

Overview

This blog provides a step-by-step guide to implementing a stopwatch timer using the Simple and Fast Multimedia Library (SFML). The stopwatch will display elapsed time in the format of hours, minutes, and seconds.

Prerequisites

  • SFML library installed
  • Basic knowledge of C++ and SFML
  • Development environment set up (e.g., Visual Studio, VS Code)

Steps to Implement a Stopwatch Timer

1. Setting Up SFML

Ensure that SFML is properly set up in your development environment. You should be able to compile and run basic SFML programs.

2. Create the Project

Create a new C++ project in your IDE and link the SFML libraries (Graphics, Window, and System).

3. Include Required Headers

Include the necessary SFML headers in your main source file.

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp> #include <SFML/Window.hpp> #include <iostream> #include <iomanip> #include <sstream>
using namespace sf;

4. Define the Stopwatch Class

Create a class to handle the stopwatch functionality.

class Stopwatch {
public: Stopwatch() : elapsedTime(0.f), running(false) { clock.restart(); } void start() { if (!running) { clock.restart(); running = true; } } void stop() { if (running) { elapsedTime += clock.getElapsedTime().asSeconds(); running = false; } } void reset() { elapsedTime = 0.f; if (running) { clock.restart(); } } std::string getTime() const { float time = elapsedTime; if (running) { time += clock.getElapsedTime().asSeconds(); } int hours = static_cast<int>(time) / 3600; int minutes = (static_cast<int>(time) % 3600) / 60; int seconds = static_cast<int>(time) % 60; std::ostringstream stream; stream << std::setw(2) << std::setfill('0') << hours << ":" << std::setw(2) << std::setfill('0') << minutes << ":" << std::setw(2) << std::setfill('0') << seconds; return stream.str(); } private: sf::Clock clock; float elapsedTime; bool running; };

5. Main Application Code

Set up the SFML window and handle input to control the stopwatch.

int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "Stopwatch Timer"); sf::Font font; if (!font.loadFromFile("arial.ttf")) { std::cerr << "Error loading font" << std::endl; return -1; } sf::Text timerText; timerText.setFont(font); timerText.setCharacterSize(50); timerText.setFillColor(sf::Color::White); timerText.setPosition(250, 250); Stopwatch stopwatch; bool running = false; sf::Clock frameClock; while (window.isOpen()) { sf::Time deltaTime = frameClock.restart(); sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) { window.close(); } } if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) { stopwatch.start(); running = true; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::P)) { stopwatch.stop(); running = false; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::R)) { stopwatch.reset(); running = false; } window.clear(); timerText.setString(stopwatch.getTime()); window.draw(timerText); window.display(); } return 0; }

6. Compilation and Running

Ensure all SFML libraries are correctly linked and compile your application. Run the executable to see the stopwatch in action.

Conclusion

This guide provided the necessary steps to create a stopwatch timer using SFML. The implementation includes basic start, stop, and reset functionalities, and displays time in hr:min:sec format. Feel free to expand upon this example to add more features or enhance the user interface.


Stay connected! Follow me on Socials.

Comments

Popular posts from this blog

How to Add Multiple Code Snippets with Copy Buttons in Blogger Posts

Understanding Focus Loss and Gain in SFML: Key Concepts and Implementation