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

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