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

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

game: convert a few integer pairs to vec2i variables.

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