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 __CASHE_HPP_ |
---|
12 | #define __CASHE_HPP_ |
---|
13 | |
---|
14 | #include <stdlib.h> |
---|
15 | |
---|
16 | #include "lisp/lisp.h" |
---|
17 | #include "sdlport/sound.h" |
---|
18 | |
---|
19 | #include "specs.h" |
---|
20 | #include "items.h" |
---|
21 | #include "particle.h" |
---|
22 | |
---|
23 | class level; |
---|
24 | |
---|
25 | class CrcedFile |
---|
26 | { |
---|
27 | friend class CrcManager; |
---|
28 | |
---|
29 | protected: |
---|
30 | CrcedFile(char const *name); |
---|
31 | ~CrcedFile(); |
---|
32 | |
---|
33 | int crc_calculated; |
---|
34 | uint32_t crc; |
---|
35 | char *filename; |
---|
36 | } ; |
---|
37 | |
---|
38 | // stores crc for each file open to avoid redundant calculations |
---|
39 | class CrcManager |
---|
40 | { |
---|
41 | private: |
---|
42 | int total_files; |
---|
43 | CrcedFile **files; |
---|
44 | |
---|
45 | public: |
---|
46 | CrcManager(); |
---|
47 | |
---|
48 | int get_filenumber(char const *filename); |
---|
49 | uint32_t get_crc(int filenumber, int &failed); |
---|
50 | void set_crc(int filenumber, uint32_t crc); |
---|
51 | char *get_filename(int filenumber); |
---|
52 | void clean_up(); |
---|
53 | int total_filenames() { return total_files; } |
---|
54 | int write_crc_file(char const *filename); |
---|
55 | int load_crc_file(char const *filename); |
---|
56 | }; |
---|
57 | |
---|
58 | /* Cache item types: |
---|
59 | * - foretile |
---|
60 | * - backtile |
---|
61 | * - character |
---|
62 | * - sound |
---|
63 | * - image |
---|
64 | * - TransImage |
---|
65 | */ |
---|
66 | |
---|
67 | struct CacheItem |
---|
68 | { |
---|
69 | friend class CacheList; |
---|
70 | |
---|
71 | protected: |
---|
72 | void *data; |
---|
73 | int32_t last_access; |
---|
74 | uint8_t type; |
---|
75 | int16_t file_number; |
---|
76 | int32_t offset; |
---|
77 | }; |
---|
78 | |
---|
79 | class CacheList |
---|
80 | { |
---|
81 | private: |
---|
82 | CacheItem *list; |
---|
83 | int32_t total, last_registered, last_access, poll_start_access; |
---|
84 | int16_t last_file; // for speed leave the last file accessed open |
---|
85 | |
---|
86 | bFILE *fp; |
---|
87 | spec_directory *last_dir; |
---|
88 | int32_t last_offset; // store the last offset so we don't have to seek if |
---|
89 | // we don't need to |
---|
90 | |
---|
91 | int AllocId(); |
---|
92 | void locate(CacheItem *i, int local_only = 0); // set up file and offset for this item |
---|
93 | void normalize(); |
---|
94 | void unmalloc(CacheItem *i); |
---|
95 | int used, // flag set when disk is accessed |
---|
96 | ful; // set when stuff has to be thrown out |
---|
97 | int *prof_data; // holds counts for each id |
---|
98 | void preload_cache_object(int type); |
---|
99 | void preload_cache(level *lev); |
---|
100 | |
---|
101 | public: |
---|
102 | CacheList(); |
---|
103 | ~CacheList(); |
---|
104 | |
---|
105 | void free_oldest(); |
---|
106 | int in_use() { if (used) { used = 0; return 1; } else return 0; } |
---|
107 | int full() { if (ful) { ful = 0; return 1; } else return 0; } |
---|
108 | int reg_object(char const *filename, LObject *object, int type, |
---|
109 | int rm_dups); // lisp object |
---|
110 | int reg(char const *filename, char const *name, int type = -1, |
---|
111 | int rm_dups = 0); // returns id to item |
---|
112 | int loaded(int id); |
---|
113 | void unreg(int id); |
---|
114 | void note_need(int id); |
---|
115 | |
---|
116 | backtile *backt(int id); |
---|
117 | foretile *foret(int id); |
---|
118 | figure *fig(int id); |
---|
119 | image *img(int id); |
---|
120 | part_frame *part(int id); |
---|
121 | sound_effect *sfx(int id); |
---|
122 | LObject *lblock(int id); |
---|
123 | char_tint *ctint(int id); |
---|
124 | |
---|
125 | void prof_init(); |
---|
126 | void prof_write(bFILE *fp); |
---|
127 | void prof_uninit(); |
---|
128 | int prof_size(); // sizeof of spec entry that will be saved |
---|
129 | void prof_poll_start(); |
---|
130 | void prof_poll_end(); |
---|
131 | int prof_is_on() { return prof_data != NULL; } // so level knows weither to save prof info or not |
---|
132 | int compare(int a, int b); // compares usage count (used by qsort) |
---|
133 | int offset_compare(int a, int b); |
---|
134 | |
---|
135 | void load_cache_prof_info(char *filename, level *lev); |
---|
136 | // sarray is a index table sorted by offset/filenum |
---|
137 | int search(int *sarray, uint16_t filenum, int32_t offset); |
---|
138 | |
---|
139 | void show_accessed(); |
---|
140 | void empty(); |
---|
141 | }; |
---|
142 | |
---|
143 | extern CacheList cache; |
---|
144 | extern CrcManager crc_manager; |
---|
145 | |
---|
146 | #endif |
---|
147 | |
---|