REVIEWED: shapes_digital_clock example

This commit is contained in:
Ray
2025-06-03 20:42:27 +02:00
parent cb369f8df7
commit 5f497d0687

View File

@ -11,42 +11,126 @@
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software * 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 "raylib.h"
#include <math.h> // needed for cos & sin functions
#include <time.h> // needed to get machine time #include <math.h> // Required for: cosf(), sinf()
#include <time.h> // Required for: time(), localtime()
#define DIGIT_SIZE 30 #define DIGIT_SIZE 30
typedef enum //----------------------------------------------------------------------------------
{ // Types and Structures Definition
NORMAL_MODE = 0, //----------------------------------------------------------------------------------
HANDS_FREE_MODE, typedef enum {
MODE_NORMAL = 0,
MODE_HANDS_FREE,
} ClockMode; } ClockMode;
typedef struct typedef struct {
{
int value; int value;
Vector2 origin; Vector2 origin;
float angle; float angle;
int length; int length;
int thickness; int thickness;
Color colour; Color color;
} Hand; } ClockHand;
typedef struct typedef struct {
{ ClockMode mode;
Hand second; ClockHand second;
Hand minute; ClockHand minute;
Hand hour; ClockHand hour;
ClockMode cm;
} Clock; } Clock;
//----------------------------------------------------------------------------------
// Module Functions Declaration
//----------------------------------------------------------------------------------
static void UpdateClock(Clock *clock); // Update clock time
static void DrawClock(Clock clock, Vector2 centerPos); // Draw clock at desired position
void UpdateClock(Clock *clock) //------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - digital clock");
// Initialize clock
Clock myClock = {
.mode = MODE_NORMAL,
.second.angle = 45,
.second.length = 140,
.second.thickness = 3,
.second.color = BEIGE,
.minute.angle = 10,
.minute.length = 130,
.minute.thickness = 7,
.minute.color = DARKGRAY,
.hour.angle = 0,
.hour.length = 100,
.hour.thickness = 7,
.hour.color = BLACK,
};
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
if (IsKeyPressed(KEY_SPACE))
{
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);
DrawCircle(400, 225, 5, BLACK); // Clock center dot
DrawClock(myClock, (Vector2){ 400, 225 }); // Clock in selected mode
DrawText("Press [SPACE] to switch clock mode", 10, 10, 20, DARKGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
//----------------------------------------------------------------------------------
// Module Functions Definition
//----------------------------------------------------------------------------------
// Update clock time
static void UpdateClock(Clock *clock)
{ {
time_t rawtime; time_t rawtime;
struct tm * timeinfo; struct tm * timeinfo;
@ -54,7 +138,7 @@ void UpdateClock(Clock *clock)
time(&rawtime); time(&rawtime);
timeinfo = localtime(&rawtime); timeinfo = localtime(&rawtime);
// updating datas // Updating time data
clock->second.value = timeinfo->tm_sec; clock->second.value = timeinfo->tm_sec;
clock->minute.value = timeinfo->tm_min; clock->minute.value = timeinfo->tm_min;
clock->hour.value = timeinfo->tm_hour; clock->hour.value = timeinfo->tm_hour;
@ -69,123 +153,33 @@ void UpdateClock(Clock *clock)
clock->second.angle = (timeinfo->tm_sec%60)*6.0f; clock->second.angle = (timeinfo->tm_sec%60)*6.0f;
clock->second.angle -= 90; clock->second.angle -= 90;
} }
void drawClock(Clock clock) // Draw clock
static void DrawClock(Clock clock, Vector2 centerPosition)
{ {
if (clock.cm == HANDS_FREE_MODE) if (clock.mode == MODE_HANDS_FREE)
{ {
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); DrawCircleLinesV(centerPosition, clock.minute.length, LIGHTGRAY);
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.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.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); 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 else if (clock.mode == MODE_NORMAL)
{ {
DrawRectanglePro( // Draw hand seconds
(Rectangle){ clock.second.origin.x, DrawRectanglePro((Rectangle){ centerPosition.x, centerPosition.y, clock.second.length, clock.second.thickness },
clock.second.origin.y, (Vector2){ 0.0f, clock.second.thickness/2.0f }, clock.second.angle, clock.second.color);
clock.second.length,
clock.second.thickness,
}
, (Vector2){0, clock.second.thickness/2},
clock.second.angle,
clock.second.colour
);
DrawRectanglePro( // Draw hand minutes
(Rectangle){ clock.minute.origin.x, DrawRectanglePro((Rectangle){ centerPosition.x, centerPosition.y, clock.minute.length, clock.minute.thickness },
clock.minute.origin.y, (Vector2){ 0.0f, clock.minute.thickness/2.0f }, clock.minute.angle, clock.minute.color);
clock.minute.length,
clock.minute.thickness,
}
, (Vector2){0, clock.minute.thickness/2},
clock.minute.angle,
clock.minute.colour
);
DrawRectanglePro( // Draw hand hours
(Rectangle){ clock.hour.origin.x, DrawRectanglePro((Rectangle){ centerPosition.x, centerPosition.y, clock.hour.length, clock.hour.thickness },
clock.hour.origin.y, (Vector2){ 0.0f, clock.hour.thickness/2.0f }, clock.hour.angle, clock.hour.color);
clock.hour.length,
clock.hour.thickness,
}
, (Vector2){0, clock.hour.thickness/2},
clock.hour.angle,
clock.hour.colour
);
} }
} }
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
Clock myClock = {
.cm = NORMAL_MODE,
.second.origin = (Vector2){400, 225},
.second.angle = 45,
.second.length = 140,
.second.thickness = 3,
.second.colour = BEIGE,
.minute.origin = (Vector2){400, 225},
.minute.angle = 10,
.minute.length = 130,
.minute.thickness = 7,
.minute.colour = DARKGRAY,
.hour.origin = (Vector2){400, 225},
.hour.angle = 0,
.hour.length = 100,
.hour.thickness = 7,
.hour.colour = BLACK,
};
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - digital clock");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
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;
}
UpdateClock(&myClock);
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawCircle(400, 225, 5, BLACK); // center dot
drawClock(myClock);
DrawText("press [SPACE] to switch clock mode", 350, 400, 10, GRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}