-
Notifications
You must be signed in to change notification settings - Fork 2
/
LEVELSEL.PAS
189 lines (147 loc) · 3.86 KB
/
LEVELSEL.PAS
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
{$A+,B-,E+,F-,G+,I-,N+,P-,Q-,R-,S-,T-,V-,X+}
unit LevelSel;
interface
procedure DoLevelSelect;
implementation
uses Dos, GDGfx, GDKeybrd, GDTimer, GDEvents,
Assets, Maps, Draw, Shared, Toolbox;
const
MAX_MAP_FILES = 127; { because i am lazy right now }
type
FoundMapFile = record
filename : string[12];
header : MapHeader;
end;
var
mapFilesList : array[0..MAX_MAP_FILES] of FoundMapFile;
mapFilesCount : word;
menuSelection : integer;
function ReadMapFileHeader(filename : string; var header : MapHeader) : boolean;
var
f : file;
n : integer;
ident : array[0..2] of char;
label ioError;
begin
ReadMapFileHeader := false;
Assign(f, filename);
Reset(f, 1);
if IOResult <> 0 then begin
Close(f);
n := IOResult; { clear i/o error flag }
exit;
end;
{ validate file type by checking for expected header }
BlockRead(f, ident, SizeOf(ident));
if (ident[0] <> 'M') or (ident[1] <> 'A') or (ident[2] <> 'P') then
goto ioError;
MemFill(@header, 0, SizeOf(header));
BlockRead(f, header, SizeOf(header), n);
if n <> SizeOf(header) then goto ioError;
ReadMapFileHeader := true;
ioError:
Close(f);
n := IOResult; { clear i/o error flag }
end;
procedure ScanForMapFiles;
var
search : SearchRec;
header : MapHeader;
i : integer;
begin
i := 0;
MemFill(@mapFilesList, 0, SizeOf(mapFilesList));
FindFirst('*.map', AnyFile, search);
while DosError = 0 do begin
ReadMapFileHeader(search.Name, header);
if search.Name <> 'TEST.MAP' then begin
mapFilesList[i].filename := search.Name;
mapFilesList[i].header := header;
inc(i);
end;
FindNext(search);
end;
mapFilesCount := i;
end;
procedure DrawLevelSelect;
var
i, x, y : integer;
idx : integer;
uiFrame : ^UIFrameBitmaps;
begin
Cls(0);
BlitSpritef(80, 10, titleSelectLevel);
UseFont(@fnt);
x := 16;
y := 70;
for i := -1 to 1 do begin;
idx := i + menuSelection;
if i = 0 then
uiFrame := @uiTomatoFrame
else
uiFrame := @uiGeneralFrame;
if (idx < 0) or (idx >= mapFilesCount) then
{DrawUIFrame(x, y, 288, 32, uiFrame^)}
else begin
with mapFilesList[idx] do begin
DrawUIFrame(x, y, 288, 32, uiFrame^);
DrawString(x+8, y+8, 15, filename);
SetClipRegion(x+8, y+16, x+8+288-16, y+16+32-16);
DrawString(x+8, y+16, 15, header.Name);
ResetClipRegion;
end;
end;
inc(y, 32);
end;
WaitForVsync;
Flip(BACKBUFFER_LAYER);
end;
procedure DoLevelSelect;
var
quit : boolean;
aborted : boolean;
event : PInputEvent;
begin
UseLayer(BACKBUFFER_LAYER);
menuSelection := 0;
ScanForMapFiles;
DrawLevelSelect;
FadeIn;
InitEvents;
quit := false;
aborted := false;
while not quit do begin
while not IsEventsEmpty do begin
event := PollEvents;
if IsKeyReleasedEvent(event, KEY_ESC) then begin
quit := true;
aborted := true;
end;
if IsKeyReleasedEvent(event, KEY_UP) then begin
dec(menuSelection);
if menuSelection < 0 then
menuSelection := 0;
end;
if IsKeyReleasedEvent(event, KEY_DOWN) then begin
inc(menuSelection);
if menuSelection >= mapFilesCount then
menuSelection := mapFilesCount-1;
end;
if IsKeyReleasedEvent(event, KEY_ENTER) then begin
if mapFilesCount > 0 then
selectedMap := mapFilesList[menuSelection].filename
else
selectedMap := '';
quit := true;
end;
end;
DrawLevelSelect;
end;
CloseEvents;
FadeOut;
if aborted then
currentGameState := StateMainMenu
else
currentGameState := StateFruitSelect;
end;
end.