source: trunk/Packages/CevoComponents/ScreenTools.pas

Last change on this file was 666, checked in by chronos, 34 hours ago
  • Added: Music volume option in game settings form.
File size: 61.2 KB
Line 
1unit ScreenTools;
2
3interface
4
5uses
6 {$IFDEF WINDOWS}
7 Windows,
8 {$ENDIF}
9 StringTables, LCLIntf, LCLType, SysUtils, Classes, Math,
10 GraphType, GraphicSet, LazFileUtils, Texture,
11 {$IFDEF DPI}Dpi.Forms, Dpi.Menus, Dpi.Graphics, Dpi.Controls, Dpi.Common{$ELSE}
12 Forms, Menus, Graphics, Controls{$ENDIF};
13
14type
15 TLoadGraphicFileOption = (gfNoError, gfNoGamma);
16 TLoadGraphicFileOptions = set of TLoadGraphicFileOption;
17
18 TFontType = (ftNormal, ftSmall, ftTiny, ftCaption, ftButton);
19
20{$IFDEF WINDOWS}
21function ChangeResolution(X, Y, bpp, freq: Integer): Boolean;
22{$ENDIF}
23procedure RestoreResolution;
24procedure EmptyMenu(MenuItems: TMenuItem; Keep: Integer = 0);
25function TurnToYear(Turn: Integer): Integer;
26function TurnToString(Turn: Integer): string;
27function MovementToString(Movement: Integer): string;
28procedure BtnFrame(Canvas: TCanvas; P: TRect; T: TTexture);
29procedure EditFrame(Canvas: TCanvas; P: TRect; T: TTexture);
30function HexStringToColor(S: string): Integer;
31function ExtractFileNameWithoutExt(const Filename: string): string;
32function LoadGraphicFile(Bmp: TBitmap; FileName: string; Options: TLoadGraphicFileOptions = []): Boolean;
33function LoadGraphicSet(const Name: string; Transparency: Boolean = True): TGraphicSet;
34procedure Dump(Dst: TBitmap; HGr: TGraphicSet; xDst, yDst, Width, Height, xGr, yGr: Integer);
35procedure BitmapReplaceColor(Dst: TBitmap; X, Y, Width, Height: Integer; OldColor, NewColor: TColor);
36procedure Sprite(Canvas: TCanvas; HGr: TGraphicSet; xDst, yDst, Width, Height, xGr, yGr: Integer);
37 overload;
38procedure Sprite(Canvas: TCanvas; xDst, yDst: Integer; GraphicSetItem: TGraphicSetItem);
39 overload;
40procedure Sprite(Dst: TBitmap; HGr: TGraphicSet; xDst, yDst, Width, Height, xGr, yGr: Integer);
41 overload;
42procedure MakeBlue(Dst: TBitmap; X, Y, Width, Height: Integer);
43procedure MakeRed(Dst: TBitmap; X, Y, Width, Height: Integer);
44procedure ImageOp_B(Dst, Src: TBitmap; xDst, yDst, xSrc, ySrc, Width, Height: Integer);
45procedure ImageOp_BCC(Dst, Src: TBitmap;
46 xDst, yDst, xSrc, ySrc, Width, Height, Color1, Color2: Integer); overload;
47procedure ImageOp_BCC(Dst, Src: TBitmap;
48 DstPos: TPoint; SrcRect: TRect; Color1, Color2: Integer); overload;
49procedure ImageOp_CBC(Dst, Src: TBitmap; xDst, yDst, xSrc, ySrc, Width, Height,
50 Color0, Color2: Integer);
51procedure ImageOp_CCC(Bmp: TBitmap; X, Y, Width, Height, Color0, Color1, Color2: Integer);
52function BitBltCanvas(DestCanvas: TCanvas; X, Y, Width, Height: Integer;
53 SrcCanvas: TCanvas; XSrc, YSrc: Integer; Rop: DWORD = SRCCOPY): Boolean; overload;
54function BitBltCanvas(Dest: TCanvas; DestRect: TRect;
55 Src: TCanvas; SrcPos: TPoint; Rop: DWORD = SRCCOPY): Boolean; overload;
56function BitBltBitmap(Dest: TBitmap; X, Y, Width, Height: Integer;
57 Src: TBitmap; XSrc, YSrc: Integer; Rop: DWORD = SRCCOPY): Boolean; overload;
58function BitBltBitmap(Dest: TBitmap; DestRect: TRect;
59 Src: TBitmap; SrcPos: TPoint; Rop: DWORD = SRCCOPY): Boolean; overload;
60procedure SLine(Canvas: TCanvas; x0, x1, Y: Integer; cl: TColor);
61procedure DLine(Canvas: TCanvas; x0, x1, Y: Integer; cl0, cl1: TColor);
62procedure Frame(Canvas: TCanvas; x0, y0, x1, y1: Integer; cl0, cl1: TColor);
63procedure RFrame(Canvas: TCanvas; x0, y0, x1, y1: Integer; cl0, cl1: TColor);
64procedure CFrame(Canvas: TCanvas; x0, y0, x1, y1, Corner: Integer; cl: TColor);
65procedure FrameImage(Canvas: TCanvas; Src: TBitmap;
66 X, Y, Width, Height, xSrc, ySrc: Integer; IsControl: Boolean = False);
67procedure GlowFrame(Dst: TBitmap; x0, y0, Width, Height: Integer; cl: TColor);
68procedure InitOrnament;
69procedure InitCityMark(Texture: TTexture);
70procedure Fill(Canvas: TCanvas; Left, Top, Width, Height, xOffset, yOffset: Integer); overload;
71procedure Fill(Canvas: TCanvas; Rect: TRect; Offset: TPoint); overload;
72procedure FillLarge(Canvas: TCanvas; x0, y0, x1, y1, xm: Integer);
73procedure FillSeamless(Canvas: TCanvas; Left, Top, Width, Height, xOffset, yOffset: Integer;
74 const Texture: TBitmap);
75procedure FillRectSeamless(Canvas: TCanvas; x0, y0, x1, y1, xOffset, yOffset: Integer;
76 const Texture: TBitmap);
77procedure PaintBackground(Canvas: TCanvas; Left, Top, Width, Height, FormWidth,
78 FormHeight: Integer);
79procedure Corner(Canvas: TCanvas; X, Y, Kind: Integer; T: TTexture);
80procedure BiColorTextOut(Canvas: TCanvas; clMain, clBack: TColor; X, Y: Integer; S: string);
81procedure LoweredTextOut(Canvas: TCanvas; cl: TColor; T: TTexture;
82 X, Y: Integer; S: string);
83function BiColorTextWidth(Canvas: TCanvas; S: string): Integer;
84procedure RisedTextOut(Canvas: TCanvas; X, Y: Integer; S: string);
85procedure LightGradient(Canvas: TCanvas; X, Y, Width, Color: Integer);
86procedure DarkGradient(Canvas: TCanvas; X, Y, Width, Kind: Integer);
87procedure VLightGradient(Canvas: TCanvas; X, Y, Height, Color: Integer);
88procedure VDarkGradient(Canvas: TCanvas; X, Y, Height, Kind: Integer);
89procedure UnderlinedTitleValue(Canvas: TCanvas; Title, Value: string; X, Y, Width: Integer);
90procedure NumberBar(Dst: TBitmap; X, Y: Integer; Cap: string; Val: Integer;
91 T: TTexture);
92procedure CountBar(Dst: TBitmap; X, Y, W: Integer; Kind: Integer;
93 Cap: string; Val: Integer; T: TTexture);
94procedure PaintProgressBar(Canvas: TCanvas; Kind, X, Y, Pos, Growth, Max: Integer;
95 T: TTexture);
96procedure PaintRelativeProgressBar(Canvas: TCanvas;
97 Kind, X, Y, Size, Pos, Growth, Max: Integer; IndicateComplete: Boolean;
98 T: TTexture);
99procedure PaintLogo(Canvas: TCanvas; X, Y, LightColor, ShadeColor: Integer);
100procedure DrawBufferEnsureSize(Width, Height: Integer);
101procedure LoadPhrases;
102procedure Texturize(Dest, Texture: TBitmap; TransparentColor: Cardinal);
103procedure DarkenImage(Bitmap: TBitmap; Change: Integer);
104{$IFNDEF DPI}
105function ScaleToNative(Value: Integer): Integer;
106function ScaleToNativeDist(Base, Value: Integer): Integer;
107function ScaleFromNative(Value: Integer): Integer;
108function BitBltBitmapPrecise(Dest: TBitmap; X, Y, Width, Height: Integer; Src: TBitmap;
109 XSrc, YSrc: Integer; Rop: DWORD = SRCCOPY; Precise: Boolean = False): Boolean;
110{$ENDIF}
111procedure UnshareBitmap(Bitmap: TBitmap);
112procedure Gtk2Fix;
113procedure Gtk2DisableControlStyling(WinControl: TWinControl);
114procedure LoadConfig(Key: string);
115procedure SaveConfig(Key: string);
116
117const
118 TransparentColor1 = $FF00FF;
119 TransparentColor2 = $7F007F;
120
121 // template positions in Templates.png
122 xNation = 1;
123 yNation = 25;
124 xCoal = 1;
125 yCoal = 148;
126
127 // Icons.bmp structure
128 xSizeBig = 56;
129 ySizeBig = 40;
130
131 GlowRange = 8;
132
133 EmptySpaceColor = $101010;
134
135 // color matrix
136 clkAge0 = 1;
137 cliTexture = 0;
138 cliBevelLight = cliTexture + 1;
139 cliBevelShade = cliTexture + 2;
140 cliTextLight = cliTexture + 3;
141 cliTextShade = cliTexture + 4;
142 cliLitText = cliTexture + 5;
143 cliMark = cliTexture + 6;
144 cliDimmedText = cliTexture + 7;
145 cliRoad = 8;
146 cliHouse = cliRoad + 1;
147 cliImp = cliRoad + 2;
148 cliImpProject = cliRoad + 3;
149 cliPage = 13;
150 cliCover = cliPage + 1;
151 clkMisc = 5;
152 cliPaper = 0;
153 cliPaperText = 1;
154 cliPaperCaption = 2;
155 clkCity = 6;
156 cliPlains = 0;
157 cliPrairie = 1;
158 cliHills = 2;
159 cliTundra = 3;
160 cliWater = 4;
161
162var
163 Phrases: TStringTable;
164 Phrases2: TStringTable;
165 GrExt: TGraphicSets;
166
167 HGrSystem: TGraphicSet;
168 CityMark1: TGraphicSetItem;
169 CityMark2: TGraphicSetItem;
170
171 HGrSystem2: TGraphicSet;
172 Ornament: TGraphicSetItem;
173 GBrainNoTerm: TGraphicSetItem;
174 GBrainSuperVirtual: TGraphicSetItem;
175 GBrainTerm: TGraphicSetItem;
176 GBrainRandom: TGraphicSetItem;
177
178 Templates: TGraphicSet;
179 Logo: TGraphicSetItem;
180 BigBook: TGraphicSetItem;
181 SmallBook: TGraphicSetItem;
182 MenuLogo: TGraphicSetItem;
183 LinkArrows: TGraphicSetItem;
184 ScienceNationDot: TGraphicSetItem;
185 ResearchIcon: TGraphicSetItem;
186 ChangeIcon: TGraphicSetItem;
187 TreasuryIcon: TGraphicSetItem;
188 StarshipDeparted: TGraphicSetItem;
189 WeightOn: TGraphicSetItem;
190 WeightOff: TGraphicSetItem;
191
192 ClickFrameColor: Integer;
193 MainTexture: TTexture;
194 Colors: TBitmap;
195 Paper: TBitmap;
196 BigImp: TBitmap;
197 DrawBuffer: TBitmap;
198 FullScreen: Boolean;
199 MusicEnabled: Boolean;
200 MusicVolume: Single;
201 TermBounds: TRect;
202 GenerateNames: Boolean;
203 InitOrnamentDone: Boolean;
204 Phrases2FallenBackToEnglish: Boolean;
205
206 UniFont: array [TFontType] of TFont;
207 Gamma: Integer; // global gamma correction (cent)
208 CustomDpiEnabled: Boolean;
209 CustomDpi: Integer;
210
211procedure LoadAssets;
212procedure UnitInit;
213procedure UnitDone;
214procedure InitGammaLookupTable;
215
216
217implementation
218
219uses
220 {$IFDEF DPI}Dpi.PixelPointer,{$ELSE}PixelPointer,{$ENDIF}
221 Directories, Sound, Registry
222 {$IFDEF LCLGTK2}, gtk2, WSProc{$ENDIF};
223
224var
225 {$IFDEF WINDOWS}
226 StartResolution: TDeviceMode;
227 ResolutionChanged: Boolean;
228 {$ENDIF}
229
230 GammaLookupTable: array [0..255] of Byte;
231
232{$IFDEF WINDOWS}
233function ChangeResolution(X, Y, bpp, freq: Integer): Boolean;
234var
235 DevMode: TDeviceMode;
236begin
237 EnumDisplaySettings(nil, 0, DevMode);
238 DevMode.dmFields := DM_PELSWIDTH or DM_PELSHEIGHT or DM_BITSPERPEL or
239 DM_DISPLAYFREQUENCY;
240 DevMode.dmPelsWidth := X;
241 DevMode.dmPelsHeight := Y;
242 DevMode.dmBitsPerPel := bpp;
243 DevMode.dmDisplayFrequency := freq;
244 Result := ChangeDisplaySettings(DevMode, 0) = DISP_CHANGE_SUCCESSFUL;
245 if Result then
246 ResolutionChanged := True;
247end;
248
249{$ENDIF}
250
251procedure RestoreResolution;
252begin
253 {$IFDEF WINDOWS}
254 if ResolutionChanged then
255 ChangeDisplaySettings(StartResolution, 0);
256 ResolutionChanged := False;
257 {$ENDIF}
258end;
259
260procedure EmptyMenu(MenuItems: TMenuItem; Keep: Integer = 0);
261var
262 MenuItem: TMenuItem;
263begin
264 if Keep = 0 then MenuItems.Clear
265 else
266 while MenuItems.Count > Keep do begin
267 MenuItem := MenuItems[MenuItems.Count - 1];
268 MenuItems.Delete(MenuItems.Count - 1);
269 FreeAndNil(MenuItem);
270 end;
271end;
272
273function TurnToYear(Turn: Integer): Integer;
274begin
275 Result := -4000;
276 if Turn <= 0 then Exit;
277
278 // Year -4000..-1000, Turn 0..60
279 Inc(Result, Min(60, Turn) * 50);
280 Dec(Turn, Min(60, Turn));
281 if Turn = 0 then Exit;
282
283 // Year -1000..0, Turn 60..100
284 Inc(Result, Min(40, Turn) * 25);
285 Dec(Turn, Min(40, Turn));
286 if Turn = 0 then Exit;
287
288 // Year 0..1500, Turn 100..175
289 Inc(Result, Min(75, Turn) * 20);
290 Dec(Turn, Min(75, Turn));
291 if Turn = 0 then Exit;
292
293 // Year 1500..1750, Turn 175..200
294 Inc(Result, Min(25, Turn) * 10);
295 Dec(Turn, Min(25, Turn));
296 if Turn = 0 then Exit;
297
298 // Year 1750..1850, Turn 200..250
299 Inc(Result, Min(50, Turn) * 2);
300 Dec(Turn, Min(50, Turn));
301 if Turn = 0 then Exit;
302
303 // Year 1850.., Turn 250..
304 Inc(Result, Turn);
305end;
306
307function TurnToString(Turn: Integer): string;
308var
309 Year: Integer;
310begin
311 if GenerateNames then
312 begin
313 Year := TurnToYear(Turn);
314 if Year < 0 then
315 Result := Format(Phrases.Lookup('BC'), [-Year])
316 else
317 Result := Format(Phrases.Lookup('AD'), [Year]);
318 end
319 else
320 Result := IntToStr(Turn);
321end;
322
323function MovementToString(Movement: Integer): string;
324begin
325 if Movement >= 1000 then
326 begin
327 Result := Char(48 + Movement div 1000);
328 Movement := Movement mod 1000;
329 end
330 else
331 Result := '';
332 Result := Result + Char(48 + Movement div 100);
333 Movement := Movement mod 100;
334 if Movement > 0 then
335 begin
336 Result := Result + '.' + Char(48 + Movement div 10);
337 Movement := Movement mod 10;
338 if Movement > 0 then
339 Result := Result + Char(48 + Movement);
340 end;
341end;
342
343procedure BtnFrame(Canvas: TCanvas; P: TRect; T: TTexture);
344begin
345 RFrame(Canvas, P.Left - 1, P.Top - 1, P.Right, P.Bottom, T.ColorBevelShade,
346 T.ColorBevelLight);
347end;
348
349procedure EditFrame(Canvas: TCanvas; P: TRect; T: TTexture);
350begin
351 Frame(Canvas, P.Left - 1, P.Top - 1, P.Right, P.Bottom, $000000, $000000);
352 Frame(Canvas, P.Left - 2, P.Top - 2, P.Right + 1, P.Bottom + 1, $000000, $000000);
353 Frame(Canvas, P.Left - 3, P.Top - 3, P.Right + 2, P.Bottom + 1, $000000, $000000);
354 RFrame(Canvas, P.Left - 4, P.Top - 4, P.Right + 3, P.Bottom + 2, T.ColorBevelShade,
355 T.ColorBevelLight);
356end;
357
358function HexCharToInt(X: Char): Integer;
359begin
360 case X of
361 '0' .. '9': Result := Ord(X) - Ord('0');
362 'A' .. 'F': Result := Ord(X) - Ord('A') + 10;
363 'a' .. 'f': Result := Ord(X) - Ord('a') + 10;
364 else Result := 0
365 end;
366end;
367
368function HexStringToColor(S: string): Integer;
369begin
370 while (Length(S) > 0) and (S[1] = ' ') do
371 Delete(S, 1, 1);
372 S := S + '000000';
373 if Gamma = 100 then
374 Result := $10 * HexCharToInt(S[1]) + $1 * HexCharToInt(S[2]) +
375 $1000 * HexCharToInt(S[3]) + $100 * HexCharToInt(S[4]) +
376 $100000 * HexCharToInt(S[5]) + $10000 * HexCharToInt(S[6])
377 else
378 Result := GammaLookupTable[$10 * HexCharToInt(S[1]) + HexCharToInt(S[2])] +
379 $100 * GammaLookupTable[$10 * HexCharToInt(S[3]) + HexCharToInt(S[4])] +
380 $10000 * GammaLookupTable[$10 * HexCharToInt(S[5]) + HexCharToInt(S[6])];
381end;
382
383function ApplyGammaToPixel(Pixel: TPixel32): TPixel32;
384begin
385 Result.R := GammaLookupTable[Pixel.R];
386 Result.G := GammaLookupTable[Pixel.G];
387 Result.B := GammaLookupTable[Pixel.B];
388end;
389
390procedure ApplyGammaToBitmap(Bitmap: TBitmap);
391var
392 PixelPtr: TPixelPointer;
393 X, Y: Integer;
394begin
395 Bitmap.BeginUpdate;
396 PixelPtr := TPixelPointer.Create(Bitmap);
397 for Y := 0 to ScaleToNative(Bitmap.Height) - 1 do begin
398 for X := 0 to ScaleToNative(Bitmap.Width) - 1 do begin
399 PixelPtr.Pixel^ := ApplyGammaToPixel(PixelPtr.Pixel^);
400 PixelPtr.NextPixel;
401 end;
402 PixelPtr.NextLine;
403 end;
404 Bitmap.EndUpdate;
405end;
406
407procedure CopyGray8BitTo24bitBitmap(Dst, Src: TRasterImage);
408var
409 SrcPtr, DstPtr: TPixelPointer;
410 X, Y: Integer;
411begin
412 //Dst.SetSize(Src.Width, Src.Height);
413 SrcPtr := TPixelPointer.Create(Src);
414 DstPtr := TPixelPointer.Create(Dst);
415 for Y := 0 to ScaleToNative(Src.Height - 1) do begin
416 for X := 0 to ScaleToNative(Src.Width - 1) do begin
417 DstPtr.PixelB := SrcPtr.PixelB;
418 DstPtr.PixelG := SrcPtr.PixelB;
419 DstPtr.PixelR := SrcPtr.PixelB;
420 SrcPtr.NextPixel;
421 DstPtr.NextPixel;
422 end;
423 SrcPtr.NextLine;
424 DstPtr.NextLine;
425 end;
426end;
427
428function LoadGraphicFile(Bmp: TBitmap; FileName: string; Options:
429 TLoadGraphicFileOptions = []): Boolean;
430var
431 Jpeg: TJpegImage;
432 Png: TPortableNetworkGraphic;
433begin
434 Result := False;
435 if ExtractFileExt(FileName) = '' then
436 FileName := FileName + '.png';
437
438 if FileExists(FileName) then begin
439 if ExtractFileExt(FileName) = '.jpg' then begin
440 Jpeg := TJpegImage.Create;
441 try
442 Jpeg.LoadFromFile(FileName);
443 if not (gfNoGamma in Options) then
444 Bmp.PixelFormat := TPixelFormat.pf24bit;
445 Bmp.SetSize(Jpeg.Width, Jpeg.Height);
446 Bmp.Canvas.Draw(0, 0, Jpeg);
447 Result := True;
448 except
449 Result := False;
450 end;
451 FreeAndNil(Jpeg);
452 end else
453 if ExtractFileExt(FileName) = '.png' then begin
454 Png := TPortableNetworkGraphic.Create;
455 try
456 Png.PixelFormat := Bmp.PixelFormat;
457 Png.LoadFromFile(FileName);
458 if not (gfNoGamma in Options) then
459 Bmp.PixelFormat := TPixelFormat.pf24bit;
460 Bmp.SetSize(Png.Width, Png.Height);
461 if (Png.RawImage.Description.Format = ricfGray) then
462 begin
463 // LCL doesn't support 8-bit colors properly. Use 24-bit instead.
464 Bmp.PixelFormat := TPixelFormat.pf24bit;
465 CopyGray8BitTo24bitBitmap(Bmp, Png);
466 end
467 else
468 Bmp.Canvas.Draw(0, 0, Png);
469 Result := True;
470 except
471 Result := False;
472 end;
473 FreeAndNil(Png);
474 end else
475 if ExtractFileExt(FileName) = '.bmp' then begin
476 try
477 Bmp.LoadFromFile(FileName);
478 if not (gfNoGamma in Options) then
479 Bmp.PixelFormat := TPixelFormat.pf24bit;
480 Result := True;
481 except
482 Result := False;
483 end;
484 end else
485 raise Exception.Create('Unsupported image file type ' + ExtractFileExt(FileName));
486 end;
487
488 if not Result then begin
489 if not (gfNoError in Options) then
490 raise Exception.Create(Format(Phrases.Lookup('FILENOTFOUND'), [FileName]));
491 end;
492
493 if (not (gfNoGamma in Options)) and (Gamma <> 100) then
494 ApplyGammaToBitmap(Bmp);
495end;
496
497function ExtractFileNameWithoutExt(const Filename: string): string;
498var
499 P: Integer;
500begin
501 Result := Filename;
502 P := Length(Result);
503 while P > 0 do begin
504 case Result[P] of
505 PathDelim: Exit;
506 {$ifdef windows}
507 '/': if ('/' in AllowDirectorySeparators) then Exit;
508 {$endif}
509 '.': Exit(Copy(Result, 1, P - 1));
510 end;
511 Dec(P);
512 end;
513end;
514
515function LoadGraphicSet(const Name: string; Transparency: Boolean = True): TGraphicSet;
516var
517 X: Integer;
518 Y: Integer;
519 OriginalColor: Integer;
520 FileName: string;
521 DataPixel: TPixelPointer;
522 MaskPixel: TPixelPointer;
523begin
524 Result := GrExt.SearchByName(Name);
525 if not Assigned(Result) then begin
526 Result := GrExt.AddNew(Name);
527 FileName := GetGraphicsDir + DirectorySeparator + Name;
528 // Do not apply gamma during file load as it would affect also transparency colors
529 if not LoadGraphicFile(Result.Data, FileName, [gfNoGamma]) then begin
530 Result := nil;
531 Exit;
532 end;
533
534 FileName := ExtractFileNameWithoutExt(FileName) + GraphicSetFileExt;
535
536 if FileExists(FileName) then
537 Result.LoadFromFile(FileName);
538
539 Result.ResetPixUsed;
540
541 if Transparency then begin
542 Result.Mask.SetSize(Result.Data.Width, Result.Data.Height);
543
544 Result.Data.BeginUpdate;
545 Result.Mask.BeginUpdate;
546 DataPixel := TPixelPointer.Create(Result.Data);
547 MaskPixel := TPixelPointer.Create(Result.Mask);
548 for Y := 0 to ScaleToNative(Result.Data.Height) - 1 do begin
549 for X := 0 to ScaleToNative(Result.Data.Width) - 1 do begin
550 OriginalColor := DataPixel.PixelARGB and $FFFFFF;
551 if (OriginalColor = TransparentColor1) or (OriginalColor = TransparentColor2) then begin
552 MaskPixel.PixelRGB := $ffffff;
553 DataPixel.PixelRGB := 0;
554 end else begin
555 MaskPixel.PixelRGB := 0;
556 end;
557 DataPixel.NextPixel;
558 MaskPixel.NextPixel;
559 end;
560 DataPixel.NextLine;
561 MaskPixel.NextLine;
562 end;
563 Result.Data.EndUpdate;
564 Result.Mask.EndUpdate;
565
566 if Gamma <> 100 then
567 ApplyGammaToBitmap(Result.Data);
568 end;
569 end;
570end;
571
572procedure Dump(Dst: TBitmap; HGr: TGraphicSet; xDst, yDst, Width, Height, xGr, yGr: Integer);
573begin
574 BitBltBitmap(Dst, xDst, yDst, Width, Height, HGr.Data, xGr, yGr);
575end;
576
577procedure BitmapReplaceColor(Dst: TBitmap; X, Y, Width, Height: Integer; OldColor, NewColor: TColor);
578var
579 XX, YY: Integer;
580 PixelPtr: TPixelPointer;
581begin
582 Dst.BeginUpdate;
583 PixelPtr := TPixelPointer.Create(Dst, ScaleToNative(X), ScaleToNative(Y));
584 for YY := 0 to ScaleToNative(Height) - 1 do begin
585 for XX := 0 to ScaleToNative(Width) - 1 do begin
586 if PixelPtr.PixelRGB = SwapRedBlue(OldColor) then begin
587 PixelPtr.PixelRGB := SwapRedBlue(NewColor);
588 end;
589 PixelPtr.NextPixel;
590 end;
591 PixelPtr.NextLine;
592 end;
593 Dst.EndUpdate;
594end;
595
596procedure MakeBlue(Dst: TBitmap; X, Y, Width, Height: Integer);
597var
598 XX, YY: Integer;
599 PixelPtr: TPixelPointer;
600begin
601 Dst.BeginUpdate;
602 PixelPtr := TPixelPointer.Create(Dst, ScaleToNative(X), ScaleToNative(Y));
603 for yy := 0 to ScaleToNative(Height) - 1 do begin
604 for xx := 0 to ScaleToNative(Width) - 1 do begin
605 PixelPtr.PixelB := PixelPtr.PixelB div 2;
606 PixelPtr.PixelG := PixelPtr.PixelG div 2;
607 PixelPtr.PixelR := PixelPtr.PixelR div 2;
608 PixelPtr.NextPixel;
609 end;
610 PixelPtr.NextLine;
611 end;
612 Dst.EndUpdate;
613end;
614
615procedure MakeRed(Dst: TBitmap; X, Y, Width, Height: Integer);
616var
617 XX, YY: Integer;
618 Gray: Integer;
619 PixelPtr: TPixelPointer;
620begin
621 Dst.BeginUpdate;
622 PixelPtr := TPixelPointer.Create(Dst, ScaleToNative(X), ScaleToNative(Y));
623 for YY := 0 to ScaleToNative(Height) - 1 do begin
624 for XX := 0 to ScaleToNative(Width) - 1 do begin
625 Gray := (Integer(PixelPtr.PixelB) + Integer(PixelPtr.PixelG) +
626 Integer(PixelPtr.PixelR)) * 85 shr 8;
627 PixelPtr.PixelB := 0;
628 PixelPtr.PixelG := 0;
629 PixelPtr.PixelR := Gray; // 255-(255-gray) div 2;
630 PixelPtr.NextPixel;
631 end;
632 PixelPtr.NextLine;
633 end;
634 Dst.EndUpdate;
635end;
636
637procedure ImageOp_B(Dst, Src: TBitmap; xDst, yDst, xSrc, ySrc, Width, Height: Integer);
638// Src is template
639// X channel = background amp (old Dst content), 128=original brightness
640var
641 X, Y: Integer;
642 Brightness, Test: Integer;
643 PixelSrc: TPixelPointer;
644 PixelDst: TPixelPointer;
645begin
646 xDst := ScaleToNative(xDst);
647 yDst := ScaleToNative(yDst);
648 xSrc := ScaleToNative(xSrc);
649 ySrc := ScaleToNative(ySrc);
650 Width := ScaleToNative(Width);
651 Height := ScaleToNative(Height);
652 //Assert(Src.PixelFormat = pf8bit);
653 Assert(Dst.PixelFormat = TPixelFormat.pf24bit);
654 if xDst < 0 then begin
655 Width := Width + xDst;
656 xSrc := xSrc - xDst;
657 xDst := 0;
658 end;
659 if yDst < 0 then begin
660 Height := Height + yDst;
661 ySrc := ySrc - yDst;
662 yDst := 0;
663 end;
664 if xDst + Width > ScaleToNative(Dst.Width) then
665 Width := ScaleToNative(Dst.Width) - xDst;
666 if yDst + Height > ScaleToNative(Dst.Height) then
667 Height := ScaleToNative(Dst.Height) - yDst;
668 if (Width < 0) or (Height < 0) then
669 Exit;
670
671 Dst.BeginUpdate;
672 Src.BeginUpdate;
673 PixelDst := TPixelPointer.Create(Dst, xDst, yDst);
674 PixelSrc := TPixelPointer.Create(Src, xSrc, ySrc);
675 for Y := 0 to Height - 1 do begin
676 for X := 0 to Width - 1 do begin
677 Brightness := PixelSrc.PixelB; // One byte for 8-bit color
678 Test := (PixelDst.PixelR * Brightness) shr 7;
679 if Test >= 256 then
680 PixelDst.PixelR := 255
681 else
682 PixelDst.PixelR := Test; // Red
683 Test := (PixelDst.PixelG * Brightness) shr 7;
684 if Test >= 256 then
685 PixelDst.PixelG := 255
686 else
687 PixelDst.PixelG := Test; // Green
688 Test := (PixelDst.PixelB * Brightness) shr 7;
689 if Test >= 256 then
690 PixelDst.PixelR := 255
691 else
692 PixelDst.PixelB := Test; // Blue
693 PixelDst.NextPixel;
694 PixelSrc.NextPixel;
695 end;
696 PixelDst.NextLine;
697 PixelSrc.NextLine;
698 end;
699 Src.EndUpdate;
700 Dst.EndUpdate;
701end;
702
703procedure ImageOp_BCC(Dst, Src: TBitmap; xDst, yDst, xSrc, ySrc, Width, Height,
704 Color1, Color2: Integer);
705// Src is template
706// B channel = background amp (old Dst content), 128=original brightness
707// G channel = Color1 amp, 128=original brightness
708// R channel = Color2 amp, 128=original brightness
709var
710 ix, iy, amp1, amp2, trans, Value: Integer;
711 SrcPixel: TPixelPointer;
712 DstPixel: TPixelPointer;
713 DstWidth, DstHeight: Integer;
714 SrcWidth, SrcHeight: Integer;
715begin
716 if xDst < 0 then begin
717 Width := Width + xDst;
718 xSrc := xSrc - xDst;
719 xDst := 0;
720 end;
721 if yDst < 0 then begin
722 Height := Height + yDst;
723 ySrc := ySrc - yDst;
724 yDst := 0;
725 end;
726 if xDst + Width > Dst.Width then
727 Width := Dst.Width - xDst;
728 if yDst + Height > Dst.Height then
729 Height := Dst.Height - yDst;
730 if (Width < 0) or (Height < 0) then
731 Exit;
732 DstWidth := ScaleToNativeDist(xDst, Width);
733 DstHeight := ScaleToNativeDist(yDst, Height);
734 SrcWidth := ScaleToNativeDist(xSrc, Width);
735 SrcHeight := ScaleToNativeDist(ySrc, Height);
736 xDst := ScaleToNative(xDst);
737 yDst := ScaleToNative(yDst);
738 xSrc := ScaleToNative(xSrc);
739 ySrc := ScaleToNative(ySrc);
740
741 Src.BeginUpdate;
742 Dst.BeginUpdate;
743 SrcPixel := TPixelPointer.Create(Src, xSrc, ySrc);
744 DstPixel := TPixelPointer.Create(Dst, xDst, yDst);
745 for iy := 0 to DstHeight - 1 do begin
746 for ix := 0 to DstWidth - 1 do begin
747 trans := SrcPixel.PixelB * 2; // green channel = transparency
748 amp1 := SrcPixel.PixelG * 2;
749 amp2 := SrcPixel.PixelR * 2;
750 if trans <> $FF then begin
751 Value := (DstPixel.PixelB * trans + ((Color2 shr 16) and $FF) *
752 amp2 + ((Color1 shr 16) and $FF) * amp1) div $FF;
753 DstPixel.PixelB := Min(Value, 255);
754
755 Value := (DstPixel.PixelG * trans + ((Color2 shr 8) and $FF) *
756 amp2 + ((Color1 shr 8) and $FF) * amp1) div $FF;
757 DstPixel.PixelG := Min(Value, 255);
758
759 Value := (DstPixel.PixelR * trans + (Color2 and $FF) *
760 amp2 + (Color1 and $FF) * amp1) div $FF;
761 DstPixel.PixelR := Min(Value, 255);
762 end;
763
764 if ix < SrcWidth - 1 then SrcPixel.NextPixel;
765 DstPixel.NextPixel;
766 end;
767 if iy < SrcHeight - 1 then SrcPixel.NextLine
768 else SrcPixel.SetX(0);
769 DstPixel.NextLine;
770 end;
771 Src.EndUpdate;
772 Dst.EndUpdate;
773end;
774
775procedure ImageOp_BCC(Dst, Src: TBitmap; DstPos: TPoint; SrcRect: TRect;
776 Color1, Color2: Integer);
777begin
778 ImageOp_BCC(Dst, Src, DstPos.X, DstPos.Y, SrcRect.Left, SrcRect.Top,
779 SrcRect.Width, SrcRect.Height, Color1, Color2);
780end;
781
782procedure ImageOp_CBC(Dst, Src: TBitmap; xDst, yDst, xSrc, ySrc, Width, Height,
783 Color0, Color2: Integer);
784// Src is template
785// B channel = Color0 amp
786// G channel = background amp (old Dst content), 128=original brightness
787// R channel = Color2 amp
788var
789 ix, iy, amp0, amp1, trans, Value: Integer;
790 SrcPixel: TPixelPointer;
791 DstPixel: TPixelPointer;
792begin
793 xDst := ScaleToNative(xDst);
794 yDst := ScaleToNative(yDst);
795 xSrc := ScaleToNative(xSrc);
796 ySrc := ScaleToNative(ySrc);
797 Width := ScaleToNative(Width);
798 Height := ScaleToNative(Height);
799 Src.BeginUpdate;
800 Dst.BeginUpdate;
801 SrcPixel := TPixelPointer.Create(Src, xSrc, ySrc);
802 DstPixel := TPixelPointer.Create(Dst, xDst, yDst);
803 for iy := 0 to Height - 1 do begin
804 for ix := 0 to Width - 1 do begin
805 trans := SrcPixel.PixelB * 2; // green channel = transparency
806 amp0 := SrcPixel.PixelG * 2;
807 amp1 := SrcPixel.PixelR * 2;
808 if trans <> $FF then begin
809 Value := (DstPixel.PixelB * trans + (Color2 shr 16 and $FF) * amp1 +
810 (Color0 shr 16 and $FF) * amp0) div $FF;
811 DstPixel.PixelB := Min(Value, 255);
812
813 Value := (DstPixel.PixelG * trans + (Color2 shr 8 and $FF) * amp1 +
814 (Color0 shr 8 and $FF) * amp0) div $FF;
815 DstPixel.PixelG := Min(Value, 255);
816
817 Value := (DstPixel.PixelR * trans + (Color2 and $FF) * amp1 +
818 (Color0 and $FF) * amp0) div $FF;
819 DstPixel.PixelR := Min(Value, 255);
820 end;
821 SrcPixel.NextPixel;
822 DstPixel.NextPixel;
823 end;
824 SrcPixel.NextLine;
825 DstPixel.NextLine;
826 end;
827 Src.EndUpdate;
828 Dst.EndUpdate;
829end;
830
831procedure ImageOp_CCC(Bmp: TBitmap; X, Y, Width, Height, Color0, Color1, Color2: Integer);
832// Bmp is template
833// B channel = Color0 amp, 128=original brightness
834// G channel = Color1 amp, 128=original brightness
835// R channel = Color2 amp, 128=original brightness
836var
837 XX, YY: Integer;
838 Red, Green: Integer;
839 PixelPtr: TPixelPointer;
840begin
841 X := ScaleToNative(X);
842 Y := ScaleToNative(Y);
843 Width := ScaleToNativeDist(X, Width);
844 Height := ScaleToNativeDist(Y, Height);
845
846 if X + Width > ScaleToNative(Bmp.Width) then
847 Width := ScaleToNative(Bmp.Width) - X;
848 if Y + Height > ScaleToNative(Bmp.Height) then
849 Height := ScaleToNative(Bmp.Height) - Y;
850 if (Width < 0) or (Height < 0) then
851 Exit;
852
853 Bmp.BeginUpdate;
854 Assert(Bmp.PixelFormat = TPixelFormat.pf24bit);
855 PixelPtr := TPixelPointer.Create(Bmp, X, Y);
856 for YY := 0 to Height - 1 do begin
857 for XX := 0 to Width - 1 do begin
858 Red := ((PixelPtr.PixelB * (Color0 and $0000FF) + PixelPtr.PixelG *
859 (Color1 and $0000FF) + PixelPtr.PixelR * (Color2 and $0000FF)) shr 8) and $ff;
860 Green := ((PixelPtr.PixelB * ((Color0 shr 8) and $0000FF) +
861 PixelPtr.PixelG * ((Color1 shr 8) and $0000FF) + PixelPtr.PixelR *
862 ((Color2 shr 8) and $0000FF)) shr 8) and $ff;
863 PixelPtr.PixelB := ((PixelPtr.PixelB * ((Color0 shr 16) and $0000FF) +
864 PixelPtr.PixelG * ((Color1 shr 16) and $0000FF) + PixelPtr.PixelR *
865 ((Color2 shr 16) and $0000FF)) shr 8) and $ff; // Blue
866 PixelPtr.PixelG := Green;
867 PixelPtr.PixelR := Red;
868 PixelPtr.NextPixel;
869 end;
870 PixelPtr.NextLine;
871 end;
872 Bmp.EndUpdate;
873end;
874
875procedure Sprite(Canvas: TCanvas; HGr: TGraphicSet; xDst, yDst, Width, Height, xGr, yGr: Integer);
876begin
877 BitBltCanvas(Canvas, xDst, yDst, Width, Height, HGr.Mask.Canvas, xGr, yGr, SRCAND);
878 BitBltCanvas(Canvas, xDst, yDst, Width, Height, HGr.Data.Canvas, xGr, yGr, SRCPAINT);
879end;
880
881procedure Sprite(Canvas: TCanvas; xDst, yDst: Integer; GraphicSetItem: TGraphicSetItem);
882begin
883 Sprite(Canvas, GraphicSetItem.GraphicSet, xDst, yDst, GraphicSetItem.Width,
884 GraphicSetItem.Height, GraphicSetItem.Left, GraphicSetItem.Top);
885end;
886
887procedure Sprite(Dst: TBitmap; HGr: TGraphicSet; xDst, yDst, Width, Height, xGr, yGr: Integer);
888begin
889 Sprite(Dst.Canvas, HGr, xDst, yDst, Width, Height, xGr, yGr);
890end;
891
892function BitBltCanvas(DestCanvas: TCanvas; X, Y, Width, Height: Integer;
893 SrcCanvas: TCanvas; XSrc, YSrc: Integer; Rop: DWORD = SRCCOPY): Boolean;
894begin
895 {$IFDEF WINDOWS}
896 {$IFDEF DPI}
897 Result := BitBlt(DestCanvas.Handle, X, Y, Width, Height, SrcCanvas.Handle, XSrc, YSrc, Rop);
898 {$ELSE}
899 // LCLIntf.BitBlt is slower than direct Windows BitBlt
900 Result := Windows.BitBlt(DestCanvas.Handle, ScaleToNative(X), ScaleToNative(Y),
901 ScaleToNative(Width), ScaleToNative(Height), SrcCanvas.Handle,
902 ScaleToNative(XSrc), ScaleToNative(YSrc), Rop);
903 {$ENDIF}
904 {$ELSE}
905 Result := BitBlt(DestCanvas.Handle, X, Y, Width, Height, SrcCanvas.Handle, XSrc, YSrc, Rop);
906 {$ENDIF}
907end;
908
909function BitBltCanvas(Dest: TCanvas; DestRect: TRect; Src: TCanvas;
910 SrcPos: TPoint; Rop: DWORD): Boolean;
911begin
912 Result := BitBltCanvas(Dest, DestRect.Left, DestRect.Top, DestRect.Width, DestRect.Height,
913 Src, SrcPos.X, SrcPos.Y, Rop);
914end;
915
916function BitBltBitmap(Dest: TBitmap; X, Y, Width, Height: Integer;
917 Src: TBitmap; XSrc, YSrc: Integer; Rop: DWORD): Boolean;
918begin
919 Result := BitBltCanvas(Dest.Canvas, X, Y, Width, Height, Src.Canvas, XSrc, YSrc, Rop);
920end;
921
922function BitBltBitmap(Dest: TBitmap; DestRect: TRect; Src: TBitmap;
923 SrcPos: TPoint; Rop: DWORD): Boolean;
924begin
925 Result := BitBltCanvas(Dest.Canvas, DestRect, Src.Canvas, SrcPos, Rop);
926end;
927
928procedure SLine(Canvas: TCanvas; x0, x1, Y: Integer; cl: TColor);
929begin
930 with Canvas do begin
931 Pen.Color := cl;
932 MoveTo(x0, Y);
933 LineTo(x1 + 1, Y);
934 end;
935end;
936
937procedure DLine(Canvas: TCanvas; x0, x1, Y: Integer; cl0, cl1: TColor);
938begin
939 with Canvas do begin
940 Pen.Color := cl0;
941 MoveTo(x0, Y);
942 LineTo(x1, Y);
943 Pen.Color := cl1;
944 MoveTo(x0 + 1, Y + 1);
945 LineTo(x1 + 1, Y + 1);
946 Pixels[x0, Y + 1] := cl0;
947 Pixels[x1, Y] := cl1;
948 end;
949end;
950
951procedure Frame(Canvas: TCanvas; x0, y0, x1, y1: Integer; cl0, cl1: TColor);
952begin
953 with Canvas do begin
954 MoveTo(x0, y1);
955 Pen.Color := cl0;
956 LineTo(x0, y0);
957 LineTo(x1, y0);
958 Pen.Color := cl1;
959 LineTo(x1, y1);
960 LineTo(x0, y1);
961 end;
962end;
963
964procedure RFrame(Canvas: TCanvas; x0, y0, x1, y1: Integer; cl0, cl1: TColor);
965begin
966 with Canvas do begin
967 Pen.Color := cl0;
968 MoveTo(x0, y0 + 1);
969 LineTo(x0, y1);
970 MoveTo(x0 + 1, y0);
971 LineTo(x1, y0);
972 Pen.Color := cl1;
973 MoveTo(x1, y0 + 1);
974 LineTo(x1, y1);
975 MoveTo(x0 + 1, y1);
976 LineTo(x1, y1);
977 end;
978end;
979
980procedure CFrame(Canvas: TCanvas; x0, y0, x1, y1, Corner: Integer; cl: TColor);
981begin
982 with Canvas do begin
983 Pen.Color := cl;
984 MoveTo(x0, y0 + Corner - 1);
985 LineTo(x0, y0);
986 LineTo(x0 + Corner, y0);
987 MoveTo(x1, y0 + Corner - 1);
988 LineTo(x1, y0);
989 LineTo(x1 - Corner, y0);
990 MoveTo(x1, y1 - Corner + 1);
991 LineTo(x1, y1);
992 LineTo(x1 - Corner, y1);
993 MoveTo(x0, y1 - Corner + 1);
994 LineTo(x0, y1);
995 LineTo(x0 + Corner, y1);
996 end;
997end;
998
999procedure FrameImage(Canvas: TCanvas; Src: TBitmap;
1000 X, Y, Width, Height, xSrc, ySrc: Integer; IsControl: Boolean = False);
1001begin
1002 if IsControl then begin
1003 Frame(Canvas, X - 1, Y - 1, X + Width, Y + Height, $B0B0B0, $FFFFFF);
1004 RFrame(Canvas, X - 2, Y - 2, X + Width + 1, Y + Height + 1, $FFFFFF, $B0B0B0);
1005 end else
1006 Frame(Canvas, X - 1, Y - 1, X + Width, Y + Height, $000000, $000000);
1007 BitBltCanvas(Canvas, X, Y, Width, Height, Src.Canvas, xSrc, ySrc);
1008end;
1009
1010procedure GlowFrame(Dst: TBitmap; x0, y0, Width, Height: Integer; cl: TColor);
1011var
1012 X, Y, ch, R: Integer;
1013 DstPtr: TPixelPointer;
1014 DpiGlowRange: Integer;
1015begin
1016 DpiGlowRange := ScaleToNative(GlowRange);
1017 X0 := ScaleToNative(X0);
1018 Y0 := ScaleToNative(Y0);
1019 Width := ScaleToNative(Width);
1020 Height := ScaleToNative(Height);
1021 Dst.BeginUpdate;
1022 DstPtr := TPixelPointer.Create(Dst, x0 - DpiGlowRange + 1, y0 - DpiGlowRange + 1);
1023 for Y := -DpiGlowRange + 1 to Height - 1 + DpiGlowRange - 1 do begin
1024 for X := -DpiGlowRange + 1 to Width - 1 + DpiGlowRange - 1 do begin
1025 if X < 0 then
1026 if Y < 0 then
1027 R := Round(Sqrt(Sqr(X) + Sqr(Y)))
1028 else if Y >= Height then
1029 R := Round(Sqrt(Sqr(X) + Sqr(Y - (Height - 1))))
1030 else
1031 R := -X
1032 else if X >= Width then
1033 if Y < 0 then
1034 R := Round(sqrt(Sqr(X - (Width - 1)) + Sqr(Y)))
1035 else if Y >= Height then
1036 R := Round(Sqrt(Sqr(X - (Width - 1)) + Sqr(Y - (Height - 1))))
1037 else
1038 R := X - (Width - 1)
1039 else if Y < 0 then
1040 R := -Y
1041 else if Y >= Height then
1042 R := Y - (Height - 1)
1043 else begin
1044 DstPtr.NextPixel;
1045 Continue;
1046 end;
1047 if R = 0 then
1048 R := 1;
1049 if R < DpiGlowRange then
1050 for ch := 0 to 2 do
1051 DstPtr.PixelPlane[2 - ch] :=
1052 (DstPtr.PixelPlane[2 - ch] * (R - 1) + (cl shr (8 * ch) and $FF) *
1053 (DpiGlowRange - R)) div (DpiGlowRange - 1);
1054 DstPtr.NextPixel;
1055 end;
1056 DstPtr.NextLine;
1057 end;
1058 Dst.EndUpdate;
1059end;
1060
1061procedure InitOrnament;
1062var
1063 P: TColor;
1064 X, Y: Integer;
1065 Light, Shade: TColor32;
1066 PixelPtr: TPixelPointer;
1067begin
1068 if InitOrnamentDone then Exit;
1069 Light := ColorToColor32(MainTexture.ColorBevelLight);
1070 // and $FCFCFC shr 2*3+MainTexture.ColorBevelShade and $FCFCFC shr 2;
1071 Shade := ColorToColor32(MainTexture.ColorBevelShade and $FCFCFC shr 2 * 3 +
1072 MainTexture.ColorBevelLight and $FCFCFC shr 2);
1073 HGrSystem2.Data.BeginUpdate;
1074 PixelPtr := TPixelPointer.Create(HGrSystem2.Data, ScaleToNative(Ornament.Left),
1075 ScaleToNative(Ornament.Top));
1076 if PixelPtr.BytesPerPixel = 3 then begin
1077 for Y := 0 to ScaleToNative(Ornament.Height) - 1 do begin
1078 for X := 0 to ScaleToNative(Ornament.Width) - 1 do begin
1079 P := Color32ToColor(PixelPtr.PixelRGB);
1080 if P = $0000FF then PixelPtr.PixelRGB := Light
1081 else if P = $FF0000 then PixelPtr.PixelRGB := Shade;
1082 PixelPtr.NextPixel;
1083 end;
1084 PixelPtr.NextLine;
1085 end;
1086 end else begin
1087 for Y := 0 to ScaleToNative(Ornament.Height) - 1 do begin
1088 for X := 0 to ScaleToNative(Ornament.Width) - 1 do begin
1089 P := Color32ToColor(PixelPtr.PixelARGB);
1090 if P = $0000FF then PixelPtr.PixelARGB := Light
1091 else if P = $FF0000 then PixelPtr.PixelARGB := Shade;
1092 PixelPtr.NextPixel;
1093 end;
1094 PixelPtr.NextLine;
1095 end;
1096 end;
1097 InitOrnamentDone := True;
1098 HGrSystem2.Data.EndUpdate;
1099end;
1100
1101procedure InitCityMark(Texture: TTexture);
1102var
1103 X: Integer;
1104 Y: Integer;
1105 Intensity: Integer;
1106begin
1107 for X := 0 to CityMark1.Width - 1 do begin
1108 for Y := 0 to CityMark1.Height - 1 do begin
1109 if HGrSystem.Mask.Canvas.Pixels[CityMark1.Left + X, CityMark1.Top + Y] = 0 then
1110 begin
1111 Intensity := HGrSystem.Data.Canvas.Pixels[CityMark1.Left +
1112 X, CityMark1.Top + Y] and $FF;
1113 HGrSystem.Data.Canvas.Pixels[CityMark2.Left + X, CityMark2.Top + Y] :=
1114 Texture.ColorMark and $FF * Intensity div $FF + Texture.ColorMark shr 8 and
1115 $FF * Intensity div $FF shl 8 + Texture.ColorMark shr 16 and
1116 $FF * Intensity div $FF shl 16;
1117 end;
1118 end;
1119 end;
1120 BitBltBitmap(HGrSystem.Mask, CityMark2.Left, CityMark2.Top, CityMark1.Width,
1121 CityMark1.Width, HGrSystem.Mask, CityMark1.Left, CityMark1.Top);
1122end;
1123
1124procedure Fill(Canvas: TCanvas; Left, Top, Width, Height, xOffset, yOffset: Integer);
1125begin
1126 //Assert((Left + xOffset >= 0) and (Left + xOffset + Width <= MainTexture.Width) and
1127 // (Top + yOffset >= 0) and (Top + yOffset + Height <= MainTexture.Height));
1128 BitBltCanvas(Canvas, Left, Top, Width, Height, MainTexture.Image.Canvas,
1129 Left + xOffset, Top + yOffset);
1130end;
1131
1132procedure Fill(Canvas: TCanvas; Rect: TRect; Offset: TPoint);
1133begin
1134 Fill(Canvas, Rect.Left, Rect.Top, Rect.Width, Rect.Height, Offset.X, Offset.Y);
1135end;
1136
1137procedure FillLarge(Canvas: TCanvas; x0, y0, x1, y1, xm: Integer);
1138
1139 function Band(I: Integer): Integer;
1140 var
1141 N: Integer;
1142 begin
1143 N := ((MainTexture.Height div 2) div (y1 - y0)) * 2;
1144 while MainTexture.Height div 2 + (I + 1) * (y1 - y0) > MainTexture.Height do
1145 Dec(I, N);
1146 while MainTexture.Height div 2 + I * (y1 - y0) < 0 do
1147 Inc(I, N);
1148 Result := I;
1149 end;
1150
1151var
1152 I: Integer;
1153begin
1154 for I := 0 to (x1 - xm) div MainTexture.Width - 1 do
1155 BitBltCanvas(Canvas, xm + I * MainTexture.Width, y0, MainTexture.Width, y1 - y0,
1156 MainTexture.Image.Canvas, 0, MainTexture.Height div 2 + Band(I) *
1157 (y1 - y0));
1158 BitBltCanvas(Canvas, xm + ((x1 - xm) div MainTexture.Width) * MainTexture.Width, y0,
1159 x1 - (xm + ((x1 - xm) div MainTexture.Width) * MainTexture.Width), y1 - y0,
1160 MainTexture.Image.Canvas, 0, MainTexture.Height div 2 + Band(
1161 (x1 - xm) div MainTexture.Width) * (y1 - y0));
1162 for I := 0 to (xm - x0) div MainTexture.Width - 1 do
1163 BitBltCanvas(Canvas, xm - (I + 1) * MainTexture.Width, y0, MainTexture.Width, y1 - y0,
1164 MainTexture.Image.Canvas, 0, MainTexture.Height div 2 +
1165 Band(-I - 1) * (y1 - y0));
1166 BitBltCanvas(Canvas, x0, y0, xm - ((xm - x0) div MainTexture.Width) *
1167 MainTexture.Width - x0, y1 - y0, MainTexture.Image.Canvas,
1168 ((xm - x0) div MainTexture.Width + 1) * MainTexture.Width - (xm - x0),
1169 MainTexture.Height div 2 + Band(-(xm - x0) div MainTexture.Width - 1) * (y1 - y0));
1170end;
1171
1172procedure FillSeamless(Canvas: TCanvas; Left, Top, Width, Height, xOffset, yOffset: Integer;
1173 const Texture: TBitmap);
1174var
1175 X, Y, x0cut, y0cut, x1cut, y1cut: Integer;
1176begin
1177 while xOffset < 0 do
1178 Inc(xOffset, Texture.Width);
1179 while yOffset < 0 do
1180 Inc(yOffset, Texture.Height);
1181 for Y := (Top + yOffset) div Texture.Height to (Top + yOffset + Height - 1) div
1182 Texture.Height do
1183 begin
1184 y0cut := Top + yOffset - Y * Texture.Height;
1185 if y0cut < 0 then
1186 y0cut := 0;
1187 y1cut := (Y + 1) * Texture.Height - (Top + yOffset + Height);
1188 if y1cut < 0 then
1189 y1cut := 0;
1190 for X := (Left + xOffset) div Texture.Width to (Left + xOffset + Width - 1) div
1191 Texture.Width do
1192 begin
1193 x0cut := Left + xOffset - X * Texture.Width;
1194 if x0cut < 0 then
1195 x0cut := 0;
1196 x1cut := (X + 1) * Texture.Width - (Left + xOffset + Width);
1197 if x1cut < 0 then
1198 x1cut := 0;
1199 BitBltCanvas(Canvas, X * Texture.Width + x0cut - xOffset,
1200 Y * Texture.Height + y0cut - yOffset, Texture.Width - x0cut - x1cut,
1201 Texture.Height - y0cut - y1cut, Texture.Canvas, x0cut, y0cut);
1202 end;
1203 end;
1204end;
1205
1206procedure FillRectSeamless(Canvas: TCanvas; x0, y0, x1, y1, xOffset, yOffset: Integer;
1207 const Texture: TBitmap);
1208begin
1209 FillSeamless(Canvas, x0, y0, x1 - x0, y1 - y0, xOffset, yOffset, Texture);
1210end;
1211
1212procedure PaintBackground(Canvas: TCanvas; Left, Top, Width, Height, FormWidth,
1213 FormHeight: Integer);
1214var
1215 X, Y: Integer;
1216 XX, YY: Integer;
1217begin
1218 if MainTexture.Height > 0 then YY := Trunc(Height / MainTexture.Height)
1219 else YY := 0;
1220 if MainTexture.Width > 0 then XX := Trunc(Width / MainTexture.Width)
1221 else XX := 0;
1222 for Y := 0 to YY do
1223 for X := 0 to XX do
1224 begin
1225 Fill(Canvas, Left + X * MainTexture.Width, Top + Y * MainTexture.Height, Width, Height,
1226 -(Left + X * MainTexture.Width), -(Top + Y * MainTexture.Height));
1227 end;
1228end;
1229
1230procedure Corner(Canvas: TCanvas; X, Y, Kind: Integer; T: TTexture);
1231begin
1232 { BitBltCanvas(Canvas, x, y, 8, 8, T.HGr.Mask.Canvas,
1233 T.xGr + 29 + Kind * 9, T.yGr + 89, SRCAND);
1234 BitBltCanvas(Canvas, X, Y, 8, 8, T.HGr.Data.Canvas,
1235 T.xGr + 29 + Kind * 9, T.yGr + 89, SRCPAINT); }
1236end;
1237
1238procedure BiColorTextOut(Canvas: TCanvas; clMain, clBack: TColor; X, Y: Integer; S: string);
1239
1240 procedure PaintIcon(X, Y, Kind: Integer);
1241 begin
1242 Sprite(Canvas, HGrSystem, X, Y + 6, 10, 10,
1243 66 + Kind mod 11 * 11, 115 + Kind div 11 * 11);
1244 end;
1245
1246var
1247 P, xp: Integer;
1248 sp: string;
1249 Shadow: Boolean;
1250 Text: string;
1251begin
1252 Inc(X);
1253 Inc(Y);
1254 for Shadow := True downto False do
1255 with Canvas do
1256 if not Shadow or (clBack <> $7F007F) then
1257 begin
1258 if Shadow then
1259 Font.Color := clBack
1260 else
1261 Font.Color := clMain;
1262 sp := S;
1263 xp := X;
1264 repeat
1265 P := Pos('%', sp);
1266 if (P = 0) or (P + 1 > Length(sp)) or not
1267 (sp[P + 1] in ['c', 'f', 'l', 'm', 'n', 'o', 'p', 'r', 't', 'w']) then
1268 begin
1269 Canvas.TextOut(xp, Y, sp);
1270 Break;
1271 end
1272 else
1273 begin
1274 Text := Copy(sp, 1, P - 1);
1275 TextOut(xp, Y, Text);
1276 Inc(xp, Canvas.TextWidth(Text));
1277 if not Shadow then
1278 case sp[P + 1] of
1279 'c': PaintIcon(xp + 1, Y, 6);
1280 'f': PaintIcon(xp + 1, Y, 0);
1281 'l': PaintIcon(xp + 1, Y, 8);
1282 'm': PaintIcon(xp + 1, Y, 17);
1283 'n': PaintIcon(xp + 1, Y, 7);
1284 'o': PaintIcon(xp + 1, Y, 16);
1285 'p': PaintIcon(xp + 1, Y, 2);
1286 'r': PaintIcon(xp + 1, Y, 12);
1287 't': PaintIcon(xp + 1, Y, 4);
1288 'w': PaintIcon(xp + 1, Y, 13);
1289 end;
1290 Inc(xp, 10);
1291 Delete(sp, 1, P + 1);
1292 end;
1293 until False;
1294 Dec(X);
1295 Dec(Y);
1296 end;
1297end;
1298
1299function BiColorTextWidth(Canvas: TCanvas; S: string): Integer;
1300var
1301 P: Integer;
1302begin
1303 Result := 1;
1304 repeat
1305 P := Pos('%', S);
1306 if (P = 0) or (P = Length(S)) then
1307 begin
1308 Inc(Result, Canvas.TextWidth(S));
1309 Break;
1310 end
1311 else
1312 begin
1313 if not (S[P + 1] in ['c', 'f', 'l', 'm', 'n', 'o', 'p', 'r', 't', 'w'])
1314 then
1315 Inc(Result, Canvas.TextWidth(Copy(S, 1, P + 1)))
1316 else
1317 Inc(Result, Canvas.TextWidth(Copy(S, 1, P - 1)) + 10);
1318 Delete(S, 1, P + 1);
1319 end;
1320 until False;
1321end;
1322
1323procedure LoweredTextOut(Canvas: TCanvas; cl: TColor; T: TTexture;
1324 X, Y: Integer; S: string);
1325begin
1326 if cl = -2 then
1327 BiColorTextOut(Canvas, (T.ColorBevelShade and $FEFEFE) shr 1,
1328 T.ColorBevelLight, X, Y, S)
1329 else if cl < 0 then
1330 BiColorTextOut(Canvas, T.ColorTextShade, T.ColorTextLight, X, Y, S)
1331 else
1332 BiColorTextOut(Canvas, cl, T.ColorTextLight, X, Y, S);
1333end;
1334
1335procedure RisedTextOut(Canvas: TCanvas; X, Y: Integer; S: string);
1336begin
1337 BiColorTextOut(Canvas, $FFFFFF, $000000, X, Y, S);
1338end;
1339
1340procedure Gradient(Canvas: TCanvas; X, Y, dx, dy, Width, Height, Color: Integer;
1341 Brightness: array of Integer);
1342var
1343 I, R, G, B: Integer;
1344begin
1345 for I := 0 to Length(Brightness) - 1 do begin // gradient
1346 R := Color and $FF + Brightness[I];
1347 if R < 0 then
1348 R := 0
1349 else if R >= 256 then
1350 R := 255;
1351 G := Color shr 8 and $FF + Brightness[I];
1352 if G < 0 then
1353 G := 0
1354 else if G >= 256 then
1355 G := 255;
1356 B := Color shr 16 and $FF + Brightness[I];
1357 if B < 0 then
1358 B := 0
1359 else if B >= 256 then
1360 B := 255;
1361 Canvas.Pen.Color := R + G shl 8 + B shl 16;
1362 Canvas.MoveTo(X + dx * I, Y + dy * I);
1363 Canvas.LineTo(X + dx * I + Width, Y + dy * I + Height);
1364 end;
1365 Canvas.Pen.Color := $000000;
1366 Canvas.MoveTo(X + 1, Y + 16 * dy + Height);
1367 Canvas.LineTo(X + 16 * dx + Width, Y + 16 * dy + Height);
1368 Canvas.LineTo(X + 16 * dx + Width, Y);
1369end;
1370
1371procedure LightGradient(Canvas: TCanvas; X, Y, Width, Color: Integer);
1372const
1373 Brightness: array [0 .. 15] of Integer =
1374 (16, 12, 8, 4, 0, -4, -8, -12, -16, -20, -24, -28, -32, -36, -40, -44);
1375begin
1376 Gradient(Canvas, X, Y, 0, 1, Width, 0, Color, Brightness);
1377end;
1378
1379procedure DarkGradient(Canvas: TCanvas; X, Y, Width, Kind: Integer);
1380const
1381 Brightness: array [0 .. 15] of Integer =
1382 (16, 12, 8, 4, 0, -4, -8, -12 - 24, -16 + 16, -20, -24, -28, -32, -36, -40, -44);
1383begin
1384 Gradient(Canvas, X, Y, 0, 1, Width, 0, HGrSystem.Data.Canvas.Pixels
1385 [187, 137 + Kind], Brightness);
1386end;
1387
1388procedure VLightGradient(Canvas: TCanvas; X, Y, Height, Color: Integer);
1389const
1390 Brightness: array [0 .. 15] of Integer =
1391 (16, 12, 8, 4, 0, -4, -8, -12, -16, -20, -24, -28, -32, -36, -40, -44);
1392begin
1393 Gradient(Canvas, X, Y, 1, 0, 0, Height, Color, Brightness);
1394end;
1395
1396procedure VDarkGradient(Canvas: TCanvas; X, Y, Height, Kind: Integer);
1397const
1398 Brightness: array [0 .. 15] of Integer =
1399 (16, 12, 8, 4, 0, -4, -8, -12 - 24, -16 + 16, -20, -24, -28, -32, -36, -40, -44);
1400begin
1401 Gradient(Canvas, X, Y, 1, 0, 0, Height,
1402 HGrSystem.Data.Canvas.Pixels[187, 137 + Kind], Brightness);
1403end;
1404
1405procedure UnderlinedTitleValue(Canvas: TCanvas; Title, Value: string; X, Y, Width: Integer);
1406begin
1407 DLine(Canvas, X, X + Width, Y + 19, MainTexture.ColorBevelLight, MainTexture.ColorBevelShade);
1408 RisedTextOut(Canvas, X, Y, Title);
1409 RisedTextOut(Canvas, X + Width - BiColorTextWidth(Canvas, Value), Y, Value);
1410end;
1411
1412procedure NumberBar(Dst: TBitmap; X, Y: Integer; Cap: string;
1413 Val: Integer; T: TTexture);
1414var
1415 S: string;
1416begin
1417 if Val > 0 then
1418 begin
1419 DLine(Dst.Canvas, X - 2, X + 170, Y + 16, T.ColorBevelShade,
1420 T.ColorBevelLight);
1421 LoweredTextOut(Dst.Canvas, -1, T, X - 2, Y, Cap);
1422 S := IntToStr(Val);
1423 RisedTextOut(Dst.Canvas, X + 170 - BiColorTextWidth(Dst.Canvas,
1424 S), Y, S);
1425 end;
1426end;
1427
1428procedure CountBar(Dst: TBitmap; X, Y, W: Integer; Kind: Integer;
1429 Cap: string; Val: Integer; T: TTexture);
1430var
1431 I, sd, ld, cl, xIcon, yIcon: Integer;
1432 S: string;
1433begin
1434 // Val := Random(40); //!!!
1435 if Val = 0 then
1436 Exit;
1437 Assert(Kind >= 0);
1438 with Dst.Canvas do
1439 begin
1440 // xIcon:=x+100;
1441 // yIcon:=y;
1442 // DLine(Dst.Canvas,x-2,x+170+32,y+16,T.ColorBevelShade,T.ColorBevelLight);
1443
1444 xIcon := X - 5;
1445 yIcon := Y + 15;
1446 DLine(Dst.Canvas, X - 2, xIcon + W + 2, yIcon + 16, T.ColorBevelShade,
1447 T.ColorBevelLight);
1448
1449 S := IntToStr(Val);
1450 if Val < 0 then
1451 cl := $0000FF
1452 else
1453 cl := -1;
1454 LoweredTextOut(Dst.Canvas, cl, T, X - 2, Y, Cap);
1455 LoweredTextOut(Dst.Canvas, cl, T,
1456 xIcon + W + 2 - BiColorTextWidth(Dst.Canvas, S), yIcon, S);
1457
1458 if (Kind = 12) and (Val >= 100) then
1459 begin // science with symbol for 100
1460 Val := Val div 10;
1461 sd := 14 * (Val div 10 + Val mod 10 - 1);
1462 if sd = 0 then
1463 sd := 1;
1464 if sd < W - 44 then
1465 ld := sd
1466 else
1467 ld := W - 44;
1468 for I := 0 to Val mod 10 - 1 do
1469 begin
1470 BitBltBitmap(Dst, xIcon + 4 + I * (14 * ld div sd), yIcon + 2 + 1, 14,
1471 14, HGrSystem.Mask, 67 + Kind mod 8 * 15,
1472 70 + Kind div 8 * 15, SRCAND);
1473 Sprite(Dst, HGrSystem, xIcon + 3 + I * (14 * ld div sd), yIcon + 2,
1474 14, 14, 67 + Kind mod 8 * 15, 70 + Kind div 8 * 15);
1475 end;
1476 for I := 0 to Val div 10 - 1 do
1477 begin
1478 BitBltBitmap(Dst, xIcon + 4 + (Val mod 10) *
1479 (14 * ld div sd) + I * (14 * ld div sd), yIcon + 3, 14, 14,
1480 HGrSystem.Mask, 67 + 7 mod 8 * 15,
1481 70 + 7 div 8 * 15, SRCAND);
1482 Sprite(Dst, HGrSystem, xIcon + 3 + (Val mod 10) *
1483 (14 * ld div sd) + I * (14 * ld div sd), yIcon + 2, 14,
1484 14, 67 + 7 mod 8 * 15,
1485 70 + 7 div 8 * 15);
1486 end;
1487 end
1488 else
1489 begin
1490 Val := Abs(Val);
1491 if Val mod 10 = 0 then
1492 sd := 14 * (Val div 10 - 1)
1493 else
1494 sd := 10 * (Val mod 10 - 1) + 14 * (Val div 10);
1495 if sd = 0 then
1496 sd := 1;
1497 if sd < W - 44 then
1498 ld := sd
1499 else
1500 ld := W - 44;
1501 for I := 0 to Val div 10 - 1 do
1502 begin
1503 BitBltBitmap(Dst, xIcon + 4 + I * (14 * ld div sd), yIcon + 3, 14, 14,
1504 HGrSystem.Mask, 67 + Kind mod 8 * 15,
1505 70 + Kind div 8 * 15, SRCAND);
1506 Sprite(Dst, HGrSystem, xIcon + 3 + I * (14 * ld div sd), yIcon + 2,
1507 14, 14, 67 + Kind mod 8 * 15, 70 + Kind div 8 * 15);
1508 end;
1509 for I := 0 to Val mod 10 - 1 do
1510 begin
1511 BitBltBitmap(Dst, xIcon + 4 + (Val div 10) *
1512 (14 * ld div sd) + I * (10 * ld div sd), yIcon + 7, 10, 10,
1513 HGrSystem.Mask, 66 + Kind mod 11 * 11,
1514 115 + Kind div 11 * 11, SRCAND);
1515 Sprite(Dst, HGrSystem, xIcon + 3 + (Val div 10) *
1516 (14 * ld div sd) + I * (10 * ld div sd), yIcon + 6, 10,
1517 10, 66 + Kind mod 11 * 11,
1518 115 + Kind div 11 * 11);
1519 end;
1520 end;
1521 end;
1522end;
1523
1524procedure PaintProgressBar(Canvas: TCanvas; Kind, X, Y, Pos, Growth, Max: Integer;
1525 T: TTexture);
1526var
1527 I: Integer;
1528begin
1529 if Pos > Max then
1530 Pos := Max;
1531 if Growth < 0 then
1532 begin
1533 Pos := Pos + Growth;
1534 if Pos < 0 then
1535 begin
1536 Growth := Growth - Pos;
1537 Pos := 0;
1538 end;
1539 end
1540 else if Pos + Growth > Max then
1541 Growth := Max - Pos;
1542 Frame(Canvas, X - 1, Y - 1, X + Max, Y + 7, $000000, $000000);
1543 RFrame(Canvas, X - 2, Y - 2, X + Max + 1, Y + 8, T.ColorBevelShade,
1544 T.ColorBevelLight);
1545 with Canvas do
1546 begin
1547 for I := 0 to Pos div 8 - 1 do
1548 BitBltCanvas(Canvas, X + I * 8, Y, 8, 7,
1549 HGrSystem.Data.Canvas, 104, 9 + 8 * Kind);
1550 BitBltCanvas(Canvas, X + 8 * (Pos div 8), Y, Pos - 8 * (Pos div 8), 7,
1551 HGrSystem.Data.Canvas, 104, 9 + 8 * Kind);
1552 if Growth > 0 then
1553 begin
1554 for I := 0 to Growth div 8 - 1 do
1555 BitBltCanvas(Canvas, X + Pos + I * 8, Y, 8, 7,
1556 HGrSystem.Data.Canvas, 112, 9 + 8 * Kind);
1557 BitBltCanvas(Canvas, X + Pos + 8 * (Growth div 8), Y,
1558 Growth - 8 * (Growth div 8), 7, HGrSystem.Data.Canvas,
1559 112, 9 + 8 * Kind);
1560 end
1561 else if Growth < 0 then
1562 begin
1563 for I := 0 to -Growth div 8 - 1 do
1564 BitBltCanvas(Canvas, X + Pos + I * 8, Y, 8, 7,
1565 HGrSystem.Data.Canvas, 104, 1);
1566 BitBltCanvas(Canvas, X + Pos + 8 * (-Growth div 8), Y, -Growth -
1567 8 * (-Growth div 8), 7,
1568 HGrSystem.Data.Canvas, 104, 1);
1569 end;
1570 Brush.Color := $000000;
1571 FillRect(Rect(X + Pos + Abs(Growth), Y, X + Max, Y + 7));
1572 Brush.Style := TBrushStyle.bsClear;
1573 end;
1574end;
1575
1576// pos and growth are relative to max, set size independent
1577procedure PaintRelativeProgressBar(Canvas: TCanvas;
1578 Kind, X, Y, Size, Pos, Growth, Max: Integer; IndicateComplete: Boolean;
1579 T: TTexture);
1580begin
1581 if Growth > 0 then
1582 PaintProgressBar(Canvas, Kind, X, Y, Pos * Size div Max,
1583 (Growth * Size + Max div 2) div Max, Size, T)
1584 else
1585 PaintProgressBar(Canvas, Kind, X, Y, Pos * Size div Max,
1586 (Growth * Size - Max div 2) div Max, Size, T);
1587 if IndicateComplete and (Pos + Growth >= Max) then
1588 Sprite(Canvas, HGrSystem, X + Size - 10, Y - 7, 23, 16, 1, 129);
1589end;
1590
1591procedure PaintLogo(Canvas: TCanvas; X, Y, LightColor, ShadeColor: Integer);
1592begin
1593 if not Assigned(DrawBuffer) then Exit;
1594 DrawBufferEnsureSize(Logo.Width, Logo.Height);
1595 UnshareBitmap(DrawBuffer);
1596 BitBltCanvas(DrawBuffer.Canvas, 0, 0, Logo.Width, Logo.Height, Canvas, X, Y);
1597 ImageOp_BCC(DrawBuffer, Templates.Data, Point(0, 0), Logo.BoundsRect,
1598 LightColor, ShadeColor);
1599 BitBltCanvas(Canvas, X, Y, Logo.Width, Logo.Height, DrawBuffer.Canvas, 0, 0);
1600end;
1601
1602procedure DrawBufferEnsureSize(Width, Height: Integer);
1603begin
1604 if (DrawBuffer.Width >= Width) and (DrawBuffer.Height >= Height) then Exit;
1605 if (DrawBuffer.Width < Width) and (DrawBuffer.Height < Height) then
1606 DrawBuffer.SetSize(Width, Height)
1607 else if DrawBuffer.Width < Width then DrawBuffer.Width := Width
1608 else if DrawBuffer.Height < Height then DrawBuffer.Height := Height;
1609 DrawBuffer.Canvas.FillRect(0, 0, DrawBuffer.Width, DrawBuffer.Height);
1610end;
1611
1612procedure LoadPhrases;
1613begin
1614 if Phrases = nil then Phrases := TStringTable.Create;
1615 if Phrases2 = nil then Phrases2 := TStringTable.Create;
1616 Phrases2FallenBackToEnglish := False;
1617 if FileExists(LocalizedFilePath('Language.txt')) then
1618 begin
1619 Phrases.LoadFromFile(LocalizedFilePath('Language.txt'));
1620 if FileExists(LocalizedFilePath('Language2.txt')) then
1621 Phrases2.LoadFromFile(LocalizedFilePath('Language2.txt'))
1622 else
1623 begin
1624 Phrases2.LoadFromFile(GetAppSharePath('Language2.txt'));
1625 Phrases2FallenBackToEnglish := True;
1626 end;
1627 end
1628 else
1629 begin
1630 Phrases.LoadFromFile(GetAppSharePath('Language.txt'));
1631 Phrases2.LoadFromFile(GetAppSharePath('Language2.txt'));
1632 end;
1633
1634 if Sounds = nil then Sounds := TStringTable.Create;
1635 if not Sounds.LoadFromFile(GetSoundsDir + DirectorySeparator + 'sound.txt') then
1636 begin
1637 FreeAndNil(Sounds);
1638 end;
1639end;
1640
1641procedure Texturize(Dest, Texture: TBitmap; TransparentColor: Cardinal);
1642var
1643 SrcPixel, DstPixel: TPixelPointer;
1644 X, Y: Integer;
1645 TexWidth, TexHeight: Integer;
1646begin
1647 // Texturize background
1648 Dest.BeginUpdate;
1649 TexWidth := Texture.Width;
1650 TexHeight := Texture.Height;
1651 DstPixel := TPixelPointer.Create(Dest);
1652 SrcPixel := TPixelPointer.Create(Texture);
1653 for Y := 0 to ScaleToNative(Dest.Height) - 1 do begin
1654 for X := 0 to ScaleToNative(Dest.Width) - 1 do begin
1655 if DstPixel.PixelRGB = TransparentColor then begin
1656 SrcPixel.SetXY(X mod TexWidth, Y mod TexHeight);
1657 DstPixel.PixelRGB := SrcPixel.PixelRGB;
1658 end;
1659 DstPixel.NextPixel;
1660 end;
1661 DstPixel.NextLine;
1662 end;
1663 Dest.EndUpdate;
1664end;
1665
1666procedure DarkenImage(Bitmap: TBitmap; Change: Integer);
1667var
1668 X, Y: Integer;
1669 PicturePixel: TPixelPointer;
1670begin
1671 Bitmap.BeginUpdate;
1672 PicturePixel := TPixelPointer.Create(Bitmap);
1673 for Y := 0 to ScaleToNative(Bitmap.Height) - 1 do begin
1674 for X := 0 to ScaleToNative(Bitmap.Width) - 1 do begin
1675 PicturePixel.PixelB := Max(PicturePixel.PixelB - Change, 0);
1676 PicturePixel.PixelG := Max(PicturePixel.PixelG - Change, 0);
1677 PicturePixel.PixelR := Max(PicturePixel.PixelR - Change, 0);
1678 PicturePixel.NextPixel;
1679 end;
1680 PicturePixel.NextLine;
1681 end;
1682 Bitmap.EndUpdate;
1683end;
1684
1685{$IFNDEF DPI}
1686function ScaleToNative(Value: Integer): Integer;
1687begin
1688 Result := Value;
1689end;
1690
1691function ScaleToNativeDist(Base, Value: Integer): Integer;
1692begin
1693 Result := Value;
1694end;
1695
1696function ScaleFromNative(Value: Integer): Integer;
1697begin
1698 Result := Value;
1699end;
1700
1701function BitBltBitmapPrecise(Dest: TBitmap; X, Y, Width, Height: Integer; Src: TBitmap;
1702 XSrc, YSrc: Integer; Rop: DWORD = SRCCOPY; Precise: Boolean = False): Boolean;
1703begin
1704 Result := BitBltBitmap(Dest, X, Y, Width, Height, Src, XSrc, YSrc, Rop);
1705end;
1706{$ENDIF}
1707
1708procedure UnshareBitmap(Bitmap: TBitmap);
1709begin
1710 // FillRect cause image data to be freed so subsequent BitBlt can access valid image data
1711 Bitmap.Canvas.FillRect(0, 0, 0, 0);
1712end;
1713
1714procedure Gtk2Fix;
1715{$IFDEF UNIX}
1716var
1717 I: Integer;
1718{$ENDIF}
1719begin
1720 {$IFDEF UNIX}
1721 // Wait and process messages little bit to avoid crash or force repaint under Gtk2
1722 for I := 0 to 10 do begin
1723 Sleep(1);
1724 Application.ProcessMessages;
1725 end;
1726 {$ENDIF}
1727end;
1728
1729procedure LoadFonts;
1730var
1731 Section: TFontType;
1732 FontScript: TextFile;
1733 Size: Integer;
1734 S: string;
1735 I: Integer;
1736 P: Integer;
1737begin
1738 Section := ftNormal;
1739 AssignFile(FontScript, LocalizedFilePath('Fonts.txt'));
1740 try
1741 Reset(FontScript);
1742 while not Eof(FontScript) do begin
1743 ReadLn(FontScript, S);
1744 if S <> '' then
1745 if S[1] = '#' then begin
1746 S := TrimRight(S);
1747 if S = '#SMALL' then Section := ftSmall
1748 else if S = '#TINY' then Section := ftTiny
1749 else if S = '#CAPTION' then Section := ftCaption
1750 else if S = '#BUTTON' then Section := ftButton
1751 else Section := ftNormal;
1752 end else begin
1753 P := Pos(',', S);
1754 if P > 0 then begin
1755 UniFont[section].Name := Trim(Copy(S, 1, P - 1));
1756 Size := 0;
1757 for I := P + 1 to Length(S) do
1758 case S[I] of
1759 '0' .. '9':
1760 Size := Size * 10 + Byte(S[I]) - 48;
1761 'B', 'b':
1762 UniFont[section].Style := UniFont[section].Style + [TFontStyle.fsBold];
1763 'I', 'i':
1764 UniFont[section].Style := UniFont[section].Style + [TFontStyle.fsItalic];
1765 end;
1766 UniFont[section].Size := Round(Size * ScaleToNative(72) / UniFont[section].PixelsPerInch);
1767 end;
1768 end;
1769 end;
1770 CloseFile(FontScript);
1771 except
1772 end;
1773end;
1774
1775procedure ReleaseFonts;
1776var
1777 Section: TFontType;
1778begin
1779 for Section := Low(TFontType) to High(TFontType) do
1780 FreeAndNil(UniFont[section]);
1781end;
1782
1783procedure InitGammaLookupTable;
1784var
1785 I: Integer;
1786 P: Integer;
1787begin
1788 GammaLookupTable[0] := 0;
1789 for I := 1 to 255 do begin
1790 P := Round(255.0 * Exp(Ln(I / 255.0) * 100.0 / Gamma));
1791 Assert((P >= 0) and (P < 256));
1792 GammaLookupTable[I] := P;
1793 end;
1794end;
1795
1796procedure Gtk2DisableControlStyling(WinControl: TWinControl);
1797begin
1798 // https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/38516
1799 {$IFDEF LCLGTK2}
1800 // parse gtkrc from string
1801 gtk_rc_parse_string(PChar('style "noengine" {' + LineEnding +
1802 'engine "" {}' + LineEnding +
1803 '}' + LineEnding +
1804 'widget "*.your-edit" style "noengine"'));
1805
1806 if WinControl.HandleAllocated then begin
1807 // set gtk name to our component
1808 gtk_widget_set_name({%H-}PGtkWidget(WinControl.Handle), 'your-edit');
1809 end;
1810 {$ENDIF}
1811end;
1812
1813procedure LoadConfig(Key: string);
1814var
1815 Reg: TRegistry;
1816begin
1817 Reg := TRegistry.Create;
1818 with Reg do try
1819 OpenKey(Key, True);
1820 if ValueExists('Gamma') then Gamma := ReadInteger('Gamma')
1821 else Gamma := 100;
1822 if Gamma <> 100 then InitGammaLookupTable;
1823 if ValueExists('CustomDpiEnabled') then CustomDpiEnabled := Reg.ReadBool('CustomDpiEnabled')
1824 else CustomDpiEnabled := False;
1825 if ValueExists('CustomDpi') then CustomDpi := Reg.ReadInteger('CustomDpi')
1826 else CustomDpi := 96;
1827 {$IFDEF DPI}
1828 if CustomDpiEnabled then Screen.Dpi := CustomDpi
1829 else Screen.Dpi := Screen.GetSystemDpi;
1830 {$ENDIF}
1831 finally
1832 Reg.Free;
1833 end;
1834end;
1835
1836procedure SaveConfig(Key: string);
1837var
1838 Reg: TRegistry;
1839begin
1840 Reg := TRegistry.Create;
1841 with Reg do try
1842 OpenKey(Key, True);
1843 WriteInteger('Gamma', Gamma);
1844 WriteBool('CustomDpiEnabled', CustomDpiEnabled);
1845 WriteInteger('CustomDpi', CustomDpi);
1846 finally
1847 Free;
1848 end;
1849end;
1850
1851procedure LoadAssets;
1852begin
1853 LoadPhrases;
1854 LoadFonts;
1855 Templates := LoadGraphicSet('Templates.png', False);
1856 with Templates do begin
1857 Logo := GetItem('Logo');
1858 BigBook := GetItem('BigBook');
1859 SmallBook := GetItem('SmallBook');
1860 MenuLogo := GetItem('MenuLogo');
1861 LinkArrows := GetItem('LinkArrows');
1862 ScienceNationDot := GetItem('ScienceNationDot');
1863 ResearchIcon := GetItem('Research');
1864 ChangeIcon := GetItem('Change');
1865 TreasuryIcon := GetItem('Treasury');
1866 StarshipDeparted := GetItem('StarshipDeparted');
1867 WeightOn := GetItem('WeightOn');
1868 WeightOff := GetItem('WeightOff');
1869 end;
1870
1871 LoadGraphicFile(Colors, GetGraphicsDir + DirectorySeparator + 'Colors.png');
1872 LoadGraphicFile(Paper, GetGraphicsDir + DirectorySeparator + 'Paper.jpg');
1873 LoadGraphicFile(BigImp, GetGraphicsDir + DirectorySeparator + 'Icons.png');
1874end;
1875
1876procedure UnitInit;
1877var
1878 Section: TFontType;
1879begin
1880 Gamma := 100;
1881 InitGammaLookupTable;
1882
1883 {$IFDEF WINDOWS}
1884 EnumDisplaySettings(nil, $FFFFFFFF, StartResolution);
1885 ResolutionChanged := False;
1886 {$ENDIF}
1887
1888 for Section := Low(TFontType) to High(TFontType) do
1889 UniFont[Section] := TFont.Create;
1890
1891 DrawBuffer := TBitmap.Create;
1892 DrawBuffer.PixelFormat := TPixelFormat.pf24bit;
1893
1894 GrExt := TGraphicSets.Create;
1895
1896 HGrSystem := LoadGraphicSet('System.png');
1897 with HGrSystem do begin
1898 CityMark1 := GetItem('CityMark1');
1899 CityMark2 := GetItem('CityMark2');
1900 end;
1901
1902 HGrSystem2 := LoadGraphicSet('System2.png');
1903 with HGrSystem2 do begin
1904 Ornament := GetItem('Ornament');
1905 GBrainNoTerm := GetItem('BrainNoTerm');
1906 GBrainSuperVirtual := GetItem('BrainSuperVirtual');
1907 GBrainTerm := GetItem('BrainTerm');
1908 GBrainRandom := GetItem('BrainRandom');
1909 end;
1910
1911 Colors := TBitmap.Create;
1912 Colors.PixelFormat := TPixelFormat.pf24bit;
1913 Paper := TBitmap.Create;
1914 Paper.PixelFormat := TPixelFormat.pf24bit;
1915 BigImp := TBitmap.Create;
1916 BigImp.PixelFormat := TPixelFormat.pf24bit;
1917 MainTexture := TTexture.Create;
1918 ClickFrameColor := HGrSystem.Data.Canvas.Pixels[187, 175];
1919 InitOrnamentDone := False;
1920 GenerateNames := True;
1921
1922 LoadAssets;
1923end;
1924
1925procedure UnitDone;
1926begin
1927 RestoreResolution;
1928 FreeAndNil(GrExt);
1929 ReleaseFonts;
1930 FreeAndNil(Phrases);
1931 FreeAndNil(Phrases2);
1932 FreeAndNil(DrawBuffer);
1933 FreeAndNil(BigImp);
1934 FreeAndNil(Paper);
1935 FreeAndNil(Colors);
1936 FreeAndNil(MainTexture);
1937end;
1938
1939end.
Note: See TracBrowser for help on using the repository browser.