| 1 |
{ |
| 2 |
Copyright 2013-2023 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Main state. } |
| 17 |
unit GameViewMain; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, |
| 22 |
CastleVectors, CastleUIControls, CastleControls, CastleKeysMouse, |
| 23 |
CastleGLImages, CastleRectangles; |
| 24 |
|
| 25 |
type |
| 26 |
TViewMain = class(TCastleView) |
| 27 |
published |
| 28 |
{ Components designed using CGE editor. |
| 29 |
These fields will be automatically initialized at Start. } |
| 30 |
MainImageControl: TCastleImageControl; |
| 31 |
LabelStatus: TCastleLabel; |
| 32 |
private |
| 33 |
Brushes: array [0..6] of TDrawableImage; |
| 34 |
procedure Draw(const Position: TVector2; const BrushIndex: TFingerIndex); |
| 35 |
public |
| 36 |
constructor Create(AOwner: TComponent); override; |
| 37 |
procedure Start; override; |
| 38 |
procedure Stop; override; |
| 39 |
procedure Resize; override; |
| 40 |
procedure Update(const SecondsPassed: Single; var HandleInput: boolean); override; |
| 41 |
function Press(const Event: TInputPressRelease): Boolean; override; |
| 42 |
function Motion(const Event: TInputMotion): Boolean; override; |
| 43 |
end; |
| 44 |
|
| 45 |
var |
| 46 |
ViewMain: TViewMain; |
| 47 |
|
| 48 |
implementation |
| 49 |
|
| 50 |
uses SysUtils, |
| 51 |
CastleUtils, CastleImages, CastleColors; |
| 52 |
|
| 53 |
constructor TViewMain.Create(AOwner: TComponent); |
| 54 |
begin |
| 55 |
inherited; |
| 56 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 57 |
end; |
| 58 |
|
| 59 |
procedure TViewMain.Start; |
| 60 |
|
| 61 |
function LoadBrush(const Name: string): TDrawableImage; |
| 62 |
begin |
| 63 |
Result := TDrawableImage.Create('castle-data:/brush_' + Name + '.png'); |
| 64 |
end; |
| 65 |
|
| 66 |
var |
| 67 |
NewImage: TRGBImage; |
| 68 |
NewDrawableImage: TDrawableImage; |
| 69 |
begin |
| 70 |
inherited; |
| 71 |
Brushes[0] := LoadBrush('red'); |
| 72 |
Brushes[1] := LoadBrush('green'); |
| 73 |
Brushes[2] := LoadBrush('blue'); |
| 74 |
Brushes[3] := LoadBrush('cross_red'); |
| 75 |
Brushes[4] := LoadBrush('cross_green'); |
| 76 |
Brushes[5] := LoadBrush('cross_blue'); |
| 77 |
|
| 78 |
{ Create new image to draw on } |
| 79 |
NewImage := TRGBImage.Create(Container.PixelsWidth, Container.PixelsHeight); |
| 80 |
NewImage.Clear(Black); |
| 81 |
NewDrawableImage := TDrawableImage.Create( |
| 82 |
NewImage, |
| 83 |
{ SmoothScaling } true, |
| 84 |
{ OwnsImage } true); |
| 85 |
MainImageControl.DrawableImage := NewDrawableImage; |
| 86 |
end; |
| 87 |
|
| 88 |
procedure TViewMain.Stop; |
| 89 |
var |
| 90 |
I: Integer; |
| 91 |
begin |
| 92 |
for I := Low(Brushes) to High(Brushes) do |
| 93 |
FreeAndNil(Brushes[I]); |
| 94 |
inherited; |
| 95 |
end; |
| 96 |
|
| 97 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: boolean); |
| 98 |
var |
| 99 |
S: String; |
| 100 |
I: Integer; |
| 101 |
begin |
| 102 |
inherited; |
| 103 |
|
| 104 |
{ Print text with FPS, Touches and other info useful for debugging } |
| 105 |
S := Format('Image: %d x %d. FPS: %s' + NL + 'Touches (%d)', [ |
| 106 |
MainImageControl.Image.Width, |
| 107 |
MainImageControl.Image.Height, |
| 108 |
Container.Fps.ToString, |
| 109 |
Container.TouchesCount |
| 110 |
]) + NL; |
| 111 |
|
| 112 |
for I := 0 to Container.TouchesCount - 1 do |
| 113 |
S := S + Format('Touch %d: FingerIndex %d, Position %s', [ |
| 114 |
I, |
| 115 |
Container.Touches[I].FingerIndex, |
| 116 |
Container.Touches[I].Position.ToString |
| 117 |
]); |
| 118 |
|
| 119 |
LabelStatus.Caption := S; |
| 120 |
end; |
| 121 |
|
| 122 |
procedure TViewMain.Resize; |
| 123 |
var |
| 124 |
NewImage: TRGBImage; |
| 125 |
NewDrawableImage: TDrawableImage; |
| 126 |
CommonRect: TFloatRectangle; |
| 127 |
begin |
| 128 |
inherited; |
| 129 |
|
| 130 |
{ When window is resized, create new TDrawableImage with new size |
| 131 |
and copy contens from previous image (as much as possible). } |
| 132 |
|
| 133 |
if (MainImageControl.DrawableImage = nil) or |
| 134 |
(MainImageControl.DrawableImage.Width <> Container.PixelsWidth) or |
| 135 |
(MainImageControl.DrawableImage.Height <> Container.PixelsHeight) then |
| 136 |
begin |
| 137 |
NewImage := TRGBImage.Create(Container.PixelsWidth, Container.PixelsHeight); |
| 138 |
NewImage.Clear(Black); |
| 139 |
|
| 140 |
NewDrawableImage := TDrawableImage.Create( |
| 141 |
NewImage, |
| 142 |
{ SmoothScaling } true, |
| 143 |
{ OwnsImage } true); |
| 144 |
{ Common part in both old and new areas } |
| 145 |
CommonRect := FloatRectangle(MainImageControl.DrawableImage.Rect * NewDrawableImage.Rect); |
| 146 |
NewDrawableImage.PrepareResources; // create OpenGL resources now, to enable DrawFrom |
| 147 |
NewDrawableImage.DrawFrom(MainImageControl.DrawableImage, CommonRect, CommonRect); |
| 148 |
MainImageControl.DrawableImage := NewDrawableImage; |
| 149 |
end; |
| 150 |
end; |
| 151 |
|
| 152 |
procedure TViewMain.Draw(const Position: TVector2; const BrushIndex: TFingerIndex); |
| 153 |
var |
| 154 |
Brush: TDrawableImage; |
| 155 |
X, Y: Integer; |
| 156 |
begin |
| 157 |
{ not enough brushes, this means your touch device supports > High(Brushes) + 1 |
| 158 |
simultaneous touches. } |
| 159 |
if BrushIndex > High(Brushes) then Exit; |
| 160 |
|
| 161 |
Brush := Brushes[BrushIndex]; |
| 162 |
X := Round(Position[0]) - Brush.Width div 2; |
| 163 |
Y := Round(Position[1]) - Brush.Height div 2; |
| 164 |
|
| 165 |
{ Old approach: drawing on CPU |
| 166 |
(assumes is a TRGBAlphaImage with PremultiplyAlpha done). } |
| 167 |
// Brush.DrawTo(ImageControl.Image, X, Y, dmAdd); |
| 168 |
// ImageControl.ImageChanged; |
| 169 |
|
| 170 |
{ New approach: fast image-to-image drawing on GPU. } |
| 171 |
MainImageControl.DrawableImage.DrawFrom(Brush, |
| 172 |
FloatRectangle(X, Y, Brush.Width, Brush.Height), |
| 173 |
FloatRectangle(Brush.Rect)); |
| 174 |
end; |
| 175 |
|
| 176 |
function TViewMain.Press(const Event: TInputPressRelease): Boolean; |
| 177 |
begin |
| 178 |
Result := inherited; |
| 179 |
if Result then Exit; |
| 180 |
|
| 181 |
if Event.EventType = itMouseButton then |
| 182 |
begin |
| 183 |
Draw(Event.Position, Event.FingerIndex); |
| 184 |
Exit(true); |
| 185 |
end; |
| 186 |
end; |
| 187 |
|
| 188 |
function TViewMain.Motion(const Event: TInputMotion): Boolean; |
| 189 |
begin |
| 190 |
Result := inherited; |
| 191 |
if Result then Exit; |
| 192 |
|
| 193 |
if Event.Pressed <> [] then |
| 194 |
begin |
| 195 |
Draw(Event.Position, Event.FingerIndex); |
| 196 |
Exit(true); |
| 197 |
end; |
| 198 |
end; |
| 199 |
|
| 200 |
end. |