source: abuse/trunk/src/imlib/mdlread.cpp @ 90

Last change on this file since 90 was 56, checked in by Sam Hocevar, 12 years ago
  • Add licensing terms to most C / C++ files (Ref #5).
File size: 5.6 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *
5 *  This software was released into the Public Domain. As with most public
6 *  domain software, no warranty is made or implied by Crack dot Com or
7 *  Jonathan Clark.
8 */
9
10#include "config.h"
11
12#include <stdio.h>
13
14#include "mdlread.hpp"
15#include "macs.hpp"
16#include "linked.hpp"
17#include "system.h"
18
19
20//  write_mdl take an array of pointer to images and a palette
21// and the generates a Moving DeLight file format containing all the
22// images.  Note, only the mode 320x200x256 is sopprted here for saving
23// images.  All images should be sized so they will fit on an mdl screen
24// but no checking of that is done hhere.
25void write_mdl(image **images, int16_t total_images, palette *pal,char *fn,
26                int16_t firstpage, int16_t images_per_page)
27{
28  FILE *fp;
29  char buf[18];
30  uint16_t xy[2],x;
31  char name[13],page;
32  unsigned char *c;
33  int16_t i;
34  palette *np;
35  clear_errors();
36  CONDITION(images && pal && fn && total_images>0,"bad parms");
37  CONDITION(pal->pal_size()==256,"MDL write only support 256 color images");
38
39  fp=fopen(fn,"wb");
40  if (!fp) set_error(imWRITE_ERROR);
41  else
42  { strcpy(buf,"JC20");            // Signature for mdl file
43    buf[4]=255;                    // 255 is graph driver 320x200x256
44    buf[5]=0;                      // graph mode 0 for this graph driver
45    buf[6]=19;                     // the BIOS mode is 19
46    fwrite(buf,7,1,fp);
47    np=pal->copy();                // make a copy before we change
48    np->shift(-COLOR_SHIFT);       // PC palette don't have 8 bit color regs
49    fwrite(np->addr(),1,768,fp);
50    delete np;                     // destroy the copy me made
51    memset(buf,0,11);              // 11 reserved bytes (0) follow
52    fwrite(buf,11,1,fp);
53    for (i=0;i<total_images;i++)
54    {
55      memset(buf,0,6);            // each image has 6 bytes of reserved 0
56      fwrite(buf,6,1,fp);
57      xy[0]=uint16_to_intel(i%100+20); xy[1]=uint16_to_intel(30);  // the x and y position on the screen
58      fwrite(xy,4,1,fp);
59      sprintf(name,"JC%-10d",i);  // set the name of the image
60      fwrite(name,12,1,fp);
61
62      page=firstpage+i/images_per_page;
63
64      fwrite(&page,1,1,fp);         // put all of the image on the first page
65      xy[0]=uint16_to_intel(images[i]->width()*images[i]->height()+4);  // calc the size of the image
66   
67      fwrite(xy,2,1,fp);
68      xy[0]=uint16_to_intel(images[i]->width());
69      fwrite(xy,2,1,fp);
70      xy[0]=uint16_to_intel(images[i]->height());
71      fwrite(xy,2,1,fp);
72      for (x=0;x<(uint16_t)images[i]->height();x++)   // write all the scan_lines for the
73      { c=images[i]->scan_line(x);            // image
74        fwrite(c,images[i]->width(),1,fp);
75      }
76    }
77    fclose(fp);                // close the file and make sure buffers empty
78  }
79}
80
81int16_t mdl_total_images(char *fn)
82{
83  char buf[800];
84  uint16_t xy[2],t;
85  FILE *fp;
86  fp=fopen(fn,"rb");
87  if (!fp)
88  { set_error(imFILE_NOT_FOUND);
89    return 0;
90  }
91  if (fread(buf,2,1,fp)!=1)
92    set_error(imFILE_CORRUPTED);
93  else if (buf[0]!='J' || buf[1]!='C')
94    set_error(imINCORRECT_FILETYPE);
95  else if (fread(buf,5,1,fp)!=1)
96    set_error(imFILE_CORRUPTED);
97  else if (buf[4]!=0x13)
98    set_error(imNOT_SUPPORTED);
99  if (current_error()) { fclose(fp); return 0;}
100  fread(buf,1,768+11,fp);
101  t=0;
102  while (!feof(fp))
103  { if (fread(buf,1,23,fp)==23)
104    {
105      fread(xy,2,1,fp);
106      xy[0]=uint16_to_local(xy[0]);
107      fseek(fp,xy[0],SEEK_CUR);
108      t++;
109    }
110  }
111  fclose(fp);
112  return t;
113}
114
115// read_mdl returns an array containing pointers to all the desired images
116// and a palette that is read form the file
117// to load image numbers 4 through 9 let start =4, end=9
118image **read_mdl(char *fn, palette *&pal, int16_t startn, int16_t endn, int16_t &total)
119{
120  FILE *fp;
121  image **im;
122  char buf[50];
123  uint16_t xy[2],i,j;
124  clear_errors();
125  make_block(sizeof(FILE));
126  im=NULL;
127  total=0;
128  startn--;
129  CHECK(fn && (startn<=endn || endn==-1) && startn>=0);
130  fp=fopen(fn,"rb");
131  if (!fp)
132  { set_error(imFILE_NOT_FOUND);
133    return NULL;
134  }
135  if (fread(buf,2,1,fp)!=1)
136    set_error(imFILE_CORRUPTED);
137  else if (buf[0]!='J' || buf[1]!='C')
138    set_error(imINCORRECT_FILETYPE);
139  else if (fread(buf,5,1,fp)!=1)
140    set_error(imFILE_CORRUPTED);
141  else if (buf[4]!=0x13)
142    set_error(imNOT_SUPPORTED);
143  else
144  {
145    make_block(sizeof(palette));
146    pal=new palette(256);
147    if (!pal)
148    {  set_error(imMEMORY_ERROR); return NULL; }
149    if (fread(pal->addr(),1,768,fp)!=768)
150      set_error(imFILE_CORRUPTED);
151    else if (fread(buf,1,11,fp)!=11)
152      set_error(imFILE_CORRUPTED);
153    else
154    {
155      pal->shift(2);
156      pal->set_all_used();
157      while (startn && !current_error())
158      { if (fread(buf,1,23,fp)!=23)
159          set_error(imFILE_CORRUPTED);
160        fread(xy,2,1,fp);
161        xy[0]=uint16_to_local(xy[0]);
162        fseek(fp,xy[0],SEEK_CUR);
163        startn--; if (endn>0) endn--;
164      }
165      if (!current_error())
166        im=(image **)jmalloc(sizeof(image *)*endn,"mdl_read::image * array");
167
168      while ((startn<endn || endn==-1) && !feof(fp) && !current_error())
169      {
170        if (fread(buf,1,23,fp)==23)
171        {
172          if (fread(&j,1,2,fp)!=2) set_error(imFILE_CORRUPTED);
173          else
174          {
175            j=uint16_to_local(j);
176            j-=4;
177            xy[0]=5; xy[1]=5;
178            if (fread(xy,1,4,fp)!=4) set_error(imFILE_CORRUPTED);
179            make_block(sizeof(image));
180            xy[0]=uint16_to_local(xy[0]);
181            xy[1]=uint16_to_local(xy[1]);
182            im[startn]=new image(xy[0],xy[1]);
183            total++;
184            for (i=0;i<xy[1];i++)
185              if (fread(im[startn]->scan_line(i),xy[0],1,fp)!=1)
186                set_error(imFILE_CORRUPTED);
187              else j-=xy[0];
188            if (j)
189              fseek(fp,j,SEEK_CUR);
190          }
191          startn++;
192        }
193      }
194    }
195  }
196  fclose(fp);
197  return im;
198}
Note: See TracBrowser for help on using the repository browser.