source: abuse/branches/lol/src/cache.h @ 724

Last change on this file since 724 was 724, checked in by Sam Hocevar, 9 years ago

build: merge all static libraries into a single one.

  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2013 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 __CACHE_HPP_
12#define __CACHE_HPP_
13
14#include <stdlib.h>
15
16#include "lisp/lisp.h"
17
18#include "imlib/specs.h"
19
20#include "sdlport/sound.h"
21
22#include "items.h"
23#include "particle.h"
24
25class Level;
26
27class CrcedFile
28{
29    friend class CrcManager;
30
31protected:
32    CrcedFile(char const *name);
33    ~CrcedFile();
34
35    String m_name;
36    uint32_t m_crc;
37    int m_calculated;
38};
39
40// stores crc for each file open to avoid redundant calculations
41class CrcManager
42{
43public:
44    CrcManager();
45
46    int GetFileNumber(String const &filename);
47    String const &GetFileName(int filenumber);
48
49    uint32_t get_crc(int filenumber, int &failed);
50    void set_crc(int filenumber, uint32_t crc);
51    void clean_up();
52    int total_filenames() { return m_files.Count(); }
53    int write_crc_file(char const *filename);
54    int load_crc_file(char const *filename);
55
56private:
57    Array<CrcedFile *> m_files;
58};
59
60/* Cache item types:
61 *  - foretile
62 *  - backtile
63 *  - character
64 *  - sound
65 *  - image
66 *  - TransImage
67 */
68
69struct CacheItem
70{
71    friend class CacheList;
72
73protected:
74    void *data;
75    int32_t last_access;
76    uint8_t type;
77    int16_t file_number;
78    int32_t offset;
79};
80
81class CacheList
82{
83private:
84    CacheItem *list;
85    int32_t total, last_registered, last_access, poll_start_access;
86    int16_t last_file; // for speed leave the last file accessed open
87
88    bFILE *fp;
89    SpecDir *last_dir;
90    int32_t last_offset; // store the last offset so we don't have to seek if
91                         // we don't need to
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 free_oldest();
108    int in_use() { if (used) { used = 0; return 1; } else return 0; }
109    int full() { if (ful) { ful = 0; return 1; } else return 0; }
110    int reg_object(char const *filename, LObject *object, int type,
111                   int rm_dups); // lisp object
112    int reg(char const *filename, char const *name, int type = -1,
113                int rm_dups = 0); // returns id to item
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    AImage *img(int id);
122    part_frame *part(int id);
123    sound_effect *sfx(int id);
124    LObject *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(String const &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.