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

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

style: remove trailing spaces, fix copyright statements.

File size: 6.7 KB
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
[494]4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
[56]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
[39]13#include <string.h>
14
[481]15#include "game.h"
[88]16
[481]17#include "specs.h"
18#include "jwindow.h"
19#include "id.h"
20#include "input.h"
21#include "fonts.h"
22#include "lisp.h"
23#include "dprint.h"
24#include "cache.h"
25#include "gui.h"
26#include "dev.h"
27#include "id.h"
28#include "demo.h"
[2]29
30extern void *save_order;         // load from "saveordr.lsp", contains a list ordering the save games
31
32extern JCFont *console_font;
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;
[494]44  for (i=0; i<MAX_SAVE_GAMES*3; i++)
[2]45  {
46    sprintf(name,"nums%04d.pcx",i+1);
[124]47    save_buts[i]=cache.reg("art/icons.spe",name,SPEC_IMAGE,1);
[2]48  }
49}
50
51
52void last_savegame_name(char *buf)
53{
[124]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);
[2]56}
57
[120]58Jwindow *create_num_window(int mx, int total_saved, image **thumb_nails)
[2]59{
60  ico_button *buts[MAX_SAVE_GAMES];
[111]61  int y = 0, i;
[123]62  int ih=cache.img(save_buts[0])->height();
[2]63  int x=0;
[494]64  for (i=0; i<total_saved; i++,y+=ih)
[2]65  {
66    if (thumb_nails) { while (!thumb_nails[x]) x++; }
[111]67    buts[i]=new ico_button(0, y, ID_LOAD_GAME_NUMBER + x,
[124]68               save_buts[x*3+0],save_buts[x*3+0],save_buts[x*3+1],save_buts[x*3+2],NULL);
[2]69    buts[i]->set_act_id(ID_LOAD_GAME_PREVIEW+x);
70    x++;
71  }
72
[494]73  for (i=0; i<total_saved-1; i++)
[2]74    buts[i]->next=buts[i+1];
75
[120]76  return wm->new_window(mx,yres/2-(Jwindow::top_border()+ih*5)/2,-1,-1,buts[0]);
[2]77}
78
79int get_save_spot()
80{
81  int i=MAX_SAVE_GAMES,last_free=0;
[494]82  for (; i>0; )
[2]83  {
84    char name[20];
85    sprintf(name,"%ssave%04d.spe", get_save_filename_prefix(),i);
[124]86    FILE *fp=open_FILE(name,"rb");
[2]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...
[124]94
[123]95  int w=cache.img(save_buts[0])->width();
[2]96  int mx=last_demo_mx-w/2;
[162]97  if(mx + w + 10 > xres) mx = xres - w - 10;
98  if(mx < 0) mx = 0;
[2]99
[120]100  Jwindow *l_win=create_num_window(mx,MAX_SAVE_GAMES,NULL);
[2]101  event ev;
102  int got_level=0;
103  int quit=0;
104  do
105  {
[106]106    wm->flush_screen();
107    wm->get_event(ev);
[2]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
[106]116  wm->close_window(l_win);
[2]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{
[124]123    sprintf(buf,"save%04d.spe",(last_save_game_number++)%MAX_SAVE_GAMES+1);
[2]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
[124]132int show_load_icon()
[2]133{
[124]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;
[2]151}
152
[39]153int load_game(int show_all, char const *title)   // return 0 if the player escapes, else return the number of the game to load
[2]154{
[124]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));
[2]160
[124]161    image *first=NULL;
[2]162
[494]163    for (start_num=0; start_num<MAX_SAVE_GAMES; start_num++)
[124]164    {
165        char name[255];
166        int fail=0;
[2]167
[124]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        {
[494]191            thumb_nails[start_num]=new image(160,100);
[124]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    }
[2]199
[124]200    if (!total_saved) return 0;
201    if (total_saved>MAX_SAVE_GAMES)
202        total_saved=MAX_SAVE_GAMES;
[2]203
[124]204    int i;
[123]205/*  int ih=cache.img(save_buts[0])->height();
[4]206  ico_button *buts[MAX_SAVE_GAMES];
[113]207  int y=0;
[2]208
209
[494]210  for (i=0; i<total_saved; i++,y+=ih)
[2]211  {
[494]212    buts[i]=new ico_button(0,y,ID_LOAD_GAME_NUMBER+i,
[124]213               save_buts[i*3+1],save_buts[i*3+1],save_buts[i*3+0],save_buts[i*3+2],NULL);
[2]214    buts[i]->set_act_id(ID_LOAD_GAME_PREVIEW+i);
215  }
216
[494]217  for (i=0; i<total_saved-1; i++)
[2]218    buts[i]->next=buts[i+1];
219*/
220
221
[124]222    Jwindow *l_win=create_num_window(0,total_saved,thumb_nails);
223    Jwindow *preview=wm->new_window(l_win->x+l_win->l+5,l_win->y,max_w,max_h,NULL,title);
[2]224
[124]225    first->put_image(preview->screen,preview->x1(),preview->y1());
[2]226
[124]227    event ev;
228    int got_level=0;
229    int quit=0;
230    do
231    {
232        wm->flush_screen();
233        wm->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;
[2]236
[124]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        }
[2]243
[124]244        if ((ev.type==EV_CLOSE_WINDOW) || (ev.type==EV_KEY && ev.key==JK_ESC))
245            quit=1;
246    } while (!got_level && !quit);
[2]247
[124]248    wm->close_window(l_win);
249    wm->close_window(preview);
[2]250
[494]251    for (i=0; i<total_saved; i++)
[124]252        if (thumb_nails[i])
253            delete thumb_nails[i];
[2]254
[124]255    return got_level;
[2]256}
Note: See TracBrowser for help on using the repository browser.