fork download
  1. #include <GL/glut.h>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <sstream>
  5. #include <chrono>
  6. #include <thread>
  7.  
  8. // Global variables for stopwatch state
  9. bool stopwatchRunning = false;
  10. std::chrono::steady_clock::time_point startTime;
  11. std::chrono::milliseconds elapsedTime(0);
  12.  
  13. // Function to render text on the screen using bitmap fonts
  14. void renderBitmapString(float x, float y, void *font, const char *string) {
  15. glRasterPos2f(x, y);
  16. for (const char *c = string; *c != '\0'; ++c) {
  17. glutBitmapCharacter(font, *c);
  18. }
  19. }
  20.  
  21. // Function to display time in HH:MM:SS:MS format
  22. void displayTime() {
  23. using namespace std::chrono;
  24.  
  25. // Calculate elapsed time
  26. auto now = steady_clock::now();
  27. auto duration = now - startTime + elapsedTime;
  28.  
  29. // Convert duration to hours, minutes, seconds, and milliseconds
  30. auto millis = duration_cast<milliseconds>(duration).count() % 1000;
  31. auto seconds = duration_cast<seconds>(duration).count() % 60;
  32. auto minutes = duration_cast<minutes>(duration).count() % 60;
  33. auto hours = duration_cast<hours>(duration).count();
  34.  
  35. std::ostringstream timeStream;
  36. timeStream << std::setw(2) << std::setfill('0') << hours << ":"
  37. << std::setw(2) << std::setfill('0') << minutes << ":"
  38. << std::setw(2) << std::setfill('0') << seconds << ":"
  39. << std::setw(3) << std::setfill('0') << millis;
  40.  
  41. std::string timeString = timeStream.str();
  42.  
  43. glClear(GL_COLOR_BUFFER_BIT);
  44. glColor3f(1.0, 1.0, 1.0); // Set the text color to white
  45.  
  46. // Display student ID
  47. renderBitmapString(-0.9, 0.9, GLUT_BITMAP_HELVETICA_18, "Student ID: MC123456789");
  48.  
  49. // Display stopwatch time in the center
  50. renderBitmapString(-0.2, 0.0, GLUT_BITMAP_HELVETICA_18, timeString.c_str());
  51.  
  52. glutSwapBuffers();
  53. }
  54.  
  55. // Function to update the display every 10 milliseconds
  56. void update(int value) {
  57. if (stopwatchRunning) {
  58. glutPostRedisplay(); // Request a redraw
  59. glutTimerFunc(10, update, 0); // Set up the next update
  60. }
  61. }
  62.  
  63. // Function to handle keyboard input
  64. void keyboard(unsigned char key, int x, int y) {
  65. if (key == 'S' || key == 's') {
  66. if (stopwatchRunning) {
  67. elapsedTime += std::chrono::steady_clock::now() - startTime;
  68. stopwatchRunning = false;
  69. } else {
  70. startTime = std::chrono::steady_clock::now();
  71. stopwatchRunning = true;
  72. glutTimerFunc(10, update, 0); // Start the update loop
  73. }
  74. } else if (key == 'R' || key == 'r') {
  75. stopwatchRunning = false;
  76. elapsedTime = std::chrono::milliseconds(0);
  77. glutPostRedisplay(); // Redraw the window
  78. }
  79. }
  80.  
  81. // Function to initialize OpenGL settings
  82. void init() {
  83. glClearColor(0.0, 0.0, 0.0, 1.0); // Set the background color to black
  84. glMatrixMode(GL_PROJECTION);
  85. glLoadIdentity();
  86. glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); // Set up orthographic projection
  87. glMatrixMode(GL_MODELVIEW);
  88. }
  89.  
  90. int main(int argc, char **argv) {
  91. glutInit(&argc, argv);
  92. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  93. glutInitWindowSize(800, 600);
  94. glutCreateWindow("Digital Stopwatch");
  95.  
  96. init();
  97.  
  98. glutDisplayFunc(displayTime); // Register display function
  99. glutKeyboardFunc(keyboard); // Register keyboard function
  100.  
  101. glutMainLoop(); // Enter the GLUT event processing loop
  102. return 0;
  103. }
  104.  
Success #stdin #stdout 0.03s 25620KB
stdin
Standard input is empty
stdout
#include <GL/glut.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <chrono>
#include <thread>

// Global variables for stopwatch state
bool stopwatchRunning = false;
std::chrono::steady_clock::time_point startTime;
std::chrono::milliseconds elapsedTime(0);

// Function to render text on the screen using bitmap fonts
void renderBitmapString(float x, float y, void *font, const char *string) {
    glRasterPos2f(x, y);
    for (const char *c = string; *c != '\0'; ++c) {
        glutBitmapCharacter(font, *c);
    }
}

// Function to display time in HH:MM:SS:MS format
void displayTime() {
    using namespace std::chrono;
    
    // Calculate elapsed time
    auto now = steady_clock::now();
    auto duration = now - startTime + elapsedTime;
    
    // Convert duration to hours, minutes, seconds, and milliseconds
    auto millis = duration_cast<milliseconds>(duration).count() % 1000;
    auto seconds = duration_cast<seconds>(duration).count() % 60;
    auto minutes = duration_cast<minutes>(duration).count() % 60;
    auto hours = duration_cast<hours>(duration).count();

    std::ostringstream timeStream;
    timeStream << std::setw(2) << std::setfill('0') << hours << ":"
               << std::setw(2) << std::setfill('0') << minutes << ":"
               << std::setw(2) << std::setfill('0') << seconds << ":"
               << std::setw(3) << std::setfill('0') << millis;

    std::string timeString = timeStream.str();

    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);  // Set the text color to white

    // Display student ID
    renderBitmapString(-0.9, 0.9, GLUT_BITMAP_HELVETICA_18, "Student ID: MC123456789");

    // Display stopwatch time in the center
    renderBitmapString(-0.2, 0.0, GLUT_BITMAP_HELVETICA_18, timeString.c_str());

    glutSwapBuffers();
}

// Function to update the display every 10 milliseconds
void update(int value) {
    if (stopwatchRunning) {
        glutPostRedisplay();  // Request a redraw
        glutTimerFunc(10, update, 0);  // Set up the next update
    }
}

// Function to handle keyboard input
void keyboard(unsigned char key, int x, int y) {
    if (key == 'S' || key == 's') {
        if (stopwatchRunning) {
            elapsedTime += std::chrono::steady_clock::now() - startTime;
            stopwatchRunning = false;
        } else {
            startTime = std::chrono::steady_clock::now();
            stopwatchRunning = true;
            glutTimerFunc(10, update, 0);  // Start the update loop
        }
    } else if (key == 'R' || key == 'r') {
        stopwatchRunning = false;
        elapsedTime = std::chrono::milliseconds(0);
        glutPostRedisplay();  // Redraw the window
    }
}

// Function to initialize OpenGL settings
void init() {
    glClearColor(0.0, 0.0, 0.0, 1.0);  // Set the background color to black
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);  // Set up orthographic projection
    glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Digital Stopwatch");

    init();

    glutDisplayFunc(displayTime);  // Register display function
    glutKeyboardFunc(keyboard);    // Register keyboard function

    glutMainLoop();  // Enter the GLUT event processing loop
    return 0;
}