bool World::pixel_colour(gdImagePtr im, int pixel, int red, int green, int blue)
{ if( gdImageGreen(im, pixel) == green
&& gdImageRed(im, pixel ) == red
&& gdImageBlue(im, pixel ) == blue )
return true;
return false;
}
// **********************************************************************************************
int World::get_sector_colour(gdImagePtr im, int pixel )
{ int i;
for(i = 0; i <= SECT_MAX; ++i )
{
if( pixel_colour(im, pixel, sector_table[i].rgb[0], sector_table[i].rgb[1], sector_table[i].rgb[2]) )
{
return sector_table[i].sector;
}
}
Log::instance().bug(eLOGLOW,"get_sector_clour: unknown colour type! Does not match anything within the sector_table. Exiting!");
exit (1);
return -1;
}
// **********************************************************************************************
int World::lookup_wild( char type )
{
int x = 0;
for( x = 0; x <= SECT_MAX; ++x )
{
if( sector_table[x].sector == type )
return x;
}
return -1;
}
// **********************************************************************************************
// *Thanks davion for this :), PNG format = HUGE time-saver :)* //
void World::save_map_png(const char *name)
{
/* Declare the image */
gdImagePtr im;
/* Declare output files */
FILE *PngOut;
int terr = 0;
/* Declare color indexes */
int image[SECT_MAX];
im = gdImageCreate(MAX_X, MAX_Y);
for(register int g = 0; g <= SECT_MAX; ++g)
{
image[g] = gdImageColorAllocate(im, sector_table[g].rgb[0], sector_table[g].rgb[1], sector_table[g].rgb[2]);
}
// using registers will give a cpu boost to this, hopefuly allowing us to load faster
for( register int y = 0 ; y < MAX_Y ; ++y )
{
for(register int x = 0; x < MAX_X ; ++x )
{
// *return the number* //
terr = lookup_wild(worldmap[MAP_MAIN][x][y]);
// *Oopsies! something went REALLY wrong* //
if(terr == -1)
terr = 0;
// *Long.. Annoying… Switch* //
gdImageLine(im, x, y, x, y, image[terr]);
}
}
/* Do the same for a JPEG-format file. */
PngOut = fopen(name, "wb");
/* Output the same image in JPEG format, using the default JPEG quality setting. */
gdImagePng(im, PngOut);//, -1);
/* Close the files. */
fclose(PngOut);
for(register int g = 0; g <= SECT_MAX; ++g)
{
gdImageColorDeallocate(im, image[g]);
}
/* Destroy the image in memory. */
gdImageDestroy(im);
im = NULL;
PngOut = NULL;
return;
}
// **********************************************************************************************
void World::save_world_map( const char *name )
{
save_map_png(name);
return;
}
// **********************************************************************************************
void World::load_world_png(const char *name)
{
FILE *pngin;
gdImagePtr im;
int pixel;
int sect;
pngin = fopen(name, "rb");
// *Check to see if we worked or not.* //
if(!pngin)
{
Log::instance().bug(eLOGLOW, "%s: could not load %s!", __PRETTY_FUNCTION__, name);
return;
}
im = gdImageCreateFromPng(pngin);
for( register int y = 0; y < gdImageSY(im) ; ++y )
{
for(register int x = 0; x < gdImageSX(im) ; ++x )
{
pixel = gdImageGetPixel(im, x, y);
if(!strcmp(name, WORLD_FILE))
{
if( ( sect = get_sector_colour(im, pixel ) ) != -1 )
{
worldmap[MAP_MAIN][x][y] = sect;
}
}
else if(!strcmp(name, PATTERN_FILE))
{
int t =0;
// t = mob_pattern(im, pixel );
worldmap[MAP_PATTERN][x][y] = t;
}
else
{
Log::instance().bug(eLOGLOW, "%s: Bad name processed!", __PRETTY_FUNCTION__);
break;
}
}
}
// *Close it down.* //
fclose(pngin);
gdImageDestroy(im);
im = NULL;
pngin = NULL;
return;
}
I'm looking for exactly what the title suggests, I had seen it many years ago in a MUD, I had played.
This map was almost entirely background colors and let you enter areas from it, and likewise leave areas to the overworld map at an x,y coordinate. Is there any snippet I've overlooked that could accomplish this?