Ignore:
Timestamp:
Mar 9, 2021, 9:19:49 AM (3 years ago)
Author:
chronos
Message:
  • Modified: Synced code with current trunk version.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/highdpi/Packages/CevoComponents/ScreenTools.pas

    r266 r303  
    1313  TTexture = record
    1414    Image: TDpiBitmap;
    15     clBevelLight, clBevelShade, clTextLight, clTextShade, clLitText, clMark,
    16     clPage, clCover: TColor;
    17   end;
     15    clBevelLight: TColor;
     16    clBevelShade: TColor;
     17    clTextLight: TColor;
     18    clTextShade: TColor;
     19    clLitText: TColor;
     20    clMark: TColor;
     21    clPage: TColor;
     22    clCover: TColor;
     23  end;
     24  TLoadGraphicFileOption = (gfNoError, gfNoGamma);
     25  TLoadGraphicFileOptions = set of TLoadGraphicFileOption;
     26
    1827
    1928{$IFDEF WINDOWS}
     
    2837procedure EditFrame(ca: TDpiCanvas; p: TRect; const T: TTexture);
    2938function HexStringToColor(S: string): integer;
    30 function LoadGraphicFile(bmp: TDpiBitmap; Path: string; Options: integer = 0): boolean;
     39function LoadGraphicFile(Bmp: TDpiBitmap; FileName: string; Options: TLoadGraphicFileOptions = []): boolean;
    3140function LoadGraphicSet(const Name: string): integer;
    3241procedure Dump(dst: TDpiBitmap; HGr, xDst, yDst, Width, Height, xGr, yGr: integer);
     
    91100function SetMainTextureByAge(Age: integer): boolean;
    92101procedure LoadPhrases;
    93 procedure Texturize(Dest, Texture: TDpiBitmap; TransparentColor: Integer);
     102procedure Texturize(Dest, Texture: TDpiBitmap; TransparentColor: Cardinal);
    94103procedure DarkenImage(Bitmap: TDpiBitmap; Change: Integer);
    95104
     
    158167  cliWater = 4;
    159168
    160   // LoadGraphicFile options
    161   gfNoError = $01;
    162   gfNoGamma = $02;
    163 
    164169type
    165170  TGrExtDescr = record { don't use dynamic strings here! }
     
    256261    MenuItem := MenuItems[MenuItems.Count - 1];
    257262    MenuItems.Delete(MenuItems.Count - 1);
    258     MenuItem.Free;
     263    FreeAndNil(MenuItem);
    259264  end;
    260265end;
    261266
    262267function TurnToYear(Turn: Integer): Integer;
    263 var
    264   I: Integer;
    265268begin
    266269  Result := -4000;
    267   for I := 1 to Turn do
    268     if Result < -1000 then Inc(Result, 50) // 0..60
    269     else if Result < 0 then Inc(Result, 25) // 60..100
    270     else if Result < 1500 then Inc(Result, 20) // 100..175
    271     else if Result < 1750 then Inc(Result, 10) // 175..200
    272     else if Result < 1850 then Inc(Result, 2) // 200..250
    273     else Inc(Result);
     270  if Turn <= 0 then Exit;
     271
     272  // Year -4000..-1000, Turn 0..60
     273  Inc(Result, Min(60, Turn) * 50);
     274  Dec(Turn, Min(60, Turn));
     275  if Turn = 0 then Exit;
     276
     277  // Year -1000..0, Turn 60..100
     278  Inc(Result, Min(40, Turn) * 25);
     279  Dec(Turn, Min(40, Turn));
     280  if Turn = 0 then Exit;
     281
     282  // Year 0..1500, Turn 100..175
     283  Inc(Result, Min(75, Turn) * 20);
     284  Dec(Turn, Min(75, Turn));
     285  if Turn = 0 then Exit;
     286
     287  // Year 1500..1750, Turn 175..200
     288  Inc(Result, Min(25, Turn) * 10);
     289  Dec(Turn, Min(25, Turn));
     290  if Turn = 0 then Exit;
     291
     292  // Year 1750..1850, Turn 200..250
     293  Inc(Result, Min(50, Turn) * 2);
     294  Dec(Turn, Min(50, Turn));
     295  if Turn = 0 then Exit;
     296
     297  // Year 1850.., Turn 250..
     298  Inc(Result, Turn);
    274299end;
    275300
     
    395420end;
    396421
    397 function LoadGraphicFile(bmp: TDpiBitmap; Path: string; Options: Integer): Boolean;
    398 var
    399   jtex: TDpiJpegImage;
     422function LoadGraphicFile(Bmp: TDpiBitmap; FileName: string; Options:
     423  TLoadGraphicFileOptions = []): Boolean;
     424var
     425  Jpeg: TDpiJpegImage;
    400426  Png: TDpiPortableNetworkGraphic;
    401427begin
    402   Result := True;
    403   if ExtractFileExt(Path) = '' then
    404     Path := Path + '.png';
    405   if ExtractFileExt(Path) = '.jpg' then begin
    406     jtex := TDpiJpegImage.Create;
    407     try
     428  Result := False;
     429  if ExtractFileExt(FileName) = '' then
     430    FileName := FileName + '.png';
     431
     432  if FileExists(FileName) then begin
     433    if ExtractFileExt(FileName) = '.jpg' then begin
     434      Jpeg := TDpiJpegImage.Create;
    408435      try
    409         jtex.LoadFromFile(Path);
     436        Jpeg.LoadFromFile(FileName);
     437        if not (gfNoGamma in Options) then
     438          Bmp.PixelFormat := pf24bit;
     439        Bmp.SetSize(Jpeg.Width, Jpeg.Height);
     440        Bmp.Canvas.Draw(0, 0, Jpeg);
     441        Result := True;
    410442      except
    411443        Result := False;
    412444      end;
    413       if Result then
    414       begin
    415         if Options and gfNoGamma = 0 then
    416           bmp.PixelFormat := pf24bit;
    417         Bmp.SetSize(jtex.Width, jtex.Height);
    418         Bmp.Canvas.Draw(0, 0, jtex);
    419       end;
    420     finally
    421       FreeAndNil(jtex);
    422     end;
    423   end
    424   else
    425   if ExtractFileExt(Path) = '.png' then begin
    426     Png := TDpiPortableNetworkGraphic.Create;
    427     try
    428       Png.PixelFormat := Bmp.PixelFormat;
     445      FreeAndNil(Jpeg);
     446    end else
     447    if ExtractFileExt(FileName) = '.png' then begin
     448      Png := TDpiPortableNetworkGraphic.Create;
    429449      try
    430         Png.LoadFromFile(Path);
    431       except
    432         Result := False;
    433         end;
    434       if Result then begin
    435         if Options and gfNoGamma = 0 then
    436           bmp.PixelFormat := pf24bit;
    437         bmp.SetSize(Png.Width, Png.Height);
     450        Png.PixelFormat := Bmp.PixelFormat;
     451        Png.LoadFromFile(FileName);
     452        if not (gfNoGamma in Options) then
     453          Bmp.PixelFormat := pf24bit;
     454        Bmp.SetSize(Png.Width, Png.Height);
    438455        if (Png.RawImage.Description.Format = ricfGray) then
    439456        begin
     
    441458          Bmp.PixelFormat := pf24bit;
    442459          CopyGray8BitTo24bitBitmap(Bmp, Png);
    443         end else
    444           Bmp.Canvas.draw(0, 0, Png);
     460        end
     461        else
     462          Bmp.Canvas.Draw(0, 0, Png);
     463        Result := True;
     464      except
     465        Result := False;
    445466      end;
    446     finally
    447467      FreeAndNil(Png);
    448     end;
    449   end else
    450   if ExtractFileExt(Path) = '.bmp' then begin
    451     try
    452       bmp.LoadFromFile(Path);
    453     except
    454       Result := False;
    455     end;
    456     if Result then begin
    457       if Options and gfNoGamma = 0 then
    458         bmp.PixelFormat := pf24bit;
    459     end;
    460   end else
    461     raise Exception.Create('Unsupported image file type ' + ExtractFileExt(Path));
     468    end else
     469    if ExtractFileExt(FileName) = '.bmp' then begin
     470      try
     471        Bmp.LoadFromFile(FileName);
     472        if not (gfNoGamma in Options) then
     473          Bmp.PixelFormat := pf24bit;
     474        Result := True;
     475      except
     476        Result := False;
     477      end;
     478    end else
     479      raise Exception.Create('Unsupported image file type ' + ExtractFileExt(FileName));
     480  end;
    462481
    463482  if not Result then begin
    464     if Options and gfNoError = 0 then
    465       raise Exception.Create(Format(Phrases.Lookup('FILENOTFOUND'), [Path]));
    466   end;
    467 
    468   if (Options and gfNoGamma = 0) and (Gamma <> 100) then
     483    if not (gfNoError in Options) then
     484      raise Exception.Create(Format(Phrases.Lookup('FILENOTFOUND'), [FileName]));
     485  end;
     486
     487  if (not (gfNoGamma in Options)) and (Gamma <> 100) then
    469488    ApplyGammaToBitmap(Bmp);
    470489end;
     
    12471266  i, r, g, b: Integer;
    12481267begin
    1249   begin
    1250     for i := 0 to 15 do
    1251     begin // gradient
    1252       r := Color and $FF + Brightness[i];
    1253       if r < 0 then
    1254         r := 0
    1255       else if r >= 256 then
    1256         r := 255;
    1257       g := Color shr 8 and $FF + Brightness[i];
    1258       if g < 0 then
    1259         g := 0
    1260       else if g >= 256 then
    1261         g := 255;
    1262       b := Color shr 16 and $FF + Brightness[i];
    1263       if b < 0 then
    1264         b := 0
    1265       else if b >= 256 then
    1266         b := 255;
    1267       ca.Pen.Color := r + g shl 8 + b shl 16;
    1268       ca.MoveTo(x + dx * i, y + dy * i);
    1269       ca.LineTo(x + dx * i + Width, y + dy * i + Height);
    1270     end;
    1271     ca.Pen.Color := $000000;
    1272     ca.MoveTo(x + 1, y + 16 * dy + Height);
    1273     ca.LineTo(x + 16 * dx + Width, y + 16 * dy + Height);
    1274     ca.LineTo(x + 16 * dx + Width, y);
    1275   end;
     1268  for i := 0 to Length(Brightness) - 1 do begin // gradient
     1269    r := Color and $FF + Brightness[i];
     1270    if r < 0 then
     1271      r := 0
     1272    else if r >= 256 then
     1273      r := 255;
     1274    g := Color shr 8 and $FF + Brightness[i];
     1275    if g < 0 then
     1276      g := 0
     1277    else if g >= 256 then
     1278      g := 255;
     1279    b := Color shr 16 and $FF + Brightness[i];
     1280    if b < 0 then
     1281      b := 0
     1282    else if b >= 256 then
     1283      b := 255;
     1284    ca.Pen.Color := r + g shl 8 + b shl 16;
     1285    ca.MoveTo(x + dx * i, y + dy * i);
     1286    ca.LineTo(x + dx * i + Width, y + dy * i + Height);
     1287  end;
     1288  ca.Pen.Color := $000000;
     1289  ca.MoveTo(x + 1, y + 16 * dy + Height);
     1290  ca.LineTo(x + 16 * dx + Width, y + 16 * dy + Height);
     1291  ca.LineTo(x + 16 * dx + Width, y);
    12761292end;
    12771293
     
    15491565end;
    15501566
    1551 procedure Texturize(Dest, Texture: TDpiBitmap; TransparentColor: Integer);
     1567procedure Texturize(Dest, Texture: TDpiBitmap; TransparentColor: Cardinal);
    15521568var
    15531569  SrcPixel, DstPixel: TPixelPointer;
     
    16451661            UniFont[section].Size :=
    16461662              Round(size * DpiScreen.PixelsPerInch / UniFont[section].PixelsPerInch * 0.8);
     1663            //UniFont[section].Size := Round(Size * 72 / UniFont[section].PixelsPerInch);
    16471664          end;
    16481665        end;
     
    16791696  LoadFonts;
    16801697  LoadGraphicFile(Templates, GetGraphicsDir + DirectorySeparator +
    1681     'Templates.png', gfNoGamma);
     1698    'Templates.png', [gfNoGamma]);
    16821699  LoadGraphicFile(Colors, GetGraphicsDir + DirectorySeparator + 'Colors.png');
    16831700  LoadGraphicFile(Paper, GetGraphicsDir + DirectorySeparator + 'Paper.jpg');
Note: See TracChangeset for help on using the changeset viewer.