View Single Post
  #17  
Old 11-26-2013, 03:15 PM
Emera Emera is offline
Delterian Hybrid
Emera's Avatar
Join Date: Mar 2011
Location: Newcastle Upon-Tyne
Posts: 1,704
Emera is a jewel in the roughEmera is a jewel in the rough
Quote:
Originally Posted by jacob_bald6225 View Post
I think mine got down to something insane like 20 seconds for a level the more I worked with it and changed how I did things.


I am a noob but heres kind of some rambling advice as to how I was doing it and saving time.

Each scan read the r,g,b color of each tile in an X(Top left to bottom right, top right to bottom left). That is scanning 32 pixels per tiles instead of 256 (all of them)-- this increased my speed by 8 times.

I stored that in a big array from the tileset.


The scanning of the level is where time was made up:
Then when scanning the level I scanned 0,0-- looped through and checked it against the array of the tileset tiles. If a match was found-- I reorganized the tileset array of tiles to move the tile that was found to the 0 position. That way, common tiles in the level are closer to the first checked.

If a tile wasn't found-- I put it in a "not found" array. Which it'd scan before the tileset. Since there usually weren't 100s of not found tiles this stopped the thing from looping through all 4000 or so tiles each time one wasn't found.
You've suggested some nice ideas here. Yeah, the time it takes to initially scan through to find matches takes forever. Right now I'm doing something like this:
  1. Add bitmap data for each tile on the tileset to a list
  2. Grab the bitmap data of each tile in the level image and generate a hash for comparrison using the following method.

    HTML Code:
    private unsafe string GetPixelHash(BitmapData bd)
    {
        string final = "";
    
        for (int y = 0; y < bd.Height; y++)
        {
            byte* row = (byte*)bd.Scan0 + (y * bd.Stride);
            for (int x = 0; x < bd.Width; x++)
            {
                final += row[x * 4].ToString();
            }
        }
    
        return final;
    }
  3. Cross compare
  4. Get tileset position of tiles
  5. Generate

Quite obviously a much more efficient method of comparison is possible since you've said that it's possible to cut down the generation time to around 20~ seconds. In my defence though, the code hasn't been re-written at all so you're really looking at my preliminary method at work!
__________________
Reply With Quote