1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| #include <math.h>
#include <time.h>
#include <stdio.h>
#include <stdbool.h>
#include <sdl2/sdl.h>
#define WND_SIZE_HORZ 600
#define WND_SIZE_VERT 600
#define WND_COLOR_R 50
#define WND_COLOR_G 100
#define WND_COLOR_B 150
#define WND_TITLE "Test degree"
#define RDR_DEFAULT_DRIVER -1
#define UNUSED(x) (void)x
#define R(x) ((x)>>16)
#define G(x) (((x)>>8) & 0xff)
#define B(x) ((x) & 0xff)
#define TO_SDL_OPAQUE_COLOR(color) (SDL_Color){R(color), G(color), B(color), SDL_ALPHA_OPAQUE}
#define PI 3.14159265358979323846
#define RAY 100
#define DISK_RAY 6
#define DISK_COLOR_1 0x808080
#define DISK_COLOR_5 0xf0f0f0
#define BG_COLOR 0x326496
#define TORADIAN(x) ((x)*(PI/180))
struct minute_disks {
SDL_Texture *p1; // le disque pour les minutes
SDL_Texture *p5; // le disque pour les multiples de 5 minutes
};
void DisplaySDLVersion(void) {
SDL_version v;
SDL_GetVersion(&v);
printf("SDL version %hd.%hd.%hd\n", v.major, v.minor, v.patch);
}
void HorzLine(SDL_Surface *dest, int x, int y, int w, int thickness, Uint32 color) {
SDL_Rect r={.x=x, .y=y, .w=w, .h=thickness};
SDL_FillRect(dest, &r, color);
}
void DrawFilledCircle(SDL_Surface *dest, int cx, int cy, int ray, Uint32 color) {
for(int x=0, y=ray, d=3-(2*ray); y>=x; ++x) {
HorzLine(dest, cx-x, cy-y, 2*x+1, 1, color);
HorzLine(dest, cx-x, cy+y, 2*x+1, 1, color);
HorzLine(dest, cx-y, cy-x, 2*y+1, 1, color);
HorzLine(dest, cx-y, cy+x, 2*y+1, 1, color);
if(d<0)
d+=4*x+6;
else {
d+=4*(x-y)+10;
--y;
}
}
}
SDL_Surface *CreateFilledCircle(int diameter, Uint32 bg_color, Uint32 fg_color) {
SDL_Surface *circle;
circle=SDL_CreateRGBSurface(0, diameter, diameter, 32, 0, 0, 0, 0);
SDL_FillRect(circle, NULL, bg_color);
DrawFilledCircle(circle, diameter/2, diameter/2, diameter/2-1, fg_color);
return(circle);
}
void CreateMinutesPoint(SDL_Renderer *rdr, struct minute_disks *m) {
SDL_Surface *tmp=CreateFilledCircle(DISK_RAY-1, BG_COLOR, DISK_COLOR_1);
m->p1=SDL_CreateTextureFromSurface(rdr, tmp);
SDL_FreeSurface(tmp);
tmp=CreateFilledCircle(DISK_RAY, BG_COLOR, DISK_COLOR_5);
m->p5=SDL_CreateTextureFromSurface(rdr, tmp);
SDL_FreeSurface(tmp);
}
void DisplayWatch(SDL_Renderer *rdr, SDL_Point *center, struct minute_disks *d) {
for(int a=0; a<360; a+=6) {
SDL_Rect r;
r.x=center->x+cos(TORADIAN(a))*RAY;
r.y=center->y+sin(TORADIAN(a))*RAY;
r.w=r.h=DISK_RAY;
SDL_Texture *tx=a%5 ? d->p1 : d->p5;
SDL_RenderCopy(rdr, tx, NULL, &r);
}
}
int main(int argc,char *argv[]) {
UNUSED(argc);
UNUSED(argv);
// DisplaySDLVersion();
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *wnd=SDL_CreateWindow(WND_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WND_SIZE_HORZ, WND_SIZE_VERT, SDL_WINDOW_SHOWN);
SDL_Renderer *rdr=SDL_CreateRenderer(wnd, RDR_DEFAULT_DRIVER, SDL_RENDERER_ACCELERATED);
SDL_Point center={.x=WND_SIZE_HORZ/2, .y=WND_SIZE_VERT/2};
struct minute_disks minutes_point;
CreateMinutesPoint(rdr, &minutes_point);
// time_t tstart=time(NULL);
int r=0;
while(true) {
SDL_SetRenderDrawColor(rdr, R(BG_COLOR), G(BG_COLOR), B(BG_COLOR), SDL_ALPHA_OPAQUE);
SDL_RenderClear(rdr);
DisplayWatch(rdr, ¢er, &minutes_point);
SDL_SetRenderDrawColor(rdr, 0xff, 0xff, 0xff, SDL_ALPHA_OPAQUE);
SDL_RenderDrawLine(rdr, center.x, center.y, center.x+cos(TORADIAN(r))*(RAY-10), center.x+sin(TORADIAN(r))*(RAY-10));
SDL_RenderPresent(rdr);
SDL_Event event;
if(SDL_PollEvent(&event))
if(event.type==SDL_QUIT || (event.type==SDL_KEYDOWN && event.key.keysym.sym==SDLK_ESCAPE))
break;
SDL_Delay(125);
// while(time(NULL)-tstart<1);
// tstart=time(NULL);
r+=360/60;
if(r>=360)
r=0;
}
SDL_DestroyTexture(minutes_point.p1);
SDL_DestroyTexture(minutes_point.p5);
SDL_DestroyRenderer(rdr);
SDL_DestroyWindow(wnd);
SDL_Quit();
return(0);
} |
Partager