Beispiel #1
0
        /// <summary>
        /// Called when a game resource is loaded.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Platformer.Engine.Resources.ResourceLoadEventArgs" /> instance containing the event data.</param>
        protected override void OnGameResourceLoaded(Object sender, ResourceLoadEventArgs e)
        {
            if (e.ResourceType == typeof(Map))
            {
                var map = e.LoadedResource as Map;
                foreach (var tileSet in map.TileSets)
                {
                    using (var decoder = new BitmapDecoder(_imagingFactory, tileSet.ImagePath, DecodeOptions.CacheOnDemand))
                    {
                        using (var formatConverter = new FormatConverter(_imagingFactory))
                        {
                            formatConverter.Initialize(
                                decoder.GetFrame(0),
                                PixelFormat.Format32bppPBGRA,
                                BitmapDitherType.DualSpiral8x8,
                                null,
                                0.0,
                                BitmapPaletteType.Custom);

                            var bitmap = SharpDX.Direct2D1.Bitmap1.FromWicBitmap(_d2dContext, formatConverter);
                            _tileSetTextures.Add(tileSet.Id, bitmap);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Called when a game resource is loaded.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Platformer.Engine.Resources.ResourceLoadEventArgs" /> instance containing the event data.</param>
        protected override void OnGameResourceLoaded(object sender, ResourceLoadEventArgs e)
        {
            if (e.ResourceType == typeof(Sprite))
            {
                // get the sprite that has been loaded
                var sprite = e.LoadedResource as Sprite;

                // find, load and store the sprite sheet for the sprite
                var imagePath = Path.Combine(_resourceService.GetResourcePath <Sprite>(), sprite.Name + ".png");
                using (var decoder = new BitmapDecoder(_imagingFactory, imagePath, DecodeOptions.CacheOnDemand))
                {
                    using (var formatConverter = new FormatConverter(_imagingFactory))
                    {
                        formatConverter.Initialize(
                            decoder.GetFrame(0),
                            PixelFormat.Format32bppPBGRA,
                            BitmapDitherType.DualSpiral8x8,
                            null,
                            0.0,
                            BitmapPaletteType.Custom);

                        var bitmap = SharpDX.Direct2D1.Bitmap1.FromWicBitmap(_d2dContext, formatConverter);
                        _spriteTextures.Add(sprite.ResourceId, bitmap);
                    }
                }

                // create a dictionary to store the sprite animations
                var rectDictionary = new Dictionary <int, RawRectangleF[]>();
                _animationRects.Add(sprite.ResourceId, rectDictionary);

                // enumerate each animation and frame
                foreach (var animation in sprite.Animations)
                {
                    var frameRects = new RawRectangleF[animation.Frames.Length];

                    var curFrame = 0;

                    foreach (var frame in animation.Frames)
                    {
                        var x      = frame.Rect.Position.X;
                        var y      = frame.Rect.Position.Y;
                        var width  = frame.Rect.Size.X;
                        var height = frame.Rect.Size.Y;

                        frameRects[curFrame] = new RawRectangleF(x, y, x + width, y + height);
                        curFrame++;
                    }

                    rectDictionary.Add(animation.Id, frameRects);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Called when a game resource is loaded.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Platformer.Engine.Resources.ResourceLoadEventArgs" /> instance containing the event data.</param>
        protected override void OnGameResourceLoaded(Object sender, ResourceLoadEventArgs e)
        {
            if (e.ResourceType == typeof(Sound))
            {
                var sound = e.LoadedResource as Sound;
                var path  = Path.Combine(_resourceService.GetResourcePath <Sound>(), sound.Name + ".wav");

                var sample = Bass.BASS_SampleLoad(path, 0L, 0, AudioPlaybackManager.NumChannels, BASSFlag.BASS_DEFAULT);
                if (sample != 0)
                {
                    _sounds.Add(e.LoadedResource.ResourceId, sample);
                }
            }
        }
        void ICefWebBrowserInternal.OnBeforeResourceLoad(ResourceLoadEventArgs ea)
        {
            // Invoke synchronously because the ResourceLoadEventArgs.Cancel property is read
            // by callers to determine whether the resource should be loaded or not.
            try
            {
                if (!IsResourceAccessible(ea.Url))
                {
                    // Give the container a chance to override
                    if (BeforeResourceLoad != null)
                    {
                        BeforeResourceLoad(this, ea);
                    }

                    if (!ea.Cancel)
                    {
                        // The URL is whitelisted by the application which takes preference. OK to load.
                        return;
                    }

                    // Requested resource is not accessible. It may not be sourced from one of the
                    // whitelisted domains, for example. Log this info to the paragon and dev tools logs.
                    ea.Cancel = true;
                    var msg = "Resource not accessible: " + ea.Url;
                    ExecuteJavaScript("window.console.error('" + msg + "');");
                    Logger.Error(msg);

                    // If the resource is the main frame, set a flag for later use.
                    if (ea.ResourceType == CefResourceType.MainFrame)
                    {
                        _mainFrameResourceNotAccessible = true;
                    }
                }
                else if (!_allowedProtocols.Contains(new Uri(ea.Url).Scheme))
                {
                    // The protocol scheme for the requested resource is not in the list
                    // of allowed protocol schemes. Cancel the request and log the failure.
                    ea.Cancel = true;
                    var msg = "Resource not accessible due to unknown protocol scheme: " + ea.Url;
                    ExecuteJavaScript("window.console.error('" + msg + "');");
                    Logger.Error(msg);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Error in OnBeforeResourceLoad : {0}. Resource loading will be aborted.", ex);
                ea.Cancel = true;
            }
        }
 /// <summary>
 /// Called when a game resource is loaded.
 /// </summary>
 /// <param name="sender">The sender.</param>
 /// <param name="e">The <see cref="Platformer.Engine.Resources.ResourceLoadEventArgs" /> instance containing the event data.</param>
 protected virtual void OnGameResourceLoaded(object sender, ResourceLoadEventArgs e)
 {
 }
Beispiel #6
0
 internal static void OnResourceLoading(object sender, ResourceLoadEventArgs e) => ResourceLoading(sender, e);
Beispiel #7
0
        /// <summary>
        /// Loads the resource data.
        /// </summary>
        /// <param name="resourceName">Name of the resource.</param>
        public GameResource LoadResource(Type resourceType, string resourceName)
        {
            GameResource retval = null;

            if (string.IsNullOrEmpty(resourceName))
            {
                return(retval);
            }

            lock (_resourceLock)
            {
                var resourceTypeName = resourceType.FullName;

                // get the resource list
                Dictionary <string, GameResource> typeResources;
                if (!_resources.TryGetValue(resourceTypeName, out typeResources))
                {
                    typeResources = new Dictionary <string, GameResource>(StringComparer.OrdinalIgnoreCase);
                    _resources.Add(resourceTypeName, typeResources);
                }

                if (!typeResources.TryGetValue(resourceName, out retval))
                {
                    var args = new ResourceLoadEventArgs();
                    args.ResourceName = resourceName;
                    args.ResourceType = resourceType;

                    TraceSource.TraceEvent(TraceEventType.Verbose, 0, Strings.LoadingResource, resourceTypeName, resourceName);

                    if (resourceType == typeof(Sprite))
                    {
                        // load the sprite
                        retval = LoadSprite(resourceName);
                    }
                    else if (resourceType == typeof(Map))
                    {
                        var mapService = _engine.GetService <IMapService>();
                        retval = mapService.LoadMap(Path.Combine(GetResourcePath <Map>(), resourceName + ".tmx"));
                    }
                    else if (resourceType == typeof(Sound))
                    {
                        var sound = new Sound()
                        {
                            Name = resourceName
                        };
                        retval = sound;
                    }

                    args.LoadedResource = retval;
                    retval.ResourceId   = _nextResourceId++;

                    typeResources.Add(resourceName, retval);
                    _resourcesById.Add(retval.ResourceId, retval);

                    // the engine has loaded what it needs, but the renderer might want to load image files etc.
                    ResourceLoaded?.Invoke(this, args);
                }
            }

            return(retval);
        }