コード例 #1
0
        public bool Import(TR2CombinedLevel level, TextureLevelMapping mapping)
        {
            // Ensure any changes already made are committed to the level
            mapping.CommitGraphics();

            using (TexturePacker packer = new TexturePacker(level.Data))
            {
                List <TRObjectTexture> textures = level.Data.ObjectTextures.ToList();
                foreach (StaticTextureSource source in mapping.LandmarkMapping.Keys)
                {
                    if (textures.Count == _maxTextures)
                    {
                        break;
                    }

                    if (!source.HasVariants)
                    {
                        continue;
                    }

                    List <Rectangle> segments = source.VariantMap[source.Variants[0]];
                    foreach (int segmentIndex in mapping.LandmarkMapping[source].Keys)
                    {
                        foreach (LandmarkTextureTarget target in mapping.LandmarkMapping[source][segmentIndex])
                        {
                            IndexedTRObjectTexture texture = CreateTexture(segments[segmentIndex]);
                            target.MappedTextureIndex = textures.Count;
                            textures.Add(texture.Texture);

                            Bitmap image;
                            if (target.BackgroundIndex != -1)
                            {
                                IndexedTRObjectTexture indexedTexture = new IndexedTRObjectTexture
                                {
                                    Index   = target.BackgroundIndex,
                                    Texture = level.Data.ObjectTextures[target.BackgroundIndex]
                                };
                                BitmapGraphics tile = GetTile(level.Data, indexedTexture.Atlas);

                                BitmapGraphics clip = new BitmapGraphics(tile.Extract(indexedTexture.Bounds));
                                clip.Overlay(source.Bitmap);
                                image = clip.Bitmap;
                            }
                            else
                            {
                                image = source.Bitmap;
                            }

                            packer.AddRectangle(new TexturedTileSegment(texture, image));
                        }
                    }
                }

                try
                {
                    packer.Pack(true);

                    // Perform the room data remapping
                    foreach (StaticTextureSource source in mapping.LandmarkMapping.Keys)
                    {
                        if (!source.HasVariants)
                        {
                            continue;
                        }

                        foreach (int segmentIndex in mapping.LandmarkMapping[source].Keys)
                        {
                            foreach (LandmarkTextureTarget target in mapping.LandmarkMapping[source][segmentIndex])
                            {
                                if (target.MappedTextureIndex == -1)
                                {
                                    // There wasn't enough space for this
                                    continue;
                                }

                                TR2Room room = level.Data.Rooms[target.RoomNumber];
                                foreach (int rectIndex in target.RectangleIndices)
                                {
                                    room.RoomData.Rectangles[rectIndex].Texture = (ushort)target.MappedTextureIndex;
                                }
                            }
                        }
                    }

                    level.Data.ObjectTextures    = textures.ToArray();
                    level.Data.NumObjectTextures = (uint)textures.Count;

                    return(true);
                }
                catch (PackingException)
                {
                    return(false);
                }
            }
        }