aboutsummaryrefslogtreecommitdiffstats
path: root/Game/Code/Classes/UCovers.pas
blob: 9cc2a5e9a440238f297a8487fee1a7305efb2ab9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
unit UCovers;

interface

{$IFDEF FPC}
  {$MODE Delphi}
{$ENDIF}

{$I switches.inc}

uses OpenGL12,
     {$IFDEF win32}
     windows,
     {$ENDIF}
     Math,
     Classes,
     SysUtils,
     {$IFNDEF FPC}
     Graphics,
     {$ENDIF}
     UThemes,
     UTexture;

type
  TCover = record
    Name:       string;
    W:          word;
    H:          word;
    Size:       integer;
    Position:   integer; // position of picture in the cache file
//    Data:       array of byte;
  end;

  TCovers = class
    Cover:      array of TCover;
    W:          word;
    H:          word;
    Size:       integer;
    Data:       array of byte;
    WritetoFile: Boolean;

    constructor Create;
    procedure Load;
    procedure Save;
    procedure AddCover(Name: string);
    function CoverExists(Name: string): boolean;
    function CoverNumber(Name: string): integer;
    procedure PrepareData(Name: string);
  end;

var
  Covers:     TCovers;

implementation

uses UMain,
     // UFiles,
     ULog,
     DateUtils;

constructor TCovers.Create;
begin
  W := 128;
  H := 128;
  Size := W*H*3;
  Load;
  WritetoFile := True;
end;

procedure TCovers.Load;
var
  F:      File;
  C:      integer; // cover number
  W:      word;
  H:      word;
  Bits:   byte;
  NLen:   word;
  Name:   string;
//  Data:   array of byte;
begin
  if FileExists(GamePath + 'covers.cache') then
  begin
    AssignFile(F, GamePath + 'covers.cache');
    Reset(F, 1);

    WritetoFile := not FileIsReadOnly(GamePath + 'covers.cache');

    SetLength(Cover, 0);

    while not EOF(F) do
    begin
      SetLength(Cover, Length(Cover)+1);

      BlockRead(F, W, 2);
      Cover[High(Cover)].W := W;

      BlockRead(F, H, 2);
      Cover[High(Cover)].H := H;

      BlockRead(F, Bits, 1);

      Cover[High(Cover)].Size := W * H * (Bits div 8);

      // test
  //    W := 128;
  //    H := 128;
  //    Bits := 24;
  //    Seek(F, FilePos(F) + 3);

      BlockRead(F, NLen, 2);
      SetLength(Name, NLen);

      BlockRead(F, Name[1], NLen);
      Cover[High(Cover)].Name := Name;

      Cover[High(Cover)].Position := FilePos(F);
      Seek(F, FilePos(F) + W*H*(Bits div 8));

  //    SetLength(Cover[High(Cover)].Data, W*H*(Bits div 8));
  //    BlockRead(F, Cover[High(Cover)].Data[0], W*H*(Bits div 8));

    end; // While

    CloseFile(F);
  end; // fileexists
end;

procedure TCovers.Save;
var
  F:      File;
  C:      integer; // cover number
  W:      word;
  H:      word;
  NLen:   word;
  Bits:   byte;
begin
{  AssignFile(F, GamePath + 'covers.cache');
  Rewrite(F, 1);

  Bits := 24;
  for C := 0 to High(Cover) do begin
    W := Cover[C].W;
    H := Cover[C].H;

    BlockWrite(F, W, 2);
    BlockWrite(F, H, 2);
    BlockWrite(F, Bits, 1);

    NLen := Length(Cover[C].Name);
    BlockWrite(F, NLen, 2);
    BlockWrite(F, Cover[C].Name[1], NLen);
    BlockWrite(F, Cover[C].Data[0], W*H*(Bits div 8));
  end;

  CloseFile(F);}
end;

procedure TCovers.AddCover(Name: string);
var
  B:      integer;
  F:      File;
  C:      integer; // cover number
  NLen:   word;
  Bits:   byte;
begin
  if not CoverExists(Name) then
  begin
    SetLength(Cover, Length(Cover)+1);
    Cover[High(Cover)].Name := Name;

    Cover[High(Cover)].W := W;
    Cover[High(Cover)].H := H;
    Cover[High(Cover)].Size := Size;

    // do not copy data. write them directly to file
//    SetLength(Cover[High(Cover)].Data, Size);
//    for B := 0 to Size-1 do
//      Cover[High(Cover)].Data[B] := CacheMipmap[B];

    if WritetoFile then
    begin
      AssignFile(F, GamePath + 'covers.cache');
      
      if FileExists(GamePath + 'covers.cache') then
      begin
        Reset(F, 1);
        Seek(F, FileSize(F));
      end
      else
      begin
        Rewrite(F, 1);
      end;

      Bits := 24;

      BlockWrite(F, W, 2);
      BlockWrite(F, H, 2);
      BlockWrite(F, Bits, 1);

      NLen := Length(Name);
      BlockWrite(F, NLen, 2);
      BlockWrite(F, Name[1], NLen);

      Cover[High(Cover)].Position := FilePos(F);
      BlockWrite(F, CacheMipmap[0], W*H*(Bits div 8));

      CloseFile(F);
    end;
  end
  else
    Cover[High(Cover)].Position := 0;
end;

function TCovers.CoverExists(Name: string): boolean;
var
  C:    integer; // cover
begin
  Result := false;
  C      := 0;
  
  while (C <= High(Cover)) and (Result = false) do
  begin
    if Cover[C].Name = Name then
      Result := true;
      
    Inc(C);
  end;
end;

function TCovers.CoverNumber(Name: string): integer;
var
  C:    integer;
begin
  Result := -1;
  C      := 0;
  
  while (C <= High(Cover)) and (Result = -1) do
  begin
    if Cover[C].Name = Name then
      Result := C;
      
    Inc(C);
  end;
end;

procedure TCovers.PrepareData(Name: string);
var
  F:  File;
  C:  integer;
begin
  if FileExists(GamePath + 'covers.cache') then
  begin
    AssignFile(F, GamePath + 'covers.cache');
    Reset(F, 1);

    C := CoverNumber(Name);
    SetLength(Data, Cover[C].Size);
    if Length(Data) < 6 then beep;
    Seek(F, Cover[C].Position);
    BlockRead(F, Data[0], Cover[C].Size);
    CloseFile(F);
  end;
end;

end.