figuring out why grass appears on my map

figuring out why grass appears on my map

Discuss the source code and development of Spring Engine in general from a technical point of view. Patches go here too.

Moderator: Moderators

Post Reply
User avatar
enetheru
Posts: 627
Joined: 11 Jun 2010, 07:32

figuring out why grass appears on my map

Post by enetheru »

so I created a blank minimal map, and it was noted that grass appeared on it(I definitely didnt put grass in there)

so I had a look: mapfile.h
SMF format shows that grass is defined as an 'MEH_Vegetation' extra header. so i take it to mean that its optional.

Code: Select all

// Defined types for extra headers

/// Not sure why this one should be used
#define MEH_None 0

/**
@brief Extension containing a ground vegetation map

This extension contains an offset to an unsigned char[mapx/4 * mapy/4] array
that defines ground vegetation, if it's missing there is no ground vegetation.

	- 0=none
	- 1=grass
	- rest undefined so far
*/
#define MEH_Vegetation 1
so I go digging into the smf loading code:
CSMFMapFile::ReadGrassMap wont assign any data because because numExtraHeaders is 0.
CSMFMapFile::ReadInfoMap is the only function that calls ReadGrassMap
CSMFReadMap::GetInfoMap calls CSMFMapFile::ReadInfoMap and is the likely suspect

Code: Select all

unsigned char* CSMFReadMap::GetInfoMap(const std::string& name, MapBitmapInfo* bmInfo)
{
	// get size
	file.GetInfoMapSize(name, bmInfo);
	if (bmInfo->width <= 0) return NULL;

	// get data
	unsigned char* data = new unsigned char[bmInfo->width * bmInfo->height];
	file.ReadInfoMap(name, data);
	return data;
}

GrassDrawer.cpp
CGrassDrawer::CGrassDrawer does this:

Code: Select all

	// load grass density from map
	{
		MapBitmapInfo grassbm;
		unsigned char* grassdata = readMap->GetInfoMap("grass", &grassbm);
		if (!grassdata) {
			grassOff = true;
			return;
		}

if I knew the most robust way to solve this it would be a pull request.
User avatar
enetheru
Posts: 627
Joined: 11 Jun 2010, 07:32

Re: figuring out why grass appears on my map

Post by enetheru »

proposed solution

Functions effected:
CSMFMapFile::ReadGrassMap
CSMFMapFile::ReadInfoMap
CSMFReadMap::GetInfoMap

so I recon, if we change ReadGrassMap return type to bool, and return false on no grass then we can propogate that through ReadInfoMap

GetInfoMap is the function that allocates the data, so we can just check the return value of ReadInfoMap and delete the data and set the pointer to NULL.

CGrassDrawer already has checks for NULL pointer return

it only changes the function signature for one function and thats only used in once place. its not the cleanest solution, but its the least impact AFAICT.
User avatar
enetheru
Posts: 627
Joined: 11 Jun 2010, 07:32

Re: figuring out why grass appears on my map

Post by enetheru »

abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Re: figuring out why grass appears on my map

Post by abma »

merged, thanks!
Post Reply

Return to “Engine”