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

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

imlib: rename trans_image to TImage. The code is now clean enough.

  • Property svn:keywords set to Id
File size: 3.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 __CASHE_HPP_
12#define __CASHE_HPP_
13
14#include <stdlib.h>
[62]15
[481]16#include "lisp/lisp.h"
17#include "sdlport/sound.h"
[62]18
[481]19#include "specs.h"
20#include "items.h"
21#include "particle.h"
[2]22
23class level;
24
[123]25class CrcedFile
26{
27    friend class CrcManager;
[2]28
[123]29protected:
30    CrcedFile(char const *name);
31    ~CrcedFile();
[2]32
[123]33    int crc_calculated;
34    uint32_t crc;
35    char *filename;
36} ;
[2]37
[123]38// stores crc for each file open to avoid redundant calculations
39class CrcManager
[2]40{
[123]41private:
42    int total_files;
43    CrcedFile **files;
[2]44
[123]45public:
46    CrcManager();
[2]47
[123]48    int get_filenumber(char const *filename);
[503]49    uint32_t get_crc(int filenumber, int &failed);
50    void set_crc(int filenumber, uint32_t crc);
51    char *get_filename(int filenumber);
[123]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};
[2]57
[123]58/* Cache item types:
59 *  - foretile
60 *  - backtile
61 *  - character
62 *  - sound
63 *  - image
[533]64 *  - TImage
[123]65 */
[2]66
[123]67struct CacheItem
[2]68{
[123]69    friend class CacheList;
[2]70
[123]71protected:
72    void *data;
[124]73    int32_t last_access;
[123]74    uint8_t type;
75    int16_t file_number;
76    int32_t offset;
77};
[2]78
[123]79class CacheList
[2]80{
[123]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
[2]85
[506]86    bFILE *fp;
[123]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
[2]90
[503]91    int AllocId();
[123]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);
[2]100
[123]101public:
102    CacheList();
103    ~CacheList();
[2]104
[123]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; }
[503]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,
[123]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);
[2]115
[123]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);
[503]122    LObject *lblock(int id);
[123]123    char_tint *ctint(int id);
[2]124
[123]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);
[2]134
[123]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);
[2]138
[123]139    void show_accessed();
140    void empty();
141};
[2]142
[123]143extern CacheList cache;
144extern CrcManager crc_manager;
145
[2]146#endif
147
Note: See TracBrowser for help on using the repository browser.