Example #1
0
        Vec2I GetNeededSize()
        {
            Vec2I result;

            ScreenControlManager screenControlManager = GetControlManager() as ScreenControlManager;

            if (screenControlManager != null)
            {
                //screen gui

                Vec2I viewportSize = screenControlManager.GuiRenderer.ViewportForScreenGuiRenderer.
                                     DimensionsInPixels.Size;

                Vec2 size = viewportSize.ToVec2() * GetScreenSize();
                if (screenControlManager.GuiRenderer._OutGeometryTransformEnabled)
                {
                    size *= screenControlManager.GuiRenderer._OutGeometryTransformScale;
                }

                result = new Vec2I((int)(size.X + .9999f), (int)(size.Y + .9999f));
            }
            else
            {
                //in-game gui

                int height = inGame3DGuiHeightInPixels;
                if (height > RenderSystem.Instance.Capabilities.MaxTextureSize)
                {
                    height = RenderSystem.Instance.Capabilities.MaxTextureSize;
                }

                Vec2  screenSize = GetScreenSize();
                float width      = (float)height * (screenSize.X / screenSize.Y) * GetControlManager().AspectRatio;
                result = new Vec2I((int)(width + .9999f), height);
            }

            if (result.X < 1)
            {
                result.X = 1;
            }
            if (result.Y < 1)
            {
                result.Y = 1;
            }
            return(result);
        }
        protected virtual void OnDestroy()
        {
            if (updateTimer != null)
            {
                updateTimer.Stop();
                updateTimer = null;
            }

            if (guiRenderer != null)
            {
                guiRenderer.Dispose();
                guiRenderer = null;
            }
            controlManager = null;

            DestroyRenderTarget();

            WPFAppWorld.renderTargetUserControls.Remove(this);
        }
