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