Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / WPF

Blend the OGRE Graphics Engine into your WPF projects

Rate me:
Please Sign up or sign in to vote.
4.85/5 (30 votes)
9 Sep 2008CPOL8 min read 182.7K   19.6K   68  
Blend a First Class Gaming Graphics Engine into your WPF application
using System;
using Mogre;

namespace OgreLib
{
    public partial class OgreImage
    {
        private double _resourceItemScalar;
        private double _currentProcess;

        protected virtual void CallResourceItemLoaded(ResourceLoadEventArgs e)
        {
            Dispatcher.Invoke((MethodInvoker)(() => OnResourceItemLoaded(e)));
        }

        protected virtual void OnResourceItemLoaded(ResourceLoadEventArgs e)
        {
            if (ResourceLoadItemProgress != null) ResourceLoadItemProgress(this, e);
        }

        private void InitResourceLoad()
        {
            ResourceGroupManager.Singleton.ResourceGroupLoadStarted += Singleton_ResourceGroupLoadStarted;
            ResourceGroupManager.Singleton.ResourceGroupScriptingStarted += Singleton_ResourceGroupScriptingStarted;
            ResourceGroupManager.Singleton.ScriptParseStarted += Singleton_ScriptParseStarted;
            ResourceGroupManager.Singleton.ResourceLoadStarted += Singleton_ResourceLoadStarted;
            ResourceGroupManager.Singleton.WorldGeometryStageStarted += Singleton_WorldGeometryStageStarted;

            _currentProcess = 0;
        }

        private void Singleton_WorldGeometryStageStarted(string description)
        {
            _currentProcess += _resourceItemScalar;
            CallResourceItemLoaded(new ResourceLoadEventArgs(description, _currentProcess));
        }

        private void Singleton_ResourceLoadStarted(ResourcePtr resource)
        {
            _currentProcess += _resourceItemScalar;
            CallResourceItemLoaded(new ResourceLoadEventArgs(resource.Name, _currentProcess));
        }

        private void Singleton_ScriptParseStarted(string scriptName)
        {
            _currentProcess += _resourceItemScalar;
            CallResourceItemLoaded(new ResourceLoadEventArgs(scriptName, _currentProcess));
        }

        private void Singleton_ResourceGroupScriptingStarted(string groupName, uint scriptCount)
        {
            _resourceItemScalar = (scriptCount > 0)
                                      ? 0.4d / scriptCount
                                      : 0;
        }

        private void Singleton_ResourceGroupLoadStarted(string groupName, uint resourceCount)
        {
            _resourceItemScalar = (resourceCount > 0)
                                      ? 0.6d / resourceCount
                                      : 0;
        }

        public event EventHandler<ResourceLoadEventArgs> ResourceLoadItemProgress;
    }

    public class ResourceLoadEventArgs : EventArgs
    {
        public ResourceLoadEventArgs(string name, double progress)
        {
            this.Name = name;
            this.Progress = progress;
        }

        public string Name { get; private set; }
        public double Progress { get; private set; }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead FNB Connect
South Africa South Africa
iOS Technical Lead at FNB
-

Computers are really sweet. Aren't they?
Yup they are...

I've always loved writing tools and components...never been very interested in playing games....always wanted to be able to write them though.

And, yes. I'm still pretty annoyed they discontinued the Amiga computer.

Comments and Discussions