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

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

imlib: implement spec_directory::FullyLoad? to force loading all
assets into memory.

  • Property svn:keywords set to Id
File size: 6.6 KB
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
[494]4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
[56]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
[2]11#ifndef __SPECS_HPP_
12#define __SPECS_HPP_
[131]13
[2]14#include <stdio.h>
15#include <fcntl.h>
16#include <stdlib.h>
[7]17#include <stdint.h>
[131]18#include <string.h>
[2]19
[481]20#include "linked.h"
[129]21
[39]22extern char const *spec_types[];
[2]23
[539]24enum
25{
26    SPEC_INVALID_TYPE = 0,
27    SPEC_COLOR_TABLE,
28    SPEC_PALETTE,
29    SPEC_, /* Unused */
30    SPEC_IMAGE,
31    SPEC_FORETILE,
32    SPEC_BACKTILE,
33    SPEC_CHARACTER,
34    SPEC_MORPH_POINTS_8,
35    SPEC_MORPH_POINTS_16,
36    SPEC_GRUE_OBJS,
37    SPEC_EXTERN_SFX,
38    SPEC_DMX_MUS,
39    SPEC_PATCHED_MORPH,
40    SPEC_NORMAL_FILE,
41    SPEC_COMPRESS1_FILE,
42    SPEC_VECTOR_IMAGE,
43    SPEC_LIGHT_LIST,
44    SPEC_GRUE_FGMAP,
45    SPEC_GRUE_BGMAP,
46    SPEC_DATA_ARRAY,
47    SPEC_CHARACTER2,
48    SPEC_PARTICLE,
49    SPEC_EXTERNAL_LCACHE,
50};
[2]51
[124]52#define SPEC_SIGNATURE    "SPEC1.0"
[2]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
[539]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 */
[2]84
[39]85void set_spec_main_file(char const *filename, int search_order=SPEC_SEARCH_OUTSIDE_INSIDE);
[2]86
[39]87void set_filename_prefix(char const *prefix);
[2]88char *get_filename_prefix();
[39]89void set_save_filename_prefix(char const *prefix);
[2]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;
[39]102  virtual int unbuffered_write(void const *buf, size_t count) = 0;
[2]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;
[39]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
[2]114  int tell();
115  virtual int file_size() = 0;
116
117  virtual ~bFILE();
118
[112]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};
[2]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
[124]139
[2]140public :
[124]141    int get_fd() const { return fd; }
[2]142
[39]143  void open_internal(char const *filename, char const *mode, int flags);
144  void open_external(char const *filename, char const *mode, int flags);
[2]145
[39]146  jFILE(char const *filename, char const *access_string);      // same as fopen parameters
[2]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
[39]150  virtual int unbuffered_write(void const *buf, size_t count);     // returns number of bytes written
[124]151  virtual int unbuffered_seek(long offset, int whence);      // whence=SEEK_SET, SEEK_CUR,
[2]152                                                             // SEEK_END, ret=0=success
153  virtual int unbuffered_tell();
154  virtual int file_size() { return file_length; }
155  virtual ~jFILE();
[124]156} ;
[2]157
158class spec_entry
159{
[546]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();
[124]165
[546]166    void Print();
[2]167
[546]168    char *name;
169    void *data;
170    unsigned long size, offset;
171    uint8_t type;
172};
[2]173
[546]174
[2]175class spec_directory
176{
177public :
[546]178    spec_directory(FILE *fp);
179    spec_directory(bFILE *fp);
180    spec_directory();
181    ~spec_directory();
[2]182
[546]183    void startup(bFILE *fp);
184    void FullyLoad(bFILE *fp);
185
[494]186//  spec_directory(char *filename);  ; ; not allowed anymore, user must construct file first!
[39]187  spec_entry *find(char const *name);
188  spec_entry *find(char const *name, int type);
[2]189  spec_entry *find(int type);
[39]190  long find_number(char const *name);
[2]191  long find_number(int type);
192  void remove(spec_entry *e);
193  void add_by_hand(spec_entry *e);
[124]194  void calc_offsets();
[112]195  long data_start_offset();  // returns the first offset past directory items
[2]196  long data_end_offset();    // this should be the end of the file
197  long type_total(int type);
[124]198  jFILE *write(char const *filename);
199  int    write(bFILE *fp);
[2]200  void print();
201  void delete_entries();   // if the directory was created by hand instead of by file
202
[546]203    int total;
204    spec_entry **entries;
205    void *data;
206    size_t size;
207};
208
[2]209/*jFILE *add_directory_entry(char *filename,
210                         unsigned short data_type,
211                         char *data_name,
212                         unsigned long data_size,
[494]213                         char *link_filename=NULL); */
[2]214
[17]215uint16_t read_uint16(FILE *fp);
216uint32_t read_uint32(FILE *fp);
217uint8_t read_uint8(FILE *fp);
[2]218
[17]219void write_uint16(FILE *fp, uint16_t x);
220void write_uint32(FILE *fp, uint32_t x);
221void write_uint8(FILE *fp, uint8_t x);
[2]222
223void set_spec_main_file(char *filename, int Search_order);
[39]224void set_file_opener(bFILE *(*open_fun)(char const *, char const *));
[2]225void set_no_space_handler(void (*handle_fun)());
[39]226bFILE *open_file(char const *filename, char const *mode);
[2]227#endif
228
Note: See TracBrowser for help on using the repository browser.