Hello @dryhfth Thanks for answer!
But I really don’t like WinForms. Because I hate cause Linux and Mac users argue that.
I really want know how do I make sub window / multiple window only DesktopGL ( WindowGL )
I can’t find example of RenderTargets with sub- or multiple-windows.
Do not use SharpDX!
Only DesktopGL!!
I have tried example:
MyGame.cs:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoCrraft.Controls;
using MonoCrraft.Properties;
using System;
using System.Collections.Generic;
using System.Threading;
namespace MonoCrraft
{
public class MyGame : Game
{
public GraphicsDeviceManager graphics { get; set; }
SpriteBatch spriteBatch;
Model model;
public Color ClearColor { get; set; }
List<Component> _gameComponents;
public MyGame()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1400;
graphics.PreferredBackBufferHeight = 840;
ResourceContentManager resxContent = new ResourceContentManager(Services, Resources.ResourceManager);
Content = resxContent;
}
protected override void Initialize()
{
Window.Title = "My Game Example";
IsMouseVisible = true;
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
model = Content.Load<Model>("box");
var SettingButton = new Button(Content.Load<Texture2D>("button"), Content.Load<SpriteFont>("font"))
{
Position = new Vector2(10, 10),
Text = "Setting",
Rectangle = new Rectangle(10, 10, 100, 30)
};
SettingButton.Click += SettingHandler;
_gameComponents = new List<Component>()
{
SettingButton
};
}
private void SettingHandler(object sender, EventArgs e)
{
using (var settingWindow = new SettingWindow(this))
{
while(settingWindow != null)
settingWindow.Run();
}
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
foreach (var component in _gameComponents)
component.Update(gameTime);
base.Update(gameTime);
}
private void CreateCube(Vector3 position, Color diffuseColor, GameTime gt)
{
foreach(ModelMesh mesh in model.Meshes)
{
foreach(BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.AmbientLightColor = Color.DimGray.ToVector3();
effect.DirectionalLight0.Direction = Vector3.Normalize(new Vector3(-1, -1.5f, 0));
effect.DirectionalLight0.DiffuseColor = diffuseColor.ToVector3() * Color.DimGray.ToVector3();
effect.DirectionalLight0.SpecularColor = Color.Wheat.ToVector3();
effect.DirectionalLight1.Enabled = true;
effect.DirectionalLight1.DiffuseColor = Color.DimGray.ToVector3() * Color.Black.ToVector3();
effect.DirectionalLight1.Direction = Vector3.Left;
effect.World = Matrix.CreateTranslation(position);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), GraphicsDevice.Viewport.AspectRatio, 1.0f, 10000.0f);
effect.View = Matrix.CreateLookAt(new Vector3(2, 3, 3), Vector3.Zero, Vector3.Up);
effect.PreferPerPixelLighting = true;
effect.SpecularPower = (float)gt.TotalGameTime.TotalSeconds * 1.5f;
effect.SpecularColor = diffuseColor.ToVector3() * Color.White.ToVector3();
effect.DiffuseColor = diffuseColor.ToVector3();
}
mesh.Draw();
}
}
protected override void Draw(GameTime gameTime)
{
ClearColor = Color.CornflowerBlue;
GraphicsDevice.Clear(ClearColor);
spriteBatch.Begin();
foreach (var component in _gameComponents)
component.Draw(gameTime, spriteBatch);
CreateCube(new Vector3(-2, -0.5f, -2), Color.DarkGreen, gameTime);
CreateCube(new Vector3(-2, -0.5f, 0), Color.YellowGreen, gameTime);
CreateCube(new Vector3(-2, -0.5f, 2), Color.Aqua, gameTime);
CreateCube(new Vector3(0, -0.5f, -2), Color.Red, gameTime);
CreateCube(new Vector3(0, -0.5f, 0), Color.Orange, gameTime);
CreateCube(new Vector3(0, -0.5f, 2), Color.DimGray, gameTime);
CreateCube(new Vector3(2, -0.5f, -2), Color.Brown, gameTime);
CreateCube(new Vector3(2, -0.5f, 0), Color.Blue, gameTime);
CreateCube(new Vector3(2, -0.5f, 2), Color.Purple, gameTime);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
SettingWindow.cs: It is sub window of MonoGame
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using MonoCrraft.Controls;
using MonoCrraft.Properties;
using System.Collections.Generic;
using System;
namespace MonoCrraft
{
public class SettingWindow : Game
{
private MyGame MyGame;
SpriteBatch spriteBatch;
List<Component> comps;
public SettingWindow(MyGame g)
{
MyGame = g;
g.graphics = new GraphicsDeviceManager(this);
g.graphics.PreferredBackBufferWidth = 400;
g.graphics.PreferredBackBufferHeight = 300;
ResourceContentManager resxContent = new ResourceContentManager(Services, Resources.ResourceManager);
Content = resxContent;
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
Window.Title = "Setting .... ";
IsMouseVisible = true;
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
var randonButton = new Button(Content.Load<Texture2D>("button"), Content.Load<SpriteFont>("font"))
{
Position = new Vector2(10, 10),
Text = "Change",
};
randonButton.Click += new EventHandler(randonHandler);
var quitButton = new Button(Content.Load<Texture2D>("button"), Content.Load<SpriteFont>("font"))
{
Position = new Vector2(10, 50),
Text = "Quit",
};
quitButton.Click += new EventHandler(quitHandler);
comps = new List<Component>()
{
randonButton,
quitButton
};
base.LoadContent();
}
/**
* Exit sub window but it doesn't work :(
*/
private void quitHandler(object sender, EventArgs e)
{
Exiting += new EventHandler<EventArgs>(exitHandler);
}
private void exitHandler(object sender, EventArgs e)
{
while(true)
{
Exit();
}
}
/**
* Change color from MyGame's background - But it doesn't work :(
*/
private void randonHandler(object sender, EventArgs e)
{
Random randon = new Random();
if(MyGame.ClearColor != Color.CornflowerBlue)
MyGame.ClearColor = new Color(randon.Next(0, 255), randon.Next(0, 255), randon.Next(0, 255));
}
protected override void Update(GameTime gameTime)
{
foreach (var component in comps)
component.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
foreach (var component in comps)
component.Draw(gameTime, spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Program.cs ( Runnable file of C#
using System;
using System.IO;
namespace MonoCrraft
{
public static class Program
{
[STAThread]
static void Main()
{
/*
* For Mono bundled if you don't know since you have bundled net apps into single executable
* It happens bundled single executable after you run executable and it throws exceptions.
*/
//if (Environment.Is64BitProcess)
// Environment.CurrentDirectory = Path.Combine("x64");
//else
// Environment.CurrentDirectory = Path.Combine("x84");
/*
* End of bundled execuatble
*/
using (var game = new MyGame())
game.Run();
}
}
}
Component.cs and Button.cs are made from Create simple button.
That is why I can’t resolve that. If you want open sub window like @willmotil he created thread since May 2017 and they don’t know what is “sub window” or “multiple window” meaning?
Sorry I fight that because LWJGL or LibGDX ( Java ) has multiple windows now and they move to Java. Hey hey hey C# will loose many million of people.
I dont like C# happens. That is why I wish we have big success with C# like many users play Unity3D, Unreal Engine and Xenko Studio and Wave Engine and CocoaEngine and UhroEngine are happy because they have multiple windows.
MonoGame has not multiple windows. What do we should that?
How do I use RenderTarget for sub window? Does it have example?