source: abuse/branches/lol/src/imlib/specs.h @ 729

Last change on this file since 729 was 729, checked in by Sam Hocevar, 8 years ago

build: sync with newer Lol Engine changes.

  • Property svn:keywords set to Id
File size: 6.3 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, by
8 *  Jonathan Clark, or by Sam Hocevar.
9 */
10
11#ifndef __SPECS_HPP_
12#define __SPECS_HPP_
13
14#include <stdio.h>
15#include <fcntl.h>
16#include <stdlib.h>
17#include <stdint.h>
18#include <string.h>
19
20extern char const *spec_types[];
21
22enum
23{
24    SPEC_INVALID_TYPE = 0,
25    SPEC_COLOR_TABLE = 1,
26    SPEC_PALETTE = 2,
27    /* Empty slot */
28    SPEC_IMAGE = 4,
29    SPEC_FORETILE = 5,
30    SPEC_BACKTILE = 6,
31    SPEC_CHARACTER = 7,
32    SPEC_MORPH_POINTS_8 = 8,
33    SPEC_MORPH_POINTS_16 = 9,
34    SPEC_GRUE_OBJS = 10,
35    SPEC_EXTERN_SFX = 11,
36    SPEC_DMX_MUS = 12,
37    SPEC_PATCHED_MORPH = 13,
38    SPEC_NORMAL_FILE = 14,
39    SPEC_COMPRESS1_FILE = 15,
40    SPEC_VECTOR_IMAGE = 16,
41    SPEC_LIGHT_LIST = 17,
42    SPEC_GRUE_FGMAP = 18,
43    SPEC_GRUE_BGMAP = 19,
44    SPEC_DATA_ARRAY = 20,
45    SPEC_CHARACTER2 = 21,
46    SPEC_PARTICLE = 22,
47    SPEC_EXTERNAL_LCACHE = 23,
48};
49
50#define SPEC_SIGNATURE    "SPEC1.0"
51#define SPEC_SIG_SIZE     8
52
53#define SPEC_FLAG_LINK    1
54
55#define SPEC_SEARCH_INSIDE_OUTSIDE 1
56#define SPEC_SEARCH_OUTSIDE_INSIDE 2
57#define SPEC_SEARCH_INSIDE_ONLY    3
58
59/*  struct spec_header
60 *  {
61 *      char signature[8];
62 *      uint16_t entries_count;
63 *      struct entry
64 *      {
65 *          uint8_t type;
66 *          uint8_t name_length;
67 *          char name[name_length];
68 *          uint8_t flags;
69 *          if (flags & LINK)
70 *          {
71 *              uint8_t filename_length;
72 *              char filename[filename_length];
73 *          }
74 *          else
75 *          {
76 *              uint32_t data_size;
77 *              uint32_t offset;
78 *          }
79 *      } entries[entries_count];
80 *  }
81 */
82
83void set_spec_main_file(char const *filename, int search_order=SPEC_SEARCH_OUTSIDE_INSIDE);
84
85void set_filename_prefix(char const *prefix);
86char *get_filename_prefix();
87void set_save_filename_prefix(char const *prefix);
88char *get_save_filename_prefix();
89#define JFILE_CLONED 1
90
91class bFILE     // base file type which other files should be derived from (jFILE & NFS for now)
92{
93  protected :
94  unsigned char *rbuf,*wbuf;
95  unsigned long rbuf_start,rbuf_end,rbuf_size,
96                wbuf_end,wbuf_size;                // can't seek while writing!
97  int flush_writes();                             // returns 0 on failure, else # of bytes written
98
99  virtual int unbuffered_read(void *buf, size_t count)  = 0;
100  virtual int unbuffered_write(void const *buf, size_t count) = 0;
101  virtual int unbuffered_tell()                         = 0;
102  virtual int unbuffered_seek(long offset, int whence)  = 0;   // whence=SEEK_SET, SEEK_CUR,
103                                                               // SEEK_END, ret=0=success
104  virtual int allow_read_buffering();
105  virtual int allow_write_buffering();
106  public :
107  bFILE();
108  virtual int open_failure() = 0;
109  int read(void *buf, size_t count);        // returns number of bytes read, calls unbuffer_read
110  int write(void const *buf, size_t count); // returns number of bytes written
111  int seek(long offset, int whence);        // whence=SEEK_SET, SEEK_CUR, SEEK_END, ret=0=success
112  int tell();
113  virtual int file_size() = 0;
114
115  virtual ~bFILE();
116
117    // read and write using little-endianness
118    uint16_t read_uint16();
119    uint32_t read_uint32();
120    uint8_t read_uint8();
121    double read_double();
122    void write_uint16(uint16_t x);
123    void write_uint32(uint32_t x);
124    void write_uint8(uint8_t x);
125    void write_double(double x);
126};
127
128class jFILE : public bFILE     // this file type will use virtual opens inside of a spe
129{
130  char *fname;
131  char *tmp_write_name;
132  int access;
133  int fd,flags;
134  long start_offset,file_length;    // offset of file from actual file begining
135
136  long current_offset;  // current offset
137
138public :
139    int get_fd() const { return fd; }
140
141  void open_internal(char const *filename, char const *mode, int flags);
142  void open_external(char const *filename, char const *mode, int flags);
143
144  jFILE(char const *filename, char const *access_string);      // same as fopen parameters
145  jFILE(FILE *file_pointer);                      // assumes fp is at begining of file
146  virtual int open_failure() { return fd<0; }
147  virtual int unbuffered_read(void *buf, size_t count);       // returns number of bytes read
148  virtual int unbuffered_write(void const *buf, size_t count);     // returns number of bytes written
149  virtual int unbuffered_seek(long offset, int whence);      // whence=SEEK_SET, SEEK_CUR,
150                                                             // SEEK_END, ret=0=success
151  virtual int unbuffered_tell();
152  virtual int file_size() { return file_length; }
153  virtual ~jFILE();
154} ;
155
156class SpecEntry
157{
158public:
159    SpecEntry(uint8_t spec_type, char const *object_name,
160              char const *link_filename,
161              unsigned long data_size, unsigned long data_offset);
162    ~SpecEntry();
163
164    void Print();
165
166    char *name;
167    void *data;
168    unsigned long size, offset;
169    uint8_t type;
170};
171
172
173class SpecDir
174{
175public:
176    SpecDir(FILE *fp);
177    SpecDir(bFILE *fp);
178    SpecDir();
179    ~SpecDir();
180
181    void startup(bFILE *fp);
182    void FullyLoad(bFILE *fp);
183
184    void Print();
185
186  SpecEntry *find(char const *name);
187  SpecEntry *find(char const *name, int type);
188  SpecEntry *find(int type);
189  int find_number(char const *name);
190  int find_number(int type);
191  void remove(SpecEntry *e);
192  void add_by_hand(SpecEntry *e);
193  void calc_offsets();
194  long data_start_offset();  // returns the first offset past directory items
195  long data_end_offset();    // this should be the end of the file
196  long type_total(int type);
197  jFILE *write(char const *filename);
198  int    write(bFILE *fp);
199  void delete_entries();   // if the directory was created by hand instead of by file
200
201    array<SpecEntry *> m_entries;
202    array<uint8_t> m_data;
203};
204
205uint16_t read_uint16(FILE *fp);
206uint32_t read_uint32(FILE *fp);
207uint8_t read_uint8(FILE *fp);
208
209void write_uint16(FILE *fp, uint16_t x);
210void write_uint32(FILE *fp, uint32_t x);
211void write_uint8(FILE *fp, uint8_t x);
212
213void set_spec_main_file(char *filename, int Search_order);
214void set_file_opener(bFILE *(*open_fun)(char const *, char const *));
215void set_no_space_handler(void (*handle_fun)());
216bFILE *open_file(char const *filename, char const *mode);
217#endif
218
Note: See TracBrowser for help on using the repository browser.