source: trunk/Packages/CoolStreaming/Demo/DynNumber/UMainForm.pas

Last change on this file was 2, checked in by chronos, 9 years ago
  • Added: TPakFile class for extraction PAK files from original game.
File size: 4.2 KB
Line 
1unit UMainForm;
2
3{$mode objfpc}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
9 ComCtrls, Spin, UDynNumber, Math;
10
11type
12
13 { TMainForm }
14
15 TMainForm = class(TForm)
16 Button1: TButton;
17 Button2: TButton;
18 Button3: TButton;
19 Button4: TButton;
20 Edit1: TEdit;
21 Edit2: TEdit;
22 Edit3: TEdit;
23 ListView1: TListView;
24 PageControl1: TPageControl;
25 SpinEdit1: TSpinEdit;
26 TabSheet1: TTabSheet;
27 TabSheet2: TTabSheet;
28 procedure Button1Click(Sender: TObject);
29 procedure Button2Click(Sender: TObject);
30 procedure Button3Click(Sender: TObject);
31 procedure Button4Click(Sender: TObject);
32 procedure FormCreate(Sender: TObject);
33 procedure FormDestroy(Sender: TObject);
34 private
35 public
36 DynamicNumber: TDynamicNumber;
37 end;
38
39var
40 MainForm: TMainForm;
41
42implementation
43
44{$R *.lfm}
45
46{ TMainForm }
47
48procedure TMainForm.Button1Click(Sender: TObject);
49var
50 I: Integer;
51 J: Integer;
52 C: Integer;
53 Parts: array of Integer;
54 N: TDynamicNumber;
55 NewItem: TListItem;
56begin
57 try
58 N := TDynamicNumber.Create;
59 ListView1.BeginUpdate;
60 ListView1.Clear;
61 for I := 0 to SpinEdit1.Value do begin
62 N.Stream.Size := 0;
63 N.WriteNumber(I);
64 NewItem := ListView1.Items.Add;
65 NewItem.Caption := IntToStr(I);
66 J := Floor(Log2(I)) + 1;
67 NewItem.SubItems.Add(FloatToStr(Round((1 - (J / N.Stream.Size)) * 100) / 100));
68 NewItem.SubItems.Add(N.Stream.AsString);
69 end;
70 finally
71 ListView1.EndUpdate;
72 N.Free;
73 end;
74end;
75
76procedure TMainForm.Button2Click(Sender: TObject);
77var
78 I: Integer;
79 II: Integer;
80 Count: Integer;
81 Parts: array of Integer;
82 PartIndex: Integer;
83 MaxValue: Integer;
84 J: Integer;
85 NewItem: TListItem;
86 N: TDynamicNumber;
87const
88 Step = 1;
89begin
90 Count := 1;
91 SetLength(Parts, Count);
92 try
93 N := TDynamicNumber.Create;
94 ListView1.BeginUpdate;
95 ListView1.Clear;
96 for II := 0 to SpinEdit1.Value do begin
97 I := II * Step;
98 // Write
99 N.Stream.Size := 0;
100 for J := 0 to Count - 2 do
101 N.Stream.WriteNumber(1, 1);
102 N.Stream.WriteNumber(0, 1);
103 for PartIndex := 0 to Count - 1 do begin
104 if PartIndex > 0 then MaxValue := Parts[PartIndex - 1] + 1
105 else MaxValue := 1;
106 for J := MaxValue - 1 downto 0 do
107 if ((Parts[PartIndex] shr J) and 1) = 1 then N.Stream.WriteNumber(1, 1)
108 else N.Stream.WriteNumber(0, 1);
109 end;
110 NewItem := ListView1.Items.Add;
111 NewItem.Caption := IntToStr(I);
112 J := Floor(Log2(I)) + 1;
113 NewItem.SubItems.Add(FloatToStr(Round((1 - (J / N.Stream.Size)) * 100) / 100));
114 NewItem.SubItems.Add(N.Stream.AsString);
115
116 // Increment value
117 for J := 0 to Step - 1 do begin
118 PartIndex := Count - 1;
119 repeat
120 Parts[PartIndex] := Parts[PartIndex] + 1;
121 if PartIndex > 0 then MaxValue := 1 shl (Parts[PartIndex - 1] + 1 + PartIndex)
122 else MaxValue := 2;
123 if Parts[PartIndex] >= MaxValue then begin
124 Parts[PartIndex] := 0;
125 PartIndex := PartIndex - 1;
126 if PartIndex < 0 then begin
127 Count := Count + 1;
128 SetLength(Parts, Count);
129 for PartIndex := 0 to Count - 1 do
130 Parts[PartIndex] := 0;
131 Break;
132 end;
133 end else Break;
134 until False;
135 end;
136 end;
137 finally
138 ListView1.EndUpdate;
139 N.Free;
140 end;
141end;
142
143procedure TMainForm.Button3Click(Sender: TObject);
144var
145 N: TDynamicNumber;
146begin
147 try
148 N := TDynamicNumber.Create;
149 N.Stream.Size := 0;
150 N.WriteNumber(StrToInt64(Edit1.Text));
151 N.Stream.Position := 0;
152 Edit2.Text := N.Stream.AsString;
153 finally
154 N.Free;
155 end;
156end;
157
158procedure TMainForm.Button4Click(Sender: TObject);
159var
160 N: TDynamicNumber;
161begin
162 try
163 N := TDynamicNumber.Create;
164 N.Stream.AsString := Edit2.Text;
165 Edit3.Text := IntToStr(N.ReadNumber);
166 finally
167 N.Free;
168 end;
169end;
170
171procedure TMainForm.FormCreate(Sender: TObject);
172begin
173 DynamicNumber := TDynamicNumber.Create;
174end;
175
176procedure TMainForm.FormDestroy(Sender: TObject);
177begin
178 DynamicNumber.Free;
179end;
180
181end.
182
Note: See TracBrowser for help on using the repository browser.