42 : iMaterialIndex(
MNone)
49 iMaterialIndex =
MNone;
59 Material = szMaterial; Texture = szTexture;
122 Order.push_back(byIndex);
129 if (!(pTexture=
new C4Texture))
return false;
139 uint32_t avg_c[4] = { 0, 0, 0, 0 };
140 for(int32_t y = 0; y < sfcSurface->
Hgt; ++y)
142 for(int32_t x = 0; x < sfcSurface->
Wdt; ++x)
145 avg_c[0] += c & 0xff;
146 avg_c[1] += (c >> 8) & 0xff;
147 avg_c[2] += (c >> 16) & 0xff;
148 avg_c[3] += (c >> 24) & 0xff;
153 double Size = sfcSurface->
Wdt * sfcSurface->
Hgt;
154 avg_c[0] =
static_cast<uint32_t
>(avg_c[0] / Size + 0.5);
155 avg_c[1] =
static_cast<uint32_t
>(avg_c[1] / Size + 0.5);
156 avg_c[2] =
static_cast<uint32_t
>(avg_c[2] / Size + 0.5);
157 avg_c[3] =
static_cast<uint32_t
>(avg_c[3] / Size + 0.5);
158 pTexture->
SetAverageColor(avg_c[0] | (avg_c[1] << 8) | (avg_c[2] << 16) | (avg_c[3] << 24));
194 if (pOverloadMaterials) *pOverloadMaterials =
false;
195 if (pOverloadTextures) *pOverloadTextures =
false;
197 for (
const char *pPos = TexMap.
getData(); pPos && *pPos; pPos =
SSearch(pPos + 1,
"\n"))
200 while (*pPos ==
'\r' || *pPos ==
'\n') pPos++;
202 if (pOverloadMaterials &&
SEqual2(pPos,
"OverloadMaterials"))
203 *pOverloadMaterials =
true;
204 if (pOverloadTextures &&
SEqual2(pPos,
"OverloadTextures"))
205 *pOverloadTextures =
true;
213 static std::regex line_terminator(
"\r?\n",
static_cast<std::regex::flag_type
>(std::regex_constants::optimize | std::regex_constants::ECMAScript));
217 int32_t iTextures = 0;
219 if (!hGroup.
LoadEntry(szEntryName,&bpMap,&map_size,1))
return 0;
222 char *end = begin + map_size;
225 for (
auto it = std::cregex_token_iterator(begin, end, line_terminator, -1); it != std::cregex_token_iterator(); ++it, ++line)
227 if (it->compare(
"OverloadMaterials") == 0)
230 if (pOverloadMaterials)
231 *pOverloadMaterials =
true;
233 else if (it->compare(
"OverloadTextures") == 0)
236 if (pOverloadTextures)
237 *pOverloadTextures =
true;
239 else if (it->length() == 0 || it->first[0] ==
'#' || std::all_of(it->first, it->second, &isspace))
254 index = std::stoul(it->str(), &separator, 10);
256 throw std::out_of_range(
"Texture index out of range");
257 value.assign(it->first + separator + 1, it->second);
259 catch (std::invalid_argument &)
261 DebugLogF(
"TexMap line %u: Texture index is not numeric",
static_cast<unsigned>(line));
264 catch (std::out_of_range &)
266 DebugLogF(
"TexMap line %u: Texture index is out of range",
static_cast<unsigned>(line));
271 std::string::const_iterator separator = std::find(value.cbegin(), value.cend(),
'-');
272 if (separator == value.cend())
274 DebugLogF(R
"(TexMap line %u: Texture name "%s" is invalid (missing "-"))", static_cast<unsigned>(line), value.c_str());
278 std::string material(value.cbegin(), separator);
279 std::string texture(separator + 1, value.cend());
281 if (
AddEntry(index, material.c_str(), texture.c_str()))
294 int32_t iRemoved = 0;
298 if (!
Entry[i].isNull())
301 LogF(
"Error in TextureMap initialization at entry %d", (
int) i);
314 sTexMapFile.
Append(
"# Automatically generated texture map\n");
315 sTexMapFile.
Append(
"# Contains material-texture-combinations added at runtime\n");
323 if (!
Entry[i].isNull())
330 size_t iBufSize = sTexMapFile.
getLength();
332 memcpy(pBuf, sTexMapFile.
getData(), iBufSize);
334 bool fSuccess = !!hGroup.
Add(szEntryName, pBuf, iBufSize,
false,
true);
335 if (!fSuccess)
delete [] pBuf;
382 LogF(
"ERROR: Texture shape %s not loaded because associated texture (%s) not found or invalid.", hGroup.
GetFullName().
getData(), texname4shape.
getData());
386 int32_t scaler_zoom = 4;
405 if (byNewIndex == byOldIndex)
return;
408 auto old_entry = std::find_if(
Order.begin(),
Order.end(),
409 [byOldIndex](
const int32_t &entry) { return entry == byOldIndex; });
410 if (old_entry !=
Order.end()) *old_entry = byNewIndex;
414 int32_t
C4TextureMap::GetIndex(
const char *szMaterial,
const char *szTexture,
bool fAddIfNotExist,
const char *szErrorIfFailed)
419 if (!
Entry[byIndex].isNull())
426 if (
Entry[byIndex].isNull())
428 if (
AddEntry(byIndex, szMaterial, szTexture))
433 if (szErrorIfFailed)
DebugLogF(
"Error getting MatTex %s-%s for %s from TextureMap: Init failed.", szMaterial, szTexture, szErrorIfFailed);
437 if (szErrorIfFailed)
DebugLogF(
"Error getting MatTex %s-%s for %s from TextureMap: %s.", szMaterial, szTexture, szErrorIfFailed, fAddIfNotExist ?
"Map is full!" :
"Entry not found.");
445 Material.
CopyUntil(szMaterialTexture,
'-');
452 if (szDefaultTexture)
453 if ((iMatTex =
GetIndex(Material.
getData(), szDefaultTexture, fAddIfNotExist)))
459 if (szErrorIfFailed)
DebugLogF(
"Error getting MatTex for %s: Invalid material", szErrorIfFailed);
498 for (pTexture=
FirstTexture,cindex=0; pTexture; pTexture=pTexture->
Next,cindex++)
511 if (!iTex)
return fg;
531 auto last_entry = std::remove_if(
Order.begin(),
Order.end(),
532 [iIndex](
const int32_t &entry) { return entry == iIndex; });
551 dwPix = texture->GetAverageColor();
554 Palette->
Colors[i] = dwPix;
564 if (fSet[j] && Palette->
Colors[i] == Palette->
Colors[j])
#define C4CFN_BitmapFiles
#define C4CFN_MaterialShapeFiles
const int C4M_MaxTexIndex
C4String * GetTextureName(int32_t texture_nr)
int32_t PixCol2Tex(BYTE pixc)
bool DensitySemiSolid(int32_t dens)
bool LogF(const char *strMessage,...)
bool DebugLogF(const char *strMessage ...)
C4MaterialMap MaterialMap
bool MatValid(int32_t mat)
BYTE Mat2PixColDefault(int32_t mat)
uint32_t UnsyncedRandom()
void SReplaceChar(char *str, char fc, char tc)
const char * SSearch(const char *szString, const char *szIndex)
bool SEqual2(const char *szStr1, const char *szStr2)
bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen)
std::enable_if< std::is_pod< T >::value >::type ZeroMem(T *lpMem, size_t dwSize)
size_t SLen(const char *sptr)
#define GetGreenValue(rgb)
#define GetBlueValue(rgb)
char * GetExtension(char *szFilename)
bool WildcardMatch(const char *szWildcard, const char *szString)
const char * GetFilenameOnly(const char *strFilename)
bool AccessNextEntry(const char *wildcard, size_t *size=nullptr, char *filename=nullptr, bool start_at_filename=false)
int EntryCount(const char *wildcard=nullptr)
StdStrBuf GetFullName() const
bool Add(const char *filename, const char *entry_name)
bool LoadEntry(const char *entry_name, char **buffer, size_t *size_info=nullptr, int zeros_to_append=0)
bool LoadEntryString(const char *entry_name, StdStrBuf *buffer)
void ResetSearch(bool reload_contents=false)
void HandleTexMapUpdate()
int32_t Get(const char *szMaterial)
DWORD PatternClr(unsigned int iX, unsigned int iY) const
bool Set(class C4Surface *sfcSource, int iZoom=0)
DWORD GetPixDw(int iX, int iY, bool fApplyModulation)
bool Read(CStdStream &hGroup, const char *extension, int iFlags)
const C4Pattern & GetPattern() const
C4Material * GetMaterial() const
bool Create(const char *szMaterial, const char *szTexture)
void SetAverageColor(uint32_t Color)
void SetMaterialShape(class C4TextureShape *s)
C4TexMapEntry Entry[C4M_MaxTexIndex]
bool CheckTexture(const char *szTexture)
int32_t GetIndex(const char *szMaterial, const char *szTexture, bool fAddIfNotExist=true, const char *szErrorIfFailed=nullptr)
int32_t GetIndexMatTex(const char *szMaterialTexture, const char *szDefaultTexture=nullptr, bool fAddIfNotExist=true, const char *szErrorIfFailed=nullptr)
bool AddTexture(const char *szTexture, C4Surface *sfcSurface)
bool SaveMap(C4Group &hGroup, const char *szEntryName)
bool HasTextures(C4Group &hGroup)
BYTE DefaultBkgMatTex(BYTE fg) const
static bool LoadFlags(C4Group &hGroup, const char *szEntryName, bool *pOverloadMaterials, bool *pOverloadTextures)
int32_t GetTextureIndex(const char *pTexName)
const char * GetTexture(int32_t iIndex)
int32_t LoadMap(C4Group &hGroup, const char *szEntryName, bool *pOverloadMaterials, bool *pOverloadTextures)
void StoreMapPalette(CStdPalette *, C4MaterialMap &rMaterials)
void MoveIndex(BYTE byOldIndex, BYTE byNewIndex)
const C4TexMapEntry * GetEntry(int32_t iIndex) const
std::vector< int32_t > Order
int32_t LoadTextures(C4Group &hGroup, C4Group *OverloadFile=nullptr)
bool AddEntry(BYTE byIndex, const char *szMaterial, const char *szTexture)
void RemoveEntry(int32_t iIndex)
void SetLength(size_t iLength)
void AppendFormat(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
void CopyUntil(const char *szString, char cUntil)
const char * getData() const
void Append(const char *pnData, size_t iChars)