source: abuse/trunk/src/loadgame.cpp @ 538

Last change on this file since 538 was 538, checked in by Sam Hocevar, 11 years ago

game: add enough code to handle 15 save slots instead of 5. The artwork
is still missing, though.

File size: 7.0 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com or
8 *  Jonathan Clark.
9 */
10
11#include "config.h"
12
13#include <string.h>
14
15#include "common.h"
16
17#include "game.h"
18
19#include "specs.h"
20#include "jwindow.h"
21#include "id.h"
22#include "input.h"
23#include "fonts.h"
24#include "lisp.h"
25#include "dprint.h"
26#include "cache.h"
27#include "gui.h"
28#include "dev.h"
29#include "id.h"
30#include "demo.h"
31
32extern void *save_order;         // load from "saveordr.lsp", contains a list ordering the save games
33
34extern JCFont *console_font;
35
36#define MAX_SAVE_GAMES 15
37#define MAX_SAVE_LINES 5
38int last_save_game_number=0;
39
40int save_buts[MAX_SAVE_GAMES * 3];
41
42
43void load_number_icons()
44{
45    for (int i = 0; i < MAX_SAVE_GAMES * 3; i++)
46    {
47        char name[100];
48        sprintf(name, "nums%04d.pcx", i % (5 * 3) + 1); // FIXME
49        save_buts[i] = cache.reg("art/icons.spe", name, SPEC_IMAGE, 1);
50    }
51}
52
53
54void last_savegame_name(char *buf)
55{
56    printf( "last_savegame_name()\n" );
57    sprintf(buf,"%ssave%04d.spe",get_save_filename_prefix(), (last_save_game_number+MAX_SAVE_GAMES-1)%MAX_SAVE_GAMES+1);
58}
59
60Jwindow *create_num_window(int mx, int total_saved, int lines, image **thumb_nails)
61{
62  ico_button *buts[MAX_SAVE_GAMES];
63  int y = 0, x = 0, i;
64  int iw=cache.img(save_buts[0])->Size().x;
65  int ih=cache.img(save_buts[0])->Size().y;
66  int maxih = ih, maxiw = iw;
67  int n=0;
68  for (i=0; i<total_saved; i++,y+=ih)
69  {
70    maxih = Max(ih, maxih);
71    maxiw = Max(iw, maxiw);
72    if (y >= lines * ih)
73    {
74        y = 0;
75        x += iw;
76    }
77    if (thumb_nails) { while (!thumb_nails[n]) n++; }
78    buts[i]=new ico_button(x, y, ID_LOAD_GAME_NUMBER + n,
79               save_buts[n*3+0],save_buts[n*3+0],save_buts[n*3+1],save_buts[n*3+2],NULL);
80    buts[i]->set_act_id(ID_LOAD_GAME_PREVIEW+n);
81    n++;
82  }
83
84  for (i=0; i<total_saved-1; i++)
85    buts[i]->next=buts[i+1];
86
87  return wm->new_window(mx,yres/2-(Jwindow::top_border()+maxih*5)/2,-1,-1,buts[0]);
88}
89
90int get_save_spot()
91{
92  int i=MAX_SAVE_GAMES,last_free=0;
93  for (; i>0; )
94  {
95    char name[20];
96    sprintf(name,"%ssave%04d.spe", get_save_filename_prefix(),i);
97    FILE *fp=open_FILE(name,"rb");
98    if (fp)
99      i=0;
100    else { last_free=i; i--; }
101    fclose(fp);
102  }
103
104  if (last_free) return last_free;    // if there are any slots not created yet...
105
106  int w=cache.img(save_buts[0])->Size().x;
107  int mx=last_demo_mx-w/2;
108  if(mx + w + 10 > xres) mx = xres - w - 10;
109  if(mx < 0) mx = 0;
110
111  Jwindow *l_win=create_num_window(mx,MAX_SAVE_GAMES,MAX_SAVE_LINES,NULL);
112  event ev;
113  int got_level=0;
114  int quit=0;
115  do
116  {
117    wm->flush_screen();
118    wm->get_event(ev);
119    if (ev.type==EV_MESSAGE && ev.message.id>=ID_LOAD_GAME_NUMBER && ev.message.id<ID_LOAD_GAME_PREVIEW)
120      got_level=ev.message.id-ID_LOAD_GAME_NUMBER+1;
121
122
123    if (ev.type==EV_CLOSE_WINDOW && ev.window==l_win)
124      quit=1;
125  } while (!got_level && !quit);
126
127  wm->close_window(l_win);
128  the_game->reset_keymap();
129  return got_level;
130}
131
132void get_savegame_name(char *buf)  // buf should be at least 50 bytes
133{
134    sprintf(buf,"save%04d.spe",(last_save_game_number++)%MAX_SAVE_GAMES+1);
135/*  FILE *fp=open_FILE("lastsave.lsp","wb");
136  if (fp)
137  {
138    fprintf(fp,"(setq last_save_game %d)\n",last_save_game_number%MAX_SAVE_GAMES);
139    fclose(fp);
140  } else dprintf("Warning unable to open lastsave.lsp for writing\n"); */
141}
142
143int show_load_icon()
144{
145    int i;
146    for( i = 0; i < MAX_SAVE_GAMES; i++ )
147    {
148        char nm[255];
149        sprintf( nm, "%ssave%04d.spe", get_save_filename_prefix(), i + 1 );
150        bFILE *fp = open_file( nm, "rb" );
151        if( fp->open_failure() )
152        {
153            delete fp;
154        }
155        else
156        {
157            delete fp;
158            return 1;
159        }
160    }
161    return 0;
162}
163
164int load_game(int show_all, char const *title)   // return 0 if the player escapes, else return the number of the game to load
165{
166    int total_saved=0;
167    image *thumb_nails[MAX_SAVE_GAMES];
168    int start_num=0;
169    int max_w=160,max_h=100;
170    memset(thumb_nails,0,sizeof(thumb_nails));
171
172    image *first=NULL;
173
174    for (start_num=0; start_num<MAX_SAVE_GAMES; start_num++)
175    {
176        char name[255];
177        int fail=0;
178
179        sprintf(name,"%ssave%04d.spe", get_save_filename_prefix(), start_num+1);
180        bFILE *fp=open_file(name,"rb");
181        if (fp->open_failure())
182        {
183            fail=1;
184        }
185        else
186        {
187            spec_directory sd(fp);
188            spec_entry *se=sd.find("thumb nail");
189            if (se && se->type==SPEC_IMAGE)
190            {
191                thumb_nails[start_num] = new image(fp, se);
192                if (thumb_nails[start_num]->Size().x>max_w) max_w=thumb_nails[start_num]->Size().x;
193                if (thumb_nails[start_num]->Size().y>max_h) max_h=thumb_nails[start_num]->Size().y;
194                if (!first) first=thumb_nails[start_num];
195                total_saved++;
196            }
197            else
198                fail=1;
199        }
200        if (fail && show_all)
201        {
202            thumb_nails[start_num] = new image(vec2i(160, 100));
203            thumb_nails[start_num]->clear();
204            console_font->put_string(thumb_nails[start_num],0,0,symbol_str("no_saved"));
205            total_saved++;
206            if (!first) first=thumb_nails[start_num];
207        }
208        delete fp;
209    }
210
211    if (!total_saved) return 0;
212    if (total_saved>MAX_SAVE_GAMES)
213        total_saved=MAX_SAVE_GAMES;
214
215    int i;
216/*  int ih=cache.img(save_buts[0])->Size().y;
217  ico_button *buts[MAX_SAVE_GAMES];
218  int y=0;
219
220
221  for (i=0; i<total_saved; i++,y+=ih)
222  {
223    buts[i]=new ico_button(0,y,ID_LOAD_GAME_NUMBER+i,
224               save_buts[i*3+1],save_buts[i*3+1],save_buts[i*3+0],save_buts[i*3+2],NULL);
225    buts[i]->set_act_id(ID_LOAD_GAME_PREVIEW+i);
226  }
227
228  for (i=0; i<total_saved-1; i++)
229    buts[i]->next=buts[i+1];
230*/
231
232
233    Jwindow *l_win=create_num_window(0,total_saved,MAX_SAVE_LINES,thumb_nails);
234    Jwindow *preview=wm->new_window(l_win->x+l_win->l+5,l_win->y,max_w,max_h,NULL,title);
235
236    first->put_image(preview->screen,preview->x1(),preview->y1());
237
238    event ev;
239    int got_level=0;
240    int quit=0;
241    do
242    {
243        wm->flush_screen();
244        wm->get_event(ev);
245        if (ev.type==EV_MESSAGE && ev.message.id>=ID_LOAD_GAME_NUMBER && ev.message.id<ID_LOAD_GAME_PREVIEW)
246            got_level=ev.message.id-ID_LOAD_GAME_NUMBER+1;
247
248        if (ev.type==EV_MESSAGE && ev.message.id>=ID_LOAD_GAME_PREVIEW && ev.message.id<ID_LOAD_PLAYER_GAME)
249        {
250            int draw_num=ev.message.id-ID_LOAD_GAME_PREVIEW;
251            preview->clear();
252            thumb_nails[draw_num]->put_image(preview->screen,preview->x1(),preview->y1());
253        }
254
255        if ((ev.type==EV_CLOSE_WINDOW) || (ev.type==EV_KEY && ev.key==JK_ESC))
256            quit=1;
257    } while (!got_level && !quit);
258
259    wm->close_window(l_win);
260    wm->close_window(preview);
261
262    for (i=0; i<total_saved; i++)
263        if (thumb_nails[i])
264            delete thumb_nails[i];
265
266    return got_level;
267}
Note: See TracBrowser for help on using the repository browser.