source: abuse/trunk/src/cache.hpp @ 123

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