1 /*
2 Copyright (C) 2007, 2010 - Bit-Blot
3
4 This file is part of Aquaria.
5
6 Aquaria is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
15 See the GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21 #include "Precacher.h"
22 #include "Quad.h"
23 #include "Core.h"
24
Precacher()25 Precacher::Precacher()
26 {
27 loadProgressCallback = NULL;
28 cleaned = true;
29 }
30
~Precacher()31 Precacher::~Precacher()
32 {
33 if (!cleaned)
34 errorLog ("Precacher shutdown unclean");
35 }
36
setBaseDir(const std::string & dir)37 void Precacher::setBaseDir(const std::string& dir)
38 {
39 basedirOverride = dir;
40 }
41
clean()42 void Precacher::clean()
43 {
44 for (unsigned int i = 0; i < renderObjects.size(); i++)
45 {
46 RenderObject *r = renderObjects[i];
47 r->destroy();
48 delete r;
49 }
50 renderObjects.clear();
51 cleaned = true;
52 }
53
loadTextureRange(const std::string & file,const std::string & type,int start,int end)54 void Precacher::loadTextureRange(const std::string &file, const std::string &type, int start, int end)
55 {
56 for (int t = start; t < end; t++)
57 {
58
59 std::ostringstream num_os;
60 num_os << t;
61
62 std::ostringstream os;
63 os << file;
64
65 for (int j = 0; j < 4 - num_os.str().size(); j++)
66 {
67 os << "0";
68 }
69
70 os << t;
71
72 os << type;
73
74 precacheTex(os.str());
75 }
76 }
77
precacherCallback(const std::string & file,intptr_t param)78 void precacherCallback(const std::string &file, intptr_t param)
79 {
80 Precacher *p = (Precacher*)param;
81 p->precacheTex(file);
82 }
83
84 // precacheTex
85 // caches one texture
86 // also support simple wildcard to cache multiple textures
87 // e.g. naija/*.png
precacheTex(const std::string & tex)88 void Precacher::precacheTex(const std::string &tex)
89 {
90 if (tex.find("txt") != std::string::npos)
91 {
92 errorLog("Call precacheList to precache a text file of gfx names, not precacheTex!");
93 }
94 if (tex.empty()) return;
95
96 std::string basedir = basedirOverride.empty() ? core->getBaseTextureDirectory() : basedirOverride;
97
98 if (core->debugLogTextures)
99 debugLog("PRECACHING: " + tex);
100
101 if (tex.find('*')!=std::string::npos)
102 {
103 if (core->debugLogTextures)
104 debugLog("searching directory");
105
106 int loc = tex.find('*');
107 std::string path = tex.substr(0, loc);
108 std::string type = tex.substr(loc+1, tex.size());
109 path = basedir + path;
110 forEachFile(path, type, precacherCallback, (intptr_t)this);
111 return;
112 }
113 else
114 {
115 if (loadProgressCallback)
116 loadProgressCallback();
117 std::string t = tex;
118 if (tex.find(basedir) != std::string::npos)
119 {
120 t = tex.substr(basedir.size(), tex.size());
121 }
122 Quad *q = new Quad;
123 q->setTexture(t);
124 q->alpha = 0;
125 renderObjects.push_back(q);
126 cleaned = false;
127 }
128 }
129
precacheList(const std::string & list,void progressCallback ())130 void Precacher::precacheList(const std::string &list, void progressCallback())
131 {
132 loadProgressCallback = progressCallback;
133 InStream in(list.c_str());
134 std::string t;
135 while (std::getline(in, t))
136 {
137 if (!t.empty())
138 {
139 #if defined(BBGE_BUILD_UNIX)
140 //debugLog("precache["+t+"]");
141 t = t.substr(0,t.size()-1);
142 debugLog("precache["+t+"]");
143 #endif
144 stringToLower(t);
145 precacheTex(t);
146 }
147 }
148 in.close();
149 loadProgressCallback = NULL;
150 }
151
152