-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathCache.h
129 lines (110 loc) · 4.92 KB
/
Cache.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/////////////////////////////////////////
//
// OpenLieroX
//
// Auxiliary Software class library
//
// based on the work of JasonB
// enhanced by Dark Charlie and Albert Zeyer
//
// code under LGPL
//
/////////////////////////////////////////
// Cache system
// Created 7/11/01
// By Jason Boettcher
#ifndef __CACHE_H__
#define __CACHE_H__
#include <SDL.h>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <cassert>
#include "SmartPointer.h"
#include "olx-types.h"
// these forward-declaration are needed here
// they will be declared in CMap.h and CGameScript.h
class SoundSample;
class CMap;
class CGameScript;
class CCache {
public:
CCache() { mutex = SDL_CreateMutex(); };
~CCache() { SDL_DestroyMutex(mutex); };
CCache(const CCache&) { assert(false); }
CCache& operator=(const CCache&) { assert(false); return *this; }
void Clear();
void ClearSounds();
void ClearExtraEntries(); // Clears cache partially - should be called from time to time
SmartPointer<SDL_Surface> GetImage__unsafe(const std::string& file);
SmartPointer<SoundSample> GetSound(const std::string& file);
SmartPointer<CMap> GetMap(const std::string& file);
SmartPointer<CGameScript> GetMod(const std::string& dir);
// Images and sounds and mods are stored in cache by reference,
// no copying is done, so it is required to use SmartPointer returned,
// if you don't want cache system to delete your image right after you've loaded it.
// Oh, don't ever call gfxFreeSurface() or FreeSoundSample() - cache will do that for you.
void SaveImage__unsafe(const std::string& file, const SmartPointer<SDL_Surface> & img);
void SaveSound(const std::string& file, const SmartPointer<SoundSample> & smp);
void SaveMod(const std::string& dir, const SmartPointer<CGameScript> & mod);
// Map is copied to cache, 'cause it will be modified during game - you should free your data yourself.
void SaveMap(const std::string& file, CMap *map);
size_t GetCacheSize();
size_t GetEntryCount();
SDL_mutex* mutex;
private:
class CacheItem_t { public:
//virtual ~CacheItem_t() {} // TODO: do we need this when there is no other virtual function?
CacheItem_t() : fSaveTime(0), iFileTimeStamp(0) {}
CacheItem_t(const CacheItem_t& oth) { operator=(oth); }
CacheItem_t(const AbsTime& savetime, Uint64 timestamp) : fSaveTime(savetime), iFileTimeStamp(timestamp) {}
// TODO: this op= was virtual; was there any reason for this? (no other class had overloaded it)
CacheItem_t& operator=(const CacheItem_t& oth) { if (&oth != this) { fSaveTime = oth.fSaveTime; iFileTimeStamp = oth.iFileTimeStamp; } return *this; }
AbsTime fSaveTime;
Uint64 iFileTimeStamp;
};
class ImageItem_t : public CacheItem_t { public:
ImageItem_t() : CacheItem_t() { bmpSurf = NULL; }
ImageItem_t(const ImageItem_t& oth) { operator=(oth); }
ImageItem_t(SmartPointer<SDL_Surface> img, const AbsTime& savetime, Uint64 timestamp) : CacheItem_t(savetime, timestamp) { bmpSurf = img; }
ImageItem_t& operator=(const ImageItem_t& oth) { CacheItem_t::operator =(oth); if (&oth != this) { bmpSurf = oth.bmpSurf; } return *this; }
SmartPointer<SDL_Surface> bmpSurf;
};
class SoundItem_t : public CacheItem_t { public:
SoundItem_t() : CacheItem_t() { sndSample = NULL; }
SoundItem_t(const SoundItem_t& oth) { operator=(oth); }
SoundItem_t(SmartPointer<SoundSample> snd, const AbsTime& savetime, Uint64 timestamp) : CacheItem_t(savetime, timestamp) { sndSample = snd; }
SoundItem_t& operator=(const SoundItem_t& oth) { CacheItem_t::operator =(oth); if (&oth != this) { sndSample = oth.sndSample; } return *this; }
SmartPointer<SoundSample> sndSample;
};
class MapItem_t : public CacheItem_t { public:
MapItem_t() : CacheItem_t() { tMap = NULL; }
MapItem_t(const MapItem_t& oth) { operator=(oth); }
MapItem_t(SmartPointer<CMap> map, const AbsTime& savetime, Uint64 timestamp) : CacheItem_t(savetime, timestamp) { tMap = map; }
MapItem_t& operator=(const MapItem_t& oth) { CacheItem_t::operator =(oth); if (&oth != this) { tMap = oth.tMap; } return *this; }
SmartPointer<CMap> tMap;
};
class ModItem_t : public CacheItem_t { public:
ModItem_t() : CacheItem_t() { tMod = NULL; }
ModItem_t(const ModItem_t& oth) { operator=(oth); }
ModItem_t(SmartPointer<CGameScript> mod, const AbsTime& savetime, Uint64 timestamp) : CacheItem_t(savetime, timestamp) { tMod = mod; }
ModItem_t& operator=(const ModItem_t& oth) { CacheItem_t::operator =(oth); if (&oth != this) { tMod = oth.tMod; } return *this; }
SmartPointer<CGameScript> tMod;
};
typedef std::map<std::string, ImageItem_t > ImageCache_t;
ImageCache_t ImageCache;
typedef std::map<std::string, SoundItem_t > SoundCache_t;
SoundCache_t SoundCache;
typedef std::map<std::string, MapItem_t > MapCache_t;
MapCache_t MapCache;
typedef std::map<std::string, ModItem_t > ModCache_t;
ModCache_t ModCache;
};
extern CCache cCache;
// Debug functions
#ifdef DEBUG
void InitCacheDebug();
void ShutdownCacheDebug();
#endif
#endif // __CACHE_H__