Changeset 192


Ignore:
Timestamp:
May 7, 2020, 12:30:04 PM (4 years ago)
Author:
chronos
Message:
  • Modified: Code cleanup.
  • Modified: Simplified initialization of brain pictures.
  • Modified: Use GetAiDir function for AI subdirectory.
Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/GameServer.pas

    r185 r192  
    122122  Directories, CityProcessing, UnitProcessing, CmdList, LCLIntf, LCLType,
    123123  LMessages, Classes, SysUtils;
     124
     125resourcestring
     126  SNoAiFound = 'No AI libraries found in directory %s';
    124127
    125128var
     
    256259  BrainBeginner := nil;
    257260
    258   if FindFirst(HomeDir + 'AI' + DirectorySeparator + '*', faDirectory or faArchive or faReadOnly, f) = 0 then
     261  if FindFirst(GetAiDir + DirectorySeparator + '*', faDirectory or faArchive or faReadOnly, f) = 0 then
    259262  repeat
    260     BasePath := HomeDir + 'AI' + DirectorySeparator + f.Name;
     263    BasePath := GetAiDir + DirectorySeparator + f.Name;
    261264    if (f.Name <> '.') and (f.Name <> '..') and DirectoryExists(BasePath) then begin
    262265      NewBrain := Brains.AddNew;
     
    272275
    273276  if Brains.GetKindCount(btAI) = 0 then
    274     raise Exception.Create(Format('No AI libraries found in directory %s', [HomeDir + 'AI']));
     277    raise Exception.Create(Format(SNoAiFound, [GetAiDir]));
    275278end;
    276279
     
    45844587constructor TBrain.Create;
    45854588begin
    4586   Picture := nil;
     4589  Picture := TBitmap.Create;
     4590  Picture.SetSize(64, 64);
    45874591end;
    45884592
    45894593destructor TBrain.Destroy;
    45904594begin
    4591   if Assigned(Picture) then Picture.Free;
     4595  FreeAndNil(Picture);
    45924596  inherited Destroy;
    45934597end;
  • trunk/Packages/CevoComponents/Directories.pas

    r190 r192  
    1515function GetGraphicsDir: string;
    1616function GetSoundsDir: string;
     17function GetAiDir: string;
    1718
    1819
     
    126127end;
    127128
     129function GetAiDir: string;
     130begin
     131  Result := HomeDir + 'AI';
     132end;
     133
    128134end.
  • trunk/Packages/CevoComponents/ScreenTools.pas

    r190 r192  
    77  Windows,
    88  {$ENDIF}
    9   StringTables, LCLIntf, LCLType, SysUtils, Classes, Graphics, Controls,
     9  StringTables, LCLIntf, LCLType, SysUtils, Classes, Graphics, Controls, Math,
    1010  Forms, Menus, GraphType;
    1111
     
    3535procedure Sprite(dst: TBitmap; HGr, xDst, yDst, Width, Height, xGr, yGr: integer);
    3636  overload;
    37 procedure MakeBlue(dst: TBitmap; x, y, w, h: integer);
     37procedure MakeBlue(dst: TBitmap; x, y, Width, Height: Integer);
    3838procedure ImageOp_B(dst, Src: TBitmap; xDst, yDst, xSrc, ySrc, w, h: integer);
    3939procedure ImageOp_BCC(dst, Src: TBitmap;
     
    4141procedure ImageOp_CCC(bmp: TBitmap; x, y, w, h, Color0, Color1, Color2: integer);
    4242function BitBltCanvas(DestCanvas: TCanvas; X, Y, Width, Height: Integer;
    43   SrcCanvas: TCanvas; XSrc, YSrc: Integer; Rop: DWORD = SRCCOPY): Boolean;
     43  SrcCanvas: TCanvas; XSrc, YSrc: Integer; Rop: DWORD = SRCCOPY): Boolean; overload;
     44function BitBltCanvas(Dest: TCanvas; DestRect: TRect;
     45  Src: TCanvas; SrcPos: TPoint; Rop: DWORD = SRCCOPY): Boolean; overload;
     46function BitBltBitmap(Dest: TBitmap; X, Y, Width, Height: Integer;
     47  Src: TBitmap; XSrc, YSrc: Integer; Rop: DWORD = SRCCOPY): Boolean; overload;
     48function BitBltBitmap(Dest: TBitmap; DestRect: TRect;
     49  Src: TBitmap; SrcPos: TPoint; Rop: DWORD = SRCCOPY): Boolean; overload;
    4450procedure SLine(ca: TCanvas; x0, x1, y: integer; cl: TColor);
    4551procedure DLine(ca: TCanvas; x0, x1, y: integer; cl0, cl1: TColor);
     
    5258procedure InitOrnament;
    5359procedure InitCityMark(const T: TTexture);
    54 procedure Fill(ca: TCanvas; Left, Top, Width, Height, xOffset, yOffset: integer);
     60procedure Fill(ca: TCanvas; Left, Top, Width, Height, xOffset, yOffset: integer); overload;
     61procedure Fill(Canvas: TCanvas; Rect: TRect; Offset: TPoint); overload;
    5562procedure FillLarge(ca: TCanvas; x0, y0, x1, y1, xm: integer);
    5663procedure FillSeamless(ca: TCanvas; Left, Top, Width, Height, xOffset, yOffset: integer;
     
    8289procedure LoadPhrases;
    8390procedure Texturize(Dest, Texture: TBitmap; TransparentColor: Integer);
     91procedure DarkenImage(Bitmap: TBitmap; Change: Integer);
    8492
    8593const
     
    527535end;
    528536
    529 procedure MakeBlue(dst: TBitmap; x, y, w, h: integer);
     537procedure MakeBlue(dst: TBitmap; x, y, Width, Height: Integer);
    530538var
    531539  XX, YY: integer;
     
    534542  Dst.BeginUpdate;
    535543  PixelPtr.Init(Dst, X, Y);
    536   for yy := 0 to h - 1 do begin
    537     for xx := 0 to w - 1 do begin
     544  for yy := 0 to Height - 1 do begin
     545    for xx := 0 to Width - 1 do begin
    538546      PixelPtr.Pixel^.B := PixelPtr.Pixel^.B div 2;
    539547      PixelPtr.Pixel^.G := PixelPtr.Pixel^.G div 2;
     
    730738    Result := True;
    731739  end else Result := BitBlt(DestCanvas.Handle, X, Y, Width, Height, SrcCanvas.Handle, XSrc, YSrc, Rop);
     740end;
     741
     742function BitBltCanvas(Dest: TCanvas; DestRect: TRect; Src: TCanvas;
     743  SrcPos: TPoint; Rop: DWORD): Boolean;
     744begin
     745  Result := BitBltCanvas(Dest, DestRect.Left, DestRect.Top, DestRect.Width, DestRect.Height,
     746    Src, SrcPos.X, SrcPos.Y, Rop);
     747end;
     748
     749function BitBltBitmap(Dest: TBitmap; X, Y, Width, Height: Integer;
     750  Src: TBitmap; XSrc, YSrc: Integer; Rop: DWORD): Boolean;
     751begin
     752  Result := BitBltCanvas(Dest.Canvas, X, Y, Width, Height, Src.Canvas, XSrc, YSrc, Rop);
     753end;
     754
     755function BitBltBitmap(Dest: TBitmap; DestRect: TRect; Src: TBitmap;
     756  SrcPos: TPoint; Rop: DWORD): Boolean;
     757begin
     758  Result := BitBltCanvas(Dest.Canvas, DestRect, Src.Canvas, SrcPos, Rop);
    732759end;
    733760
     
    901928  BitBltCanvas(ca, Left, Top, Width, Height, MainTexture.Image.Canvas,
    902929    Left + xOffset, Top + yOffset);
     930end;
     931
     932procedure Fill(Canvas: TCanvas; Rect: TRect; Offset: TPoint);
     933begin
     934  Fill(Canvas, Rect.Left, Rect.Top, Rect.Width, Rect.Height, Offset.X, Offset.Y);
    903935end;
    904936
     
    14261458end;
    14271459
     1460procedure DarkenImage(Bitmap: TBitmap; Change: Integer);
     1461var
     1462  x, y: integer;
     1463  PicturePixel: TPixelPointer;
     1464begin
     1465  Bitmap.BeginUpdate;
     1466  PicturePixel.Init(Bitmap);
     1467  for y := 0 to Bitmap.Height - 1 do begin
     1468    for x := 0 to Bitmap.Width - 1 do begin
     1469      PicturePixel.Pixel^.B := Max(PicturePixel.Pixel^.B - Change, 0);
     1470      PicturePixel.Pixel^.G := Max(PicturePixel.Pixel^.G - Change, 0);
     1471      PicturePixel.Pixel^.R := Max(PicturePixel.Pixel^.R - Change, 0);
     1472      PicturePixel.NextPixel;
     1473    end;
     1474    PicturePixel.NextLine;
     1475  end;
     1476  Bitmap.EndUpdate;
     1477end;
     1478
    14281479procedure LoadFonts;
    14291480var
  • trunk/Start.pas

    r190 r192  
    154154    procedure LoadConfig;
    155155    procedure SaveConfig;
     156    procedure LoadAiBrainsPictures;
    156157  end;
    157158
     
    433434  r0, r1: HRgn;
    434435  Location: TPoint;
     436  PlayerSlot: TPlayerSlot;
    435437  AIBrains: TBrains;
    436   PlayerSlot: TPlayerSlot;
    437438begin
    438439  PlayerSlots := TPlayerSlots.Create;
     
    528529    CustomizeBtn.ButtonIndex := 2;
    529530
    530   Brains[0].Picture := TBitmap.Create;
    531   Brains[0].Picture.SetSize(64, 64);
    532   BitBltCanvas(Brains[0].Picture.Canvas, 0, 0, 64, 64,
    533     GrExt[HGrSystem2].Data.Canvas, 1, 111);
    534   Brains[1].Picture := TBitmap.Create;
    535   Brains[1].Picture.SetSize(64, 64);
    536   BitBltCanvas(Brains[1].Picture.Canvas, 0, 0, 64, 64,
    537     GrExt[HGrSystem2].Data.Canvas, 66, 111);
    538   Brains[2].Picture := TBitmap.Create;
    539   Brains[2].Picture.SetSize(64, 64);
    540   BitBltCanvas(Brains[2].Picture.Canvas, 0, 0, 64, 64,
    541     GrExt[HGrSystem2].Data.Canvas, 131, 111);
    542   Brains[3].Picture := TBitmap.Create;
    543   Brains[3].Picture.SetSize(64, 64);
    544   BitBltCanvas(Brains[3].Picture.Canvas, 0, 0, 64, 64,
    545     GrExt[HGrSystem2].Data.Canvas, 131, 46);
    546 
    547   AIBrains := TBrains.Create(False);
    548   Brains.GetByKind(btAI, AIBrains);
    549   for i := 0 to AIBrains.Count - 1 do
    550   with AIBrains[I] do
    551   begin
    552     AIBrains[i].Picture := TBitmap.Create;
    553     if not LoadGraphicFile(AIBrains[i].Picture, HomeDir + 'AI' + DirectorySeparator +
    554       FileName + DirectorySeparator + FileName + '.png', gfNoError) then begin
    555       AIBrains[i].Picture.SetSize(64, 64);
    556       with AIBrains[i].Picture.Canvas do begin
    557         Brush.Color := $904830;
    558         FillRect(Rect(0, 0, 64, 64));
    559         Font.Assign(UniFont[ftTiny]);
    560         Font.Style := [];
    561         Font.Color := $5FDBFF;
    562         Textout(32 - TextWidth(FileName) div 2,
    563           32 - TextHeight(FileName) div 2, FileName);
    564       end;
    565     end;
    566   end;
    567   AIBrains.Free;
     531  BitBltBitmap(BrainNoTerm.Picture, 0, 0, 64, 64, GrExt[HGrSystem2].Data, 1, 111);
     532  BitBltBitmap(BrainSuperVirtual.Picture, 0, 0, 64, 64, GrExt[HGrSystem2].Data, 66, 111);
     533  BitBltBitmap(BrainTerm.Picture, 0, 0, 64, 64, GrExt[HGrSystem2].Data, 131, 111);
     534  BitBltBitmap(BrainRandom.Picture, 0, 0, 64, 64, GrExt[HGrSystem2].Data, 131, 46);
     535  LoadAiBrainsPictures;
    568536
    569537  EmptyPicture := TBitmap.Create;
     
    712680end;
    713681
     682procedure TStartDlg.LoadAiBrainsPictures;
     683var
     684  AIBrains: TBrains;
     685  I: Integer;
     686begin
     687  AIBrains := TBrains.Create(False);
     688  Brains.GetByKind(btAI, AIBrains);
     689  for i := 0 to AIBrains.Count - 1 do
     690  with AIBrains[I] do begin
     691    if not LoadGraphicFile(AIBrains[i].Picture, GetAiDir + DirectorySeparator +
     692      FileName + DirectorySeparator + FileName + '.png', gfNoError) then begin
     693      with AIBrains[i].Picture.Canvas do begin
     694        Brush.Color := $904830;
     695        FillRect(Rect(0, 0, 64, 64));
     696        Font.Assign(UniFont[ftTiny]);
     697        Font.Style := [];
     698        Font.Color := $5FDBFF;
     699        Textout(32 - TextWidth(FileName) div 2,
     700          32 - TextHeight(FileName) div 2, FileName);
     701      end;
     702    end;
     703  end;
     704  AIBrains.Free;
     705end;
     706
    714707procedure TStartDlg.DrawAction(y, IconIndex: integer; HeaderItem, TextItem: string);
    715708begin
     
    828821  if Page = pgMain then
    829822  begin
    830     if SelectedAction >= maNone then // mark selected action
     823    if SelectedAction <> maNone then // mark selected action
    831824      for i := 0 to (ClientWidth - 2 * ActionSideBorder) div wBuffer + 1 do
    832825      begin
     
    852845      if MainAction in ActionsOffered then
    853846        case MainAction of
    854           maConfig:
    855             DrawAction(y, 25, 'ACTIONHEADER_CONFIG', 'ACTION_CONFIG');
    856           maManual:
    857             DrawAction(y, 19, 'ACTIONHEADER_MANUAL', 'ACTION_MANUAL');
    858           maCredits:
    859             DrawAction(y, 22, 'ACTIONHEADER_CREDITS', 'ACTION_CREDITS');
    860           maAIDev:
    861             DrawAction(y, 24, 'ACTIONHEADER_AIDEV', 'ACTION_AIDEV');
     847          maConfig: DrawAction(y, 25, 'ACTIONHEADER_CONFIG', 'ACTION_CONFIG');
     848          maManual: DrawAction(y, 19, 'ACTIONHEADER_MANUAL', 'ACTION_MANUAL');
     849          maCredits: DrawAction(y, 22, 'ACTIONHEADER_CREDITS', 'ACTION_CREDITS');
     850          maAIDev: DrawAction(y, 24, 'ACTIONHEADER_AIDEV', 'ACTION_AIDEV');
    862851          maWeb:
    863852            begin
     
    10951084
    10961085procedure TStartDlg.FormShow(Sender: TObject);
    1097 var
    1098   x, y: integer;
    1099   PicturePixel: TPixelPointer;
    11001086begin
    11011087  SetMainTextureByAge(-1);
    11021088  List.Font.Color := MainTexture.clMark;
    11031089
    1104   Fill(EmptyPicture.Canvas, 0, 0, 64, 64, (wMaintexture - 64) div 2,
    1105     (hMaintexture - 64) div 2);
    1106   // darken texture for empty slot
    1107   EmptyPicture.BeginUpdate;
    1108   PicturePixel.Init(EmptyPicture);
    1109   for y := 0 to 63 do begin
    1110     for x := 0 to 64 - 1 do begin
    1111       PicturePixel.Pixel^.B := Max(PicturePixel.Pixel^.B - 28, 0);
    1112       PicturePixel.Pixel^.G := Max(PicturePixel.Pixel^.G - 28, 0);
    1113       PicturePixel.Pixel^.R := Max(PicturePixel.Pixel^.R - 28, 0);
    1114       PicturePixel.NextPixel;
    1115     end;
    1116     PicturePixel.NextLine;
    1117   end;
    1118   EmptyPicture.EndUpdate;
     1090  Fill(EmptyPicture.Canvas, Bounds(0, 0, 64, 64),
     1091    Point((wMaintexture - 64) div 2, (hMaintexture - 64) div 2));
     1092
     1093  DarkenImage(EmptyPicture, 28);
    11191094
    11201095  Difficulty[0] := Diff0;
Note: See TracChangeset for help on using the changeset viewer.