예제 #1
0
 private void cmdInitialize_Click(object sender, RoutedEventArgs e)
 {
     _context = AquaLib.aqua_initialize(100, 75, 20, 10 /* frames per second */ * 3600 /* seconds per hour */ * 4 /* hours */);
 }
예제 #2
0
        private void tbConfigureLights_Click(object sender, RoutedEventArgs e)
        {
            if (tbConfigureLights.IsChecked ?? false)
            {
                if (_context == IntPtr.Zero)
                {
                    tbConfigureLights.IsChecked = false;
                    return;
                }

                if (_lightMap != IntPtr.Zero)
                {
                    AquaLib.aqua_free_light_map(_lightMap);
                }

                _lights.Clear();
                _lightMap          = IntPtr.Zero;
                _lightMapBuffer    = null;
                pLights.Data       = new PathGeometry();
                imgLightMap.Source = null;

                if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
                {
                    const int NumLights = 300;

                    AquaLib.aqua_get_frame_size(_context, out int width, out int height);

                    double totalArea    = width * height * 2;
                    double areaPerLight = totalArea / NumLights;

                    double squareSide = Math.Sqrt(areaPerLight);

                    double x = squareSide * 0.25;
                    double y = squareSide * 0.5;

                    double minX = squareSide * 0.25;
                    double maxX = width - squareSide * 0.25;
                    int    dx   = 1;

                    double xScale = imgDisplay.ActualWidth / width;
                    double yScale = imgDisplay.ActualHeight / height;

                    for (int i = 0; i < NumLights; i++)
                    {
                        var scaledPosition   = new AquaPoint(x, y);
                        var physicalPosition = new Point(x * xScale, y * yScale);

                        AddLight(physicalPosition, scaledPosition);

                        x += dx * (squareSide * 0.5);

                        if ((dx > 0) && (x > maxX))
                        {
                            x  = 2 * maxX - x;
                            y += squareSide;
                            dx = -1;
                        }
                        else if ((dx < 0) && (x < minX))
                        {
                            x  = 2 * minX - x;
                            y += squareSide;
                            dx = +1;
                        }
                    }

                    tbConfigureLights.IsChecked = false;
                }
            }

            if (!(tbConfigureLights.IsChecked ?? false))
            {
                if (_lights.Count > 0)
                {
                    try
                    {
                        _lightMap = AquaLib.aqua_generate_light_map(_context, _lights.Count, _lights.ToArray());

                        AquaLib.aqua_light_map_get_light_for_pixel(_lightMap, out int width, out int height, null);
                        _lightMapBuffer = new int[width * height];
                        AquaLib.aqua_light_map_get_light_for_pixel(_lightMap, out width, out height, _lightMapBuffer);

                        AquaLib.aqua_light_map_get_light_pixel_count(_lightMap, out int numLights, null);

                        int[] randomColor = new int[numLights];

                        int opaque = unchecked ((int)0xFF000000);

                        for (int i = 0; i < numLights; i++)
                        {
                            randomColor[i] = opaque | _rnd.Next(0, 16777216);
                        }

                        int[] imageBuffer = new int[width * height];

                        for (int i = 0; i < imageBuffer.Length; i++)
                        {
                            if (_lightMapBuffer[i] >= 0)
                            {
                                imageBuffer[i] = randomColor[_lightMapBuffer[i]];
                            }
                        }

                        imgLightMap.Source = BitmapSource.Create(
                            width,
                            height,
                            96.0,
                            96.0,
                            PixelFormats.Pbgra32,
                            null,
                            imageBuffer,
                            width * 4);
                    }
                    catch
                    {
                        if (_lightMap != IntPtr.Zero)
                        {
                            AquaLib.aqua_free_light_map(_lightMap);
                        }

                        _lightMap = IntPtr.Zero;

                        throw;
                    }
                }
            }
        }