-
Notifications
You must be signed in to change notification settings - Fork 1
/
loadgame.cpp
273 lines (229 loc) · 7.32 KB
/
loadgame.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
/*
* Abuse - dark 2D side-scrolling platform game
* Copyright (c) 1995 Crack dot Com
* Copyright (c) 2005-2011 Sam Hocevar <[email protected]>
*
* This software was released into the Public Domain. As with most public
* domain software, no warranty is made or implied by Crack dot Com, by
* Jonathan Clark, or by Sam Hocevar.
*/
#if defined HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include "common.h"
#include "game.h"
#include "specs.h"
#include "jwindow.h"
#include "id.h"
#include "input.h"
#include "fonts.h"
#include "lisp.h"
#include "dprint.h"
#include "cache.h"
#include "gui.h"
#include "dev.h"
#include "id.h"
#include "demo.h"
extern void *save_order; // load from "saveordr.lsp", contains a list ordering the save games
extern JCFont *console_font;
#define MAX_SAVE_GAMES 15
#define MAX_SAVE_LINES 5
int last_save_game_number=0;
int save_buts[MAX_SAVE_GAMES * 3];
void load_number_icons()
{
for (int i = 0; i < MAX_SAVE_GAMES * 3; i++)
{
char name[100];
sprintf(name, "nums%04d.pcx", i + 1);
save_buts[i] = cache.reg("art/icons.spe", name, SPEC_IMAGE, 1);
}
}
void last_savegame_name(char *buf)
{
printf( "last_savegame_name()\n" );
sprintf(buf,"%ssave%04d.spe",get_save_filename_prefix(), (last_save_game_number+MAX_SAVE_GAMES-1)%MAX_SAVE_GAMES+1);
}
Jwindow *create_num_window(int mx, int total_saved, int lines, image **thumbnails)
{
ico_button *buts[MAX_SAVE_GAMES];
int y = 0, x = 0, i;
int iw=cache.img(save_buts[0])->Size().x;
int ih=cache.img(save_buts[0])->Size().y;
int maxih = ih, maxiw = iw;
int n=0;
for (i=0; i<total_saved; i++,y+=ih)
{
maxih = Max(ih, maxih);
maxiw = Max(iw, maxiw);
if (y >= lines * ih)
{
y = 0;
x += iw;
}
if (thumbnails) { while (!thumbnails[n]) n++; }
buts[i]=new ico_button(x, y, ID_LOAD_GAME_NUMBER + n,
save_buts[n*3+0],save_buts[n*3+0],save_buts[n*3+1],save_buts[n*3+2],NULL);
buts[i]->set_act_id(ID_LOAD_GAME_PREVIEW+n);
n++;
}
for (i=0; i<total_saved-1; i++)
buts[i]->next=buts[i+1];
return wm->CreateWindow(ivec2(mx, yres / 2 - (Jwindow::top_border() + maxih * 5) / 2), ivec2(-1), buts[0]);
}
int get_save_spot()
{
int i=MAX_SAVE_GAMES,last_free=0;
for (; i>0; )
{
char name[20];
sprintf(name,"%ssave%04d.spe", get_save_filename_prefix(),i);
FILE *fp=open_FILE(name,"rb");
if (fp)
i=0;
else { last_free=i; i--; }
fclose(fp);
}
if (last_free) return last_free; // if there are any slots not created yet...
int w=cache.img(save_buts[0])->Size().x;
int mx=last_demo_mpos.x-w/2;
if(mx + w + 10 > xres) mx = xres - w - 10;
if(mx < 0) mx = 0;
Jwindow *l_win=create_num_window(mx,MAX_SAVE_GAMES,MAX_SAVE_LINES,NULL);
Event ev;
int got_level=0;
int quit=0;
do
{
wm->flush_screen();
wm->get_event(ev);
if (ev.type==EV_MESSAGE && ev.message.id>=ID_LOAD_GAME_NUMBER && ev.message.id<ID_LOAD_GAME_PREVIEW)
got_level=ev.message.id-ID_LOAD_GAME_NUMBER+1;
if (ev.type==EV_CLOSE_WINDOW && ev.window==l_win)
quit=1;
} while (!got_level && !quit);
wm->close_window(l_win);
the_game->reset_keymap();
return got_level;
}
void get_savegame_name(char *buf) // buf should be at least 50 bytes
{
sprintf(buf,"save%04d.spe",(last_save_game_number++)%MAX_SAVE_GAMES+1);
/* FILE *fp=open_FILE("lastsave.lsp","wb");
if (fp)
{
fprintf(fp,"(setq last_save_game %d)\n",last_save_game_number%MAX_SAVE_GAMES);
fclose(fp);
} else dprintf("Warning unable to open lastsave.lsp for writing\n"); */
}
int show_load_icon()
{
int i;
for( i = 0; i < MAX_SAVE_GAMES; i++ )
{
char nm[255];
sprintf( nm, "%ssave%04d.spe", get_save_filename_prefix(), i + 1 );
bFILE *fp = open_file( nm, "rb" );
if( fp->open_failure() )
{
delete fp;
}
else
{
delete fp;
return 1;
}
}
return 0;
}
int load_game(int show_all, char const *title) // return 0 if the player escapes, else return the number of the game to load
{
int total_saved=0;
image *thumbnails[MAX_SAVE_GAMES];
int start_num=0;
int max_w=160,max_h=100;
memset(thumbnails,0,sizeof(thumbnails));
image *first=NULL;
for (start_num=0; start_num<MAX_SAVE_GAMES; start_num++)
{
char name[255];
int fail=0;
sprintf(name,"%ssave%04d.spe", get_save_filename_prefix(), start_num+1);
bFILE *fp=open_file(name,"rb");
if (fp->open_failure())
{
fail=1;
}
else
{
spec_directory sd(fp);
spec_entry *se=sd.find("thumb nail");
if (se && se->type==SPEC_IMAGE)
{
thumbnails[start_num] = new image(fp, se);
if (thumbnails[start_num]->Size().x>max_w) max_w=thumbnails[start_num]->Size().x;
if (thumbnails[start_num]->Size().y>max_h) max_h=thumbnails[start_num]->Size().y;
if (!first) first=thumbnails[start_num];
total_saved++;
}
else
fail=1;
}
if (fail && show_all)
{
thumbnails[start_num] = new image(ivec2(160, 100));
thumbnails[start_num]->clear();
console_font->PutString(thumbnails[start_num], ivec2(0), symbol_str("no_saved"));
total_saved++;
if (!first) first=thumbnails[start_num];
}
delete fp;
}
if (!total_saved) return 0;
if (total_saved>MAX_SAVE_GAMES)
total_saved=MAX_SAVE_GAMES;
int i;
/* int ih=cache.img(save_buts[0])->Size().y;
ico_button *buts[MAX_SAVE_GAMES];
int y=0;
for (i=0; i<total_saved; i++,y+=ih)
{
buts[i]=new ico_button(0,y,ID_LOAD_GAME_NUMBER+i,
save_buts[i*3+1],save_buts[i*3+1],save_buts[i*3+0],save_buts[i*3+2],NULL);
buts[i]->set_act_id(ID_LOAD_GAME_PREVIEW+i);
}
for (i=0; i<total_saved-1; i++)
buts[i]->next=buts[i+1];
*/
// Create thumbnail window 5 pixels to the right of the list window
Jwindow *l_win=create_num_window(0,total_saved,MAX_SAVE_LINES,thumbnails);
Jwindow *preview=wm->CreateWindow(l_win->m_pos + ivec2(l_win->m_size.x + 5, 0), ivec2(max_w, max_h), NULL, title);
preview->m_surf->PutImage(first, ivec2(preview->x1(), preview->y1()));
Event ev;
int got_level=0;
int quit=0;
do
{
// disable game controller during save/load screen so that mouse can be used
wm->enable_controller(false);
wm->flush_screen();
wm->get_event(ev);
if (ev.type==EV_MESSAGE && ev.message.id>=ID_LOAD_GAME_NUMBER && ev.message.id<ID_LOAD_GAME_PREVIEW)
got_level=ev.message.id-ID_LOAD_GAME_NUMBER+1;
if (ev.type==EV_MESSAGE && ev.message.id>=ID_LOAD_GAME_PREVIEW && ev.message.id<ID_LOAD_PLAYER_GAME)
{
int draw_num=ev.message.id-ID_LOAD_GAME_PREVIEW;
preview->clear();
preview->m_surf->PutImage(thumbnails[draw_num], ivec2(preview->x1(), preview->y1()));
}
if ((ev.type==EV_CLOSE_WINDOW) || (ev.type==EV_KEY && ev.key==JK_ESC))
quit=1;
} while (!got_level && !quit);
wm->enable_controller(true);
wm->close_window(l_win);
wm->close_window(preview);
for (i=0; i<total_saved; i++)
delete thumbnails[i];
return got_level;
}