Example #3
0
        protected override void OnDestroy()
        {
            MapSystemWorld.MapDestroy();
            if (EntitySystemWorld.Instance != null)
            {
                EntitySystemWorld.Instance.WorldDestroy();
            }

            Server_DestroyServer("The server has been destroyed");
            Client_DisconnectFromServer();

            EntitySystemWorld.Shutdown();

            GameControlsManager.Shutdown();

            ControlsWorld.Shutdown();
            controlManager = null;

            EngineConsole.Shutdown();

            instance = null;
            base.OnDestroy();
        }
        private bool CreateRenderTarget()
        {
            DestroyRenderTarget();

            if (RendererWorld.Instance == null)
            {
                return(false);
            }

            Vec2I textureSize = GetDemandTextureSize();

            if (textureSize.X < 1 || textureSize.Y < 1)
            {
                return(false);
            }

            string textureName = TextureManager.Instance.GetUniqueName("WPFRenderTexture");

            int hardwareFSAA = 0;

            if (!RendererWorld.InitializationOptions.AllowSceneMRTRendering)
            {
                if (!int.TryParse(RendererWorld.InitializationOptions.FullSceneAntialiasing, out hardwareFSAA))
                {
                    hardwareFSAA = 0;
                }
            }

            texture = TextureManager.Instance.Create(textureName, Texture.Type.Type2D, textureSize,
                                                     1, 0, Engine.Renderer.PixelFormat.R8G8B8, Texture.Usage.RenderTarget, false, hardwareFSAA);

            if (texture == null)
            {
                return(false);
            }

            currentTextureSize = textureSize;

            renderTexture                     = texture.GetBuffer().GetRenderTarget();
            renderTexture.AutoUpdate          = false;
            renderTexture.AllowAdditionalMRTs = true;

            camera = SceneManager.Instance.CreateCamera(
                SceneManager.Instance.GetUniqueCameraName("UserControl"));
            camera.Purpose = Camera.Purposes.MainCamera;

            //update camera settings
            camera.NearClipDistance = cameraNearFarClipDistance.Minimum;
            camera.FarClipDistance  = cameraNearFarClipDistance.Maximum;
            camera.AspectRatio      = (float)texture.Size.X / (float)texture.Size.Y;
            camera.FixedUp          = cameraFixedUp;
            camera.Position         = cameraPosition;
            camera.Direction        = cameraDirection;
            camera.Fov               = cameraFov;
            camera.ProjectionType    = cameraProjectionType;
            camera.OrthoWindowHeight = cameraOrthoWindowHeight;

            viewport = renderTexture.AddViewport(camera);

            //Initialize HDR compositor for HDR render technique
            if (EngineApp.RenderTechnique == "HDR")
            {
                viewport.AddCompositor("HDR", 0);
                viewport.SetCompositorEnabled("HDR", true);
            }

            //Initialize Fast Approximate Antialiasing (FXAA)
            {
                bool   useMRT = RendererWorld.InitializationOptions.AllowSceneMRTRendering;
                string fsaa   = RendererWorld.InitializationOptions.FullSceneAntialiasing;
                if ((useMRT && (fsaa == "" || fsaa == "RecommendedSetting") && IsActivateFXAAByDefault()) ||
                    fsaa == "FXAA")
                {
                    if (RenderSystem.Instance.HasShaderModel3())
                    {
                        InitializeFXAACompositor();
                    }
                }
            }

            //add listener
            renderTargetListener = new ViewRenderTargetListener(this);
            renderTexture.AddListener(renderTargetListener);

            if (guiRenderer == null)
            {
                guiRenderer = new GuiRenderer(viewport);
            }
            else
            {
                guiRenderer.ChangeViewport(viewport);
            }

            if (controlManager == null)
            {
                controlManager = new ScreenControlManager(guiRenderer);
            }

            //initialize D3DImage output
            if (d3dImageIsSupported && allowUsingD3DImage)
            {
                // create a D3DImage to host the scene and monitor it for changes in front buffer availability
                if (d3dImage == null)
                {
                    d3dImage = new D3DImage();
                    d3dImage.IsFrontBufferAvailableChanged += D3DImage_IsFrontBufferAvailableChanged;
                    CompositionTarget.Rendering            += D3DImage_OnRendering;
                }

                // set output to background image
                Background = new ImageBrush(d3dImage);

                // set the back buffer using the new scene pointer
                HardwarePixelBuffer            buffer = texture.GetBuffer(0, 0);
                GetD3D9HardwarePixelBufferData data   = new GetD3D9HardwarePixelBufferData();
                data.hardwareBuffer = buffer._GetRealObject();
                data.outPointer     = IntPtr.Zero;
                unsafe
                {
                    GetD3D9HardwarePixelBufferData *pData = &data;
                    if (!RenderSystem.Instance.CallCustomMethod("Get D3D9HardwarePixelBuffer getSurface", (IntPtr)pData))
                    {
                        Log.Fatal("Get D3D9HardwarePixelBuffer getSurface failed.");
                    }
                }
                d3dImage.Lock();
                d3dImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, data.outPointer);
                d3dImage.Unlock();
            }

            return(true);
        }
        protected override void OnDestroy()
        {
            MultiViewRenderingManager.Shutdown();

            MapSystemWorld.MapDestroy();
            if (EntitySystemWorld.Instance != null)
                EntitySystemWorld.Instance.WorldDestroy();

            Server_DestroyServer("The server has been destroyed");
            Client_DisconnectFromServer();

            EntitySystemWorld.Shutdown();

            GameControlsManager.Shutdown();

            ControlsWorld.Shutdown();
            controlManager = null;

            EngineConsole.Shutdown();

            instance = null;
            base.OnDestroy();
        }
        protected override bool OnCreate()
        {
            instance = this;

            ChangeToBetterDefaultSettings();

            if (!base.OnCreate())
                return false;

            SoundVolume = soundVolume;
            MusicVolume = musicVolume;

            controlManager = new ScreenControlManager(ScreenGuiRenderer);
            if (!ControlsWorld.Init())
                return false;

            _ShowSystemCursor = _ShowSystemCursor;
            _DrawFPS = _DrawFPS;
            MaterialScheme = materialScheme;

            Log.Handlers.InvisibleInfoHandler += InvisibleLog_Handlers_InfoHandler;
            Log.Handlers.InfoHandler += Log_Handlers_InfoHandler;
            Log.Handlers.WarningHandler += Log_Handlers_WarningHandler;
            Log.Handlers.ErrorHandler += Log_Handlers_ErrorHandler;
            Log.Handlers.FatalHandler += Log_Handlers_FatalHandler;

            //Camera
            Camera camera = RendererWorld.Instance.DefaultCamera;
            camera.NearClipDistance = .1f;
            camera.FarClipDistance = 1000.0f;
            camera.FixedUp = Vec3.ZAxis;
            camera.Fov = 90;
            camera.Position = new Vec3(100, 100, 10);
            camera.LookAt(new Vec3(0, 0, 0));

            Control programLoadingWindow = ControlDeclarationManager.Instance.CreateControl(
                "Gui\\ProgramLoadingWindow.gui");
            if (programLoadingWindow != null)
                controlManager.Controls.Add(programLoadingWindow);

            //Subcribe to callbacks during engine loading. We will render scene from callback.
            LongOperationCallbackManager.Subscribe(LongOperationCallbackManager_LoadingCallback,
                programLoadingWindow);

            //load materials.
            if (!HighLevelMaterialManager.Instance.NeedLoadAllMaterialsAtStartup)
            {
                //prevent double initialization of materials after startup by means CreateEmptyMaterialsForFasterStartupInitialization = true.
                ShaderBaseMaterial.CreateEmptyMaterialsForFasterStartupInitialization = true;
                if (!HighLevelMaterialManager.Instance.LoadAllMaterials())
                {
                    LongOperationCallbackManager.Unsubscribe();
                    return true;
                }
                ShaderBaseMaterial.CreateEmptyMaterialsForFasterStartupInitialization = false;
            }

            RenderScene();

            //Game controls
            GameControlsManager.Init();

            //EntitySystem
            if (!EntitySystemWorld.Init(new EntitySystemWorld()))
            {
                LongOperationCallbackManager.Unsubscribe();
                return true;
            }

            //load autorun map
            string mapName = GetAutorunMapName();
            bool mapLoadingFailed = false;
            if (mapName != "")
            {
                //hide loading window.
                LongOperationCallbackManager.Unsubscribe();
                if (programLoadingWindow != null)
                    programLoadingWindow.SetShouldDetach();

                if (!ServerOrSingle_MapLoad(mapName, EntitySystemWorld.Instance.DefaultWorldType, false))
                    mapLoadingFailed = true;
            }

            //finish initialization of materials and hide loading window.
            ShaderBaseMaterial.FinishInitializationOfEmptyMaterials();
            LongOperationCallbackManager.Unsubscribe();
            if (programLoadingWindow != null)
                programLoadingWindow.SetShouldDetach();

            //if no autorun map play music and go to EngineLogoWindow.
            if (Map.Instance == null && !mapLoadingFailed)
            {
                GameMusic.MusicPlay("Sounds\\Music\\MainMenu.ogg", true);
                controlManager.Controls.Add(new EngineLogoWindow());
            }

            //register "showProfilingTool" console command
            if (EngineConsole.Instance != null)
            {
                EngineConsole.Instance.AddCommand("showProfilingTool", ConsoleCommand_ShowProfilingTool);
                EngineConsole.Instance.AddCommand("Menu", ConsoleCommand_LoadMainMenu);
            }
            //example of custom input device
            //ExampleCustomInputDevice.InitDevice();

            return true;
        }
