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

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