Bonjour Roland.
je n'ai pas vu comment passer directement le nom du fichier au constructeur de l'objet TPiece
Il faut modifier les paramètres du constructeur : constructor Create(AOwner: TComponent; AFileName: string); reintroduce;
Mais est-ce la bonne solution ?
Je vois que tu préfères charger tes images à partir de fichiers (au lieu de les compiler dans un fichier ressources).
Avantage : possibilité de modifier les dessins sans devoir recompiler le programme. Donc un utilisateur peut très bien modifier lui-même sans problème.
Inconvénient (?) : obligation de fournir les fichiers png. Ce n'en est pas un, beaucoup de programmes ne se contentent plus d'un seul fichier (l'exécutable).
Lorsque j'ai une série d'images à mettre en mémoire, perso, j'utilise une liste, en l'occurence un TObjectList, dans laquelle chaque objet contient un bitmap.
Un composant (une pièce dans ton cas) n'a donc plus besoin d'avoir son propre bitmap. Il suffit de lui renseigner la variable liste et l'index de l'image dans cette liste.
Dans le code ci-dessous, le Roi blanc a l'index image 0, la dame l'index 1, les deux tours l'index 2... les huit pions ont l'index 5...
En cas de promotion d'un pion en dame (p.ex), il suffit de modifier l'index de l'image dans le composant. Je parle uniquement de l'image, pcq il faut évidemment modifier le type de la pièce (mais là je ne sais pas comment tu souhaites le faire).
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
| unit Unit2;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Controls, Graphics, Forms, contnrs, Fileutil, BGRABitmap;
type
TBGRABitmapItem = class(TObject)
private
FBitmap: TBGRABitmap;
public
constructor Create(ABitmapName: string);
destructor Destroy; override;
property Bitmap: TBGRABitmap read FBitmap write FBitmap;
end;
TBGRABitmapCollection = class(TObjectList)
private
function GetItem(Index: Integer): TBGRABitmapItem;
public
property Item[AIndex: integer]: TBGRABitmapItem read GetItem; default;
function NewBGRABitmap(AFileName: string): integer;
end;
{variables globales}
var
BGRACollection : TBGRABitmapCollection;
procedure LoadImages;
implementation
constructor TBGRABitmapItem.Create(ABitmapName: string);
begin
inherited Create;
FBitmap:= TBGRABitmap.Create(UTF8ToSys(ABitmapName));
end;
destructor TBGRABitmapItem.Destroy;
begin
FreeAndNil(FBitmap);
inherited Destroy;
end;
function TBGRABitmapCollection.GetItem(Index: Integer): TBGRABitmapItem;
begin
Result := TBGRABitmapItem(inherited GetItem(Index));
end;
function TBGRABitmapCollection.NewBGRABitmap(AFileName: string): integer;
begin
Result:= inherited Add(TBGRABitmapItem.Create(AFileName));
end;
procedure LoadImages;
var
Path: string;
begin
Path:= IncludeTrailingPathDelimiter(Application.Location + 'Images');
BGRACollection.NewBGRABitmap(Path + 'BRoi.png'); // index = 0
BGRACollection.NewBGRABitmap(Path + 'BDame.png'); // index = 1
BGRACollection.NewBGRABitmap(Path + 'BTour.png'); // index = 2
BGRACollection.NewBGRABitmap(Path + 'BFou.png'); // index = 3
BGRACollection.NewBGRABitmap(Path + 'BCavalier.png'); // ...
BGRACollection.NewBGRABitmap(Path + 'BPion.png');
BGRACollection.NewBGRABitmap(Path + 'NRoi.png');
BGRACollection.NewBGRABitmap(Path + 'NDame.png');
BGRACollection.NewBGRABitmap(Path + 'NTour.png');
BGRACollection.NewBGRABitmap(Path + 'NFou.png');
BGRACollection.NewBGRABitmap(Path + 'NCavalier.png');
BGRACollection.NewBGRABitmap(Path + 'NPion.png');
end;
initialization
BGRACollection := TBGRABitmapCollection.Create;
LoadImages;
finalization
BGRACollection.Free;
end. |
Dans l'événement Paint du composant pièce, le code devient (FImageId = l'indice de l'image):
BGRACollection[FImageId].Bitmap.Draw(Canvas, 0, 0, false);
Cordialement
Thierry
Partager