Isometric map renderer. Creates a bitmap of the map.
Example #1
0
        public AddWorldPopup( WorldListEntry world ) {
            InitializeComponent();

            fileBrowser.Filter = MapLoadFilter;

            cBackup.Items.AddRange( WorldListEntry.BackupEnumNames );

            bwLoader.DoWork += AsyncLoad;
            bwLoader.RunWorkerCompleted += AsyncLoadCompleted;

            bwGenerator.WorkerReportsProgress = true;
            bwGenerator.DoWork += AsyncGen;
            bwGenerator.ProgressChanged += AsyncGenProgress;
            bwGenerator.RunWorkerCompleted += AsyncGenCompleted;

            bwRenderer.WorkerReportsProgress = true;
            bwRenderer.DoWork += AsyncDraw;
            bwRenderer.ProgressChanged += AsyncDrawProgress;
            bwRenderer.RunWorkerCompleted += AsyncDrawCompleted;

            renderer = new IsoCat();
            // event routed through BackgroundWorker to avoid cross-thread invocation issues
            renderer.ProgressChanged +=
                ( progressSender, progressArgs ) =>
                bwRenderer.ReportProgress( progressArgs.ProgressPercentage, progressArgs.UserState );

            nMapWidth.Validating += MapDimensionValidating;
            nMapHeight.Validating += MapDimensionValidating;
            nMapLength.Validating += MapDimensionValidating;

            cAccess.Items.Add( "(everyone)" );
            cBuild.Items.Add( "(everyone)" );
            foreach( Rank rank in RankManager.Ranks ) {
                cAccess.Items.Add( MainForm.ToComboBoxOption( rank ) );
                cBuild.Items.Add( MainForm.ToComboBoxOption( rank ) );
            }

            progressBar.Visible = false;
            tStatus1.Text = "";
            tStatus2.Text = "";

            World = world;
            cPreviewMode.SelectedIndex = 0;

            savePreviewDialog.Filter =
                "PNG Image|*.png|TIFF Image|*.tif;*.tiff|Bitmap Image|*.bmp|JPEG Image|*.jpg;*.jpeg";
            savePreviewDialog.Title = "Saving preview image...";

            cGenerator.Items.AddRange( MapGenUtil.GeneratorList.Select( gen => gen.Name ).ToArray() );
            cGenerator.SelectedIndex = 0;

            tsbLoadPreset.DropDownItemClicked += tsbLoadPreset_DropDownItemClicked;
            tsbCopyGenSettings.DropDownItemClicked += tsbImportSettings_DropDownItemClicked;
            Shown += LoadMap;
        }
Example #2
0
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("Usage: fCraftImg.exe <map filename> <output png> [rotation (0-3)] [mode] [x y z x2 y2 z2]");
                return;
            }

            int    i        = 0;
            string filename = args[i++];
            string output   = args[i++];
            int    rotation = 0;

            if (args.Length >= i + 1)
            {
                rotation = System.Convert.ToInt32(args[i++]);
            }
            int mode = 0;

            if (args.Length >= i + 1)
            {
                mode = System.Convert.ToInt32(args[i++]);
            }

            int[] chunkCoords = new int[6];
            for (int pos = 0; pos < 6; pos++)
            {
                if (args.Length < i + 1)
                {
                    break;
                }
                chunkCoords[pos] = System.Convert.ToInt32(args[i++]);
            }

            try {
                Map map = fCraft.MapConversion.MapUtility.Load(filename);
                map.CalculateShadows();
                fCraft.GUI.IsoCat renderer = new fCraft.GUI.IsoCat(map, (fCraft.GUI.IsoCatMode)mode, rotation);
                renderer.ChunkCoords = chunkCoords;

                Rectangle        cropRectangle;
                BackgroundWorker bwRenderer = new BackgroundWorker();
                bwRenderer.WorkerReportsProgress = true;
                Bitmap rawImage = renderer.Draw(out cropRectangle, bwRenderer);

                Bitmap outputImage = rawImage.Clone(cropRectangle, rawImage.PixelFormat);
                outputImage.Save(output, System.Drawing.Imaging.ImageFormat.Png);
            } catch (Exception ex) {
                Console.WriteLine("An Error Occured!");
                Console.Write(ex);
            }
        }
