From 5f497d068788722c6a4b02e6bcc6a35ea7d2e8a7 Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 3 Jun 2025 20:42:27 +0200 Subject: [PATCH] REVIEWED: `shapes_digital_clock` example --- examples/shapes/shapes_digital_clock.c | 202 ++++++++++++------------- 1 file changed, 98 insertions(+), 104 deletions(-) diff --git a/examples/shapes/shapes_digital_clock.c b/examples/shapes/shapes_digital_clock.c index 12a8c8dfe..8aa887c3c 100644 --- a/examples/shapes/shapes_digital_clock.c +++ b/examples/shapes/shapes_digital_clock.c @@ -11,113 +11,46 @@ * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, * BSD-like license that allows static linking with closed source software * -* Copyright (c) 2025-2025 Hamza RAHAL (@hmz-rhl) +* Copyright (c) 2025 Hamza RAHAL (@hmz-rhl) * ********************************************************************************************/ #include "raylib.h" -#include // needed for cos & sin functions -#include // needed to get machine time + +#include // Required for: cosf(), sinf() +#include // Required for: time(), localtime() #define DIGIT_SIZE 30 -typedef enum -{ - NORMAL_MODE = 0, - HANDS_FREE_MODE, +//---------------------------------------------------------------------------------- +// Types and Structures Definition +//---------------------------------------------------------------------------------- +typedef enum { + MODE_NORMAL = 0, + MODE_HANDS_FREE, } ClockMode; -typedef struct -{ +typedef struct { int value; Vector2 origin; float angle; int length; int thickness; - Color colour; -} Hand; + Color color; +} ClockHand; -typedef struct -{ - Hand second; - Hand minute; - Hand hour; - ClockMode cm; +typedef struct { + ClockMode mode; + ClockHand second; + ClockHand minute; + ClockHand hour; } Clock; - -void UpdateClock(Clock *clock) -{ - time_t rawtime; - struct tm * timeinfo; - - time(&rawtime); - timeinfo = localtime(&rawtime); - - // updating datas - clock->second.value = timeinfo->tm_sec; - clock->minute.value = timeinfo->tm_min; - clock->hour.value = timeinfo->tm_hour; - - clock->hour.angle = (timeinfo->tm_hour % 12)*180.0/6.0f; - clock->hour.angle += (timeinfo->tm_min % 60)*30/60.0f; - clock->hour.angle -= 90 ; - - clock->minute.angle = (timeinfo->tm_min % 60)*6.0f; - clock->minute.angle += (timeinfo->tm_sec % 60)*6/60.0f; - clock->minute.angle -= 90 ; - - clock->second.angle = (timeinfo->tm_sec % 60)*6.0f; - clock->second.angle -= 90 ; - -} - -void drawClock(Clock clock) -{ - if (clock.cm == HANDS_FREE_MODE) - { - DrawText(TextFormat("%i", clock.second.value), clock.second.origin.x + (clock.second.length - 10)*cos(clock.second.angle*(float)(M_PI/180)) - DIGIT_SIZE/2, clock.second.origin.y + clock.second.length*sin(clock.second.angle*(float)(M_PI/180)) - DIGIT_SIZE/2, DIGIT_SIZE, GRAY); - - DrawText(TextFormat("%i", clock.minute.value), clock.minute.origin.x + clock.minute.length*cos(clock.minute.angle*(float)(M_PI/180)) - DIGIT_SIZE/2, clock.minute.origin.y + clock.minute.length*sin(clock.minute.angle*(float)(M_PI/180)) - DIGIT_SIZE/2, DIGIT_SIZE, RED); - - DrawText(TextFormat("%i", clock.hour.value), clock.hour.origin.x + clock.hour.length*cos(clock.hour.angle*(float)(M_PI/180)) - DIGIT_SIZE/2, clock.hour.origin.y + clock.hour.length*sin(clock.hour.angle*(float)(M_PI/180)) - DIGIT_SIZE/2, DIGIT_SIZE, GOLD); - } - else - { - DrawRectanglePro( - (Rectangle){ clock.second.origin.x, - clock.second.origin.y, - clock.second.length, - clock.second.thickness, - } - , (Vector2){0, clock.second.thickness/2}, - clock.second.angle, - clock.second.colour - ); - - DrawRectanglePro( - (Rectangle){ clock.minute.origin.x, - clock.minute.origin.y, - clock.minute.length, - clock.minute.thickness, - } - , (Vector2){0, clock.minute.thickness/2}, - clock.minute.angle, - clock.minute.colour - ); - - DrawRectanglePro( - (Rectangle){ clock.hour.origin.x, - clock.hour.origin.y, - clock.hour.length, - clock.hour.thickness, - } - , (Vector2){0, clock.hour.thickness/2}, - clock.hour.angle, - clock.hour.colour - ); - } -} +//---------------------------------------------------------------------------------- +// Module Functions Declaration +//---------------------------------------------------------------------------------- +static void UpdateClock(Clock *clock); // Update clock time +static void DrawClock(Clock clock, Vector2 centerPos); // Draw clock at desired position //------------------------------------------------------------------------------------ // Program main entry point @@ -128,31 +61,29 @@ int main(void) //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [shapes] example - digital clock"); + // Initialize clock Clock myClock = { - .cm = NORMAL_MODE, + .mode = MODE_NORMAL, - .second.origin = (Vector2){400, 225}, .second.angle = 45, .second.length = 140, .second.thickness = 3, - .second.colour = BEIGE, + .second.color = BEIGE, - .minute.origin = (Vector2){400, 225}, .minute.angle = 10, .minute.length = 130, .minute.thickness = 7, - .minute.colour = DARKGRAY, + .minute.color = DARKGRAY, - .hour.origin = (Vector2){400, 225}, .hour.angle = 0, .hour.length = 100, .hour.thickness = 7, - .hour.colour = BLACK, + .hour.color = BLACK, }; - InitWindow(screenWidth, screenHeight, "raylib [shapes] example - digital clock"); - SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- @@ -160,23 +91,27 @@ int main(void) while (!WindowShouldClose()) // Detect window close button or ESC key { // Update + //---------------------------------------------------------------------------------- if (IsKeyPressed(KEY_SPACE)) { - myClock.cm = (myClock.cm == HANDS_FREE_MODE) ? NORMAL_MODE : HANDS_FREE_MODE; + if (myClock.mode == MODE_HANDS_FREE) myClock.mode = MODE_NORMAL; + else if (myClock.mode == MODE_NORMAL) myClock.mode = MODE_HANDS_FREE; } UpdateClock(&myClock); + //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); - ClearBackground(RAYWHITE); + ClearBackground(RAYWHITE); - DrawCircle(400, 225, 5, BLACK); // center dot - drawClock(myClock); + DrawCircle(400, 225, 5, BLACK); // Clock center dot + + DrawClock(myClock, (Vector2){ 400, 225 }); // Clock in selected mode - DrawText("press [SPACE] to switch clock mode", 350, 400, 10, GRAY); + DrawText("Press [SPACE] to switch clock mode", 10, 10, 20, DARKGRAY); EndDrawing(); //---------------------------------------------------------------------------------- @@ -188,4 +123,63 @@ int main(void) //-------------------------------------------------------------------------------------- return 0; +} + +//---------------------------------------------------------------------------------- +// Module Functions Definition +//---------------------------------------------------------------------------------- + +// Update clock time +static void UpdateClock(Clock *clock) +{ + time_t rawtime; + struct tm * timeinfo; + + time(&rawtime); + timeinfo = localtime(&rawtime); + + // Updating time data + clock->second.value = timeinfo->tm_sec; + clock->minute.value = timeinfo->tm_min; + clock->hour.value = timeinfo->tm_hour; + + clock->hour.angle = (timeinfo->tm_hour%12)*180.0/6.0f; + clock->hour.angle += (timeinfo->tm_min%60)*30/60.0f; + clock->hour.angle -= 90; + + clock->minute.angle = (timeinfo->tm_min%60)*6.0f; + clock->minute.angle += (timeinfo->tm_sec%60)*6/60.0f; + clock->minute.angle -= 90; + + clock->second.angle = (timeinfo->tm_sec%60)*6.0f; + clock->second.angle -= 90; +} + +// Draw clock +static void DrawClock(Clock clock, Vector2 centerPosition) +{ + if (clock.mode == MODE_HANDS_FREE) + { + DrawCircleLinesV(centerPosition, clock.minute.length, LIGHTGRAY); + + DrawText(TextFormat("%i", clock.second.value), centerPosition.x + (clock.second.length - 10)*cosf(clock.second.angle*(float)(PI/180)) - DIGIT_SIZE/2, centerPosition.y + clock.second.length*sinf(clock.second.angle*(float)(PI/180)) - DIGIT_SIZE/2, DIGIT_SIZE, GRAY); + + DrawText(TextFormat("%i", clock.minute.value), clock.minute.origin.x + clock.minute.length*cosf(clock.minute.angle*(float)(PI/180)) - DIGIT_SIZE/2, centerPosition.y + clock.minute.length*sinf(clock.minute.angle*(float)(PI/180)) - DIGIT_SIZE/2, DIGIT_SIZE, RED); + + DrawText(TextFormat("%i", clock.hour.value), centerPosition.x + clock.hour.length*cosf(clock.hour.angle*(float)(PI/180)) - DIGIT_SIZE/2, centerPosition.y + clock.hour.length*sinf(clock.hour.angle*(float)(PI/180)) - DIGIT_SIZE/2, DIGIT_SIZE, GOLD); + } + else if (clock.mode == MODE_NORMAL) + { + // Draw hand seconds + DrawRectanglePro((Rectangle){ centerPosition.x, centerPosition.y, clock.second.length, clock.second.thickness }, + (Vector2){ 0.0f, clock.second.thickness/2.0f }, clock.second.angle, clock.second.color); + + // Draw hand minutes + DrawRectanglePro((Rectangle){ centerPosition.x, centerPosition.y, clock.minute.length, clock.minute.thickness }, + (Vector2){ 0.0f, clock.minute.thickness/2.0f }, clock.minute.angle, clock.minute.color); + + // Draw hand hours + DrawRectanglePro((Rectangle){ centerPosition.x, centerPosition.y, clock.hour.length, clock.hour.thickness }, + (Vector2){ 0.0f, clock.hour.thickness/2.0f }, clock.hour.angle, clock.hour.color); + } } \ No newline at end of file