static void TakeScreenshotSample(List <Sample> samples, string repository, string category, string name, string imagesDirectory)
        {
            string exeFileName = Path.Combine(repository, category, name, ExePath, name + ".dll");

            if (!File.Exists(exeFileName))
            {
                return;
            }

            var assembly = Assembly.LoadFrom(exeFileName);
            var type     = assembly.GetType(name + ".MainGameComponent");

            if (type == null)
            {
                return;
            }

            Console.WriteLine(category + "-" + name);
            Directory.SetCurrentDirectory(Path.GetDirectoryName(exeFileName));

            var mainGameComponent = (IGameComponent)Activator.CreateInstance(type);

            samples.Add(new Sample(mainGameComponent.MinimalFeatureLevel, repository, category, name));

            var deviceResourcesOptions = new DeviceResourcesOptions
            {
                ForceWarp = true,
                UseHighestFeatureLevel = false
            };

            var deviceResources = new RenderTargetDeviceResources(
                ScreenshotImageWidth,
                ScreenshotImageHeight,
                mainGameComponent.MinimalFeatureLevel,
                deviceResourcesOptions);

            mainGameComponent.CreateDeviceDependentResources(deviceResources);
            mainGameComponent.CreateWindowSizeDependentResources();
            mainGameComponent.Update(null);
            mainGameComponent.Render();

            deviceResources.SaveBackBuffer(Path.Combine(imagesDirectory, category + "-" + name + ".jpg"));

            mainGameComponent.ReleaseWindowSizeDependentResources();
            mainGameComponent.ReleaseDeviceDependentResources();
            deviceResources.Release();
        }
Example #2
0
        public static byte[] Generate(uint width, uint height, string optFileName, RenderFace side, RenderMethod method)
        {
            var options = new DeviceResourcesOptions
            {
                UseHighestFeatureLevel = false,
                PreferMultisampling    = true
            };

            var deviceResources = new RenderTargetDeviceResources(width, height, D3D11FeatureLevel.FeatureLevel100, options);
            var optComponent    = new OptComponent(optFileName);

            try
            {
                optComponent.CreateDeviceDependentResources(deviceResources);
                optComponent.CreateWindowSizeDependentResources();
                optComponent.Update(null);

                optComponent.WorldMatrix =
                    XMMatrix.Translation(optComponent.OptCenter.X, -optComponent.OptCenter.Z, optComponent.OptCenter.Y)
                    * XMMatrix.ScalingFromVector(XMVector.Replicate(0.95f / optComponent.OptSize));

                switch (side)
                {
                case RenderFace.Top:
                default:
                    optComponent.ViewMatrix = XMMatrix.LookAtRH(new XMFloat3(0, 2, 0), new XMFloat3(0, 0, 0), new XMFloat3(0, 0, 1));
                    break;

                case RenderFace.Front:
                    optComponent.ViewMatrix = XMMatrix.LookAtRH(new XMFloat3(0, 0, 2), new XMFloat3(0, 0, 0), new XMFloat3(0, 1, 0));
                    break;

                case RenderFace.Side:
                    optComponent.ViewMatrix = XMMatrix.LookAtRH(new XMFloat3(2, 0, 0), new XMFloat3(0, 0, 0), new XMFloat3(0, 1, 0));
                    break;
                }

                optComponent.ProjectionMatrix = XMMatrix.OrthographicRH(1, 1, 0, 5);

                var context = deviceResources.D3DContext;
                context.OutputMergerSetRenderTargets(new[] { deviceResources.D3DRenderTargetView }, deviceResources.D3DDepthStencilView);
                context.ClearRenderTargetView(deviceResources.D3DRenderTargetView, new[] { 0.0f, 0.0f, 0.0f, 0.0f });
                context.ClearDepthStencilView(deviceResources.D3DDepthStencilView, D3D11ClearOptions.Depth, 1.0f, 0);

                optComponent.Render();
                deviceResources.Present();

                var buffer = deviceResources.GetBackBufferContent();

                for (int i = 0; i < width * height; i++)
                {
                    byte a = buffer[i * 4 + 3];
                    buffer[i * 4 + 3] = a == 0 ? (byte)0 : (byte)255;
                }

                switch (method)
                {
                case RenderMethod.Color:
                default:
                    return(buffer);

                case RenderMethod.GrayScaleLight:
                    return(ConvertToGrayScaleLight(buffer, (int)width, (int)height));

                case RenderMethod.GrayScaleDark:
                    return(ConvertToGrayScaleDark(buffer, (int)width, (int)height));

                case RenderMethod.Blue:
                    return(ConvertToBlue(buffer, (int)width, (int)height));
                }
            }
            finally
            {
                optComponent.ReleaseWindowSizeDependentResources();
                optComponent.ReleaseDeviceDependentResources();
                deviceResources.Release();
            }
        }