source: abuse/branches/lol/src/loadgame.cpp @ 732

Last change on this file since 732 was 732, checked in by Sam Hocevar, 8 years ago

build: SDL2 compilation fixes.

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