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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
|
#include <stdlib.h>
#include <stdio.h>
/*#include <iostream.h>*/
#include <math.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
//void ::afficher(SDL_surface *ecran = NULL, position_souris.x, position_souris.y, touche)
/* ***************************************************************************************************************************************************************
test en regroupant tout dans le main
************************************************************************************************************************************************************** */
void genererGrille(int posx,int posy,int grille[9][9],int valrempl,int grilledep[9][9],SDL_Surface* ecran, TTF_Font* police,SDL_Color couleurRouge,SDL_Color couleurNoire)
{
SDL_Surface* text;
SDL_Rect position;
char tempchar[0]; // tableau de caractere à une entree
int tempi,tempj,temp;
double tempf;
// Nous donne les numeros des cases(position.x,position.y)
// Conversion des coordonnées(pixels) en cases
temp=posx; // On met posx dans temp
tempf=temp/50; // On divise temp par 50 et on le met dans tempf
temp=ceil(tempf); // On prend l'arrondi superieur de tempf et on le met dans temp
temp=temp-1; // Decalage à gauche
position.y=temp; // On met temp dans position.y --> on a l'ordonnée de la case(x,position.y)
temp=posy-100;
tempf=temp/50;
temp=ceil(tempf);
temp=temp-1;
position.x=temp;
grille[position.x][position.y]=valrempl; //On met les valeurs de remplissage (valrempl) dans les cases
// On parcourt le tableau
for (int i=0 ; i<9 ; i++)
{
text = TTF_RenderText_Blended(police, "SUDOKU", couleurRouge); /* Ecriture du texte dans la SDL_Surface "texte" en mode Blended (optimal) */
for (int j=0 ; j<9 ; j++)
{
if (grilledep[i][j]==-1) //autorisation d'écrire -> couleur noire
{
//couleur noire (chiffres à l origine dans la grille)
if (grille[i][j]!=-1) //vide==-1 ou non!=-1
{
// Conversion des cases en coordonnées(pixels) --> on convertit "i" en coordonnées et on le met dans position.y
tempi=i;
tempi=tempi+1; //changement d'indice a cause du tableau qui commence a 0 et pas 1
tempi=tempi*50;
tempi=tempi+100;
position.y=tempi-35;
// Conversion des cases en coordonnées --> on convertit "j" en coordonnées et on le met dans position.x
tempj=j;
tempj=tempj+1;
tempj=tempj*50;
tempj=tempj+100;
position.x=tempj-49;
itoa(grille[position.x][position.y],tempchar,10); //convertit un "int" en "char" --> convertit grille en
text= TTF_RenderText_Blended(police, tempchar, couleurRouge);
SDL_BlitSurface(text, NULL, ecran, &position);
SDL_Flip(ecran);
}
}
else
{
//couleur rouge (chiffres que nous ecrirons dans la grille)
tempi=i;
tempi=tempi+1;
tempi=tempi*50;
tempi=tempi+100;
position.y=tempi-35;
tempj=j;
tempj=tempj+1;
tempj=tempj*50;
tempj=tempj+100; //changemant d'indice a cause du tableau qui commence a 0 et pas 1
position.x=tempj-49;
itoa(grilledep[i][j],tempchar,10);
text= TTF_RenderText_Blended(police, tempchar, couleurNoire);
SDL_BlitSurface(text, NULL, ecran, &position);
SDL_Flip(ecran);
}
}
}
}
/*
void gestioncase(int valrempl,int x,int y)
{
int tempx=0,tempy=0;
tempx=convabscase(x);
tempy=convabscase(y);
if (grilledep[tempx][tempy]==-1)
{
grille[tempx][tempy]=valrempl; //ne pas oublier le test d'autorisation d'écriture
}
}
int fileMoiLaValeur(int x)
{
int temp;
temp=convabscase(x);
return(x);
}
*/
int grilledep[9][9]={8,4,-1,-1,-1,-1,7,6,-1,
7,-1,-1,8,-1,1,2,-1,-1,
-1,9,6,4,-1,7,-1,-1,5,
-1,3,1,-1,7,-1,-1,2,8,
2,-1,9,-1,3,8,1,-1,4,
-1,8,-1,-1,9,-1,-1,7,-1,
9,2,-1,-1,-1,-1,5,-1,6,
3,-1,-1,2,1,5,4,9,-1,
-1,1,-1,-1,4,-1,3,-1,-1};
int grille[9][9]={8,4,-1,-1,-1,-1,7,6,-1,
7,-1,-1,8,-1,1,2,-1,-1,
-1,9,6,4,-1,7,-1,-1,5,
-1,3,1,-1,7,-1,-1,2,8,
2,-1,9,-1,3,8,1,-1,4,
-1,8,-1,-1,9,-1,-1,7,-1,
9,2,-1,-1,-1,-1,5,-1,6,
3,-1,-1,2,1,5,4,9,-1,
-1,1,-1,-1,4,-1,3,-1,-1};
int convabscase(int x) // fonction convertissant les coordonnées en case
{
int temp;
double tempf;
temp=temp-100;
tempf=temp/50;
temp=ceil(tempf);
temp=temp-1; //changemant d'indice a cause du tableau qui commence a 0 et pas à 1
return(temp);
}
int convcaseabs(int x) // fonction convertissant les cases en coordonnées
{
int temp;
temp=temp+1;
temp=temp*50;
temp=temp+100; //changemant d'indice a cause du tableau qui commence a 0 et pas à 1
return(temp);
}
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
/* **********************************************************************************************************************************************************
Intitialisation
*********************************************************************************************************************************************************** */
SDL_Surface *ecran = NULL, *imageDeFond= NULL, *texte = NULL, *fond = NULL; // Le pointeur qui va stocker la surface, le texte, l'image et le fond de l'écran
SDL_Rect position;
SDL_Rect position_souris; //déclaration des variables
SDL_Event event;
TTF_Font *police = NULL;
TTF_Font *police2 = NULL;
SDL_Color couleurRouge =
{
248, 16, 3
};
SDL_Color couleurNoire =
{
0, 0, 0
};
SDL_Color couleurBleue =
{
0, 0, 255
}
; // déclaration de la couleur d'écriture
int continuer = 1;
int tabl[9]=
{
1,2,3,4,5,6,7,8,9
};
int valutilt=0;
// char tps[20] = ""; /* Tableau de char suffisamment grand */
int touche;
int valrempl=1;
double tempf;
position.x = 100;
position.y = 100;
SDL_Init(SDL_INIT_VIDEO); //Charge le système d'affichage (vidéo).
TTF_Init();
ecran = SDL_SetVideoMode(650, 750, 32, SDL_HWSURFACE|SDL_DOUBLEBUF); // On tente d'ouvrir une fenêtre, double buffering
if (ecran == NULL) // Si l'ouverture a échoué, on écrit l'erreur et on arrête
{
fprintf(stderr, "Impossible de charger le mode vidéo : %s\nq", SDL_GetError());
exit(EXIT_FAILURE);
}
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 50, 187, 27));
SDL_WM_SetCaption("Sudoku", NULL);
police = TTF_OpenFont("papyrus.ttf", 72);
// police2 = TTF_OpenFont("papyrus.ttf", 40); /* Chargement de la police */
texte = TTF_RenderText_Blended(police, "SUDOKU", couleurRouge); /* Ecriture du texte dans la SDL_Surface "texte" en mode Blended (optimal) */
position.x = 100;
position.y = 150;
SDL_BlitSurface(fond, NULL, ecran, &position); /* Blit du fond */
imageDeFond = SDL_LoadBMP("tableau.bmp");
SDL_BlitSurface(imageDeFond, NULL, ecran, &position);
SDL_Flip(ecran);
position.x = 110;
position.y = 10;
SDL_BlitSurface(texte, NULL, ecran, &position); /* Blit du texte par-dessus */
SDL_Flip(ecran);
//gestion de la souris
genererGrille(101,101,grille,0,grilledep,ecran,police,couleurRouge,couleurNoire);
while (continuer==1)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = 0;
break;
case SDL_MOUSEBUTTONUP:
position_souris.x = event.button.x - 40; /* On change les coordonnées de la souris avec une correction de position*/
position_souris.y = event.button.y - 10;
if ((position_souris.x >=75 &&position_souris.x <=525)&&(position_souris.y >=100 &&position_souris.y <=575)) //tablSudo
{
position.x = 100;
position.y = 150;
SDL_BlitSurface(fond, NULL, ecran, &position); /* Blit du fond */
imageDeFond = SDL_LoadBMP("tableau.bmp");
SDL_BlitSurface(imageDeFond, NULL, ecran, &position);
SDL_Flip(ecran);
genererGrille(position_souris.x,position_souris.y,grille,valrempl,grilledep,ecran,police,couleurRouge,couleurNoire);
}
else if ((position_souris.x >=75 &&position_souris.x <=525)&&(position_souris.y >=625 &&position_souris.y <=675)) //tablRempl
{
position_souris.x=position_souris.x-100;
tempf=position_souris.x/50;
position_souris.x=ceil(tempf);
valrempl=position_souris.x;
// valrempl=gettablrempl(position_souris.x);
/*valutilt=position_souris.x;
valutilt=valutilt-100;
valutilt=valutilt/50;
valutilt=ceil(valutilt);
valutilt=valutilt-1;
valrempl=tabl[valutilt];
*/
// Jeu.rafraichissement(); //seulement si on affiche le numéro en cours
}
}
}
/*
SDL_Delay(2000);
SDL_FreeSurface(imageDeFond);
TTF_CloseFont(police);
TTF_Quit(); // Arrêt de la TTL (libération de la mémoire).
SDL_FreeSurface(texte);
SDL_Quit(); // Arrêt de la SDL (libération de la mémoire).
return 0;*/
} |
Partager