source: abuse/trunk/src/imlib/specs.h @ 682

Last change on this file since 682 was 567, checked in by Sam Hocevar, 10 years ago

imlib: add explicit enum values for easier source code reading.

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