Example #3
0
        public AddWorldPopup( WorldListEntry world ) {
            InitializeComponent();
            renderer = new IsoCat();
            fileBrowser.Filter = MapLoadFilter;

            cBackup.Items.AddRange( WorldListEntry.BackupEnumNames );
            cTemplates.Items.AddRange( Enum.GetNames( typeof( MapGenTemplate ) ) );
            cTheme.Items.AddRange( Enum.GetNames( typeof( MapGenTheme ) ) );

            bwLoader.DoWork += AsyncLoad;
            bwLoader.RunWorkerCompleted += AsyncLoadCompleted;

            bwGenerator.DoWork += AsyncGen;
            bwGenerator.WorkerReportsProgress = true;
            bwGenerator.ProgressChanged += AsyncGenProgress;
            bwGenerator.RunWorkerCompleted += AsyncGenCompleted;

            bwRenderer.WorkerReportsProgress = true;
            bwRenderer.WorkerSupportsCancellation = true;
            bwRenderer.DoWork += AsyncDraw;
            bwRenderer.ProgressChanged += AsyncDrawProgress;
            bwRenderer.RunWorkerCompleted += AsyncDrawCompleted;

            nMapWidth.Validating += MapDimensionValidating;
            nMapLength.Validating += MapDimensionValidating;
            nMapHeight.Validating += MapDimensionValidating;

            cAccess.Items.Add( "(everyone)" );
            cBuild.Items.Add( "(everyone)" );
            foreach( Rank rank in RankManager.Ranks ) {
                cAccess.Items.Add( MainForm.ToComboBoxOption( rank ) );
                cBuild.Items.Add( MainForm.ToComboBoxOption( rank ) );
            }

            tStatus1.Text = "";
            tStatus2.Text = "";

            World = world;

            savePreviewDialog.Filter = "PNG Image|*.png|TIFF Image|*.tif;*.tiff|Bitmap Image|*.bmp|JPEG Image|*.jpg;*.jpeg";
            savePreviewDialog.Title = "Saving preview image...";

            browseTemplateDialog.Filter = "MapGenerator Template|*.ftpl";
            browseTemplateDialog.Title = "Opening a MapGenerator template...";

            saveTemplateDialog.Filter = browseTemplateDialog.Filter;
            saveTemplateDialog.Title = "Saving a MapGenerator template...";

            Shown += LoadMap;
        }
Example #4
0
        void RenderLoop() {
            renderer = MakeRenderer();
            using( MemoryStream ms = new MemoryStream() ) {
                // loop terminates with the rest of the program (this is a background thread)
                while( true ) {
                    // wait (block) until a map is available for drawing
                    RenderTask task = inQueue.WaitDequeue();
                    try {
                        // render the map
                        IsoCatResult result = renderer.Draw( task.Map );
                        task.Map = null;

                        // crop image (if needed)
                        Image image;
                        if( p.Uncropped ) {
                            image = result.Bitmap;
                        } else {
                            image = result.Bitmap.Clone( result.CropRectangle, result.Bitmap.PixelFormat );
                            result.Bitmap.Dispose();
                        }

                        // encode image
                        if( p.ExportFormat.Equals( ImageFormat.Jpeg ) ) {
                            EncoderParameters encoderParams = new EncoderParameters();
                            encoderParams.Param[0] = new EncoderParameter( Encoder.Quality, p.JpegQuality );
                            image.Save( ms, p.ImageEncoder, encoderParams );
                        } else if( p.ExportFormat.Equals( ImageFormat.Gif ) ) {
                            OctreeQuantizer q = new OctreeQuantizer( 255, 8 );
                            image = q.Quantize( image );
                            image.Save( ms, p.ExportFormat );
                        } else {
                            image.Save( ms, p.ExportFormat );
                        }
                        image.Dispose();

                        // store result as a byte[]
                        task.Result = ms.ToArray();

                    } catch( Exception ex ) {
                        task.Exception = ex;
                    }

                    // send stack to the results queue
                    outQueue.Enqueue( task );
                    ms.SetLength( 0 );
                }
            }
        }
Example #5
0
 void AsyncDraw( object sender, DoWorkEventArgs e ) {
     stopwatch = Stopwatch.StartNew();
     renderer = new IsoCat( Map, IsoCatMode.Normal, previewRotation );
     Rectangle cropRectangle;
     if( bwRenderer.CancellationPending ) return;
     Bitmap rawImage = renderer.Draw( out cropRectangle, bwRenderer );
     if( bwRenderer.CancellationPending ) return;
     if( rawImage != null ) {
         previewImage = rawImage.Clone( cropRectangle, rawImage.PixelFormat );
     }
     renderer = null;
     GC.Collect( GC.MaxGeneration, GCCollectionMode.Optimized );
 }
