-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unit1.cpp
692 lines (617 loc) · 23.2 KB
/
Unit1.cpp
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "About.h"
#include "ConnectionBox.h"
#include <System.IniFiles.hpp>
#include <Vcl.Imaging.pngimage.hpp>
#include <memory>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
, FPlayer1Choice(TPlayerMove::None) // On a rien choisi encore
, FPlayer2Choice(TPlayerMove::None) // L'adversaire n'a rien choisi encore
, FTieCount(0)
, FWinCount(0)
, FLostCount(0)
{
Caption = "RPS Online";
String Temp_IPServer;
// Read settings from .ini file
TIniFile *LIniFile = nullptr;
try
{
LIniFile = new TIniFile(ChangeFileExt(Application->ExeName, ".ini"));
Temp_IPServer = LIniFile->ReadString ( "Setting", "IP", "192.168.0.2" );
FMusicEnabled = LIniFile->ReadBool ( "Setting", "Music", true );
FSoundEnabled = LIniFile->ReadBool ( "Setting", "Sound", true);
PortCom = LIniFile->ReadInteger( "Setting", "Port", 1024 );
}
__finally
{
delete LIniFile;
}
// Prend les paramètres que ICQ envoie (Détection d'arguments)
// Arguments: ip_address user_name
IPServer = ParamStr(1);
NickServer = ParamStr(2);
// Sert a faire un vrai Random
randomize();
// Met les numéros de Port par défaut
ClientSocket->Port = PortCom;
ServerSocket->Port = PortCom;
// Audio
PlayerMidi->FileName = ExtractFilePath(Application->ExeName) + "\\rps.mid"; // Spécifie le fichier son
Music1->Checked = !FMusicEnabled; // Par défaut on met de la musique (true=musique)
Music1Click(nullptr);
Sound1->Checked = !FSoundEnabled; // Par défaut on met du son (true=son)
Sound1Click(nullptr);
// On load les images du fichier RES
LoadPng(Logo->Picture->Bitmap, "PNG_TITLE");
FBitmapRockRight = new Graphics::TBitmap();
LoadPng(FBitmapRockRight, "PNG_ROCK");
FBitmapPaperRight = new Graphics::TBitmap();
LoadPng(FBitmapPaperRight, "PNG_PAPER");
FBitmapScissorsRight = new Graphics::TBitmap();
LoadPng(FBitmapScissorsRight, "PNG_SCISSORS");
FBitmapRockLeft = new Graphics::TBitmap();
FBitmapRockLeft->Assign(FBitmapRockRight);
FlipImageH(FBitmapRockLeft);
FBitmapPaperLeft = new Graphics::TBitmap();
FBitmapPaperLeft->Assign(FBitmapPaperRight);
FlipImageH(FBitmapPaperLeft);
FBitmapScissorsLeft = new Graphics::TBitmap();
FBitmapScissorsLeft->Assign(FBitmapScissorsRight);
FlipImageH(FBitmapScissorsLeft);
// On réutilise les mêmes images
Roche1->Glyph = FBitmapRockLeft;
Papier1->Glyph = FBitmapPaperLeft;
Ciseaux1->Glyph = FBitmapScissorsLeft;
// Met les valeurs par défaut
if (IPServer.IsEmpty() == true)
{ // S'il n'y a rien d'écrit
IPServer = Temp_IPServer;
Listen(true);
}
else
{
ConnectServer(IPServer);
}
if (NickServer.IsEmpty() == true)
{ // S'il n'y a rien d'écrit
NickServer = "CPU Opponent"; // Versus Distant
}
}
//---------------------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
try
{
// Write settings to .ini file
TIniFile *LIniFile = nullptr;
try
{
LIniFile = new TIniFile(ChangeFileExt(Application->ExeName, ".ini"));
LIniFile->WriteString ( "Setting", "IP", IPServer );
LIniFile->WriteBool ( "Setting", "Music", FMusicEnabled ); // 0 = Off
LIniFile->WriteBool ( "Setting", "Sound", FSoundEnabled ); // 1 = On
}
__finally
{
delete LIniFile;
}
}
catch(...)
{
// No exception allowed in destructor
}
// On récupère la mémoire
delete FBitmapRockLeft;
delete FBitmapPaperLeft;
delete FBitmapScissorsLeft;
delete FBitmapRockRight;
delete FBitmapPaperRight;
delete FBitmapScissorsRight;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ConnectServer(const String IP)
{
if (ClientSocket->Active == true)
{ // Si Client enable
ClientSocket->Active = false; // On le disable
}
ClientSocket->Host = IP; // On écrit le IP
ClientSocket->Active = true; // On enable le Client
Listen(false); // On arrête d'écouter
StatusBar1->Panels->Items[0]->Text = "Connecting to " + IP;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Listen(bool AListen)
{
if (AListen == true) // Si cocher (menu)
{
ClientSocket->Active = false; // Disable le Client
ServerSocket->Active = true; // Enable le Serveur
StatusBar1->Panels->Items[0]->Text = "Listening..."; // Écrit bar d'état
Disconnectec1->Enabled = false;
// Si on avait fait un choix avant d'être déconnecter, on enable tout
Roche1->Enabled = true;
Papier1->Enabled = true;
Ciseaux1->Enabled = true;
}
else
{
if (ServerSocket->Active == true) // Si Socket serveur enable
{
ServerSocket->Active = false; // Disable le Serveur
}
StatusBar1->Panels->Items[0]->Text = ""; // Écrit dans la bar d'état
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Compare()
{
TCanvas *LCanvas = GLancer->Canvas;
SetBkMode(LCanvas->Handle, TRANSPARENT);
LCanvas->Font->Name = "Arial";
LCanvas->Font->Size = 30;
LCanvas->Font->Style = TFontStyles() << TFontStyle::fsBold;
SetTextAlign (LCanvas->Handle, TA_CENTER);
int x = GLancer->Width; // Emplacement des images
int y = GLancer->Height - 10;
if (FPlayer1Choice != TPlayerMove::None && FPlayer2Choice != TPlayerMove::None)
{
// Affichage des mains Joueurs DEUX et UN
switch ( FPlayer2Choice )
{
case TPlayerMove::Rock:
LCanvas->Draw(x - FBitmapRockRight->Width , y - FBitmapRockRight->Height + 7, FBitmapRockRight);
break;
case TPlayerMove::Paper:
LCanvas->Draw(x - FBitmapPaperRight->Width , y - FBitmapRockRight->Height + 16, FBitmapPaperRight);
break;
case TPlayerMove::Scissors:
LCanvas->Draw(x - FBitmapScissorsRight->Width , y - FBitmapRockRight->Height + 8, FBitmapScissorsRight);
break;
default:
break;
}
switch ( FPlayer1Choice )
{
case TPlayerMove::Rock:
LCanvas->Draw(0, y - FBitmapRockLeft->Height + 7, FBitmapRockLeft);
break;
case TPlayerMove::Paper:
LCanvas->Draw(0, y - FBitmapRockLeft->Height + 16, FBitmapPaperLeft);
break;
case TPlayerMove::Scissors:
LCanvas->Draw(0, y - FBitmapRockLeft->Height + 8, FBitmapScissorsLeft);
break;
default:
break;
}
x = GLancer->Width / 2; // Emplacement du texte
y = 0;
String LText;
// Détermine si c'est une victoire, défaite ou nulle
if ((FPlayer1Choice == TPlayerMove::Rock && FPlayer2Choice == TPlayerMove::Scissors) ||
(FPlayer1Choice == TPlayerMove::Paper && FPlayer2Choice == TPlayerMove::Rock) ||
(FPlayer1Choice == TPlayerMove::Scissors && FPlayer2Choice == TPlayerMove::Paper))
{
FWinCount++;
Win->Caption = FWinCount;
LText = "WIN";
}
if ((FPlayer2Choice == TPlayerMove::Rock && FPlayer1Choice == TPlayerMove::Scissors) ||
(FPlayer2Choice == TPlayerMove::Paper && FPlayer1Choice == TPlayerMove::Rock) ||
(FPlayer2Choice == TPlayerMove::Scissors && FPlayer1Choice == TPlayerMove::Paper))
{
FLostCount++;
Lost->Caption = FLostCount;
LText = "LOST";
}
if ((FPlayer2Choice == TPlayerMove::Rock && FPlayer1Choice == TPlayerMove::Rock) ||
(FPlayer2Choice == TPlayerMove::Paper && FPlayer1Choice == TPlayerMove::Paper) ||
(FPlayer2Choice == TPlayerMove::Scissors && FPlayer1Choice == TPlayerMove::Scissors))
{
FTieCount++;
Tie->Caption = FTieCount;
LText = "TIE";
}
// Output text
LCanvas->Font->Color = static_cast<TColor>(RGB(255, 254, 255));
LCanvas->TextOut(x + 1, y + 1, LText);
LCanvas->Font->Color = clBlack;
LCanvas->TextOut(x, y, LText);
// On réactive pour une nouvelle joute
FPlayer1Choice = TPlayerMove::None;
FPlayer2Choice = TPlayerMove::None;
// On enable les boutons pour pouvoir jouer à nouveau
Roche1->Enabled = true;
Papier1->Enabled = true;
Ciseaux1->Enabled = true;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Reception(TCustomWinSocket *Socket)
{
TBytes LBytes;
// ReceiveLength is not guaranteed to be accurate, so we add 100 more bytes
LBytes.Length = Socket->ReceiveLength() + 100;
int LRecCount = Socket->ReceiveBuf(&LBytes[0], LBytes.Length);
if(LRecCount <= 0)
{ // Nothing received
return;
}
const String LText = TEncoding::UTF8->GetString(LBytes, 0, LRecCount);
if (LText == "!ChoixEvoyer$1" || LText == "!ChoixEvoyer$2" || LText == "!ChoixEvoyer$3")
{
if (LText == "!ChoixEvoyer$1")
{
FPlayer2Choice = TPlayerMove::Rock;
}
else if (LText == "!ChoixEvoyer$2")
{
FPlayer2Choice = TPlayerMove::Paper;
}
else if (LText == "!ChoixEvoyer$3")
{
FPlayer2Choice = TPlayerMove::Scissors;
}
else
{
throw Exception("Invalid choice!");
}
Compare();
StatusBar1->Panels->Items[0]->Text = "Waiting for you";
}
else if (LText == "éèNewGameéè")
{
// NewGame1Click(nullptr);
Win->Caption = "0"; // On met l'affichage à zéro
Lost->Caption = "0";
Tie->Caption = "0";
FWinCount = 0; // On met les scores à zéro
FLostCount = 0;
FTieCount = 0;
// On enlève les mains pour mettre le logo
GLancer->Canvas->FillRect(Rect(0, 0, GLancer->Width, GLancer->Height));
Logo->Visible = true; // Remet le titre (logo)
}
else
{
Memo2->Lines->Add(NickServer + ": " + LText); // Met le texte dans Memo2
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Play(TPlayerMove AChoice)
{
// On enlève toutes les mains
GLancer->Canvas->FillRect(Rect(0, 0, GLancer->Width, GLancer->Height));
Logo->Visible = false; // Enlève le titre
FPlayer1Choice = AChoice; // Choix du joueur UN
TBytes LBytes = TEncoding::UTF8->GetBytes(String("!ChoixEvoyer$") + static_cast<int>(FPlayer1Choice));
if (IsServer == true)
{ // On envoie des instructions au Serveur
ServerSocket->Socket->Connections[0]->SendBuf(&LBytes[0], LBytes.Length);
}
else
{ // On envoie des instructions au client
ClientSocket->Socket->SendBuf(&LBytes[0], LBytes.Length);
}
if (ClientSocket->Active == true || IsServer == true) // Joue contre un vrai joueur
{
StatusBar1->Panels->Items[0]->Text = "Waiting for opponent";
// On disable les boutons pour que le joueur attende l'autre joueur
Roche1->Enabled = false;
Papier1->Enabled = false;
Ciseaux1->Enabled = false;
Compare();
}
else
{ // On joue contre le CPU
FPlayer2Choice = TPlayerMove(random(3) + 1); // Choix du computer, super AI
Compare();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Quitter1Click(TObject *Sender)
{
Close(); // Ferme le programme
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Disconnectec1Click(TObject *Sender)
{
Listen(true); // On se met en mode d'écoute
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SendClick(TObject *Sender)
{
if (Memo1->Text.IsEmpty() == true)
{
return;
}
if ((ClientSocket->Active == true || IsServer == true))
{
TBytes LBytes = TEncoding::UTF8->GetBytes(Memo1->Text);
if (IsServer == true)
{ // On envoie la dernière ligne de Memo1 par le Serveur
ServerSocket->Socket->Connections[0]->SendBuf(&LBytes[0], LBytes.Length);
}
else
{ // On envoie la dernière ligne de Memo1 par le client
ClientSocket->Socket->SendBuf(&LBytes[0], LBytes.Length);
}
Memo2->Lines->Add("You: " + Memo1->Text); // Met le texte dans Memo2
Memo1->Clear(); // Efface la zone de message
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Connecttoopponent1Click(TObject *Sender)
{
auto LConnectionBox = std::make_unique<TForm2>(nullptr, &IPServer);
const int LReturn = LConnectionBox->ShowModal();
if(LReturn == mrOk && IPServer.IsEmpty() == false)
{
ConnectServer(IPServer);
NewGame1Click(nullptr); // On fait une nouvelle game
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocketError(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
// Écrit un message d'erreur
ShowMessage(String("Error connecting to: ") + IPServer + ".");
ErrorCode = 0;
Listen(true); // On est en écoute
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocketRead(TObject *Sender,
TCustomWinSocket *Socket)
{
Reception(Socket); // Réception d'un message
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocketAccept(TObject *Sender,
TCustomWinSocket *Socket)
{
// Se produit dans un socket serveur immédiatement après l'acceptation de la
// connexion avec un socket client.
IsServer = true; // On met IsServer à VRAI, on est client et connecté
StatusBar1->Panels->Items[0]->Text = "Connect to: " + Socket->RemoteAddress;
Disconnectec1->Enabled = true;
NickServer = "Opponent"; // Versus Distant
// C'est ici que l'on envoie les setting du serveur
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocketClientConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
Memo2->Lines->Clear(); // Efface les lignes de Memo2 (Réception)
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocketClientDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
Listen(true); // On est en écoute
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocketClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
Reception(Socket); // Réception d'un message
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocketConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1->Panels->Items[0]->Text = "Connect to: " + Socket->RemoteHost;
// Écrit dans la bar d'état l'adresse à la quelle il tente de se connecter
Disconnectec1->Enabled = true;
NickServer = "Opponent"; // Versus Distant
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClientSocketDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
Listen(true); // On est en écoute
}
//---------------------------------------------------------------------------
void __fastcall TForm1::About1Click(TObject *Sender)
{
AboutBox->ShowModal(); // Affiche le About
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NewGame1Click(TObject *Sender)
{
if ((ClientSocket->Active == true || IsServer == true)) // Joue online
{
TBytes LBytes = TEncoding::UTF8->GetBytes("éèNewGameéè");
if (IsServer == true)
{ // On envoie des instructions au Serveur
ServerSocket->Socket->Connections[0]->SendBuf(&LBytes[0], LBytes.Length);
}
else
{ // On envoie des instructions au client
ClientSocket->Socket->SendBuf(&LBytes[0], LBytes.Length);
}
}
// Reset les comptes de moves
FPlayerStats.Reset();
// On met l'affichage à zéro
Win->Caption = "0";
Lost->Caption = "0";
Tie->Caption = "0";
// On met les scores à zéro
FWinCount = 0;
FLostCount = 0;
FTieCount = 0;
// On enlève les mains pour mettre le logo
GLancer->Canvas->FillRect(Rect(0, 0, GLancer->Width, GLancer->Height));
Logo->Visible = true; // Remet le titre (logo)
// Si on avait fait un choix avant d'être déconnecter, on enable tout
Roche1->Enabled = true;
Papier1->Enabled = true;
Ciseaux1->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::HelpTopic1Click(TObject *Sender)
{
ShellExecuteW(Handle,
L"open",
L"https://crayon2000.github.io/RPS-Online",
nullptr, nullptr, SW_SHOWDEFAULT);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Roche1Click(TObject *Sender)
{
if (FSoundEnabled == true)
{
PlaySoundW(L"rock.wav", nullptr, SND_FILENAME | SND_ASYNC | SND_NODEFAULT);
}
FPlayerStats.Rock++; // Compte le nombre de Roche
Play(TPlayerMove::Rock); // On choisi Roche
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Papier1Click(TObject *Sender)
{
if (FSoundEnabled == true)
{
PlaySoundW(L"paper.wav", nullptr, SND_FILENAME | SND_ASYNC | SND_NODEFAULT);
}
FPlayerStats.Paper++; // Compte le nombre de Papier
Play(TPlayerMove::Paper); // On choisi Papier
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Ciseaux1Click(TObject *Sender)
{
if (FSoundEnabled == true)
{
PlaySoundW(L"scissors.wav", nullptr, SND_FILENAME | SND_ASYNC | SND_NODEFAULT);
}
FPlayerStats.Scissors++; // Compte le nombre de Ciseaux
Play(TPlayerMove::Scissors); // On choisi Ciseaux
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Sound1Click(TObject *Sender)
{
Sound1->Checked = !Sound1->Checked;
FSoundEnabled = Sound1->Checked;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PlayerMidiNotify(TObject *Sender)
{
// C'est ici que l'on fait looper la musique
// Play déclanche un PlayerMidi->Notify = true;
if (FMusicEnabled == true)
{
PlayerMidi->Play();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Music1Click(TObject *Sender)
{
Music1->Checked = !Music1->Checked;
if (Music1->Checked == true)
{
// Start music
FMusicEnabled = true;
try
{
PlayerMidi->Open();
PlayerMidi->Play();
}
catch ( ... )
{
}
}
else
{
// Stop music
FMusicEnabled = false;
PlayerMidi->Stop();
PlayerMidi->Close();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Statistics1Click(TObject *Sender)
{
const float Total = FTieCount + FWinCount + FLostCount;
ShowMessage(String("Winning ratio: ") + FWinCount/Total*100 + "%\nNumber of Round: " + Total + "\nNumber of Rock: " + FPlayerStats.Rock + "\nNumber of Paper: " + FPlayerStats.Paper + "\nNumber of Cissors: " + FPlayerStats.Scissors);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LoadPng(Graphics::TBitmap *ABitmapImage, const String AIdentifier)
{
if(ABitmapImage == nullptr)
{
throw EArgumentException(Sysutils::Format(System_Rtlconsts_SParamIsNil, ARRAYOFCONST(("ABitmapImage"))));
}
auto PngImage = std::make_unique<Pngimage::TPngImage>();
PngImage->LoadFromResourceName((NativeUInt)HInstance, AIdentifier);
ABitmapImage->Assign(PngImage.get());
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FlipImageH(Graphics::TBitmap *AImage)
{
if(AImage == nullptr)
{
throw EArgumentException(Sysutils::Format(System_Rtlconsts_SParamIsNil, ARRAYOFCONST(("AImage"))));
}
const TRect LSourceRect = Rect(0, 0, AImage->Width, AImage->Height);
AImage->Canvas->CopyRect(Rect(AImage->Width - 1, 0, -1, AImage->Height),
AImage->Canvas, LSourceRect);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FlipImageV(Graphics::TBitmap *AImage)
{
if(AImage == nullptr)
{
throw EArgumentException(Sysutils::Format(System_Rtlconsts_SParamIsNil, ARRAYOFCONST(("AImage"))));
}
const TRect LSourceRect = Rect(0, 0, AImage->Width, AImage->Height);
AImage->Canvas->CopyRect(Rect(0, AImage->Height - 1, AImage->Width, -1),
AImage->Canvas, LSourceRect);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ReplaceColor(Graphics::TBitmap* ABitmap, TColor AOldColor, TColor ANewColor)
{
if(ABitmap == nullptr)
{
throw EArgumentException(Sysutils::Format(System_Rtlconsts_SParamIsNil, ARRAYOFCONST(("ABitmap"))));
}
if(ABitmap->PixelFormat != TPixelFormat::pf32bit)
{
return;
}
const int LOldColor = ColorToRGB(AOldColor);
const BYTE LOldColorRed = GetRValue(LOldColor);
const BYTE LOldColorGreen = GetGValue(LOldColor);
const BYTE LOldColorBlue = GetBValue(LOldColor);
const int LNewColor = ColorToRGB(ANewColor);
const BYTE LNewColorRed = GetRValue(LNewColor);
const BYTE LNewColorGreen = GetGValue(LNewColor);
const BYTE LNewColorBlue = GetBValue(LNewColor);
for(int y = 0; y < ABitmap->Height; ++y)
{
TRGBQuad* Pixel = static_cast<TRGBQuad*>(ABitmap->ScanLine[y]);
for(int x = 0; x < ABitmap->Width; ++x)
{
if(Pixel->rgbRed == LOldColorRed &&
Pixel->rgbGreen == LOldColorGreen &&
Pixel->rgbBlue == LOldColorBlue)
{
Pixel->rgbRed = LNewColorRed;
Pixel->rgbGreen = LNewColorGreen;
Pixel->rgbBlue = LNewColorBlue;
}
Pixel++;
}
}
}
//---------------------------------------------------------------------------