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

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

style: remove trailing spaces, fix copyright statements.

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