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

Last change on this file since 512 was 494, checked in by Sam Hocevar, 12 years ago

style: remove trailing spaces, fix copyright statements.

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