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
|
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <locale.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
/* Definition de certaines constantes */
#define RAYON 80
#define rectifHEU 30
#define rectifMIN 15
/* Fonctions statiques */
static void allume_pixel(int x, int y,Uint32 couleur);
static void dessineHorloge(void);
static void dessine(void);
static void afficheSecondes(int seconde);
static void afficheMinutes(int minute);
static void afficheHeures(int heure, int minute);
static void tracerSegment(int xi,int yi,int xf,int yf, Uint32 couleur);
/* Structure pour les variables globales */
typedef struct sVarGlob
{
SDL_Surface *ecran, *points;
int init;
}
SVarGlob;
/* Variable globale */
SVarGlob param =
{
NULL, NULL, 0
};
/* Fonction main */
int main (int argc, char *argv[])
{
SDL_Event event;
int done = 0;
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);
SDL_FreeSurface(icone);
param.ecran = SDL_SetVideoMode(200, 200, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( param.ecran == NULL )
{
fprintf(stderr, "Impossible de mettre la fenêtre en 200x200x8 : %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
param.points = IMG_Load("points.bmp");
if (!param.points)
{
fprintf(stderr, "Erreur chargement image : %s.\n", SDL_GetError());
exit(EXIT_FAILURE);
}
SDL_WM_SetCaption("Horloge",NULL);
while(!done)
{
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
done = 1;
break;
default:
break;
}
}
dessine();
}
(void) argc;
(void) argv;
return EXIT_SUCCESS;
}
/* Sert pour dessiner le cadran */
/* Celui ci est composé de 12 points */
void dessineHorloge(void)
{
SDL_Rect positionPixel;
int compteur;
int angle = 30; /* Angle entre chaque points/heures sur le cercle */
int decalage = 10; /* Un décalage pour bien centrer le cadran */
/* Certainement un bricolage de ma part */
SDL_FillRect(param.ecran, NULL, SDL_MapRGB(param.ecran->format,0,0,0));
/* La boucle for permet de calculer la position, grâce à de la trigo..., */
/* des points que l'on blitera */
for (compteur = 12; compteur > 0; compteur --)
{
positionPixel.x = RAYON * cos((M_PI * (angle) / 180) * compteur) +RAYON
+ param.points->w + decalage;
positionPixel.y = RAYON * sin((M_PI * (angle) / 180) * compteur) +RAYON
+ param.points->h + decalage;
SDL_BlitSurface(param.points, NULL, param.ecran, &positionPixel);
}
}
/* Le commentaire est identique pour les affichages suivants */
/* Donc ici, c'est pour afficher l'aiguille des secondes */
void afficheSecondes(int seconde)
{
SDL_Rect positionCentre;
positionCentre.x = param.ecran->w/2;
positionCentre.y = param.ecran->h/2;
SDL_Rect positionFin;
Uint32 rose = SDL_MapRGB(param.ecran->format, 255, 4, 255);
double R = RAYON;
double A; /* l'angle */
/* que l'on calcule.... dur, dur la trigonométrie */
A = (M_PI_2)-seconde*(M_PI)/30;
/* de même pour la position "Finale" de l'aiguille */
/* l'aiguille est un segment du centre, vers le cercle du cadran */
/* on calcul donc cette dernière position pour pouvoir tracer le segment */
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 = param.ecran->w/2;
positionCentre.y = param.ecran->h/2;
SDL_Rect positionFin;
Uint32 bleu = SDL_MapRGB(param.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, int minute)
{
SDL_Rect positionCentre;
positionCentre.x = param.ecran->w/2;
positionCentre.y = param.ecran->h/2;
SDL_Rect positionFin;
Uint32 blanc = SDL_MapRGB(param.ecran->format, 255, 255, 255);
double R = RAYON-rectifHEU;
double A;
A =(M_PI_2)-heure*(M_PI)/6;
/* Contrairement aux minutes et secondes, on fait avancer cette aiguille */
/* un peu différement. Son mouvement est basé sur celui des minutes. */
/* A chaque minutes, l'aiguille avance d'un cran. */
positionFin.x = (int)(cos( A+ ((((M_PI_2)-minute*(M_PI))/30)*0.5)/6 )*R + positionCentre.x);
positionFin.y = (int)(-sin(A+ ((((M_PI_2)-minute*(M_PI))/30))*0.5/6)*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) ;
}
}
}
void allume_pixel(int x, int y,Uint32 couleur)
{
SDL_Rect position;
position.x = x;
position.y = y;
position.w = 2;
position.h = 2;
SDL_FillRect(param.ecran, &position, couleur);
}
void dessine()
{
struct tm *tPre; /* structure pour le temps présent */
time_t timePresent;
/* récupération du temps */
timePresent = time (NULL);
tPre = localtime(&timePresent);
/* Affichage */
dessineHorloge();
afficheMinutes(tPre->tm_min);
afficheHeures(tPre->tm_hour, tPre->tm_min);
afficheSecondes(tPre->tm_sec);
SDL_Flip(param.ecran);
} |
Partager