Exemple #1
0
        /// <summary>
        /// Adds a resource to the scene
        /// </summary>
        /// <typeparam name="ResourceType">The type of the resource.</typeparam>
        /// <param name="resourceFactory">The factory method which creates the resource object.</param>
        /// <param name="resourceKey">The key for the newly generated resource.</param>
        /// <returns></returns>
        internal NamedOrGenericKey AddResource <ResourceType>(Func <ResourceType> resourceFactory, NamedOrGenericKey resourceKey)
            where ResourceType : Resource
        {
            resourceFactory.EnsureNotNull(nameof(resourceFactory));

            InitializeResourceDictionaries(true);

            if (resourceKey == NamedOrGenericKey.Empty)
            {
                resourceKey = GraphicsCore.GetNextGenericResourceKey();
            }
            foreach (ResourceDictionary actResourceDict in m_registeredResourceDicts)
            {
                actResourceDict.AddResource(resourceKey, resourceFactory());
            }
            return(resourceKey);
        }
        /// <summary>
        /// Performs default initialization logic.
        /// </summary>
        /// <returns></returns>
        public static async Task InitializeDefaultAsync()
        {
            if (s_current != null)
            {
                return;
            }

#if DESKTOP
            if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
            {
                return;
            }
#endif

            if (m_defaultInitTask != null)
            {
                await m_defaultInitTask;
                return;
            }
            else
            {
                // Initialize application object
                if (!SeeingSharpApplication.IsInitialized)
                {
                    m_defaultInitTask = SeeingSharpApplication.InitializeAsync(
                        typeof(GraphicsCore).GetTypeInfo().Assembly,
                        new Assembly[] {
                        typeof(ResourceLink).GetTypeInfo().Assembly,
                    },
                        new string[0]);
                    await m_defaultInitTask.ConfigureAwait(false);
                }

                // Initialize graphics core object
                if (!GraphicsCore.IsInitialized)
                {
                    GraphicsCore.Initialize(false, false);
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Prepares rendering (Loads all needed resources).
        /// </summary>
        /// <param name="renderState">Current render state.</param>
        internal void PrepareRendering(RenderState renderState)
        {
            List <SceneObject> invalidObjects = null;

            // Load all resources
            int sceneObjectArrayLength = m_sceneObjects.Count;

            SceneObject[] sceneObjectArray = m_sceneObjects.GetBackingArray();
            for (int loop = 0; loop < sceneObjectArrayLength; loop++)
            {
                SceneObject actObject = sceneObjectArray[loop];

                try
                {
                    // Load all resources of the object
                    if (!actObject.IsLoaded(renderState.Device))
                    {
                        actObject.LoadResources(renderState.Device, renderState.CurrentResources);
                    }
                }
                catch (Exception ex)
                {
                    GraphicsCore.PublishInternalExceptionInfo(
                        ex, InternalExceptionLocation.Loading3DObject);

                    //Build list of invalid objects
                    if (invalidObjects == null)
                    {
                        invalidObjects = new List <SceneObject>();
                    }
                    invalidObjects.Add(actObject);
                }
            }

            //Remove all invalid objects
            if (invalidObjects != null)
            {
                HandleInvalidObjects(invalidObjects);
            }
        }
Exemple #4
0
        /// <summary>
        /// Initializes this scene object.
        /// </summary>
        /// <param name="throwExceptionIfUnable"></param>
        private void InitializeResourceDictionaries(bool throwExceptionIfUnable)
        {
            if (m_initialized)
            {
                return;
            }

            GraphicsCore.Touch();
            if (!GraphicsCore.IsInitialized)
            {
                return;
            }

            // Create all ResourceDictionary objects
            foreach (EngineDevice actDevice in GraphicsCore.Current.LoadedDevices)
            {
                m_registeredResourceDicts.AddObject(
                    new ResourceDictionary(actDevice),
                    actDevice.DeviceIndex);
            }

            m_initialized = true;
        }
Exemple #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FactoryHandlerDWrite"/> class.
 /// </summary>
 /// <param name="core">The core.</param>
 internal FactoryHandlerDWrite(GraphicsCore core)
 {
     //Create DirectWrite Factory object
     m_factory = new DWrite.Factory(DWrite.FactoryType.Shared);
 }
Exemple #6
0
        /// <summary>
        /// Adds the given resource to the dictionary.
        /// </summary>
        /// <param name="resource">The resource to add.</param>
        /// <param name="resourceKey">The key of the resource.</param>
        internal ResourceType AddResource <ResourceType>(NamedOrGenericKey resourceKey, ResourceType resource)
            where ResourceType : Resource
        {
            //Perform some checks
            if (resource == null)
            {
                throw new ArgumentNullException("resource");
            }
            if (resource.Dictionary != null)
            {
                if (resource.Dictionary == this)
                {
                    return(resource);
                }
                if (resource.Dictionary != this)
                {
                    throw new ArgumentException("Given resource belongs to another ResourceDictionary!", "resource");
                }
            }

            //Check given keys
            if ((!resource.IsKeyEmpty) && (!resourceKey.IsEmpty) && (resource.Key != resourceKey))
            {
                throw new ArgumentException("Unable to override existing key on given resource!");
            }

            //Remove another resource with the same name
            if (!resource.IsKeyEmpty)
            {
                RemoveResource(resource.Key);
            }
            if (!resourceKey.IsEmpty)
            {
                RemoveResource(resourceKey);
            }

            //Apply a valid key on the given resource object
            if (resource.Key.IsEmpty)
            {
                if (resourceKey.IsEmpty)
                {
                    resource.Key = GraphicsCore.GetNextGenericResourceKey();
                }
                else
                {
                    resource.Key = resourceKey;
                }
            }

            //Add the resource
            ResourceInfo newResource = new ResourceInfo(resource);

            m_resources[resource.Key] = newResource;
            if (newResource.RenderableResource != null)
            {
                m_renderableResources.Add(newResource.RenderableResource);
            }

            //Register this dictionary on the resource
            resource.Dictionary = this;

            return(resource);
        }