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

Last change on this file since 48 was 39, checked in by Sam Hocevar, 14 years ago
  • Fix almost 2,000 warnings by using proper "const" keywords where needed.
File size: 5.9 KB
Line 
1#include <string.h>
2
3#include "specs.hpp"
4#include "jwindow.hpp"
5#include "id.hpp"
6#include "input.hpp"
7#include "fonts.hpp"
8#include "lisp.hpp"
9#include "dprint.hpp"
10#include "cache.hpp"
11#include "gui.hpp"
12#include "dev.hpp"
13#include "id.hpp"
14#include "demo.hpp"
15#include "game.hpp"
16
17extern void *save_order;         // load from "saveordr.lsp", contains a list ordering the save games
18
19extern JCFont *console_font;
20
21extern window_manager *eh;
22
23#define MAX_SAVE_GAMES 5
24int last_save_game_number=0;
25
26int 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    save_buts[i]=cash.reg("art/icons.spe",name,SPEC_IMAGE,1);
37  }
38}
39
40
41void last_savegame_name(char *buf)
42{
43        printf( "last_savegame_name()\n" );
44        sprintf(buf,"%ssave%04d.spe",get_save_filename_prefix(), (last_save_game_number+MAX_SAVE_GAMES-1)%MAX_SAVE_GAMES+1);
45}
46
47jwindow *create_num_window(int mx, int total_saved, image **thumb_nails)
48{
49  ico_button *buts[MAX_SAVE_GAMES];
50  int y=WINDOW_FRAME_TOP,i;
51  int ih=cash.img(save_buts[0])->height();
52  int x=0;
53  for (i=0;i<total_saved;i++,y+=ih)
54  {
55    if (thumb_nails) { while (!thumb_nails[x]) x++; }
56    buts[i]=new ico_button(WINDOW_FRAME_LEFT,y,ID_LOAD_GAME_NUMBER+x,
57                           save_buts[x*3+0],save_buts[x*3+0],save_buts[x*3+1],save_buts[x*3+2],NULL);
58    buts[i]->set_act_id(ID_LOAD_GAME_PREVIEW+x);
59    x++;
60  }
61
62  for (i=0;i<total_saved-1;i++)
63    buts[i]->next=buts[i+1];
64
65  return eh->new_window(mx,yres/2-(WINDOW_FRAME_TOP+ih*5)/2,-1,-1,buts[0]);
66}
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,"%ssave%04d.spe", get_save_filename_prefix(),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((unsigned)(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[255];
127                sprintf( nm, "%ssave%04d.spe", get_save_filename_prefix(), i + 1 );
128                bFILE *fp = open_file( nm, "rb" );
129                if( fp->open_failure() )
130                {
131                        delete fp;
132                }
133                else
134                {
135                        delete fp;
136                        return 1;
137                }
138        }
139        return 0;
140}
141
142int load_game(int show_all, char const *title)   // return 0 if the player escapes, else return the number of the game to load
143{
144        int total_saved=0;
145        image *thumb_nails[MAX_SAVE_GAMES];
146        int start_num=0;
147        int max_w=160,max_h=100;
148        memset(thumb_nails,0,sizeof(thumb_nails));
149
150        image *first=NULL;
151
152        for (start_num=0;start_num<MAX_SAVE_GAMES;start_num++)
153        {
154                char name[255];
155                int fail=0;
156
157                sprintf(name,"%ssave%04d.spe", get_save_filename_prefix(), start_num+1);
158                bFILE *fp=open_file(name,"rb");
159                if (fp->open_failure())
160                {
161                        fail=1;
162                }
163                else
164                {
165                        spec_directory sd(fp);
166                        spec_entry *se=sd.find("thumb nail");
167                        if (se && se->type==SPEC_IMAGE)
168                        {
169                                thumb_nails[start_num]=new image(se,fp);
170                                if (thumb_nails[start_num]->width()>max_w) max_w=thumb_nails[start_num]->width();
171                                if (thumb_nails[start_num]->height()>max_h) max_h=thumb_nails[start_num]->height();
172                                if (!first) first=thumb_nails[start_num];
173                                total_saved++;
174                        }
175                        else
176                                fail=1;
177                }
178                if (fail && show_all)
179                {
180                        thumb_nails[start_num]=new image(160,100);     
181                        thumb_nails[start_num]->clear();
182                        console_font->put_string(thumb_nails[start_num],0,0,symbol_str("no_saved"));
183                        total_saved++;
184                        if (!first) first=thumb_nails[start_num];
185                }
186                delete fp;
187        }
188
189        if (!total_saved) return 0;
190        if (total_saved>MAX_SAVE_GAMES)
191                total_saved=MAX_SAVE_GAMES;
192
193        int i;
194/*  int 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.