source: abuse/trunk/src/loader2.cpp @ 512

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

imlib: use vec2i for image::size and unroll all necessary changes
everywhere else in the code.

File size: 11.9 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 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 or
8 *  Jonathan Clark.
9 */
10
11#include "config.h"
12
13#include <math.h>
14
15#include "common.h"
16
17#include "timing.h"
18#include "loader2.h"
19#include "chars.h"
20#include "specs.h"
21#include "lisp.h"
22#include "jrand.h"
23#include "menu.h"
24#include "dev.h"
25#include "director.h"
26
27#include "dev.h"
28#include "light.h"
29#include "dprint.h"
30#include "particle.h"
31#include "clisp.h"
32#include "compiled.h"
33#include "sbar.h"
34#include "help.h"
35#include "loadgame.h"
36#include "nfserver.h"
37#include "specache.h"
38
39extern int past_startup;
40
41property_manager *prop;
42int *backtiles;
43int *foretiles;
44JCFont *big_font,*console_font;
45int nforetiles,nbacktiles,f_wid,f_hi,b_wid,b_hi,total_songs=0,sfx_volume,music_volume,sound_avail=0;
46song *current_song=NULL;
47
48uint16_t current_start_type,start_position_type,last_start_number;
49int light_buttons[13];
50int joy_picts[2*9];
51palette *pal;
52
53int big_font_pict=-1,small_font_pict=-1,console_font_pict=-1,cdc_logo;
54
55int title_screen;
56
57color_filter *color_table;
58
59
60int border_tile,window_texture,
61    raise_volume,lower_volume,record_button,play_button,music_button,sfx_button,
62    window_colors,pause_image,damage_pict,block_pict,vmm_image,earth,earth_mask,clouds,
63    numbers[10],ok_button,cancel_button;
64
65int start_running=0;
66
67int c_mouse1,c_mouse2,c_normal,c_target;
68
69long bg_xmul,bg_xdiv,bg_ymul,bg_ydiv;    // brackground scroll rates
70char mouse_scrolling=0,palettes_locked=1,view_shift_disabled=0;
71
72int light_connection_color;
73
74
75image *load_image(spec_entry *e, bFILE *fp)
76{
77  image *im=new image(e,fp);
78  if (scale_mult!=1 || scale_div!=1)
79    im->resize(im->Size().x*scale_mult/scale_div,im->Size().y*scale_mult/scale_div);
80  return im;
81}
82
83image *load_image(bFILE *fp)
84{
85  image *im=new image(fp);
86  if (scale_mult!=1 || scale_div!=1)
87    im->resize(im->Size().x*scale_mult/scale_div,im->Size().y*scale_mult/scale_div);
88
89  return im;
90}
91
92void use_file(char *filename, bFILE *&fp, spec_directory *&sd)
93{
94  char fn[100];
95  fp=open_file(filename,"rb");
96  if (fp->open_failure())
97  {
98    delete fp;
99    sprintf(fn,"art/%s",filename);
100    fp=open_file(fn,"rb");
101    if (fp->open_failure())
102    {
103      printf("Unable to open file %s\n",filename);
104      delete fp;
105      exit(1);
106    }
107  }
108  sd=new spec_directory(fp);
109}
110
111void done_file(bFILE *&fp, spec_directory *&sd)
112{
113  delete fp;
114  delete sd;
115}
116
117void insert_tiles(char *filename)
118{
119  bFILE *fp=open_file(filename,"rb");
120  if (!fp->open_failure())
121  {
122    int ft=0,bt=0;
123    spec_directory sd(fp);
124    delete fp;
125    int i=0;
126    for (; i<sd.total; i++)
127    {
128      spec_entry *se=sd.entries[i];
129      if (se->type==SPEC_FORETILE)
130        ft++;
131      else if (se->type==SPEC_BACKTILE)
132        bt++;
133    }
134    if (bt)
135      printf("%s : adding %d background tiles (range %d-%d)\n",
136         filename,bt,nbacktiles,nbacktiles+bt-1);
137    if (ft)
138      printf("%s : adding %d foreground tiles (range %d-%d)\n",
139         filename,ft,nforetiles,nforetiles+bt-1);
140    if (!ft && !bt)
141      printf("Warning : file %s has no background or foreground tiles\n",filename);
142    else
143    {
144      int fon=nforetiles,bon=nbacktiles;
145      if (ft)
146        foretiles=(int *)realloc(foretiles,sizeof(int)*(nforetiles+ft));
147      if (bt)
148        backtiles=(int *)realloc(backtiles,sizeof(int)*(nbacktiles+bt));
149
150      for (i=0; i<sd.total; i++)
151      {
152    if (sd.entries[i]->type==SPEC_FORETILE)
153    {
154      foretiles[fon]=cache.reg(filename,sd.entries[i]->name);
155      fon++;
156      nforetiles++;
157    }
158    if (sd.entries[i]->type==SPEC_BACKTILE)
159    {
160      backtiles[bon]=cache.reg(filename,sd.entries[i]->name);
161      bon++;
162      nbacktiles++;
163    }
164      }
165    }
166  } else
167    printf("Warning : insert_tiles -> file %s could not be read from\n",filename);
168}
169
170void load_tiles(Cell *file_list)
171{
172  bFILE *fp;
173  spec_directory *sd;
174  spec_entry *spe;
175
176
177  int num;
178
179
180
181  Cell *fl;
182  int old_fsize=nforetiles,
183      old_bsize=nbacktiles;
184
185  for (fl=file_list; !NILP(fl); fl=lcdr(fl))
186  {
187    fp=open_file(lstring_value(lcar(fl)),"rb");
188    if (fp->open_failure())
189    {
190      printf("Warning : open %s for reading\n",lstring_value(lcar(fl)));
191      delete fp;
192    }
193    else
194    {
195      sd=new spec_directory(fp);
196      delete fp;
197      int i;
198      for (i=0; i<sd->total; i++)
199      {
200    spe=sd->entries[i];
201        switch (spe->type)
202        {
203          case SPEC_BACKTILE :
204            if (!sscanf(spe->name,"%d",&num))
205              printf("Warning : background tile %s has no number\n",spe->name);
206            else if (nbacktiles<=num) nbacktiles=num+1;
207          break;
208
209          case SPEC_FORETILE :
210            if (!sscanf(spe->name,"%d",&num))
211              printf("Warning : foreground tile %s has no number\n",spe->name);
212            else if (nforetiles<=num) nforetiles=num+1;
213          break;
214        }
215      }
216      delete sd;
217    }
218  }
219
220  // enlarge the arrays if needed.
221  if (nbacktiles>old_bsize)
222  {
223    backtiles=(int *)realloc(backtiles,sizeof(int)*nbacktiles);
224    memset(backtiles+old_bsize,-1,(nbacktiles-old_bsize)*sizeof(int));
225  }
226
227  if (nforetiles>old_fsize)
228  {
229    foretiles=(int *)realloc(foretiles,sizeof(int)*nforetiles);
230    memset(foretiles+old_fsize,-1,(nforetiles-old_fsize)*sizeof(int));
231  }
232
233
234// now load them up
235  for (fl=file_list; !NILP(fl); fl=lcdr(fl))
236  {
237    char const *fn=lstring_value(lcar(fl));
238    fp=open_file(fn,"rb");
239    if (!fp->open_failure())
240    {
241      sd=new spec_directory(fp);
242      delete fp;
243
244      int i;
245      for (i=0; i<sd->total; i++)
246      {
247    spe=sd->entries[i];
248        switch (spe->type)
249        {
250          case SPEC_BACKTILE :
251
252            if (sscanf(spe->name,"%d",&num))
253        {
254          if (backtiles[num]>=0)
255          {
256        dprintf("Warning : background tile %d redefined\n",num);
257        cache.unreg(backtiles[num]);
258          }
259          backtiles[num]=cache.reg(fn,spe->name,SPEC_BACKTILE);
260        }
261            break;
262          case SPEC_FORETILE :
263            if (sscanf(spe->name,"%d",&num))
264        {
265          if (foretiles[num]>=0)
266          {
267        dprintf("Warning : foreground tile %d redefined\n",num);
268        cache.unreg(foretiles[num]);
269          }
270          foretiles[num]=cache.reg(fn,spe->name,SPEC_FORETILE);
271        }
272            break;
273        }
274      }
275      delete sd;
276    } else delete fp;
277  }
278
279}
280
281
282extern unsigned char fnt6x13[192*104];
283char lsf[256]="abuse.lsp";
284
285void load_data(int argc, char **argv)
286{
287    total_objects=0;
288    total_weapons=0;
289    weapon_types=NULL;
290    figures=NULL;
291    nforetiles=nbacktiles=0;
292    foretiles=NULL;
293    backtiles=NULL;
294    pal=NULL;
295    color_table=NULL;
296
297# if 0
298    int should_save_sd_cache = 0;
299
300    char *cachepath;
301    cachepath = (char *)malloc( strlen( get_save_filename_prefix() ) + 12 + 1 );
302    sprintf( cachepath, "%ssd_cache.tmp", get_save_filename_prefix() );
303
304    bFILE *load = open_file( cachepath, "rb" );
305    if( !load->open_failure() )
306    {
307        sd_cache.load( load );
308    }
309    else
310    {
311        should_save_sd_cache = 1;
312    }
313    delete load;
314#endif
315
316  if (!net_start())              // don't let them specify a startup file we are connect elsewhere
317  {
318    for (int i=1; i<argc; i++)
319    {
320      if (!strcmp(argv[i],"-lsf"))
321      {
322    i++;
323    strcpy(lsf,argv[i]);
324      }
325      if (!strcmp(argv[i],"-a"))
326      {
327    i++;
328    sprintf(lsf,"addon/%s/%s.lsp",argv[i],argv[i]);
329      }
330    }
331  } else if (!get_remote_lsf(net_server,lsf))
332  {
333    dprintf("Unable to get remote lsf from %s\n",net_server);
334    exit(0);
335  }
336  char prog[100];
337  char const *cs;
338
339  c_mouse1=cache.reg("art/dev.spe","c_mouse1",SPEC_IMAGE,0);
340  c_mouse2=cache.reg("art/dev.spe","c_mouse2",SPEC_IMAGE,0);
341  c_normal=cache.reg("art/dev.spe","c_normal",SPEC_IMAGE,0);
342  c_target=cache.reg("art/dev.spe","c_target",SPEC_IMAGE,0);
343
344
345  sprintf(prog,"(load \"%s\")\n",lsf);
346
347  cs=prog;
348  if (!LObject::Compile(cs)->Eval())
349  {
350    printf("unable to open file '%s'\n",lsf);
351    exit(0);
352  }
353  compiled_init();
354  clear_tmp();
355  resize_tmp(0x4000);
356
357  dprintf("Engine : Registering base graphics\n");
358  for (int z=0; z<=11; z++)
359  {
360    char nm[10];
361    sprintf(nm,"l%d",z);
362    light_buttons[z]=cache.reg("art/dev.spe",nm,SPEC_IMAGE,0);
363  }
364
365
366  image *tmp_image=new image(192,104,fnt6x13);
367  big_font=new JCFont(tmp_image);
368  delete tmp_image;
369
370
371  char const *ff;
372  // FIXME: unnecessary duplicate call
373  if (DEFINEDP(LSymbol::FindOrCreate("frame_file")->GetValue()))
374    ff = lstring_value(LSymbol::FindOrCreate("frame_file")->GetValue());
375  else
376    ff = "art/frame.spe";
377
378  ok_button   =      cache.reg(ff,"dev_ok",SPEC_IMAGE);
379  cancel_button  =   cache.reg(ff,"cancel",SPEC_IMAGE);
380
381//  clouds      =      cache.reg(ff,"clouds",SPEC_IMAGE);
382
383  lower_volume=      cache.reg(ff,"lower_volume",SPEC_IMAGE);
384  raise_volume=      cache.reg(ff,"raise_volume",SPEC_IMAGE);
385  music_button=      cache.reg(ff,"music",SPEC_IMAGE);
386  sfx_button=        cache.reg(ff,"sound_fx",SPEC_IMAGE);
387  record_button=     cache.reg(ff,"record",SPEC_IMAGE);
388  play_button=       cache.reg(ff,"play",SPEC_IMAGE);
389  window_colors=     cache.reg(ff,"window_colors",SPEC_IMAGE);
390  pause_image=       cache.reg(ff,"pause_image",SPEC_IMAGE);
391  vmm_image=         cache.reg(ff,"vmm",SPEC_IMAGE);
392  border_tile=       cache.reg(ff,"border_tile",SPEC_IMAGE);
393  window_texture=    cache.reg(ff,"window_texture",SPEC_IMAGE);
394
395
396  help_screens=NULL;
397  total_help_screens=0;
398
399  if (DEFINEDP(symbol_value(l_help_screens)))
400  {
401    void *v=symbol_value(l_help_screens);
402    char *ff=lstring_value(CAR(v));  v=CDR(v);
403    total_help_screens=0;
404    while (v) { total_help_screens++; v=CDR(v); }
405    if (total_help_screens)
406    {
407      help_screens=(int *)malloc(sizeof(int)*total_help_screens);
408      v=CDR(symbol_value(l_help_screens));
409      int i=0;
410      for (; v; v=CDR(v),i++)
411        help_screens[i]=cache.reg(ff,lstring_value(CAR(v)),SPEC_IMAGE);
412    }
413    else
414      dprintf("Warning no help images following filename\n");
415  }
416
417  int i;
418  for (i=1; i<argc; i++)
419  {
420    if (!strcmp(argv[i],"-ec"))
421      l_empty_cache->SetValue(true_symbol);
422    if (!strcmp(argv[i],"-t"))
423    {
424      i++;
425      insert_tiles(argv[i]);
426    }
427  }
428
429  if (DEFINEDP(symbol_value(l_title_screen)))
430    title_screen=cache.reg_object(NULL,(LObject *)symbol_value(l_title_screen),SPEC_IMAGE,1);
431  else title_screen=-1;
432
433  if (DEFINEDP(symbol_value(l_cdc_logo)))
434    cdc_logo=cache.reg_object(NULL,(LObject *)symbol_value(l_cdc_logo),SPEC_IMAGE,1);
435  else cdc_logo=-1;
436
437  start_position_type=0xffff;
438  for(i=0; i<total_objects; i++)
439    if (!strcmp(object_names[i],"START"))
440      start_position_type=i;
441  if (start_position_type==0xffff)
442  {
443    printf("No object named START, cannot start game.\n");
444    exit(0);
445  }
446
447  sbar.load();
448
449  load_number_icons();
450
451
452  ERROR(nbacktiles,"No background tiles defined!");
453  ERROR(nforetiles,"No foreground tiles defined!");
454  ERROR(foretiles[0]>=0,"No black (0) foreground tile defined!");
455  ERROR(backtiles[0]>=0,"No black (0) background tile defined!");
456  ERROR(big_font_pict!=-1 || small_font_pict!=-1,
457    "No font loaded (use load_big_font or load_small_font)!");
458  f_wid=cache.foret(foretiles[0])->im->width();
459  f_hi=cache.foret(foretiles[0])->im->height();
460  b_wid=cache.backt(backtiles[0])->im->Size().x;
461  b_hi=cache.backt(backtiles[0])->im->Size().y;
462
463#if 0
464    if( should_save_sd_cache )
465    {
466        bFILE *save = open_file( cachepath, "wb" );
467        if( !save->open_failure() )
468        {
469            sd_cache.save( save );
470        }
471        delete save;
472    }
473#endif
474
475    sd_cache.clear();
476    past_startup = 1;
477#if 0
478    free( cachepath );
479#endif
480}
481
482
483
484
485
486char *load_script(char *name)
487{
488  char fn[100];
489  char *s;
490
491  sprintf(fn,"%s",name);
492  bFILE *fp=open_file(fn,"rb");
493  if (fp->open_failure())
494  {
495    delete fp;
496    return NULL;
497  }
498
499  long l=fp->file_size();
500  s=(char *)malloc(l+1);
501  ERROR(s,"Malloc error in load_script");
502
503  fp->read(s,l);
504  s[l]=0;
505  delete fp;
506  return s;
507}
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
Note: See TracBrowser for help on using the repository browser.