forked from cyxx/f2bgl
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmenu.cpp
285 lines (253 loc) · 8.51 KB
/
menu.cpp
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* Fade To Black engine rewrite
* Copyright (C) 2006-2012 Gregory Montoir ([email protected])
*/
#include "game.h"
#include "render.h"
enum {
kOptionSave,
kOptionLoad,
};
static const int kMsgMenuSave = 51;
static const int kMsgMenuCancel = 55;
static const int kMsgMenuLoad = 56;
static const char *kMenuFnTga_s = "f2bgl-freesav%d.tga";
static int _currentOption = kOptionSave;
static int _rotateDirection;
static int _rotateTargetPitch;
static bool _resumeMusic;
static struct SaveLoadTexture {
int16_t texKey;
int w, h;
const uint8_t *data;
} _saveLoadTexture;
static struct {
int num;
bool used;
char description[256];
SaveLoadTexture texture;
uint8_t *screenshotData;
} _saveLoadSlots[kSaveLoadSlots];
void Game::resetObjectAnim(GameObject *o) {
GameObjectAnimation *anim = &o->anim;
anim->aniheadData = _res.getData(kResType_ANI, anim->animKey, "ANIHEAD");
anim->currentAnimKey = _res.getChild(kResType_ANI, anim->animKey);
anim->anikeyfData = _res.getData(kResType_ANI, anim->currentAnimKey, "ANIKEYF");
anim->framesCount = 0;
anim->ticksCount = 0;
}
void Game::initMenu() {
setPalette(_roomsTable[_objectsPtrTable[kObjPtrConrad]->room].palKey);
GameObject *o = _objectsPtrTable[kObjPtrSaveloadOption];
resetObjectAnim(o);
o->specialData[1][20] = (_currentOption == kOptionLoad) ? 1 : 0;
loadMenuObjectMesh(o, o->anim.currentAnimKey);
o->specialData[1][9] = 16;
SceneObject *so = &_sceneObjectsTable[0];
so->o = o;
so->x = 0;
so->y = -(38 << kPosShift);
so->z = 56 << kPosShift;
so->pitch = 512;
assert(kSaveLoadSlots == 8);
static const uint8_t texIndexLut[] = { 7, 6, 5, 4, 3, 2, 1, 0 };
// enumerate the saves
memset(_saveLoadSlots, 0, sizeof(_saveLoadSlots));
for (int i = 1; i < kSaveLoadSlots; ++i) {
_saveLoadSlots[i].num = -i;
_saveLoadSlots[i].used = hasSavedGameState(_saveLoadSlots[i].num);
snprintf(_saveLoadSlots[i].description, sizeof(_saveLoadSlots[i].description), "freesav%d", i);
if (_saveLoadSlots[i].used) {
char filename[32];
snprintf(filename, sizeof(filename), kMenuFnTga_s, i);
_saveLoadSlots[i].screenshotData = loadTGA(filename, &_saveLoadSlots[i].texture.w, &_saveLoadSlots[i].texture.h);
_saveLoadSlots[i].texture.data = _saveLoadSlots[i].screenshotData;
if (_saveLoadSlots[i].texture.data && _saveLoadSlots[i].texture.w > 0 && _saveLoadSlots[i].texture.h > 0) {
_render->prepareTextureRgb(_saveLoadSlots[i].texture.data, _saveLoadSlots[i].texture.w, _saveLoadSlots[i].texture.h, kSaveLoadTexKey + texIndexLut[i]); }
}
_saveLoadSlots[i].texture.texKey = kSaveLoadTexKey + texIndexLut[i];
}
// current game state screenshot
_saveLoadTexture.data = _render->captureScreen(&_saveLoadTexture.w, &_saveLoadTexture.h);
_render->prepareTextureRgb(_saveLoadTexture.data, _saveLoadTexture.w, _saveLoadTexture.h, kSaveLoadTexKey + texIndexLut[0]);
_snd.playMidi("savemap.xmi");
_resumeMusic = true;
}
void Game::finiMenu() {
for (int i = 0; i < kSaveLoadSlots; ++i) {
_render->releaseTexture(kSaveLoadTexKey + i); // should be texIndexLut[i] but equivalent as the loop iterates from 0 to 7
free(_saveLoadSlots[i].screenshotData);
memset(&_saveLoadSlots[i].texture, 0, sizeof(_saveLoadSlots[i].texture));
}
if (_resumeMusic) {
_snd.playMidi(_objectsPtrTable[kObjPtrWorld]->objKey, _snd._musicKey);
}
}
void Game::loadMenuObjectMesh(GameObject *o, int16_t key) {
SceneObject *so = &_sceneObjectsTable[0];
key = _res.getChild(kResType_ANI, key);
const uint8_t *p_anifram = _res.getData(kResType_ANI, key, "ANIFRAM");
if (p_anifram[2] == 9) {
uint8_t *p_poly3d;
uint8_t *p_form3d = initMesh(kResType_F3D, READ_LE_UINT16(p_anifram), &so->verticesData, &so->polygonsData, o, &p_poly3d, &o->specialData[1][20]);
so->verticesCount = READ_LE_UINT16(p_form3d + 18);
// find texture ids for save states screenshots
int textureIdCount = 0;
const uint8_t *polygonsData = so->polygonsData;
int count = *polygonsData++;
assert((count & 0x80) == 0);
while (count != 0) {
const int color = READ_LE_UINT16(polygonsData); polygonsData += 2;
const int indexSize = (count & 0x40) != 0 ? 1 : 2;
polygonsData += ((count & 15) + 1) * indexSize;
if (((color >> 8) & 31) == 12) {
_saveLoadTextureIdTable[textureIdCount] = (color & 255);
++textureIdCount;
if (textureIdCount >= kSaveLoadSlots) {
break;
}
}
count = *polygonsData++;
}
}
}
static void toggleOption(GameObject *o) {
switch (_currentOption) {
case kOptionLoad:
_currentOption = kOptionSave;
break;
case kOptionSave:
_currentOption = kOptionLoad;
break;
}
o->specialData[1][20] = (_currentOption == kOptionLoad) ? 1 : 0;
}
static int getSaveSlot(int pitch) {
return ((pitch + 512) & 1023) / (1024 / kSaveLoadSlots);
}
static bool pointerTap(const PlayerInput &inp, int x, int y, int w, int h) {
if (!inp.pointers[0][0].down && inp.pointers[0][1].down) {
if (inp.pointers[0][0].x >= x && inp.pointers[0][0].x < x + w) {
if (inp.pointers[0][0].y >= y && inp.pointers[0][0].y < y + h) {
return true;
}
}
}
return false;
}
bool Game::doMenu() {
_render->clearScreen();
_render->setupProjection(kProjMenu);
_render->setIgnoreDepth(false);
SceneObject *so = &_sceneObjectsTable[0];
_render->beginObjectDraw(so->x, so->y, so->z, so->pitch, kPosShift);
drawSceneObjectMesh(so);
_render->endObjectDraw();
_render->setupProjection(kProj2D);
GameObject *o = _objectsPtrTable[kObjPtrSaveloadOption];
if (getMessage(_objectsPtrTable[kObjPtrWorld]->objKey, _currentOption == kOptionLoad ? kMsgMenuLoad : kMsgMenuSave, &_tmpMsg)) {
memset(&_drawCharBuf, 0, sizeof(_drawCharBuf));
int w, h;
getStringRect((const char *)_tmpMsg.data, _tmpMsg.font, &w, &h);
const int x = (kScreenWidth - w) / 2;
const int y = 8;
drawString(x, y, (const char *)_tmpMsg.data, _tmpMsg.font, 0);
if (pointerTap(inp, x, y, w, h)) {
toggleOption(o);
loadMenuObjectMesh(o, o->anim.currentAnimKey);
}
}
if (_rotateDirection != 0) {
so->pitch += o->specialData[1][9] * _rotateDirection;
so->pitch &= 1023;
if (so->pitch == _rotateTargetPitch) {
_rotateDirection = 0;
}
} else {
if ((inp.dirMask & kInputDirLeft) || pointerTap(inp, 0, 50, 64, 100)) {
_rotateDirection = 1;
_rotateTargetPitch = (so->pitch + 128) & 1023;
}
if ((inp.dirMask & kInputDirRight) || pointerTap(inp, 256, 50, 64, 100)) {
_rotateDirection = -1;
_rotateTargetPitch = (so->pitch - 128) & 1023;
}
}
if (inp.dirMask & kInputDirUp) {
inp.dirMask &= ~kInputDirUp;
toggleOption(o);
loadMenuObjectMesh(o, o->anim.currentAnimKey);
}
if (inp.dirMask & kInputDirDown) {
inp.dirMask &= ~kInputDirDown;
toggleOption(o);
loadMenuObjectMesh(o, o->anim.currentAnimKey);
}
const int saveSlot = getSaveSlot(so->pitch);
bool selectSlot = false;
memset(&_drawCharBuf, 0, sizeof(_drawCharBuf));
if (saveSlot == 0) {
if (getMessage(_objectsPtrTable[kObjPtrWorld]->objKey, kMsgMenuCancel, &_tmpMsg)) {
int w, h;
getStringRect((const char *)_tmpMsg.data, _tmpMsg.font, &w, &h);
const int x = (kScreenWidth - w) / 2;
const int y = kScreenHeight / 2 + 80;
drawString(x, y, (const char *)_tmpMsg.data, _tmpMsg.font, 0);
if (pointerTap(inp, x, y, w, h)) {
selectSlot = true;
}
}
} else {
int w, h;
getStringRect(_saveLoadSlots[saveSlot].description, kFontNormale, &w, &h);
const int x = (kScreenWidth - w) / 2;
const int y = kScreenHeight / 2 + 80;
drawString(x, y, _saveLoadSlots[saveSlot].description, kFontNormale, 0);
if (pointerTap(inp, x, y, w, h)) {
selectSlot = true;
}
}
if (inp.ctrlKey) {
inp.ctrlKey = false;
selectSlot = true;
}
if (inp.enterKey) {
inp.enterKey = false;
selectSlot = true;
}
if (selectSlot) {
switch (_currentOption) {
case kOptionSave:
if (saveSlot == 0) { // cancel
return false;
} else {
_saveLoadSlots[saveSlot].used = false;
if (!_saveLoadSlots[saveSlot].used) {
saveGameState(_saveLoadSlots[saveSlot].num);
char filename[32];
snprintf(filename, sizeof(filename), kMenuFnTga_s, saveSlot);
saveTGA(filename, _saveLoadTexture.data, _saveLoadTexture.w, _saveLoadTexture.h, true);
// game state saved, return to the game
setGameStateSave(saveSlot);
return false;
}
}
break;
case kOptionLoad:
if (saveSlot == 0) { // cancel
return false;
} else {
if (_saveLoadSlots[saveSlot].used) {
loadGameState(_saveLoadSlots[saveSlot].num);
// game state loaded, return to the game
setGameStateLoad(saveSlot);
// and do not resume level music (done in saveload)
_resumeMusic = false;
return false;
}
}
break;
}
}
return true;
}