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

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

imlib: remove dead code.

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
[512]15#include "common.h"
16
[481]17#include "game.h"
[88]18
[481]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"
[2]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;
[494]46  for (i=0; i<MAX_SAVE_GAMES*3; i++)
[2]47  {
48    sprintf(name,"nums%04d.pcx",i+1);
[124]49    save_buts[i]=cache.reg("art/icons.spe",name,SPEC_IMAGE,1);
[2]50  }
51}
52
53
54void last_savegame_name(char *buf)
55{
[124]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);
[2]58}
59
[120]60Jwindow *create_num_window(int mx, int total_saved, image **thumb_nails)
[2]61{
62  ico_button *buts[MAX_SAVE_GAMES];
[111]63  int y = 0, i;
[512]64  int ih=cache.img(save_buts[0])->Size().y;
[2]65  int x=0;
[494]66  for (i=0; i<total_saved; i++,y+=ih)
[2]67  {
68    if (thumb_nails) { while (!thumb_nails[x]) x++; }
[111]69    buts[i]=new ico_button(0, y, ID_LOAD_GAME_NUMBER + x,
[124]70               save_buts[x*3+0],save_buts[x*3+0],save_buts[x*3+1],save_buts[x*3+2],NULL);
[2]71    buts[i]->set_act_id(ID_LOAD_GAME_PREVIEW+x);
72    x++;
73  }
74
[494]75  for (i=0; i<total_saved-1; i++)
[2]76    buts[i]->next=buts[i+1];
77
[120]78  return wm->new_window(mx,yres/2-(Jwindow::top_border()+ih*5)/2,-1,-1,buts[0]);
[2]79}
80
81int get_save_spot()
82{
83  int i=MAX_SAVE_GAMES,last_free=0;
[494]84  for (; i>0; )
[2]85  {
86    char name[20];
87    sprintf(name,"%ssave%04d.spe", get_save_filename_prefix(),i);
[124]88    FILE *fp=open_FILE(name,"rb");
[2]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...
[124]96
[512]97  int w=cache.img(save_buts[0])->Size().x;
[2]98  int mx=last_demo_mx-w/2;
[162]99  if(mx + w + 10 > xres) mx = xres - w - 10;
100  if(mx < 0) mx = 0;
[2]101
[120]102  Jwindow *l_win=create_num_window(mx,MAX_SAVE_GAMES,NULL);
[2]103  event ev;
104  int got_level=0;
105  int quit=0;
106  do
107  {
[106]108    wm->flush_screen();
109    wm->get_event(ev);
[2]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
[106]118  wm->close_window(l_win);
[2]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{
[124]125    sprintf(buf,"save%04d.spe",(last_save_game_number++)%MAX_SAVE_GAMES+1);
[2]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
[124]134int show_load_icon()
[2]135{
[124]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;
[2]153}
154
[39]155int load_game(int show_all, char const *title)   // return 0 if the player escapes, else return the number of the game to load
[2]156{
[124]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));
[2]162
[124]163    image *first=NULL;
[2]164
[494]165    for (start_num=0; start_num<MAX_SAVE_GAMES; start_num++)
[124]166    {
167        char name[255];
168        int fail=0;
[2]169
[124]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            {
[523]182                thumb_nails[start_num] = new image(fp, se);
[512]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;
[124]185                if (!first) first=thumb_nails[start_num];
186                total_saved++;
187            }
188            else
189                fail=1;
190        }
191        if (fail && show_all)
192        {
[513]193            thumb_nails[start_num] = new image(vec2i(160, 100));
[124]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    }
[2]201
[124]202    if (!total_saved) return 0;
203    if (total_saved>MAX_SAVE_GAMES)
204        total_saved=MAX_SAVE_GAMES;
[2]205
[124]206    int i;
[512]207/*  int ih=cache.img(save_buts[0])->Size().y;
[4]208  ico_button *buts[MAX_SAVE_GAMES];
[113]209  int y=0;
[2]210
211
[494]212  for (i=0; i<total_saved; i++,y+=ih)
[2]213  {
[494]214    buts[i]=new ico_button(0,y,ID_LOAD_GAME_NUMBER+i,
[124]215               save_buts[i*3+1],save_buts[i*3+1],save_buts[i*3+0],save_buts[i*3+2],NULL);
[2]216    buts[i]->set_act_id(ID_LOAD_GAME_PREVIEW+i);
217  }
218
[494]219  for (i=0; i<total_saved-1; i++)
[2]220    buts[i]->next=buts[i+1];
221*/
222
223
[124]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);
[2]226
[124]227    first->put_image(preview->screen,preview->x1(),preview->y1());
[2]228
[124]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;
[2]238
[124]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        }
[2]245
[124]246        if ((ev.type==EV_CLOSE_WINDOW) || (ev.type==EV_KEY && ev.key==JK_ESC))
247            quit=1;
248    } while (!got_level && !quit);
[2]249
[124]250    wm->close_window(l_win);
251    wm->close_window(preview);
[2]252
[494]253    for (i=0; i<total_saved; i++)
[124]254        if (thumb_nails[i])
255            delete thumb_nails[i];
[2]256
[124]257    return got_level;
[2]258}
Note: See TracBrowser for help on using the repository browser.