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

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

imlib: some cleaning up in trans_image before I templatise half of it.

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