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

Last change on this file since 513 was 513, checked in by Sam Hocevar, 11 years ago

imlib: some more vec2i transition for simplicity.

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