root / Client / UfrmRadar.pas @ 0:95bd93c28625
History | View | Annotate | Download (7.5 kB)
| 1 | (*
|
|---|---|
| 2 | * CDDL HEADER START |
| 3 | * |
| 4 | * The contents of this file are subject to the terms of the |
| 5 | * Common Development and Distribution License, Version 1.0 only |
| 6 | * (the "License"). You may not use this file except in compliance |
| 7 | * with the License. |
| 8 | * |
| 9 | * You can obtain a copy of the license at |
| 10 | * http://www.opensource.org/licenses/cddl1.php. |
| 11 | * See the License for the specific language governing permissions |
| 12 | * and limitations under the License. |
| 13 | * |
| 14 | * When distributing Covered Code, include this CDDL HEADER in each |
| 15 | * file and include the License file at |
| 16 | * http://www.opensource.org/licenses/cddl1.php. If applicable, |
| 17 | * add the following below this CDDL HEADER, with the fields enclosed |
| 18 | * by brackets "[]" replaced with your own identifying * information: |
| 19 | * Portions Copyright [yyyy] [name of copyright owner] |
| 20 | * |
| 21 | * CDDL HEADER END |
| 22 | * |
| 23 | * |
| 24 | * Portions Copyright 2007 Andreas Schneider |
| 25 | *) |
| 26 | unit UfrmRadar;
|
| 27 | |
| 28 | {$mode objfpc}{$H+} |
| 29 | |
| 30 | interface
|
| 31 | |
| 32 | uses
|
| 33 | Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls, |
| 34 | ImagingClasses, ImagingComponents, ImagingTypes, UEnhancedMemoryStream, crc, |
| 35 | StdCtrls; |
| 36 | |
| 37 | type
|
| 38 | |
| 39 | TRadarColorMap = array of Word; |
| 40 | |
| 41 | { TfrmRadarMap }
|
| 42 | |
| 43 | TfrmRadarMap = class(TForm)
|
| 44 | lblPosition: TLabel; |
| 45 | pbRadar: TPaintBox; |
| 46 | pnlBottom: TPanel; |
| 47 | sbMain: TScrollBox; |
| 48 | procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); |
| 49 | procedure FormCreate(Sender: TObject);
|
| 50 | procedure FormDestroy(Sender: TObject);
|
| 51 | procedure FormResize(Sender: TObject);
|
| 52 | procedure pbRadarMouseDown(Sender: TObject; Button: TMouseButton;
|
| 53 | Shift: TShiftState; X, Y: Integer); |
| 54 | procedure pbRadarMouseLeave(Sender: TObject);
|
| 55 | procedure pbRadarMouseMove(Sender: TObject; Shift: TShiftState; X,
|
| 56 | Y: Integer); |
| 57 | procedure pbRadarPaint(Sender: TObject);
|
| 58 | protected
|
| 59 | FRadar: TSingleImage; |
| 60 | FRadarDependencies: TList; |
| 61 | procedure OnRadarHandlingPacket(ABuffer: TEnhancedMemoryStream);
|
| 62 | procedure RefreshRadar(ARadarMap: TRadarColorMap);
|
| 63 | procedure RepaintRadar;
|
| 64 | public
|
| 65 | property Radar: TSingleImage read FRadar; |
| 66 | property Dependencies: TList read FRadarDependencies; |
| 67 | end;
|
| 68 | |
| 69 | var
|
| 70 | frmRadarMap: TfrmRadarMap; |
| 71 | |
| 72 | implementation
|
| 73 | |
| 74 | uses
|
| 75 | UdmNetwork, UGameResources, UPacketHandlers, UPackets, UfrmInitialize, |
| 76 | UfrmMain, UGraphicHelper; |
| 77 | |
| 78 | { TfrmRadarMap }
|
| 79 | |
| 80 | procedure TfrmRadarMap.FormCreate(Sender: TObject);
|
| 81 | begin
|
| 82 | FRadar := TSingleImage.CreateFromParams(ResMan.Landscape.Width, |
| 83 | ResMan.Landscape.Height, ifA8R8G8B8); |
| 84 | pbRadar.Width := FRadar.Width; |
| 85 | pbRadar.Height := FRadar.Height; |
| 86 | sbMain.ClientWidth := FRadar.Width; |
| 87 | sbMain.ClientHeight := FRadar.Height; |
| 88 | ClientWidth := sbMain.Width + sbMain.VertScrollBar.Size; |
| 89 | ClientHeight := sbMain.Height + sbMain.HorzScrollBar.Size + pnlBottom.Height; |
| 90 | Constraints.MaxWidth := Width; |
| 91 | Constraints.MaxHeight := Height; |
| 92 | |
| 93 | FRadarDependencies := TList.Create; |
| 94 | |
| 95 | RegisterPacketHandler($0D, TPacketHandler.Create(0, @OnRadarHandlingPacket)); |
| 96 | |
| 97 | dmNetwork.Send(TRequestRadarChecksumPacket.Create); |
| 98 | end;
|
| 99 | |
| 100 | procedure TfrmRadarMap.FormClose(Sender: TObject; var CloseAction: TCloseAction); |
| 101 | begin
|
| 102 | CloseAction := caHide; |
| 103 | end;
|
| 104 | |
| 105 | procedure TfrmRadarMap.FormDestroy(Sender: TObject);
|
| 106 | var
|
| 107 | radarMap: TRadarColorMap; |
| 108 | x, y: Integer; |
| 109 | radarMapFile: TFileStream; |
| 110 | begin
|
| 111 | RegisterPacketHandler($0D, nil); |
| 112 | |
| 113 | SetLength(radarMap, FRadar.Width * FRadar.Height); |
| 114 | for x := 0 to FRadar.Width - 1 do |
| 115 | for y := 0 to FRadar.Height - 1 do |
| 116 | radarMap[x * FRadar.Height + y] := EncodeUOColor(PInteger(FRadar.PixelPointers[x, y])^); |
| 117 | |
| 118 | radarMapFile := TFileStream.Create(IncludeTrailingPathDelimiter( |
| 119 | ExtractFilePath(Application.ExeName)) + 'RadarMap.cache', fmCreate);
|
| 120 | radarMapFile.Write(radarMap[0], Length(radarMap) * SizeOf(Word));
|
| 121 | radarMapFile.Free; |
| 122 | |
| 123 | if FRadarDependencies <> nil then FreeAndNil(FRadarDependencies); |
| 124 | if FRadar <> nil then FreeAndNil(FRadar); |
| 125 | end;
|
| 126 | |
| 127 | procedure TfrmRadarMap.FormResize(Sender: TObject);
|
| 128 | begin
|
| 129 | sbMain.AutoScroll := (Width < Constraints.MaxWidth) or (Height < Constraints.MaxHeight);
|
| 130 | end;
|
| 131 | |
| 132 | procedure TfrmRadarMap.pbRadarMouseDown(Sender: TObject; Button: TMouseButton;
|
| 133 | Shift: TShiftState; X, Y: Integer); |
| 134 | begin
|
| 135 | frmMain.SetPos(X * 8, Y * 8); |
| 136 | end;
|
| 137 | |
| 138 | procedure TfrmRadarMap.pbRadarMouseLeave(Sender: TObject);
|
| 139 | begin
|
| 140 | lblPosition.Caption := '';
|
| 141 | end;
|
| 142 | |
| 143 | procedure TfrmRadarMap.pbRadarMouseMove(Sender: TObject; Shift: TShiftState; X,
|
| 144 | Y: Integer); |
| 145 | begin
|
| 146 | lblPosition.Caption := Format('X: %d, Y: %d', [X * 8, Y * 8]); |
| 147 | end;
|
| 148 | |
| 149 | procedure TfrmRadarMap.pbRadarPaint(Sender: TObject);
|
| 150 | var
|
| 151 | posX, posY: Word; |
| 152 | begin
|
| 153 | DisplayImage(pbRadar.Canvas, 0, 0, FRadar); |
| 154 | posX := frmMain.X div 8; |
| 155 | posY := frmMain.Y div 8; |
| 156 | pbRadar.Canvas.Pen.Color := clBlack; |
| 157 | pbRadar.Canvas.Pen.Style := psSolid; |
| 158 | pbRadar.Canvas.Brush.Color := clRed; |
| 159 | pbRadar.Canvas.Brush.Style := bsSolid; |
| 160 | pbRadar.Canvas.Ellipse(posX - 3, posY - 3, posX + 3, posY + 3); |
| 161 | {pbRadar.Canvas.Pen.Color := clRed;
|
| 162 | pbRadar.Canvas.Pen.Style := psDash; |
| 163 | pbRadar.Canvas.Line(0, posY, pbRadar.Width, posY); |
| 164 | pbRadar.Canvas.Line(posX, 0, posX, pbRadar.Height);} |
| 165 | end;
|
| 166 | |
| 167 | procedure TfrmRadarMap.OnRadarHandlingPacket(ABuffer: TEnhancedMemoryStream);
|
| 168 | var
|
| 169 | subID: Byte; |
| 170 | checksum, realChecksum: Cardinal; |
| 171 | radarMapFile: TFileStream; |
| 172 | radarMapFileName: string;
|
| 173 | radarMap: TRadarColorMap; |
| 174 | x, y: Integer; |
| 175 | begin
|
| 176 | subID := ABuffer.ReadByte; |
| 177 | case subID of |
| 178 | $01: //checksum |
| 179 | begin
|
| 180 | checksum := ABuffer.ReadCardinal; |
| 181 | realChecksum := crc32(0, nil, 0); |
| 182 | radarMapFileName := IncludeTrailingPathDelimiter(ExtractFilePath( |
| 183 | Application.ExeName)) + 'RadarMap.cache';
|
| 184 | if FileExists(radarMapFileName) then |
| 185 | begin
|
| 186 | radarMapFile := TFileStream.Create(radarMapFileName, fmOpenRead); |
| 187 | SetLength(radarMap, radarMapFile.Size div SizeOf(Word));
|
| 188 | radarMapFile.Read(radarMap[0], radarMapFile.Size);
|
| 189 | radarMapFile.Free; |
| 190 | |
| 191 | realChecksum := crc32(realChecksum, @radarMap[0], Length(radarMap) * SizeOf(Word));
|
| 192 | end;
|
| 193 | |
| 194 | if checksum <> realChecksum then |
| 195 | begin
|
| 196 | frmInitialize.lblStatus.Caption := 'Updating Radar Map';
|
| 197 | frmInitialize.Show; |
| 198 | frmInitialize.SetModal; |
| 199 | //frmMain.Enabled := False;
|
| 200 | dmNetwork.Send(TRequestRadarMapPacket.Create); |
| 201 | end else |
| 202 | RefreshRadar(radarMap); |
| 203 | end;
|
| 204 | $02: //radar map |
| 205 | begin
|
| 206 | SetLength(radarMap, (ABuffer.Size - ABuffer.Position) div SizeOf(Word));
|
| 207 | ABuffer.Read(radarMap[0], Length(radarMap) * SizeOf(Word));
|
| 208 | RefreshRadar(radarMap); |
| 209 | //frmMain.Enabled := True;
|
| 210 | frmInitialize.UnsetModal; |
| 211 | frmInitialize.Hide; |
| 212 | end;
|
| 213 | $03: //update radar |
| 214 | begin
|
| 215 | x := ABuffer.ReadWord; |
| 216 | y := ABuffer.ReadWord; |
| 217 | PInteger(FRadar.PixelPointers[x, y])^ := DecodeUOColor(ABuffer.ReadWord); |
| 218 | RepaintRadar; |
| 219 | end;
|
| 220 | end;
|
| 221 | end;
|
| 222 | |
| 223 | procedure TfrmRadarMap.RefreshRadar(ARadarMap: TRadarColorMap);
|
| 224 | var
|
| 225 | x, y: Integer; |
| 226 | begin
|
| 227 | for x := 0 to FRadar.Width - 1 do |
| 228 | for y := 0 to FRadar.Height - 1 do |
| 229 | PInteger(FRadar.PixelPointers[x, y])^ := DecodeUOColor(ARadarMap[x * FRadar.Height + y]); |
| 230 | RepaintRadar; |
| 231 | end;
|
| 232 | |
| 233 | procedure TfrmRadarMap.RepaintRadar;
|
| 234 | var
|
| 235 | i: Integer; |
| 236 | begin
|
| 237 | pbRadar.Repaint; |
| 238 | for i := 0 to FRadarDependencies.Count - 1 do |
| 239 | TWinControl(FRadarDependencies.Items[i]).Repaint; |
| 240 | end;
|
| 241 | |
| 242 | initialization
|
| 243 | {$I UfrmRadar.lrs}
|
| 244 | |
| 245 | end.
|
| 246 |