source: abuse/tags/pd/macabuse/src/loadgame.c @ 49

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