source: abuse/trunk/src/imlib/glread.cpp @ 56

Last change on this file since 56 was 56, checked in by Sam Hocevar, 11 years ago
  • Add licensing terms to most C / C++ files (Ref #5).
File size: 3.2 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 "macs.hpp"
15#include "image.hpp"
16#include "palette.hpp"
17#include "video.hpp"
18#include "system.h"
19
20image *read_glfont(char *fn)
21{
22  image *im,*sub;
23  uint16_t length,y;
24  uint8_t size,first,width,height,gsize,last;
25  FILE *fp;
26  fp=fopen(fn,"rb");
27  if (!fp) return NULL;
28  fread(&length,1,2,fp);  length=uint16_to_local(length);
29  fread(&size,1,1,fp);
30  fread(&first,1,1,fp);
31  if (size+first>255) { set_error(imFILE_CORRUPTED); fclose(fp); return NULL; }
32  fread(&width,1,1,fp);
33  fread(&height,1,1,fp);
34  fread(&gsize,1,1,fp);
35  make_block(sizeof(image));
36  im=new image(width*32,height*8);
37  make_block(sizeof(image));
38  sub=new image(width,height);
39  im->clear();  // in case all the fonts aren't in the file, clear extra area
40  last=first+size-1;
41  while (first<=last)
42  {
43    for (y=0;(int)y<(int)height;y++)
44    {
45      fread(sub->scan_line(y),1,gsize/height,fp);
46      sub->unpack_scanline(y);
47    }
48    sub->put_image(im,(first%32)*width,(first/32)*height);
49    first++;
50  }
51  delete sub;
52  return im;
53}
54
55image *read_pic(char *fn, palette *&pal)
56{
57  image *im;
58  char x[4],bpp;
59  uint8_t *sl=NULL,esc,c,n,marker,vmode;
60  uint16_t w,h,len,bufsize,blocks,sn,esize,edesc;
61  int xx,yy;
62  FILE *fp;
63  im=NULL;
64  fp=fopen(fn,"rb");
65
66  fread(&x[0],1,2,fp);
67  fread(&w,1,2,fp);
68  fread(&h,1,2,fp);
69  w=uint16_to_local(w);  h=uint16_to_local(h);
70  fread(x,1,4,fp);
71  fread(&bpp,1,1,fp);
72  fread(&marker,1,1,fp);
73  if (marker!=0xff)
74  { fclose(fp); set_error(imFILE_CORRUPTED); return NULL; }
75
76  im=new image(w,h);
77
78  fread(&vmode,1,1,fp);
79  fread(&edesc,1,2,fp);
80  edesc=uint16_to_local(edesc);
81  fread(&esize,1,2,fp);
82  esize=uint16_to_local(esize);
83  if (esize==768 && !pal)
84  { pal=new palette(1<<bpp);
85    fread(pal->addr(),1,(1<<bpp)*3,fp);
86    pal->shift(2);
87  }
88  else if (esize)
89    fseek(fp,esize,SEEK_CUR);
90  fread(&blocks,1,2,fp);
91  blocks=uint16_to_local(blocks);
92
93  yy=h; xx=w;
94
95  while (blocks-- && w>=1 && yy>=0)
96  {
97    fread(&bufsize,1,2,fp);
98    bufsize=uint16_to_local(bufsize);
99    fread(&len,1,2,fp);
100    len=uint16_to_local(len);
101    fread(&esc,1,1,fp);
102    while (yy>=0 && len)
103    {
104      fread(&c,1,1,fp);
105      if (c!=esc)
106      {
107        if (xx>=w) { yy--; xx=0; sl=im->scan_line(yy);
108        if (yy==h) printf("bufsize=%d\n",bufsize); CHECK(yy<h); }
109        sl[xx++]=c;     len--;
110      }
111      else
112      {
113        fread(&n,1,1,fp);
114        if (n!=0)
115        {
116          fread(&c,1,1,fp);
117          while (n-- && yy>=0 && len)
118          {
119            if (xx>=w) { yy--; xx=0; sl=im->scan_line(yy);
120              if (yy==h) printf("bufsize=%d\n",bufsize); CHECK(yy<h); }
121            sl[xx++]=c; len--;
122          }
123        }
124        else
125        {
126          fread(&sn,1,2,fp);
127          sn=uint16_to_local(sn);
128          fread(&c,1,1,fp);
129          while (sn-- && yy>=0 && len)
130          {
131            if (xx>=w) { yy--; xx=0; sl=im->scan_line(yy); CHECK(yy<h); }
132            sl[xx++]=c; len--;
133          }
134        }
135
136      }
137    }
138  }
139  fclose(fp);
140  return im;
141}
142
143image *read_clp(char *fn)
144{
145  palette *pal=NULL;
146  image *im=read_pic(fn,pal);
147  if (pal) delete pal;
148  return im;
149}
150
Note: See TracBrowser for help on using the repository browser.