Example #7
0
        protected override bool OnCreate()
        {
            instance = this;

            ChangeToBetterDefaultSettings();

            if (!base.OnCreate())
            {
                return(false);
            }

            SoundVolume = soundVolume;
            MusicVolume = musicVolume;

            controlManager = new ScreenControlManager(ScreenGuiRenderer);
            if (!ControlsWorld.Init())
            {
                return(false);
            }

            _ShowSystemCursor = _ShowSystemCursor;
            _DrawFPS          = _DrawFPS;
            MaterialScheme    = materialScheme;

            Log.Handlers.InfoHandler    += Log_Handlers_InfoHandler;
            Log.Handlers.WarningHandler += Log_Handlers_WarningHandler;
            Log.Handlers.ErrorHandler   += Log_Handlers_ErrorHandler;
            Log.Handlers.FatalHandler   += Log_Handlers_FatalHandler;

            //Camera
            Camera camera = RendererWorld.Instance.DefaultCamera;

            camera.NearClipDistance = .1f;
            camera.FarClipDistance  = 1000.0f;
            camera.FixedUp          = Vec3.ZAxis;
            camera.Fov      = 90;
            camera.Position = new Vec3(-10, -10, 10);
            camera.LookAt(new Vec3(0, 0, 0));

            EControl programLoadingWindow = ControlDeclarationManager.Instance.CreateControl(
                "Gui\\ProgramLoadingWindow.gui");

            if (programLoadingWindow != null)
            {
                controlManager.Controls.Add(programLoadingWindow);
            }

            //Subcribe to callbacks during engine loading. We will render scene from callback.
            LongOperationCallbackManager.Subscribe(LongOperationCallbackManager_LoadingCallback,
                                                   programLoadingWindow);

            if (!HighLevelMaterialManager.Instance.LoadAllMaterials())
            {
                LongOperationCallbackManager.Unsubscribe();
                return(true);
            }

            RenderScene();

            //Game controls
            GameControlsManager.Init();

            //EntitySystem
            if (!EntitySystemWorld.Init(new EntitySystemWorld()))
            {
                LongOperationCallbackManager.Unsubscribe();
                return(true);
            }

            LongOperationCallbackManager.Unsubscribe();

            //close loading window.
            if (programLoadingWindow != null)
            {
                programLoadingWindow.SetShouldDetach();
            }

            string mapName = "";

            if (autorunMapName != "" && autorunMapName.Length > 2)
            {
                mapName = autorunMapName;
                if (!mapName.Contains("\\") && !mapName.Contains("/"))
                {
                    mapName = "Maps/" + mapName + "/Map.map";
                }
            }

            if (!WebPlayerMode)
            {
                string[] commandLineArgs = Environment.GetCommandLineArgs();
                if (commandLineArgs.Length > 1)
                {
                    string name = commandLineArgs[1];
                    if (name[0] == '\"' && name[name.Length - 1] == '\"')
                    {
                        name = name.Substring(1, name.Length - 2);
                    }
                    name = name.Replace('/', '\\');

                    string dataDirectory = VirtualFileSystem.ResourceDirectoryPath;
                    dataDirectory = dataDirectory.Replace('/', '\\');

                    if (name.Length > dataDirectory.Length)
                    {
                        if (string.Compare(name.Substring(0, dataDirectory.Length), dataDirectory, true) == 0)
                        {
                            name = name.Substring(dataDirectory.Length + 1);
                        }
                    }

                    mapName = name;
                }
            }

            if (mapName != "")
            {
                if (!ServerOrSingle_MapLoad(mapName, EntitySystemWorld.Instance.DefaultWorldType, false))
                {
                    //Error
                    foreach (EControl control in controlManager.Controls)
                    {
                        if (control is MessageBoxWindow && !control.IsShouldDetach())
                        {
                            return(true);
                        }
                    }

                    GameMusic.MusicPlay("Sounds\\Music\\MainMenu.ogg", true);
                    controlManager.Controls.Add(new EngineLogoWindow());
                }
            }
            else
            {
                GameMusic.MusicPlay("Sounds\\Music\\MainMenu.ogg", true);
                controlManager.Controls.Add(new EngineLogoWindow());
            }

            //showDebugInformation console command
            if (EngineConsole.Instance != null)
            {
                EngineConsole.Instance.AddCommand("showDebugInformationWindow",
                                                  ConsoleCommand_ShowDebugInformationWindow);
            }

            //example of custom input device
            //ExampleCustomInputDevice.InitDevice();

            return(true);
        }
        bool CreateRenderTarget()
        {
            DestroyRenderTarget();

            if( RendererWorld.Instance == null )
                return false;

            Vec2I textureSize = GetDemandTextureSize();
            if( textureSize.X < 1 || textureSize.Y < 1 )
                return false;

            string textureName = TextureManager.Instance.GetUniqueName( "WPFRenderTexture" );

            int hardwareFSAA = 0;
            if( !RendererWorld.InitializationOptions.AllowSceneMRTRendering )
            {
                if( !int.TryParse( RendererWorld.InitializationOptions.FullSceneAntialiasing, out hardwareFSAA ) )
                    hardwareFSAA = 0;
            }

            texture = TextureManager.Instance.Create( textureName, Texture.Type.Type2D, textureSize,
                1, 0, Engine.Renderer.PixelFormat.R8G8B8, Texture.Usage.RenderTarget, false, hardwareFSAA );

            if( texture == null )
                return false;

            currentTextureSize = textureSize;

            renderTexture = texture.GetBuffer().GetRenderTarget();
            renderTexture.AutoUpdate = false;
            renderTexture.AllowAdditionalMRTs = true;

            camera = SceneManager.Instance.CreateCamera(
                SceneManager.Instance.GetUniqueCameraName( "UserControl" ) );
            camera.Purpose = Camera.Purposes.MainCamera;

            //update camera settings
            camera.NearClipDistance = cameraNearFarClipDistance.Minimum;
            camera.FarClipDistance = cameraNearFarClipDistance.Maximum;
            camera.AspectRatio = (float)texture.Size.X / (float)texture.Size.Y;
            camera.FixedUp = cameraFixedUp;
            camera.Position = cameraPosition;
            camera.Direction = cameraDirection;
            camera.Fov = cameraFov;
            camera.ProjectionType = cameraProjectionType;
            camera.OrthoWindowHeight = cameraOrthoWindowHeight;

            viewport = renderTexture.AddViewport( camera );

            //Initialize HDR compositor for HDR render technique
            if( EngineApp.RenderTechnique == "HDR" )
            {
                viewport.AddCompositor( "HDR", 0 );
                viewport.SetCompositorEnabled( "HDR", true );
            }

            //Initialize Fast Approximate Antialiasing (FXAA)
            {
                bool useMRT = RendererWorld.InitializationOptions.AllowSceneMRTRendering;
                string fsaa = RendererWorld.InitializationOptions.FullSceneAntialiasing;
                if( ( useMRT && ( fsaa == "" || fsaa == "RecommendedSetting" ) && IsActivateFXAAByDefault() ) ||
                    fsaa == "FXAA" )
                {
                    if( RenderSystem.Instance.HasShaderModel3() )
                        InitializeFXAACompositor();
                }
            }

            //add listener
            renderTargetListener = new ViewRenderTargetListener( this );
            renderTexture.AddListener( renderTargetListener );

            if( guiRenderer == null )
                guiRenderer = new GuiRenderer( viewport );
            else
                guiRenderer.ChangeViewport( viewport );

            if( controlManager == null )
                controlManager = new ScreenControlManager( guiRenderer );

            //initialize D3DImage output
            if( d3dImageIsSupported && allowUsingD3DImage )
            {
                // create a D3DImage to host the scene and monitor it for changes in front buffer availability
                if( d3dImage == null )
                {
                    d3dImage = new D3DImage();
                    d3dImage.IsFrontBufferAvailableChanged += D3DImage_IsFrontBufferAvailableChanged;
                    CompositionTarget.Rendering += D3DImage_OnRendering;
                }

                // set output to background image
                Background = new ImageBrush( d3dImage );

                // set the back buffer using the new scene pointer
                HardwarePixelBuffer buffer = texture.GetBuffer( 0, 0 );
                GetD3D9HardwarePixelBufferData data = new GetD3D9HardwarePixelBufferData();
                data.hardwareBuffer = buffer._GetRealObject();
                data.outPointer = IntPtr.Zero;
                unsafe
                {
                    GetD3D9HardwarePixelBufferData* pData = &data;
                    if( !RenderSystem.Instance.CallCustomMethod( "Get D3D9HardwarePixelBuffer getSurface", (IntPtr)pData ) )
                        Log.Fatal( "Get D3D9HardwarePixelBuffer getSurface failed." );
                }
                d3dImage.Lock();
                d3dImage.SetBackBuffer( D3DResourceType.IDirect3DSurface9, data.outPointer );
                d3dImage.Unlock();
            }

            return true;
        }
        protected virtual void OnDestroy()
        {
            if( updateTimer != null )
            {
                updateTimer.Stop();
                updateTimer = null;
            }

            if( guiRenderer != null )
            {
                guiRenderer.Dispose();
                guiRenderer = null;
            }
            controlManager = null;

            DestroyRenderTarget();

            WPFAppWorld.renderTargetUserControls.Remove( this );
        }
        protected override bool OnCreate()
        {
            instance = this;

            ChangeToBetterDefaultSettings();

            if( !base.OnCreate() )
                return false;

            SoundVolume = soundVolume;
            MusicVolume = musicVolume;

            controlManager = new ScreenControlManager();
            if( !ControlsWorld.Init() )
                return false;

            _ShowSystemCursor = _ShowSystemCursor;
            _DrawFPS = _DrawFPS;
            MaterialScheme = materialScheme;

            EControl programLoadingWindow = ControlDeclarationManager.Instance.CreateControl(
                "Gui\\ProgramLoadingWindow.gui" );
            if( programLoadingWindow != null )
                controlManager.Controls.Add( programLoadingWindow );

            RenderScene();

            Log.Handlers.InfoHandler += Log_Handlers_InfoHandler;
            Log.Handlers.WarningHandler += Log_Handlers_WarningHandler;
            Log.Handlers.ErrorHandler += Log_Handlers_ErrorHandler;
            Log.Handlers.FatalHandler += Log_Handlers_FatalHandler;

            //Camera
            Camera camera = RendererWorld.Instance.DefaultCamera;
            camera.NearClipDistance = .1f;
            camera.FarClipDistance = 1000.0f;
            camera.FixedUp = Vec3.ZAxis;
            camera.Fov = 90;
            camera.Position = new Vec3( -10, -10, 10 );
            camera.LookAt( new Vec3( 0, 0, 0 ) );

            if( programLoadingWindow != null )
                programLoadingWindow.SetShouldDetach();

            //Game controls
            GameControlsManager.Init();

            //EntitySystem
            if( !EntitySystemWorld.Init( new EntitySystemWorld() ) )
                return true;// false;

            string mapName = "";

            if( autorunMapName != "" && autorunMapName.Length > 2 )
            {
                mapName = autorunMapName;
                if( !mapName.Contains( "\\" ) && !mapName.Contains( "/" ) )
                    mapName = "Maps/" + mapName + "/Map.map";
            }

            if( !WebPlayerMode )
            {
                string[] commandLineArgs = Environment.GetCommandLineArgs();
                if( commandLineArgs.Length > 1 )
                {
                    string name = commandLineArgs[ 1 ];
                    if( name[ 0 ] == '\"' && name[ name.Length - 1 ] == '\"' )
                        name = name.Substring( 1, name.Length - 2 );
                    name = name.Replace( '/', '\\' );

                    string dataDirectory = VirtualFileSystem.ResourceDirectoryPath;
                    dataDirectory = dataDirectory.Replace( '/', '\\' );

                    if( name.Length > dataDirectory.Length )
                        if( string.Compare( name.Substring( 0, dataDirectory.Length ), dataDirectory, true ) == 0 )
                            name = name.Substring( dataDirectory.Length + 1 );

                    mapName = name;
                }
            }

            if( mapName != "" )
            {
                if( !ServerOrSingle_MapLoad( mapName, EntitySystemWorld.Instance.DefaultWorldType, false ) )
                {
                    //Error
                    foreach( EControl control in controlManager.Controls )
                    {
                        if( control is MessageBoxWindow && !control.IsShouldDetach() )
                            return true;
                    }

                    GameMusic.MusicPlay( "Sounds\\Music\\Bumps.ogg", true );
                    controlManager.Controls.Add( new EngineLogoWindow() );
                }
            }
            else
            {
                GameMusic.MusicPlay("Sounds\\Music\\Bumps.ogg", true);
                controlManager.Controls.Add( new EngineLogoWindow() );
            }

            //showDebugInformation console command
            if( EngineConsole.Instance != null )
            {
                EngineConsole.Instance.AddCommand( "showDebugInformationWindow",
                    ConsoleCommand_ShowDebugInformationWindow );
            }

            //example of custom input device
            //ExampleCustomInputDevice.InitDevice();

            return true;
        }
