Skip to content

Commit

Permalink
Handle unknown lava textures
Browse files Browse the repository at this point in the history
TODO: Check animation values. They do not seem to be ok
  • Loading branch information
Flamefire committed Aug 29, 2015
1 parent c7b0472 commit 6cdd9f3
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 50 deletions.
40 changes: 25 additions & 15 deletions src/Loader.cpp
Expand Up @@ -952,9 +952,10 @@ void Loader::ClearTerrainTextures()
{
for(std::map<TerrainType, glArchivItem_Bitmap*>::iterator it = terrainTextures.begin(); it != terrainTextures.end(); ++it)
delete it->second;
for(std::map<TerrainType, libsiedler2::ArchivInfo*>::iterator it = terrainTexturesAnim.begin(); it != terrainTexturesAnim.end(); ++it)
delete it->second;
terrainTextures.clear();
water.clear();
lava.clear();
terrainTexturesAnim.clear();
borders.clear();
roads.clear();
roads_points.clear();
Expand Down Expand Up @@ -997,17 +998,22 @@ bool Loader::CreateTerrainTextures(void)
Rect(242, 160, 50, 16),
};

bool waterLoaded = false;

for(unsigned char i=0; i<TT_COUNT; ++i)
{
TerrainType t = TerrainType(i);
if(t == TT_LAVA)
ExtractAnimatedTexture(lava, TerrainData::GetPosInTexture(t), 4, 248);
else if(TerrainData::IsWater(t))
if(TerrainData::IsWater(t))
{
// All water uses the same texture, so load only once
if(!water.getCount())
ExtractAnimatedTexture(water, TerrainData::GetPosInTexture(t), 8, 240);
}else
if(waterLoaded)
continue;
t = TT_WATER;
waterLoaded = true;
}
if(TerrainData::IsAnimated(t))
terrainTexturesAnim[t] = ExtractAnimatedTexture(TerrainData::GetPosInTexture(t), TerrainData::GetFrameCount(t), TerrainData::GetStartColor(t));
else
terrainTextures[t] = ExtractTexture(TerrainData::GetPosInTexture(t));
}

Expand All @@ -1027,11 +1033,13 @@ bool Loader::CreateTerrainTextures(void)