Example #6
0
        static int Main( string[] args ) {
            Logger.Logged += OnLogged;

            ReturnCode optionParsingResult = ParseOptions( args );
            if( optionParsingResult != ReturnCode.Success ) {
                return (int)optionParsingResult;
            }

            // check if input path exists, and if it's a file or directory
            bool directoryMode;
            try {
                if( File.Exists( inputPath ) ) {
                    directoryMode = false;
                    if( outputDirName == null ) {
                        outputDirName = Paths.GetDirectoryNameOrRoot( inputPath );
                    }

                } else if( Directory.Exists( inputPath ) ) {
                    directoryMode = true;
                    if( outputDirName == null ) {
                        outputDirName = Paths.GetDirectoryNameOrRoot( inputPath );
                    }

                } else {
                    Console.Error.WriteLine( "MapRenderer: Cannot locate \"{0}\"", inputPath );
                    return (int)ReturnCode.InputDirNotFound;
                }

                if( !Directory.Exists( outputDirName ) ) {
                    Directory.CreateDirectory( outputDirName );
                }

            } catch( Exception ex ) {
                Console.Error.WriteLine( "MapRenderer: {0}: {1}",
                                         ex.GetType().Name,
                                         ex.Message );
                return (int)ReturnCode.PathError;
            }

            // check recursive flag
            if( recursive && !directoryMode ) {
                Console.Error.WriteLine( "MapRenderer: Recursive flag is given, but input is not a directory." );
            }

            // check input filter
            if( inputFilter != null && !directoryMode ) {
                Console.Error.WriteLine( "MapRenderer: Filter param is given, but input is not a directory." );
            }

            // initialize image encoder
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
            imageEncoder = codecs.FirstOrDefault( codec => codec.FormatID == exportFormat.Guid );
            if( imageEncoder == null ) {
                Console.Error.WriteLine( "MapRenderer: Specified image encoder is not supported." );
                return (int)ReturnCode.UnsupportedSaveFormat;
            }

            // create and configure the renderer
            renderer = new IsoCat {
                SeeThroughLava = seeThroughLava,
                SeeThroughWater = seeThroughWater,
                Mode = mode,
                Gradient = !noGradient,
                DrawShadows = !noShadows
            };
            if( mode == IsoCatMode.Chunk ) {
                renderer.ChunkCoords[0] = region.XMin;
                renderer.ChunkCoords[1] = region.YMin;
                renderer.ChunkCoords[2] = region.ZMin;
                renderer.ChunkCoords[3] = region.ZMax;
                renderer.ChunkCoords[4] = region.YMax;
                renderer.ChunkCoords[5] = region.XMax;
            }
            switch( angle ) {
                case 90:
                    renderer.Rotation = 1;
                    break;
                case 180:
                    renderer.Rotation = 2;
                    break;
                case 270:
                case -90:
                    renderer.Rotation = 3;
                    break;
            }

            if( !recursive && mapImporter != null && mapImporter.StorageType == MapStorageType.Directory ) {
                // single-directory map
                RenderOneMap( new DirectoryInfo( inputPath ) );

            } else if( !directoryMode ) {
                // single-file map
                RenderOneMap( new FileInfo( inputPath ) );

            } else {
                // possible single-directory conversion
                if( !recursive && RenderOneMap( new DirectoryInfo( inputPath ) ) ) {
                    return (int)ReturnCode.Success;
                }

                // go through all files inside the given directory
                SearchOption recursiveOption = ( recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly );
                DirectoryInfo inputDirInfo = new DirectoryInfo( inputPath );
                if( inputFilter == null ) inputFilter = "*";
                foreach( var dir in inputDirInfo.GetDirectories( inputFilter, recursiveOption ) ) {
                    RenderOneMap( dir );
                }
                foreach( var file in inputDirInfo.GetFiles( inputFilter, recursiveOption ) ) {
                    RenderOneMap( file );
                }
            }

            return (int)ReturnCode.Success;
        }
Example #7
0
 IsoCat MakeRenderer() {
     // create and configure the renderer
     IsoCat newRenderer = new IsoCat {
         SeeThroughLava = p.SeeThroughLava,
         SeeThroughWater = p.SeeThroughWater,
         Mode = p.Mode,
         Gradient = !p.NoGradient,
         DrawShadows = !p.NoShadows
     };
     if( p.Mode == IsoCatMode.Chunk ) {
         newRenderer.Chunk = p.Region;
     }
     switch( p.Angle ) {
         case 90:
             newRenderer.Rotation = 1;
             break;
         case 180:
             newRenderer.Rotation = 2;
             break;
         case 270:
         case -90:
             newRenderer.Rotation = 3;
             break;
     }
     return newRenderer;
 }
