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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <locale.h>
#include <SDL/SDL.h>
#include <SDL/SDL_Image.h>
#define RAYON 80
#define rectifHEU 30
#define rectifMIN 15
void programme(void);
void allume_pixel(int x, int y,Uint32 couleur);
void dessineHorloge(void);
void recupTemps(void);
void afficheSecondes(int seconde);
void afficheMinutes(int minute);
void afficheHeures(int heure);
void tracerSegment(int xi,int yi,int xf,int yf, Uint32 couleur);
SDL_Surface *ecran;
int init = 0;
int main (int argc, char *argv[])
{
atexit(SDL_Quit);
if (SDL_Init(SDL_INIT_VIDEO) == -1)
{
fprintf(stderr, "Erreur lors de l'initialisation de la SDL : %s\n", SDL_GetError());
exit (EXIT_FAILURE);
}
SDL_Surface *icone = IMG_Load("horloge.gif");
if (!icone)
{
fprintf(stderr, "Impossible de charger l'image : %s.\n",SDL_GetError);
exit(EXIT_FAILURE);
}
SDL_WM_SetIcon(icone, NULL);
ecran = SDL_SetVideoMode(200, 200, 32, SDL_HWSURFACE);
if ( ecran == NULL )
{
fprintf(stderr, "Impossible de mettre la fenêtre en 200x200x8 : %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
SDL_WM_SetCaption("Horloge","horloge.ico");
dessineHorloge();
programme();
return EXIT_SUCCESS;
}
void programme(void)
{
SDL_Event event;
int done = 0;
/* On ignore certains évenements qui font scintiller l'horloge */
SDL_EventState (SDL_MOUSEMOTION , SDL_IGNORE);
SDL_EventState (SDL_ACTIVEEVENT, SDL_IGNORE);
SDL_EventState (SDL_KEYDOWN, SDL_IGNORE);
SDL_EventState (SDL_KEYUP, SDL_IGNORE);
while(!done)
{
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
done = 1;
break;
default:
break;
}
}
recupTemps();
}
}
void dessineHorloge(void)
{
SDL_Rect positionPixel;
SDL_Surface *points = IMG_Load("points.bmp");
if (!points)
{
fprintf(stderr, "Impossible de charger l'image : %s.\n", SDL_GetError());
exit(EXIT_FAILURE);
}
int rayon = 80;
int compteur;
int angle = 30;
int decalage = 10;
positionPixel.x = positionPixel.y = 0;
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format,0,0,0));
SDL_BlitSurface(ecran, NULL, ecran, &positionPixel);
for (compteur = 12; compteur > 0; compteur --)
{
positionPixel.x = rayon * cos((M_PI * (angle) / 180) * compteur) +rayon + points->w + decalage;
positionPixel.y = rayon * sin((M_PI * (angle) / 180) * compteur) +rayon + points->h + decalage;
SDL_BlitSurface(points, NULL, ecran, &positionPixel);
}
SDL_Flip(ecran);
SDL_FreeSurface(points);
}
void recupTemps(void)
{
time_t timePresent;
struct tm *tPre, *tAnc;
timePresent = time (NULL);
tPre = localtime(&timePresent);
if (init == 0)
{
tAnc->tm_sec = 0;
tAnc->tm_min = 0;
tAnc->tm_hour = 0;
afficheSecondes(tPre->tm_sec);
afficheMinutes(tPre->tm_min);
afficheHeures(tPre->tm_hour);
init = 1;
}
else
{
if (tAnc->tm_sec != tPre->tm_sec)
{
dessineHorloge();
afficheMinutes(tPre->tm_min);
afficheHeures(tPre->tm_hour);
afficheSecondes(tPre->tm_sec);
tAnc->tm_sec = tPre->tm_sec;
}
}
}
void afficheSecondes(int seconde)
{
SDL_Rect positionCentre;
positionCentre.x = ecran->w/2;
positionCentre.y = ecran->h/2;
SDL_Rect positionFin;
Uint32 rose = SDL_MapRGB(ecran->format, 255, 4, 255);
double R = RAYON;
double A;
A = (M_PI_2)-seconde*(M_PI)/30;
positionFin.x = (int)(cos(A)*R + positionCentre.x);
positionFin.y = (int)(-sin(A)*R + positionCentre.y);
tracerSegment(positionCentre.x, positionCentre.y, positionFin.x, positionFin.y, rose);
}
void afficheMinutes(int minute)
{
SDL_Rect positionCentre;
positionCentre.x = ecran->w/2;
positionCentre.y = ecran->h/2;
SDL_Rect positionFin;
Uint32 bleu = SDL_MapRGB(ecran->format, 4, 4, 255);
double R = RAYON - rectifMIN;
double A;
A = (M_PI_2)-minute*(M_PI)/30;
positionFin.x = (int)(cos(A)*R + positionCentre.x);
positionFin.y = (int)(-sin(A)*R + positionCentre.y);
tracerSegment(positionCentre.x, positionCentre.y, positionFin.x, positionFin.y, bleu);
}
void afficheHeures(int heure)
{
SDL_Rect positionCentre;
positionCentre.x = ecran->w/2;
positionCentre.y = ecran->h/2;
SDL_Rect positionFin;
Uint32 blanc = SDL_MapRGB(ecran->format, 255, 255, 255);
double R = RAYON-rectifHEU;
double A;
A =(M_PI_2)-heure*(M_PI)/6;
positionFin.x = (int)(cos(A)*R + positionCentre.x);
positionFin.y = (int)(-sin(A)*R + positionCentre.y);
tracerSegment(positionCentre.x, positionCentre.y, positionFin.x, positionFin.y, blanc);
}
/* Algo de Bresenham */
/* http://raphaello.univ-fcomte.fr/Ig/Algorithme/ */
void tracerSegment(int xi,int yi,int xf,int yf, Uint32 couleur)
{
int dx,dy,i,xinc,yinc,cumul,x,y ;
x = xi ;
y = yi ;
dx = xf - xi ;
dy = yf - yi ;
xinc = ( dx > 0 ) ? 1 : -1 ;
yinc = ( dy > 0 ) ? 1 : -1 ;
dx = abs(dx) ;
dy = abs(dy) ;
allume_pixel(x,y, couleur) ;
if ( dx > dy )
{
cumul = dx / 2 ;
for ( i = 1 ; i <= dx ; i++ )
{
x += xinc ;
cumul += dy ;
if (cumul >= dx)
{
cumul -= dx ;
y += yinc ;
}
allume_pixel(x,y, couleur) ;
}
}
else
{
cumul = dy / 2 ;
for ( i = 1 ; i <= dy ; i++ )
{
y += yinc ;
cumul += dx ;
if ( cumul >= dy )
{
cumul -= dy ;
x += xinc ;
}
allume_pixel(x,y,couleur) ;
}
}
SDL_Flip(ecran);
}
void allume_pixel(int x, int y,Uint32 couleur)
{
SDL_Rect position;
position.x = x;
position.y = y;
SDL_Surface *surface;
surface = SDL_CreateRGBSurface(SDL_HWSURFACE, 2,2,32,0,0,0,0);
SDL_FillRect(surface, NULL, couleur);
SDL_BlitSurface(surface, NULL, ecran, &position);
SDL_FreeSurface(surface);
} |
Partager