I have three projects in one solution:
- a form with some tool (see it as a level-editor), in which there is PictureBox where I want to redirect drawing of the game.
- My game, with some methods and its Update/Draw methods:
- A launcher (where I can start the game or start the level-editor)
protected override void Update(GameTime gameTime) {...}
protected override void Draw(GameTime gameTime) {...}
This is the launcher's "Open game in editor mode" button code:
private void cmdEditorMode_Click(object sender, EventArgs e)
{
using (Game1 game_instance = new Game1(true))
{
using (MapEditor form = new MapEditor())
{
form.Show();
form.game_wrapper = new GameWrapperForLevelEditor(
game_instance,
form.pctSurface.Handle,
form,
form.pctSurface);
game_instance.Run();
}
}
}
Note that:
true
in Game1 constructor is just a bool used to tell the game to open it in editor mode.GameWrapperForLevelEditor
is another class used to make the form of the level editor and the Game itself in relation.
In particular (to draw inside the pictureBox drawSurface):
public GameWrapperForLevelEditor(
Game1 game,
IntPtr drawSurface,
System.Windows.Forms.Form parentForm,
System.Windows.Forms.PictureBox surfacePictureBox)
{
graphics = new GraphicsDeviceManager(this);
//...
//...other things
//...
graphics.PreparingDeviceSettings +=
new EventHandler<PreparingDeviceSettingsEventArgs>(graphics_PreparingDeviceSettings);
}
and:
void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
{
e.GraphicsDeviceInformation.PresentationParameters.DeviceWindowHandle = drawSurface;
}
This is the launcher's Main code:
using System;
namespace Launcher
{
#if WINDOWS || XBOX
static class MAIN
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
using (Launcher_form form = new Launcher_form())
{
form.Show();
//Creo un'istanza del picture_box_render all'interno del form
form.picture_box_render = new launcher_picture_render(form.pctLauncher.Handle);
form.picture_box_render.Run();
}
}
}
#endif
}
Note that:
- I use here another game (
picture_box_render
) because in my launcher's form I put a picture box where I want to run an animation that is rendered as another game loop.
Finally the draw part:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(game.LastRender, Vector2.Zero, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
My problems are:
game_instance.Run();
(up) gives me this error (translated from italian, so sorry):
Starting a second message loop on a single thread is not valid operation Use Form.ShowDialog.
game.LastRender
is Always empty because the game never run.game.LastRender
is just aRenderTarget2D
where Game1 draw method draws the game.
As a solution, I thought that I may don't game_instance.Run();
, but just use game.Update(...)
and game.Draw(...)
inside my wrapper class. The problem is that these two methods are protected
, and if I try to set them public
it says me that I can't.
What am I doing wrong?