00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "MVirtuel.h"
00012
00013
00014 MUTEX MVirtuel::MutexEcran = MutexInit(&DBMutex);
00015
00016 MVirtuel::MVirtuel(int iColor)
00017 {
00018 #ifdef WIN32
00019 Buffer = CreateConsoleScreenBuffer(
00020 GENERIC_READ|GENERIC_WRITE,
00021 NULL,
00022 NULL,
00023 CONSOLE_TEXTMODE_BUFFER,
00024 NULL
00025 );
00026 #else
00027
00028 #endif
00029
00030 for(int i = 1; i < NBL;i++)
00031 SetColor(1, i, iColor, NBC);
00032
00033 ColorFond = iColor;
00034 ColorFenetre = COLOR_FG|ColorFond;
00035 ColorCadre = COLOR_FR|ColorFond;
00036 ColorText = COLOR_FW|ColorFond;
00037 }
00038
00039 MVirtuel::~MVirtuel()
00040 {
00041 CloseHandle(hBuffer);
00042 }
00043
00044 int MVirtuel::SetColor(int iPosX, int iPosY, int iColor, int iLength)
00045 {
00046 int *Data;
00047 COORD Pos;
00048 int i = 0;
00049 Pos.X = PosX;
00050 Pos.Y = PosY;
00051
00052 Data = new WORD[Length];
00053 for(i; i < Length; Data[i++] = Color);
00054
00055 MutexLock(&MutexEcran);
00056
00057 WriteConsoleOutputAttribute(
00058 hBuffer,
00059 Data,
00060 Length,
00061 Pos,
00062 (LPDWORD)Length
00063 );
00064 return ReleaseMutex(hMutexEcran);
00065 }
00066
00067 BOOL MVirtuel::Write(int PosX, int PosY, char *Data, WORD Color)
00068 {
00069 DWORD DataSize = strlen(Data);
00070 COORD Pos;
00071 Pos.X = PosX;
00072 Pos.Y = PosY;
00073
00074 if (!Color)
00075 Color = wColorText;
00076
00077 SetColor(PosX, PosY, Color, DataSize);
00078
00079 WaitForSingleObject(hMutexEcran, INFINITE);
00080 WriteConsoleOutputCharacter(
00081 hBuffer,
00082 Data,
00083 DataSize,
00084 Pos,
00085 &DataSize
00086 );
00087 return ReleaseMutex(hMutexEcran);
00088 }
00089
00090 BOOL MVirtuel::Write(int PosX, int PosY, int Data, WORD Color)
00091 {
00092 char Tampon[16];
00093 DWORD DataSize = 0;
00094 COORD Pos;
00095 Pos.X = PosX;
00096 Pos.Y = PosY;
00097
00098 if (!Color)
00099 Color = wColorText;
00100
00101 _itoa(Data, Tampon, 10);
00102 DataSize = strlen(Tampon);
00103
00104 SetColor(PosX, PosY, Color, DataSize);
00105
00106 WaitForSingleObject(hMutexEcran, INFINITE);
00107 WriteConsoleOutputCharacter(
00108 hBuffer,
00109 Tampon,
00110 DataSize,
00111 Pos,
00112 &DataSize
00113 );
00114 return ReleaseMutex(hMutexEcran);
00115 }
00116
00117 BOOL MVirtuel::Write(int PosX, int PosY, float Data, WORD Color)
00118 {
00119 char Tampon[16];
00120 char Float[5];
00121 DWORD DataSize = 0;
00122 COORD Pos;
00123 Pos.X = PosX;
00124 Pos.Y = PosY;
00125
00126 if (!Color)
00127 Color = wColorText;
00128
00129 _itoa((int)Data, Tampon, 10);
00130 strcat(Tampon, ".\0");
00131 _itoa((int)((Data-(int)Data)*100), Float, 10);
00132 strcat(Tampon, Float);
00133 DataSize = strlen(Tampon);
00134
00135 SetColor(PosX, PosY, Color, DataSize);
00136
00137 WaitForSingleObject(hMutexEcran, INFINITE);
00138 WriteConsoleOutputCharacter(
00139 hBuffer,
00140 Tampon,
00141 DataSize,
00142 Pos,
00143 &DataSize
00144 );
00145 return ReleaseMutex(hMutexEcran);
00146 }
00147
00148 BOOL MVirtuel::Print()
00149 {
00150 return SetConsoleActiveScreenBuffer(
00151 hBuffer
00152 );
00153 }
00154
00155 BOOL MVirtuel::Copy(MVirtuel *Source)
00156 {
00157 COORD dwBufferCoord;
00158 COORD dwBufferSize;
00159 CHAR_INFO Buffer[NBL*NBC];
00160 SMALL_RECT Zone;
00161
00162 wColorFond = Source->GetColorFond();
00163 wColorFenetre = Source->GetColorFenetre();
00164 wColorCadre = Source->GetColorCadre();
00165 wColorText = Source->GetColorText();
00166
00167 dwBufferCoord.X = 0;
00168 dwBufferCoord.Y = 0;
00169 dwBufferSize.X = NBC;
00170 dwBufferSize.Y = NBL;
00171 Zone.Left = 0;
00172 Zone.Top = 0;
00173 Zone.Right = NBC-1;
00174 Zone.Bottom = NBL-1;
00175
00176 ReadConsoleOutput(
00177 Source->hBuffer,
00178 Buffer,
00179 dwBufferSize,
00180 dwBufferCoord,
00181 &Zone
00182 );
00183
00184 WaitForSingleObject(hMutexEcran, INFINITE);
00185 WriteConsoleOutput(
00186 hBuffer,
00187 Buffer,
00188 dwBufferSize,
00189 dwBufferCoord,
00190 &Zone
00191 );
00192 return ReleaseMutex(hMutexEcran);
00193 }
00194
00195 BOOL MVirtuel::SaisieChar(char *Titre, char *Text, int PosX, int PosY, int SizeText, int MaxX, int MaxY)
00196 {
00197 int Key;
00198 char * Chaine = new char[SizeText+1];
00199 int NbCar = strlen(Text);
00200 int i = 0, j =0;
00201 char CAR;
00202 MVirtuel Temp;
00203
00204 Temp.Copy(this);
00205
00206 for(i = PosX; i < MaxX; i++)
00207 for(j = PosY; j < MaxY; j++)
00208 Temp.Write(i, j, " ", GetColorFond());
00209
00210 for(i = PosX; i< MaxX; i++)
00211 {
00212 Temp.Write(i, PosY, Car[8], GetColorFenetre());
00213 Temp.Write(i, MaxY, Car[8], GetColorFenetre());
00214 }
00215 for(i = PosY; i< MaxY; i++)
00216 {
00217 Temp.Write(PosX, i, Car[9], GetColorFenetre());
00218 Temp.Write(MaxX, i, Car[9], GetColorFenetre());
00219 }
00220
00221 Temp.Write(PosX, PosY, Car[0], GetColorFenetre());
00222 Temp.Write(MaxX, PosY, Car[1], GetColorFenetre());
00223 Temp.Write(PosX, MaxY, Car[2], GetColorFenetre());
00224 Temp.Write(MaxX, MaxY, Car[3], GetColorFenetre());
00225
00226 Temp.Write(PosX+(MaxX-PosX-strlen(Titre))/2, PosY+2, Titre);
00227
00228 strcpy(Chaine, Text);
00229 for(i = strlen(Text)+1; i <= SizeText; Chaine[i++] = ' ');
00230 Chaine[SizeText+1] = '\0';
00231 do
00232 {
00233 Chaine[NbCar] = '_';
00234 Temp.Write(PosX+2, PosY+4, Chaine);
00235 Temp.Print();
00236 Chaine[NbCar] = ' ';
00237
00238 Key = _getch();
00239 switch(Key)
00240 {
00241 case 27 :
00242 fflush(stdin);
00243 return FALSE;
00244 break;
00245
00246 case 13 :
00247 Chaine[NbCar] = '\0';
00248 fflush(stdin);
00249 strcpy(Text, Chaine);
00250 return TRUE;
00251 break;
00252
00253 case 32 :
00254 if(NbCar < SizeText)
00255 NbCar++;
00256 break;
00257
00258 case 44 :
00259 if(NbCar < SizeText)
00260 {
00261 Chaine[NbCar] = ',';
00262 NbCar++;
00263 }
00264 break;
00265
00266 case 46 :
00267 if(NbCar < SizeText)
00268 {
00269 Chaine[NbCar] = '.';
00270 NbCar++;
00271 }
00272 break;
00273 case 39 :
00274 if(NbCar < SizeText)
00275 {
00276 Chaine[NbCar] = '\'';
00277 NbCar++;
00278 }
00279 break;
00280
00281 case 58 :
00282 if(NbCar < SizeText)
00283 {
00284 Chaine[NbCar] = ':';
00285 NbCar++;
00286 }
00287 break;
00288
00289 case 8 :
00290 if(NbCar > 0)
00291 {
00292 NbCar--;
00293 for(i=NbCar; i<SizeText; i++)
00294 Chaine[i] = ' ';
00295 }
00296 break;
00297
00298 default :
00299 if(isalnum(Key) && (NbCar < SizeText))
00300 {
00301 Chaine[NbCar] = (char)Key;
00302 NbCar++;
00303 }
00304 break;
00305 }
00306 }
00307 while(1);
00308
00309 fflush(stdin);
00310 return FALSE;
00311 }
00312
00313 BOOL MVirtuel::SaisieFloat(char *Titre, float *Float, int PosX, int PosY, int MaxX, int MaxY)
00314 {
00315 int Key;
00316 char * Chaine = new char[10];
00317 int NbCar = 0;
00318 int i = 0, j =0;
00319 char CAR;
00320 MVirtuel Temp;
00321 BOOL Point = TRUE;
00322
00323 Temp.Copy(this);
00324
00325 for(i = PosX; i < MaxX; i++)
00326 for(j = PosY; j < MaxY; j++)
00327 Temp.Write(i, j, " ", GetColorFond());
00328
00329 for(i = PosX; i< MaxX; i++)
00330 {
00331 Temp.Write(i, PosY, Car[8], GetColorFenetre());
00332 Temp.Write(i, MaxY, Car[8], GetColorFenetre());
00333 }
00334 for(i = PosY; i< MaxY; i++)
00335 {
00336 Temp.Write(PosX, i, Car[9], GetColorFenetre());
00337 Temp.Write(MaxX, i, Car[9], GetColorFenetre());
00338 }
00339
00340 Temp.Write(PosX, PosY, Car[0], GetColorFenetre());
00341 Temp.Write(MaxX, PosY, Car[1], GetColorFenetre());
00342 Temp.Write(PosX, MaxY, Car[2], GetColorFenetre());
00343 Temp.Write(MaxX, MaxY, Car[3], GetColorFenetre());
00344
00345 Temp.Write(PosX+(MaxX-PosX-strlen(Titre))/2, PosY+2, Titre);
00346
00347 if(!(*Float))
00348 {
00349 *Chaine = ' ';
00350 NbCar = 0;
00351 Point = FALSE;
00352 }
00353 else
00354 {
00355 strcpy(Chaine, ftoa(*Float));
00356 NbCar = strlen(Chaine);
00357 }
00358 for(i = NbCar+1; i < 10; Chaine[i++] = ' ');
00359 Chaine[10] = '\0';
00360
00361 do
00362 {
00363 Chaine[NbCar] = '_';
00364 Temp.Write(PosX+2, PosY+4, Chaine);
00365 Temp.Print();
00366 Chaine[NbCar] = ' ';
00367
00368 Key = _getch();
00369 switch(Key)
00370 {
00371 case 27 :
00372 fflush(stdin);
00373 return FALSE;
00374 break;
00375
00376 case 13 :
00377 Chaine[NbCar] = '\0';
00378 fflush(stdin);
00379 *Float = (float)atof(Chaine);
00380 return TRUE;
00381 break;
00382 case 46 :
00383 if((NbCar > 0) && (NbCar < 10) && (!Point))
00384 {
00385 Chaine[NbCar] = '.';
00386 NbCar++;
00387 Point = TRUE;
00388 }
00389 break;
00390 case 8 :
00391 if(NbCar > 0)
00392 {
00393 NbCar--;
00394 if(Chaine[NbCar] = '.')
00395 Point = FALSE;
00396 for(i=NbCar; i < 9; i++)
00397 Chaine[i] = ' ';
00398 }
00399 break;
00400 default :
00401 if(isdigit(Key) && (NbCar < 9))
00402 {
00403 Chaine[NbCar] = (char)Key;
00404 NbCar++;
00405 }
00406 break;
00407 }
00408 }
00409 while(1);
00410
00411 fflush(stdin);
00412 return FALSE;
00413 }
00414
00415 BOOL MVirtuel::SaisieInt(char *Titre, int *Int, int PosX, int PosY, int MaxX, int MaxY)
00416 {
00417 int Key;
00418 char * Chaine = new char[10];
00419 int NbCar = 0;
00420 int i = 0, j =0;
00421 char CAR;
00422 MVirtuel Temp;
00423
00424 Temp.Copy(this);
00425
00426 for(i = PosX; i < MaxX; i++)
00427 for(j = PosY; j < MaxY; j++)
00428 Temp.Write(i, j, " ", GetColorFond());
00429
00430 for(i = PosX; i< MaxX; i++)
00431 {
00432 Temp.Write(i, PosY, Car[8], GetColorFenetre());
00433 Temp.Write(i, MaxY, Car[8], GetColorFenetre());
00434 }
00435 for(i = PosY; i< MaxY; i++)
00436 {
00437 Temp.Write(PosX, i, Car[9], GetColorFenetre());
00438 Temp.Write(MaxX, i, Car[9], GetColorFenetre());
00439 }
00440
00441 Temp.Write(PosX, PosY, Car[0], GetColorFenetre());
00442 Temp.Write(MaxX, PosY, Car[1], GetColorFenetre());
00443 Temp.Write(PosX, MaxY, Car[2], GetColorFenetre());
00444 Temp.Write(MaxX, MaxY, Car[3], GetColorFenetre());
00445
00446 Temp.Write(PosX+(MaxX-PosX-strlen(Titre))/2, PosY+2, Titre);
00447
00448 if(!(*Int))
00449 {
00450 *Chaine = ' ';
00451 NbCar = 0;
00452 }
00453 else
00454 {
00455 strcpy(Chaine, dtoa(*Int));
00456 NbCar = strlen(Chaine);
00457 }
00458 for(i = NbCar+1; i < 10; Chaine[i++] = ' ');
00459 Chaine[10] = '\0';
00460
00461 do
00462 {
00463 Chaine[NbCar] = '_';
00464 Temp.Write(PosX+2, PosY+4, Chaine);
00465 Temp.Print();
00466 Chaine[NbCar] = ' ';
00467
00468 Key = _getch();
00469 switch(Key)
00470 {
00471 case 27 :
00472 fflush(stdin);
00473 return FALSE;
00474 break;
00475
00476 case 13 :
00477 Chaine[NbCar] = '\0';
00478 fflush(stdin);
00479 *Int = (int)atoi(Chaine);
00480 return TRUE;
00481 break;
00482 case 8 :
00483 if(NbCar > 0)
00484 {
00485 NbCar--;
00486 for(i=NbCar; i < 9; i++)
00487 Chaine[i] = ' ';
00488 }
00489 break;
00490 default :
00491 if(isdigit(Key) && (NbCar < 9))
00492 {
00493 Chaine[NbCar] = (char)Key;
00494 NbCar++;
00495 }
00496 break;
00497 }
00498 }
00499 while(1);
00500
00501 fflush(stdin);
00502 return FALSE;
00503 }
00504
00505 void MVirtuel::Fenetre(char *Text, int SizeX, int SizeY, int Pause)
00506 {
00507 int NbCar = strlen(Text);
00508 int x, y;
00509 char CAR;
00510 MVirtuel Temp;
00511 int CoinX1 = (NBC-SizeX)/2, CoinY1 = (NBL-SizeY)/2;
00512 int CoinX2 = CoinX1+SizeX , CoinY2 = CoinY1+SizeY ;
00513
00514 Temp.Copy(this);
00515
00516 for(y = CoinY1; y < CoinY2; y++)
00517 for(x = CoinX1; x < CoinX2; x++)
00518 Temp.Write(x, y, " ", GetColorFond());
00519
00520 for(x = CoinX1; x < CoinX2; x++)
00521 {
00522 Temp.Write(x, CoinY1, Car[8], GetColorFenetre());
00523 Temp.Write(x, CoinY2, Car[8], GetColorFenetre());
00524 }
00525 for(y = CoinY1; y < CoinY2; y++)
00526 {
00527 Temp.Write(CoinX1, y, Car[9], GetColorFenetre());
00528 Temp.Write(CoinX2, y, Car[9], GetColorFenetre());
00529 }
00530
00531 Temp.Write(CoinX1, CoinY1, Car[0], GetColorFenetre());
00532 Temp.Write(CoinX2, CoinY1, Car[1], GetColorFenetre());
00533 Temp.Write(CoinX1, CoinY2, Car[2], GetColorFenetre());
00534 Temp.Write(CoinX2, CoinY2, Car[3], GetColorFenetre());
00535
00536 Temp.Write(CoinX1+2, CoinY1+2, Text);
00537
00538 Temp.Print();
00539
00540 if(!Pause)
00541 {
00542 PAUSE;
00543 }
00544 else
00545 {
00546 clock_t Start = clock();
00547 while(clock() <= (Start + Pause));
00548 }
00549 }
00550
00551 void MVirtuel::Cadre(int PosX, int PosY, int MaxX, int MaxY, char *Titre, char *Aide)
00552 {
00553 int Tx, Ty;
00554 char CAR;
00555
00556 for(Ty = PosY; Ty < MaxY; Ty++)
00557 for(Tx = PosX; Tx < MaxX; Tx++)
00558 Write(Tx, Ty, " ", GetColorFond());
00559
00560 for(Ty = PosY; Ty < MaxY-1; Ty++)
00561 {
00562 Write(PosX, Ty, Car[9], GetColorCadre()|GetColorFond());
00563 Write(MaxX-1, Ty, Car[9], GetColorCadre()|GetColorFond());
00564 }
00565 for(Tx = PosX; Tx < MaxX-1; Tx++)
00566 {
00567 Write(Tx, PosY, Car[8], GetColorCadre());
00568 Write(Tx, PosY+2, Car[8], GetColorCadre());
00569 Write(Tx, MaxY-1, Car[8], GetColorCadre());
00570 }
00571 Write(PosX, PosY, Car[0], GetColorCadre());
00572 Write(MaxX-1, PosY, Car[1], GetColorCadre());
00573 Write(PosX, MaxY-1, Car[2], GetColorCadre());
00574 Write(MaxX-1, MaxY-1, Car[3], GetColorCadre());
00575 Write(PosX, PosY+2, Car[4], GetColorCadre());
00576 Write(MaxX-1, PosY+2, Car[5], GetColorCadre());
00577
00578 Write(PosX+((MaxX-PosX-strlen(Titre))/2), PosY+1, Titre);
00579
00580 Write(PosX+2, MaxY-1, Aide);
00581 }
00582