source: abuse/tags/pd/abuse/src/loadgame.c

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