glArchivItem_Bitmap& Loader::GetTerrainTexture(TerrainType t, unsigned animationFrame/* = 0*/)
{
if(t == TT_LAVA)
return *dynamic_cast<glArchivItem_Bitmap*>(lava.get(animationFrame));
else if(TerrainData::IsWater(t))
return *dynamic_cast<glArchivItem_Bitmap*>(water.get(animationFrame));
else
if(TerrainData::IsAnimated(t))
{
libsiedler2::ArchivInfo* archive = terrainTexturesAnim[t];
if(!archive)
throw std::runtime_error("Invalid terrain texture requested");
return *dynamic_cast<glArchivItem_Bitmap*>(archive->get(animationFrame));
}else
{
glArchivItem_Bitmap* bmp = terrainTextures[t];
if(!bmp)
Expand Down Expand Up @@ -1076,7 +1084,7 @@ glArchivItem_Bitmap_Raw* Loader::ExtractTexture(const Rect& rect)
*
* @author OLiver
*/
void Loader::ExtractAnimatedTexture(libsiedler2::ArchivInfo& destination, const Rect& rect, unsigned char color_count, unsigned char start_index)
libsiedler2::ArchivInfo* Loader::ExtractAnimatedTexture(const Rect& rect, unsigned char color_count, unsigned char start_index)
{
libsiedler2::ArchivItem_Palette* palette = GetTexPaletteN(1);
glArchivItem_Bitmap* image = GetTexImageN(0);
Expand All @@ -1093,6 +1101,7 @@ void Loader::ExtractAnimatedTexture(libsiedler2::ArchivInfo& destination, const
bitmap.setPalette(palette);
bitmap.setFormat(libsiedler2::FORMAT_PALETTED);

libsiedler2::ArchivInfo* destination = new libsiedler2::ArchivInfo();
for(unsigned char i = 0; i < color_count; ++i)
{
for(std::vector<unsigned char>::iterator it = buffer.begin(); it != buffer.end(); ++it)
Expand All @@ -1106,8 +1115,9 @@ void Loader::ExtractAnimatedTexture(libsiedler2::ArchivInfo& destination, const

bitmap.create(width, height, &buffer.front(), width, height, libsiedler2::FORMAT_PALETTED, palette);

destination.pushC(&bitmap);
destination->pushC(&bitmap);
}
return destination;
}

///////////////////////////////////////////////////////////////////////////////
Expand Down
5 changes: 2 additions & 3 deletions src/Loader.h
Expand Up @@ -85,7 +85,7 @@ class Loader : public Singleton<Loader>
bool LoadFile(const std::string& pfad, const libsiedler2::ArchivItem_Palette* palette, libsiedler2::ArchivInfo* archiv);
bool LoadArchiv(const std::string& pfad, const libsiedler2::ArchivItem_Palette* palette, libsiedler2::ArchivInfo* archiv);
glArchivItem_Bitmap_Raw* ExtractTexture(const Rect& rect);
void ExtractAnimatedTexture(libsiedler2::ArchivInfo& destination, const Rect& rect, unsigned char color_count, unsigned char start_index);
libsiedler2::ArchivInfo* ExtractAnimatedTexture(const Rect& rect, unsigned char color_count, unsigned char start_index);

bool LoadFilesFromArray(const unsigned int files_count, const unsigned int* files, bool load_always = true);
bool LoadLsts(unsigned int dir);
Expand Down Expand Up @@ -118,8 +118,7 @@ class Loader : public Singleton<Loader>
/// Terraintextures (unanimated)
std::map<TerrainType, glArchivItem_Bitmap*> terrainTextures;
/// Terraintextures (animated) (currently only water and lava)
libsiedler2::ArchivInfo water;
libsiedler2::ArchivInfo lava;
std::map<TerrainType, libsiedler2::ArchivInfo*> terrainTexturesAnim;

unsigned char lastgfx;
libsiedler2::ArchivInfo* nation_gfx[NAT_COUNT];
Expand Down
35 changes: 18 additions & 17 deletions src/TerrainRenderer.cpp
Expand Up @@ -305,25 +305,26 @@ void TerrainRenderer::UpdateTriangleTerrain(const MapPoint pt, const GameWorldVi
{
unsigned int pos = GetTriangleIdx(pt);

// TODO: Check those offsets for lava2-lava4
TerrainType t1 = gwv.GetNode(pt).t1;
bool isWaterOrLava = TerrainData::IsWater(t1) || t1 == TT_LAVA;
gl_texcoords[pos].pos[0].x = (isWaterOrLava) ? 0.4375f : 0.45f;
gl_texcoords[pos].pos[0].y = (isWaterOrLava) ? 0.0f : 0.45f;
gl_texcoords[pos].pos[1].y = (isWaterOrLava) ? 0.445312f : 0.0f;
gl_texcoords[pos].pos[1].x = (isWaterOrLava) ? 0.0f : 0.225f;
gl_texcoords[pos].pos[2].x = (isWaterOrLava) ? 0.84375f : 0.0f;
gl_texcoords[pos].pos[2].y = (isWaterOrLava) ? 0.445312f : 0.45f;
bool isAnimated = TerrainData::IsAnimated(t1);
gl_texcoords[pos].pos[0].x = (isAnimated) ? 0.4375f : 0.45f;
gl_texcoords[pos].pos[0].y = (isAnimated) ? 0.0f : 0.45f;
gl_texcoords[pos].pos[1].y = (isAnimated) ? 0.445312f : 0.0f;
gl_texcoords[pos].pos[1].x = (isAnimated) ? 0.0f : 0.225f;
gl_texcoords[pos].pos[2].x = (isAnimated) ? 0.84375f : 0.0f;
gl_texcoords[pos].pos[2].y = (isAnimated) ? 0.445312f : 0.45f;

++pos;

TerrainType t2 = gwv.GetNode(pt).t2;
isWaterOrLava = TerrainData::IsWater(t2) || t2 == TT_LAVA;
gl_texcoords[pos].pos[0].x = (isWaterOrLava) ? 0.4375f : 0.0f;
gl_texcoords[pos].pos[0].y = (isWaterOrLava) ? 0.859375f : 0.0f;
gl_texcoords[pos].pos[1].x = (isWaterOrLava) ? 0.84375f : 0.235f;
gl_texcoords[pos].pos[1].y = (isWaterOrLava) ? 0.445312f : 0.45f;
gl_texcoords[pos].pos[2].x = (isWaterOrLava) ? 0.0f : 0.47f;
gl_texcoords[pos].pos[2].y = (isWaterOrLava) ? 0.445312f : 0.0f;
isAnimated = TerrainData::IsAnimated(t2);
gl_texcoords[pos].pos[0].x = (isAnimated) ? 0.4375f : 0.0f;
gl_texcoords[pos].pos[0].y = (isAnimated) ? 0.859375f : 0.0f;
gl_texcoords[pos].pos[1].x = (isAnimated) ? 0.84375f : 0.235f;
gl_texcoords[pos].pos[1].y = (isAnimated) ? 0.445312f : 0.45f;
gl_texcoords[pos].pos[2].x = (isAnimated) ? 0.0f : 0.47f;
gl_texcoords[pos].pos[2].y = (isAnimated) ? 0.445312f : 0.0f;


/// Bei Vertexbuffern das die Daten aktualisieren
Expand Down Expand Up @@ -742,10 +743,10 @@ void TerrainRenderer::Draw(const GameWorldView& gwv, unsigned int* water)
continue;
unsigned animationFrame;
TerrainType tt = TerrainType(t);
if(tt == TT_LAVA)
animationFrame = GAMECLIENT.GetGlobalAnimation(4, 5, 4, 0);
if(TerrainData::IsLava(tt))
animationFrame = GAMECLIENT.GetGlobalAnimation(TerrainData::GetFrameCount(tt), 5, 4, 0);
else if(TerrainData::IsWater(tt))
animationFrame = GAMECLIENT.GetGlobalAnimation(8, 5, 2, 0);
animationFrame = GAMECLIENT.GetGlobalAnimation(TerrainData::GetFrameCount(tt), 5, 2, 0);
else
animationFrame = 0;

Expand Down
114 changes: 101 additions & 13 deletions src/gameData/TerrainData.cpp
Expand Up @@ -44,10 +44,9 @@ TerrainType TerrainData::MapIdx2Terrain(unsigned char mapIdx)
case 16: return TT_LAVA;
case 18: return TT_MOUNTAINMEADOW;
case 19: return TT_WATER2;
case 20:
case 21:
case 22:
assert(false); // Should not be used in maps!
case 20: return TT_LAVA2;
case 21: return TT_LAVA3;
case 22: return TT_LAVA4;
case 34: return TT_BUILDABLE_MOUNTAIN;
default:
assert(false); // Unknown/invalid texture
Expand Down Expand Up @@ -76,13 +75,59 @@ Rect TerrainData::GetPosInTexture(TerrainType t)
case TT_MOUNTAINMEADOW: return Rect(48, 144, 48, 48);
case TT_WATER: return Rect(192, 48, 55, 56);
case TT_LAVA: return Rect(192, 104, 55, 56);
case TT_LAVA2: return Rect(66, 223, 31, 32);
case TT_LAVA3: return Rect(99, 223, 31, 32);
case TT_LAVA4: return Rect(132, 223, 31, 32);
case TT_WATER2:
case TT_BUILDABLE_WATER:
throw std::logic_error("Use normal water for that!");
}
throw std::logic_error("Invalid parameters given");
}

bool TerrainData::IsAnimated(TerrainType t)
{
return IsWater(t) || IsLava(t);
}

unsigned TerrainData::GetFrameCount(TerrainType t)
{
// TODO: Get frame count for lava2-lava4
switch (t)
{
case TT_WATER:
case TT_WATER2:
case TT_BUILDABLE_WATER:
return 8;
case TT_LAVA:
case TT_LAVA2:
case TT_LAVA3:
case TT_LAVA4:
return 4;
default:
return 1;
}
}

unsigned char TerrainData::GetStartColor(TerrainType t)
{
// TODO: Get start color for lava2-lava4
switch (t)
{
case TT_WATER:
case TT_WATER2:
case TT_BUILDABLE_WATER:
return 240;
case TT_LAVA:
case TT_LAVA2:
case TT_LAVA3:
case TT_LAVA4:
return 248;
default:
return 0;
}
}

unsigned TerrainData::GetColor(LandscapeType landsCape, TerrainType t)
{
switch (landsCape)
Expand All @@ -105,7 +150,11 @@ unsigned TerrainData::GetColor(LandscapeType landsCape, TerrainType t)
case TT_STEPPE: return 0xFF88b028;
case TT_MOUNTAINMEADOW: return 0xFF9c8058;
case TT_WATER: return 0xFF1038a4;
case TT_LAVA: return 0xFFc02020;
case TT_LAVA:
case TT_LAVA2:
case TT_LAVA3:
case TT_LAVA4:
return 0xFFc02020;
case TT_WATER2: return 0xFF2259EA;
case TT_BUILDABLE_WATER: return 0xFF1038a4;
case TT_BUILDABLE_MOUNTAIN: return 0xFF9c8058;
Expand All @@ -129,7 +178,11 @@ unsigned TerrainData::GetColor(LandscapeType landsCape, TerrainType t)
case TT_STEPPE: return 0xFF88b028;
case TT_MOUNTAINMEADOW: return 0xFF001820;
case TT_WATER: return 0xFF454520;
case TT_LAVA: return 0xFFC32020;
case TT_LAVA:
case TT_LAVA2:
case TT_LAVA3:
case TT_LAVA4:
return 0xFFC32020;
case TT_WATER2: return 0xFF454520;
case TT_BUILDABLE_WATER: return 0xFF454520;
case TT_BUILDABLE_MOUNTAIN: return 0xFF706454;
Expand All @@ -153,7 +206,11 @@ unsigned TerrainData::GetColor(LandscapeType landsCape, TerrainType t)
case TT_STEPPE: return 0xFF88b15e;
case TT_MOUNTAINMEADOW: return 0xFF94a0c0;
case TT_WATER: return 0xFF1038a4;
case TT_LAVA: return 0xFFc02020;
case TT_LAVA:
case TT_LAVA2:
case TT_LAVA3:
case TT_LAVA4:
return 0xFFc02020;
case TT_WATER2: return 0xFF1344C4;
case TT_BUILDABLE_WATER: return 0xFF1038a4;
case TT_BUILDABLE_MOUNTAIN: return 0xFF60607c;
Expand Down Expand Up @@ -193,6 +250,9 @@ EdgeType TerrainData::GetEdgeType(LandscapeType landsCape, TerrainType t)
case TT_BUILDABLE_WATER:
return ET_WATER;
case TT_LAVA:
case TT_LAVA2:
case TT_LAVA3:
case TT_LAVA4:
return ET_NONE;
}
break;
Expand All @@ -201,6 +261,9 @@ EdgeType TerrainData::GetEdgeType(LandscapeType landsCape, TerrainType t)
{
case TT_SNOW:
case TT_LAVA:
case TT_LAVA2:
case TT_LAVA3:
case TT_LAVA4:
return ET_NONE;
case TT_DESERT:
case TT_STEPPE:
Expand Down Expand Up @@ -253,6 +316,9 @@ EdgeType TerrainData::GetEdgeType(LandscapeType landsCape, TerrainType t)
case TT_MOUNTAINMEADOW:
return ET_SNOW;
case TT_LAVA:
case TT_LAVA2:
case TT_LAVA3:
case TT_LAVA4:
return ET_NONE;
}
}
Expand Down Expand Up @@ -285,7 +351,10 @@ const signed char TERRAIN_DRAW_PRIORITY[LT_COUNT][TT_COUNT][TT_COUNT] =
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 0, 1 },
{ -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0 },
{ -1, -1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, -1, 0, 1, 1, -1, -1 }
{ -1, -1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, -1, 0, 1, 1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, 0 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, 0, 0 }
},
// Wasteland
{
Expand All @@ -307,7 +376,10 @@ const signed char TERRAIN_DRAW_PRIORITY[LT_COUNT][TT_COUNT][TT_COUNT] =
{ 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 0, 1 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1 }
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1 },
{ 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1 },
{ 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, 0 },
{ 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, 0, 0 }
},
// Winterworld
{
Expand All @@ -329,7 +401,10 @@ const signed char TERRAIN_DRAW_PRIORITY[LT_COUNT][TT_COUNT][TT_COUNT] =
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ 0, 1, 0, 1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 0, 0 },
{ 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0 },
{ -1, -1, -1, -1, -1, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1 }
{ -1, -1, -1, -1, -1, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, 0 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, 0, 0 }
}
};

Expand Down Expand Up @@ -372,13 +447,12 @@ unsigned char TerrainData::GetEdgeType(LandscapeType landsCape, TerrainType t1,

bool TerrainData::IsUseable(TerrainType t)
{
if(IsLava(t) || IsWater(t))
return false;
switch (t)
{
case TT_SNOW:
case TT_SWAMPLAND:
case TT_WATER:
case TT_LAVA:
case TT_WATER2:
return false;
default:
return true;
Expand Down Expand Up @@ -426,6 +500,20 @@ bool TerrainData::IsWater(TerrainType t)
}
}

bool TerrainData::IsLava(TerrainType t)
{
switch (t)
{
case TT_LAVA:
case TT_LAVA2:
case TT_LAVA3:
case TT_LAVA4:
return true;
default:
return false;
}
}

bool TerrainData::IsMountain(TerrainType t)
{
switch (t)
Expand Down

0 comments on commit 6cdd9f3

Please sign in to comment.