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

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

Fuck the history, I'm renaming all .hpp files to .h for my own sanity.

  • Property svn:keywords set to Id
File size: 7.0 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#ifndef __SPECS_HPP_
11#define __SPECS_HPP_
12
13#include <stdio.h>
14#include <fcntl.h>
15#include <stdlib.h>
16#include <stdint.h>
17#include <string.h>
18
19#include "linked.h"
20#include "system.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    void set_read_buffer_size(long size);
118};
119
120class jFILE : public bFILE     // this file type will use virtual opens inside of a spe
121{
122  char *fname;
123  char *tmp_write_name;
124  int access;
125  int fd,flags;
126  long start_offset,file_length;    // offset of file from actual file begining
127
128  long current_offset;  // current offset
129
130public :
131    int get_fd() const { return fd; }
132
133  void open_internal(char const *filename, char const *mode, int flags);
134  void open_external(char const *filename, char const *mode, int flags);
135
136  jFILE(char const *filename, char const *access_string);      // same as fopen parameters
137  jFILE(FILE *file_pointer);                      // assumes fp is at begining of file
138  virtual int open_failure() { return fd<0; }
139  virtual int unbuffered_read(void *buf, size_t count);       // returns number of bytes read
140  virtual int unbuffered_write(void const *buf, size_t count);     // returns number of bytes written
141  virtual int unbuffered_seek(long offset, int whence);      // whence=SEEK_SET, SEEK_CUR,
142                                                             // SEEK_END, ret=0=success
143  virtual int unbuffered_tell();
144  virtual int file_size() { return file_length; }
145  virtual ~jFILE();
146} ;
147
148class spec_entry
149{
150public :
151  char *name;
152  unsigned long size,offset;
153  unsigned char type;
154
155  spec_entry(unsigned char spec_type,
156             char const *object_name,
157             char const *link_filename,
158             unsigned long data_size,
159             unsigned long data_offset)
160  { type = spec_type;
161    name = strdup(object_name);
162    size = data_size;
163    offset = data_offset;
164  }
165  void print();
166  ~spec_entry() { if (name) free(name); }
167} ;
168
169
170class spec_directory
171{
172public :
173  void startup(bFILE *fp);
174
175  int total;
176  spec_entry **entries;
177  void *data;
178  long size;
179//  spec_directory(char *filename);  ;; not allowed anymore, user must construct file first!
180  spec_directory(FILE *fp);
181  spec_directory(bFILE *fp);
182  spec_directory();
183  spec_entry *find(char const *name);
184  spec_entry *find(char const *name, int type);
185  spec_entry *find(int type);
186  long find_number(char const *name);
187  long find_number(int type);
188  void remove(spec_entry *e);
189  void add_by_hand(spec_entry *e);
190  void calc_offsets();
191  long data_start_offset();  // returns the first offset past directory items
192  long data_end_offset();    // this should be the end of the file
193  long type_total(int type);
194  jFILE *write(char const *filename);
195  int    write(bFILE *fp);
196  void print();
197  void delete_entries();   // if the directory was created by hand instead of by file
198  ~spec_directory();
199} ;
200
201/*jFILE *add_directory_entry(char *filename,
202                         unsigned short data_type,
203                         char *data_name,
204                         unsigned long data_size,
205                         char *link_filename=NULL);*/
206
207uint16_t read_uint16(FILE *fp);
208uint32_t read_uint32(FILE *fp);
209uint32_t read_other_uint32(FILE *fp);
210uint16_t read_other_uint16(FILE *fp);
211uint8_t read_uint8(FILE *fp);
212
213void write_uint16(FILE *fp, uint16_t x);
214void write_uint32(FILE *fp, uint32_t x);
215void write_other_uint16(FILE *fp, uint16_t x);
216void write_other_uint32(FILE *fp, uint32_t x);
217void write_uint8(FILE *fp, uint8_t x);
218
219void set_spec_main_file(char *filename, int Search_order);
220void set_file_opener(bFILE *(*open_fun)(char const *, char const *));
221void set_no_space_handler(void (*handle_fun)());
222bFILE *open_file(char const *filename, char const *mode);
223#endif
224
225
226
227
228
229
230
Note: See TracBrowser for help on using the repository browser.