Example #11
0
        Vec2I GetNeededSize()
        {
            Vec2I result;

            ScreenControlManager screenControlManager = GetControlManager() as ScreenControlManager;

            if (screenControlManager != null)
            {
                //screen gui

                Vec2I viewportSize = screenControlManager.GuiRenderer.ViewportForScreenGuiRenderer.DimensionsInPixels.Size;

                Vec2 size = viewportSize.ToVec2() * GetScreenSize();
                if (screenControlManager.GuiRenderer._OutGeometryTransformEnabled)
                {
                    size *= screenControlManager.GuiRenderer._OutGeometryTransformScale;
                }

                result = new Vec2I((int)(size.X + .9999f), (int)(size.Y + .9999f));
            }
            else
            {
                //in-game gui

                int   height     = inGame3DGuiHeightInPixels;
                Vec2  screenSize = GetScreenSize();
                float width      = (float)height * (screenSize.X / screenSize.Y) * GetControlManager().AspectRatio;
                result = new Vec2I((int)(width + .9999f), height);

                //int height = inGame3DGuiHeightInPixels;
                //if( height > RenderSystem.Instance.Capabilities.MaxTextureSize )
                //   height = RenderSystem.Instance.Capabilities.MaxTextureSize;

                //Vec2 screenSize = GetScreenSize();
                //float width = (float)height * ( screenSize.X / screenSize.Y ) * GetControlManager().AspectRatio;
                //result = new Vec2I( (int)( width + .9999f ), height );
            }

            if (result.X < 1)
            {
                result.X = 1;
            }
            if (result.Y < 1)
            {
                result.Y = 1;
            }

            //fix max texture size
            if (result.X > RenderSystem.Instance.Capabilities.MaxTextureSize || result.Y > RenderSystem.Instance.Capabilities.MaxTextureSize)
            {
                float divideX = (float)result.X / (float)RenderSystem.Instance.Capabilities.MaxTextureSize;
                float divideY = (float)result.Y / (float)RenderSystem.Instance.Capabilities.MaxTextureSize;
                float divide  = Math.Max(Math.Max(divideX, divideY), 1);
                if (divide != 1)
                {
                    result = (result.ToVec2() / divide).ToVec2I();
                    if (result.X > RenderSystem.Instance.Capabilities.MaxTextureSize)
                    {
                        result.X = RenderSystem.Instance.Capabilities.MaxTextureSize;
                    }
                    if (result.Y > RenderSystem.Instance.Capabilities.MaxTextureSize)
                    {
                        result.Y = RenderSystem.Instance.Capabilities.MaxTextureSize;
                    }
                }
            }

            return(result);
        }
        bool CreateRenderTarget()
        {
            DestroyRenderTarget();

            if (RendererWorld.Instance == null)
            {
                return(false);
            }

            Vec2I size = new Vec2I(ClientRectangle.Size.Width, ClientRectangle.Size.Height);

            if (size.X < 1 || size.Y < 1)
            {
                return(false);
            }

            renderWindow = RendererWorld.Instance.CreateRenderWindow(Handle, size);
            if (renderWindow == null)
            {
                return(false);
            }

            renderWindow.AutoUpdate          = false;
            renderWindow.AllowAdditionalMRTs = true;

            camera = SceneManager.Instance.CreateCamera(
                SceneManager.Instance.GetUniqueCameraName("UserControl"));
            camera.Purpose = Camera.Purposes.MainCamera;

            //update camera settings
            camera.NearClipDistance = cameraNearFarClipDistance.Minimum;
            camera.FarClipDistance  = cameraNearFarClipDistance.Maximum;
            camera.AspectRatio      = (float)renderWindow.Size.X / (float)renderWindow.Size.Y;
            camera.FixedUp          = cameraFixedUp;
            camera.Position         = cameraPosition;
            camera.Direction        = cameraDirection;
            camera.Fov               = cameraFov;
            camera.ProjectionType    = cameraProjectionType;
            camera.OrthoWindowHeight = cameraOrthoWindowHeight;

            viewport = renderWindow.AddViewport(camera);

            //Initialize HDR compositor for HDR render technique
            if (EngineApp.RenderTechnique == "HDR")
            {
                viewport.AddCompositor("HDR", 0);
                viewport.SetCompositorEnabled("HDR", true);
            }

            //Initialize Fast Approximate Antialiasing (FXAA)
            {
                bool   useMRT = RendererWorld.InitializationOptions.AllowSceneMRTRendering;
                string fsaa   = RendererWorld.InitializationOptions.FullSceneAntialiasing;
                if ((useMRT && (fsaa == "" || fsaa == "RecommendedSetting") && IsActivateFXAAByDefault()) ||
                    fsaa == "FXAA")
                {
                    if (RenderSystem.Instance.HasShaderModel3())
                    {
                        InitializeFXAACompositor();
                    }
                }
            }

            //add listener
            renderTargetListener = new ViewRenderTargetListener(this);
            renderWindow.AddListener(renderTargetListener);

            if (guiRenderer == null)
            {
                guiRenderer = new GuiRenderer(viewport);
            }
            else
            {
                guiRenderer.ChangeViewport(viewport);
            }

            if (controlManager == null)
            {
                controlManager = new ScreenControlManager(guiRenderer);
            }

            return(true);
        }
        private bool CreateRenderTarget()
        {
            DestroyRenderTarget();

            if (RendererWorld.Instance == null)
                return false;

            Vec2I size = new Vec2I(ClientRectangle.Size.Width, ClientRectangle.Size.Height);
            if (size.X < 1 || size.Y < 1)
                return false;

            renderWindow = RendererWorld.Instance.CreateRenderWindow(Handle, size);
            if (renderWindow == null)
                return false;

            renderWindow.AutoUpdate = false;
            renderWindow.AllowAdditionalMRTs = true;

            camera = SceneManager.Instance.CreateCamera(
                SceneManager.Instance.GetUniqueCameraName("UserControl"));
            camera.Purpose = Camera.Purposes.MainCamera;

            //update camera settings
            camera.NearClipDistance = cameraNearFarClipDistance.Minimum;
            camera.FarClipDistance = cameraNearFarClipDistance.Maximum;
            camera.AspectRatio = (float)renderWindow.Size.X / (float)renderWindow.Size.Y;
            camera.FixedUp = cameraFixedUp;
            camera.Position = cameraPosition;
            camera.Direction = cameraDirection;
            camera.Fov = cameraFov;
            camera.ProjectionType = cameraProjectionType;
            camera.OrthoWindowHeight = cameraOrthoWindowHeight;

            viewport = renderWindow.AddViewport(camera);

            //Initialize HDR compositor for HDR render technique
            if (EngineApp.RenderTechnique == "HDR")
            {
                viewport.AddCompositor("HDR", 0);
                viewport.SetCompositorEnabled("HDR", true);
            }

            //Initialize Fast Approximate Antialiasing (FXAA)
            {
                bool useMRT = RendererWorld.InitializationOptions.AllowSceneMRTRendering;
                string fsaa = RendererWorld.InitializationOptions.FullSceneAntialiasing;
                if ((useMRT && (fsaa == "" || fsaa == "RecommendedSetting") && IsActivateFXAAByDefault()) ||
                    fsaa == "FXAA")
                {
                    if (RenderSystem.Instance.HasShaderModel3())
                        InitializeFXAACompositor();
                }
            }

            //add listener
            renderTargetListener = new ViewRenderTargetListener(this);
            renderWindow.AddListener(renderTargetListener);

            if (guiRenderer == null)
                guiRenderer = new GuiRenderer(viewport);
            else
                guiRenderer.ChangeViewport(viewport);

            if (controlManager == null)
                controlManager = new ScreenControlManager(guiRenderer);

            return true;
        }
        protected override void OnLoad( EventArgs e )
        {
            base.OnLoad( e );

            needCreateRenderWindow = true;
            controlManager = new ScreenControlManager();

            float interval = ( automaticUpdateFPS != 0 ) ?
                ( ( 1.0f / automaticUpdateFPS ) * 1000.0f ) : 100;
            updateTimer = new Timer();
            updateTimer.Interval = (int)interval;
            updateTimer.Tick += updateTimer_Tick;
            updateTimer.Enabled = true;

            WindowsAppWorld.renderTargetUserControls.Add( this );
        }
        protected virtual void OnDestroy()
        {
            if( updateTimer != null )
            {
                updateTimer.Dispose();
                updateTimer = null;
            }

            DestroyRenderTarget();

            controlManager = null;

            WindowsAppWorld.renderTargetUserControls.Remove( this );
        }