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
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2013 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, by
8 *  Jonathan Clark, or by Sam Hocevar.
9 */
10
11#if HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <string.h>
16
17#include "common.h"
18
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
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"
34
35extern void *save_order;         // load from "saveordr.lsp", contains a list ordering the save games
36
37extern JCFont *console_font;
38
39#define MAX_SAVE_GAMES 15
40#define MAX_SAVE_LINES 5
41int last_save_game_number=0;
42
43int save_buts[MAX_SAVE_GAMES * 3];
44
45
46void load_number_icons()
47{
48    for (int i = 0; i < MAX_SAVE_GAMES * 3; i++)
49    {
50        char name[100];
51        sprintf(name, "nums%04d.pcx", i + 1);
52        save_buts[i] = cache.reg("art/icons.spe", name, SPEC_IMAGE, 1);
53    }
54}
55
56
57void last_savegame_name(char *buf)
58{
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);
61}
62
63AWindow *create_num_window(int mx, int total_saved, int lines, AImage **thumbnails)
64{
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;
69  int maxih = ih, maxiw = iw;
70  int n=0;
71  for (int i = 0; i < total_saved; i++, y += ih, n++)
72  {
73    maxih = lol::max(ih, maxih);
74    maxiw = lol::max(iw, maxiw);
75    if (y >= lines * ih)
76    {
77        y = 0;
78        x += iw;
79    }
80    if (thumbnails) { while (!thumbnails[n]) n++; }
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;
85  }
86
87  return wm->CreateWindow(ivec2(mx, yres / 2 - (AWindow::top_border() + maxih * 5) / 2), ivec2(-1), "", buts);
88}
89
90int get_save_spot()
91{
92  int i=MAX_SAVE_GAMES,last_free=0;
93  for (; i>0; )
94  {
95    char name[20];
96    sprintf(name,"%ssave%04d.spe", get_save_filename_prefix(),i);
97    FILE *fp=open_FILE(name,"rb");
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...
105
106  int w=cache.img(save_buts[0])->Size().x;
107  int mx=last_demo_mpos.x-w/2;
108  if(mx + w + 10 > xres) mx = xres - w - 10;
109  if(mx < 0) mx = 0;
110
111  AWindow *l_win=create_num_window(mx,MAX_SAVE_GAMES,MAX_SAVE_LINES,NULL);
112  Event ev;
113  int got_level=0;
114  int quit=0;
115  do
116  {
117    wm->flush_screen();
118    wm->get_event(ev);
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
127  wm->close_window(l_win);
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{
134    sprintf(buf,"save%04d.spe",(last_save_game_number++)%MAX_SAVE_GAMES+1);
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
143int show_load_icon()
144{
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;
162}
163
164int load_game(int show_all, char const *title)   // return 0 if the player escapes, else return the number of the game to load
165{
166    int total_saved=0;
167    AImage *thumbnails[MAX_SAVE_GAMES];
168    int start_num=0;
169    int max_w=160,max_h=100;
170    memset(thumbnails,0,sizeof(thumbnails));
171
172    AImage *first=NULL;
173
174    for (start_num=0; start_num<MAX_SAVE_GAMES; start_num++)
175    {
176        char name[255];
177        int fail=0;
178
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        {
187            SpecDir sd(fp);
188            SpecEntry *se=sd.find("thumb nail");
189            if (se && se->type==SPEC_IMAGE)
190            {
191                thumbnails[start_num] = new AImage(fp, se);
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];
195                total_saved++;
196            }
197            else
198                fail=1;
199        }
200        if (fail && show_all)
201        {
202            thumbnails[start_num] = new AImage(ivec2(160, 100));
203            thumbnails[start_num]->clear();
204            console_font->PutString(thumbnails[start_num], ivec2::zero, symbol_str("no_saved"));
205            total_saved++;
206            if (!first) first=thumbnails[start_num];
207        }
208        delete fp;
209    }
210
211    if (!total_saved) return 0;
212    if (total_saved>MAX_SAVE_GAMES)
213        total_saved=MAX_SAVE_GAMES;
214
215    int i;
216/*  int ih=cache.img(save_buts[0])->Size().y;
217  ico_button *buts[MAX_SAVE_GAMES];
218  int y=0;
219
220
221  for (i=0; i<total_saved; i++,y+=ih)
222  {
223    buts[i]=new ico_button(0,y,ID_LOAD_GAME_NUMBER+i,
224               save_buts[i*3+1],save_buts[i*3+1],save_buts[i*3+0],save_buts[i*3+2],NULL);
225    buts[i]->set_act_id(ID_LOAD_GAME_PREVIEW+i);
226  }
227
228  for (i=0; i<total_saved-1; i++)
229    buts[i]->next=buts[i+1];
230*/
231
232    // Create thumbnail window 5 pixels to the right of the list window
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);
235
236    preview->m_surf->PutImage(first, ivec2(preview->x1(), preview->y1()));
237
238    Event ev;
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;
247
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();
252            preview->m_surf->PutImage(thumbnails[draw_num], ivec2(preview->x1(), preview->y1()));
253        }
254
255        if ((ev.type==EV_CLOSE_WINDOW) || (ev.type==EV_KEY && ev.key==JK_ESC))
256            quit=1;
257    } while (!got_level && !quit);
258
259    wm->close_window(l_win);
260    wm->close_window(preview);
261
262    for (i=0; i<total_saved; i++)
263        delete thumbnails[i];
264
265    return got_level;
266}
Note: See TracBrowser for help on using the repository browser.