The basic realization idea is this: there is a dial picture and three pointer pictures, the dial picture as the background, the pointer picture starting point in the center of the dial. Gets the system time, calculates the pointer angle, and redraw. Listen for the Exit event and the program exits.
The implementation effect is as follows:
Specific code:
#include "sdl2/sdl.h"
#include <stdio.h>
#include <time.h>
const int screen_width = 512;
const int screen_height = 489;
Sdl_window *window = NULL;
Sdl_renderer *renderer = NULL;
/* Print out error information * *
void ShowError () {
printf ("%s\n", Sdl_geterror ());
}
/* Load image by FileName * *
sdl_texture* LoadImage (char file[]) {
Sdl_surface *loadedimage = NULL;
Sdl_texture *texture = NULL;
Loadedimage = sdl_loadbmp (file);
if (loadedimage!= NULL) {
Texture = sdl_createtexturefromsurface (renderer, loadedimage);
Sdl_freesurface (Loadedimage);
}
Else
ShowError ();
return texture;
}
* Show the Backgroud * *
void Applybackground (Sdl_texture *tex, Sdl_renderer *rend) {
Sdl_rect POS;
Sdl_querytexture (tex, NULL, NULL, &POS.W, &pos.h);
pos.x = Pos.y = 0;
Sdl_rendercopy (renderer, Tex, NULL, NULL);
}
* Show the pointers by angle * *
void Applysurface (Sdl_texture *tex, Sdl_renderer *rend, double angle) {
int x = screen_width/2+7;
int y = SCREEN_HEIGHT/2;
Sdl_rendererflip Flip = Sdl_flip_horizontal | sdl_flip_vertical;
Sdl_rect POS;
Sdl_querytexture (tex, NULL, NULL, &POS.W, &pos.h);
pos.x = x;
Pos.y = y;
Sdl_point p = {0, 0};
Sdl_rendercopyex (Renderer, Tex, NULL, &pos, Angle, &p, flip);
}
int main () {
if (Sdl_init (sdl_init_everything) = =-1) {
ShowError ();
return 1;
}
/* Initial Window * *
window = Sdl_createwindow ("Clock", sdl_windowpos_centered,
Sdl_windowpos_centered, Screen_width, Screen_height, Sdl_window_shown);
if (window = = NULL) {
ShowError ();
return 2;
}
/* Initial Renderer * *
Renderer = sdl_createrenderer (window,-1, sdl_renderer_accelerated | Sdl_renderer_presentvsync);
if (renderer = = NULL) {
ShowError ();
return 3;
}
/* Load Pictures * *
Sdl_texture *background = null, *hour = NULL,
*min = null, *second = NULL;
Background = LoadImage ("Background.bmp");
hour = LoadImage ("Hour.bmp");
min = LoadImage ("Min.bmp");
Second = LoadImage ("Second.bmp");
if (background = NULL | hour = NULL | min = NULL | second = NULL) {
return 4;
}
time_t Rawtime;
struct TM *timeinfo;
int quit = 0;
sdl_event E;
/* begin to run */
while (!quit) {
time (&rawtime);
ti Meinfo = LocalTime (&rawtime);
/* Get system time */
int h = timeinfo->tm_hour%, M = TIMEINFO-&G T;tm_min,
s = timeinfo->tm_sec
/* calculate their angles */< br> double angle_s = s*1.0*6-90;
double angle_m = m*1.0*6 + angle_s/360*6-90;
double angle_h = h*1.0*30 + ANGLE_M/360/2-90;
&NBSP;&NBSP
/* repaint */
sdl_renderclear (renderer);
Applybackground (background, renderer);
applysurface (hour, renderer, angle_h);
applysurface (min, renderer, angle_m);
applysurface (second, renderer, angle_s);
Sdl_renderpresent (renderer);
Sdl_delay (1000);
/* Way to quit * *
while (Sdl_pollevent (&e)) {
if (E.type = = sdl_quit)//Close the window
Quit = 1;
if (E.type = = Sdl_mousebuttondown)//Click the mouse
Quit = 1;
}
}
/* Free Memory * *
Sdl_destroytexture (background);
Sdl_destroytexture (hour);
Sdl_destroytexture (min);
Sdl_destroytexture (second);
Sdl_destroyrenderer (renderer);
Sdl_destroywindow (window);
Sdl_quit ();
return 0;
}