Example #8
0
        static int Main( string[] args ) {
            Logger.Logged += OnLogged;
            Logger.DisableFileLogging();

            ReturnCode optionParsingResult = ParseOptions( args );
            if( optionParsingResult != ReturnCode.Success ) {
                return (int)optionParsingResult;
            }

            // parse importer name
            if( importerName != null && !importerName.Equals( "auto", StringComparison.OrdinalIgnoreCase ) ) {
                MapFormat importFormat;
                if( !EnumUtil.TryParse( importerName, out importFormat, true ) ) {
                    Console.Error.WriteLine( "Unsupported importer \"{0}\"", importerName );
                    PrintUsage();
                    return (int)ReturnCode.UnrecognizedImporter;
                }
                mapImporter = MapUtility.GetImporter( importFormat );
                if( mapImporter == null ) {
                    Console.Error.WriteLine( "Loading from \"{0}\" is not supported", importFormat );
                    PrintUsage();
                    return (int)ReturnCode.UnsupportedLoadFormat;
                }
            }

            // check input paths
            bool hadFile = false,
                 hadDir = false;
            foreach( string inputPath in inputPathList ) {
                if( hadDir ) {
                    Console.Error.WriteLine( "MapRenderer: Only one directory may be specified at a time." );
                    return (int)ReturnCode.ArgumentError;
                }
                // check if input path exists, and if it's a file or directory
                try {
                    if( File.Exists( inputPath ) ) {
                        hadFile = true;
                    } else if( Directory.Exists( inputPath ) ) {
                        hadDir = true;
                        if( hadFile ) {
                            Console.Error.WriteLine( "MapRenderer: Cannot mix directories and files in input." );
                            return (int)ReturnCode.ArgumentError;
                        }
                        directoryMode = true;
                        if( !outputDirGiven ) {
                            outputDirName = inputPath;
                        }
                    } else {
                        Console.Error.WriteLine( "MapRenderer: Cannot locate \"{0}\"", inputPath );
                        return (int)ReturnCode.InputPathNotFound;
                    }
                } catch( Exception ex ) {
                    Console.Error.WriteLine( "MapRenderer: {0}: {1}",
                                             ex.GetType().Name,
                                             ex.Message );
                    return (int)ReturnCode.PathError;
                }
            }

            // initialize image encoder
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
            imageEncoder = codecs.FirstOrDefault( codec => codec.FormatID == exportFormat.Guid );
            if( imageEncoder == null ) {
                Console.Error.WriteLine( "MapRenderer: Specified image encoder is not supported." );
                return (int)ReturnCode.UnsupportedSaveFormat;
            }

            // create and configure the renderer
            renderer = new IsoCat {
                SeeThroughLava = seeThroughLava,
                SeeThroughWater = seeThroughWater,
                Mode = mode,
                Gradient = !noGradient,
                DrawShadows = !noShadows
            };
            if( mode == IsoCatMode.Chunk ) {
                renderer.ChunkCoords[0] = region.XMin;
                renderer.ChunkCoords[1] = region.YMin;
                renderer.ChunkCoords[2] = region.ZMin;
                renderer.ChunkCoords[3] = region.XMax;
                renderer.ChunkCoords[4] = region.YMax;
                renderer.ChunkCoords[5] = region.ZMax;
            }
            switch( angle ) {
                case 90:
                    renderer.Rotation = 1;
                    break;
                case 180:
                    renderer.Rotation = 2;
                    break;
                case 270:
                case -90:
                    renderer.Rotation = 3;
                    break;
            }

            // check recursive flag
            if( recursive && !directoryMode ) {
                Console.Error.WriteLine( "MapRenderer: Recursive flag is given, but input is not a directory." );
                return (int)ReturnCode.ArgumentError;
            }

            // check input filter
            if( inputFilter != null && !directoryMode ) {
                Console.Error.WriteLine( "MapRenderer: Filter param is given, but input is not a directory." );
                return (int)ReturnCode.ArgumentError;
            }

            // check regex filter
            if( useRegex ) {
                try {
                    filterRegex = new Regex( inputFilter );
                } catch( ArgumentException ex ) {
                    Console.Error.WriteLine( "MapRenderer: Cannot parse filter regex: {0}",
                                             ex.Message );
                    return (int)ReturnCode.ArgumentError;
                }
            }

            // check if output dir exists; create it if needed
            if( outputDirName != null ) {
                try {
                    if( !Directory.Exists( outputDirName ) ) {
                        Directory.CreateDirectory( outputDirName );
                    }
                } catch( Exception ex ) {
                    Console.Error.WriteLine( "MapRenderer: Error checking output directory: {0}: {1}",
                                             ex.GetType().Name, ex.Message );
                }
            }

            // process inputs, one path at a time
            foreach( string inputPath in inputPathList ) {
                ReturnCode code = ProcessInputPath( inputPath );
                if( code != ReturnCode.Success ) {
                    return (int)code;
                }
            }
            return (int)ReturnCode.Success;
        }