source: abuse/trunk/src/cache.h @ 503

Last change on this file since 503 was 503, checked in by Sam Hocevar, 11 years ago

cache: fix a crash caused by empty SFX objects being reused for other
purposes but still being deleted later on.

  • Property svn:keywords set to Id
File size: 3.7 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 __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
23class level;
24
25class CrcedFile
26{
27    friend class CrcManager;
28
29protected:
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
39class CrcManager
40{
41private:
42    int total_files;
43    CrcedFile **files;
44
45public:
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 *  - trans_image
65 */
66
67struct CacheItem
68{
69    friend class CacheList;
70
71protected:
72    void *data;
73    int32_t last_access;
74    uint8_t type;
75    int16_t file_number;
76    int32_t offset;
77};
78
79class CacheList
80{
81private:
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,*cache_file,*cache_read_file;
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    int16_t lcache_number;
92
93    int AllocId();
94    void locate(CacheItem *i, int local_only = 0); // set up file and offset for this item
95    void normalize();
96    void unmalloc(CacheItem *i);
97    int used, // flag set when disk is accessed
98        ful;  // set when stuff has to be thrown out
99    int *prof_data; // holds counts for each id
100    void preload_cache_object(int type);
101    void preload_cache(level *lev);
102
103public:
104    CacheList();
105    ~CacheList();
106
107    void create_lcache();
108    void free_oldest();
109    int in_use() { if (used) { used = 0; return 1; } else return 0; }
110    int full() { if (ful) { ful = 0; return 1; } else return 0; }
111    int reg_object(char const *filename, LObject *object, int type,
112                   int rm_dups); // lisp object
113    int reg(char const *filename, char const *name, int type = -1,
114                int rm_dups = 0); // returns id to item
115    int loaded(int id);
116    void unreg(int id);
117    void note_need(int id);
118
119    backtile *backt(int id);
120    foretile *foret(int id);
121    figure *fig(int id);
122    image *img(int id);
123    part_frame *part(int id);
124    sound_effect *sfx(int id);
125    LObject *lblock(int id);
126    char_tint *ctint(int id);
127
128    void prof_init();
129    void prof_write(bFILE *fp);
130    void prof_uninit();
131    int  prof_size(); // sizeof of spec entry that will be saved
132    void prof_poll_start();
133    void prof_poll_end();
134    int  prof_is_on() { return prof_data != NULL; }   // so level knows weither to save prof info or not
135    int compare(int a, int b); // compares usage count (used by qsort)
136    int offset_compare(int a, int b);
137
138    void load_cache_prof_info(char *filename, level *lev);
139    // sarray is a index table sorted by offset/filenum
140    int search(int *sarray, uint16_t filenum, int32_t offset);
141
142    void show_accessed();
143    void empty();
144};
145
146extern CacheList cache;
147extern CrcManager crc_manager;
148
149#endif
150
Note: See TracBrowser for help on using the repository browser.