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

Last change on this file since 2 was 2, checked in by Sam Hocevar, 14 years ago
  • imported original 0.7.0 tarball
File size: 6.0 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        printf( "last_savegame_name()\n" );
43        sprintf(buf,"%ssave%04d.spe",get_save_filename_prefix(), (last_save_game_number+MAX_SAVE_GAMES-1)%MAX_SAVE_GAMES+1);
44}
45
46jwindow *create_num_window(int mx, int total_saved, image **thumb_nails)
47{
48  ico_button *buts[MAX_SAVE_GAMES];
49  int y=WINDOW_FRAME_TOP,i;
50  int ih=cash.img(save_buts[0])->height();
51  int x=0;
52  for (i=0;i<total_saved;i++,y+=ih)
53  {
54    if (thumb_nails) { while (!thumb_nails[x]) x++; }
55    buts[i]=new ico_button(WINDOW_FRAME_LEFT,y,ID_LOAD_GAME_NUMBER+x,
56                           save_buts[x*3+0],save_buts[x*3+0],save_buts[x*3+1],save_buts[x*3+2],NULL);
57    buts[i]->set_act_id(ID_LOAD_GAME_PREVIEW+x);
58    x++;
59  }
60
61  for (i=0;i<total_saved-1;i++)
62    buts[i]->next=buts[i+1];
63
64  return eh->new_window(mx,yres/2-(WINDOW_FRAME_TOP+ih*5)/2,-1,-1,buts[0]);
65}
66
67FILE *open_FILE(char *filename, char *mode);
68
69int get_save_spot()
70{
71  int i=MAX_SAVE_GAMES,last_free=0;
72  for (;i>0;)
73  {
74    char name[20];
75    sprintf(name,"%ssave%04d.spe", get_save_filename_prefix(),i);
76    FILE *fp=open_FILE(name,"rb"); 
77    if (fp)
78      i=0;
79    else { last_free=i; i--; }
80    fclose(fp);
81  }
82
83  if (last_free) return last_free;    // if there are any slots not created yet...
84       
85  int w=cash.img(save_buts[0])->width();
86  int mx=last_demo_mx-w/2;
87  if((unsigned)(mx + w + 10) > xres) mx = xres - w - 10;
88  if( mx < 0) mx = 0;
89
90  jwindow *l_win=create_num_window(mx,MAX_SAVE_GAMES,NULL);
91  event ev;
92  int got_level=0;
93  int quit=0;
94  do
95  {
96    eh->flush_screen();
97    eh->get_event(ev);
98    if (ev.type==EV_MESSAGE && ev.message.id>=ID_LOAD_GAME_NUMBER && ev.message.id<ID_LOAD_GAME_PREVIEW)
99      got_level=ev.message.id-ID_LOAD_GAME_NUMBER+1;
100
101
102    if (ev.type==EV_CLOSE_WINDOW && ev.window==l_win)
103      quit=1;
104  } while (!got_level && !quit);
105
106  eh->close_window(l_win);
107  the_game->reset_keymap();
108  return got_level;
109}
110
111void get_savegame_name(char *buf)  // buf should be at least 50 bytes
112{
113        sprintf(buf,"save%04d.spe",(last_save_game_number++)%MAX_SAVE_GAMES+1);
114/*  FILE *fp=open_FILE("lastsave.lsp","wb");
115  if (fp)
116  {
117    fprintf(fp,"(setq last_save_game %d)\n",last_save_game_number%MAX_SAVE_GAMES);
118    fclose(fp);
119  } else dprintf("Warning unable to open lastsave.lsp for writing\n"); */
120}
121
122int show_load_icon()
123{
124        int i;
125        for( i = 0; i < MAX_SAVE_GAMES; i++ )
126        {
127                char nm[255];
128                sprintf( nm, "%ssave%04d.spe", get_save_filename_prefix(), i + 1 );
129                bFILE *fp = open_file( nm, "rb" );
130                if( fp->open_failure() )
131                {
132                        delete fp;
133                }
134                else
135                {
136                        delete fp;
137                        return 1;
138                }
139        }
140        return 0;
141}
142
143int load_game(int show_all, char *title)   // return 0 if the player escapes, else return the number of the game to load
144{
145        int total_saved=0;
146        image *thumb_nails[MAX_SAVE_GAMES];
147        int start_num=0;
148        int max_w=160,max_h=100;
149        memset(thumb_nails,0,sizeof(thumb_nails));
150
151        image *first=NULL;
152
153        for (start_num=0;start_num<MAX_SAVE_GAMES;start_num++)
154        {
155                char name[255];
156                int fail=0;
157
158                sprintf(name,"%ssave%04d.spe", get_save_filename_prefix(), start_num+1);
159                bFILE *fp=open_file(name,"rb");
160                if (fp->open_failure())
161                {
162                        fail=1;
163                }
164                else
165                {
166                        spec_directory sd(fp);
167                        spec_entry *se=sd.find("thumb nail");
168                        if (se && se->type==SPEC_IMAGE)
169                        {
170                                thumb_nails[start_num]=new image(se,fp);
171                                if (thumb_nails[start_num]->width()>max_w) max_w=thumb_nails[start_num]->width();
172                                if (thumb_nails[start_num]->height()>max_h) max_h=thumb_nails[start_num]->height();
173                                if (!first) first=thumb_nails[start_num];
174                                total_saved++;
175                        }
176                        else
177                                fail=1;
178                }
179                if (fail && show_all)
180                {
181                        thumb_nails[start_num]=new image(160,100);     
182                        thumb_nails[start_num]->clear();
183                        console_font->put_string(thumb_nails[start_num],0,0,symbol_str("no_saved"));
184                        total_saved++;
185                        if (!first) first=thumb_nails[start_num];
186                }
187                delete fp;
188        }
189
190        if (!total_saved) return 0;
191        if (total_saved>MAX_SAVE_GAMES)
192                total_saved=MAX_SAVE_GAMES;
193
194        int i,ih=cash.img(save_buts[0])->height();
195/*  ico_button *buts[MAX_SAVE_GAMES];
196  int y=WINDOW_FRAME_TOP;
197
198
199  for (i=0;i<total_saved;i++,y+=ih)
200  {
201    buts[i]=new ico_button(WINDOW_FRAME_LEFT,y,ID_LOAD_GAME_NUMBER+i,                 
202                           save_buts[i*3+1],save_buts[i*3+1],save_buts[i*3+0],save_buts[i*3+2],NULL);
203    buts[i]->set_act_id(ID_LOAD_GAME_PREVIEW+i);
204  }
205
206  for (i=0;i<total_saved-1;i++)
207    buts[i]->next=buts[i+1];
208*/
209
210
211        jwindow *l_win=create_num_window(0,total_saved,thumb_nails);
212        jwindow *preview=eh->new_window(l_win->x+l_win->l+5,l_win->y,max_w,max_h,NULL,title);
213
214        first->put_image(preview->screen,preview->x1(),preview->y1());
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        return got_level;
245}
Note: See TracBrowser for help on using the repository browser.