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

Last change on this file since 534 was 534, checked in by Sam Hocevar, 11 years ago

imlib: remove a lot of dead code, especially from the linked list and
the WAV reader.

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